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
8a953d19fc61eb1f6129deec79a9702252b611a2
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/check_constants.lean
6fbe76ef26bae093370484be419f282a30689d4a
[ "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
16,039
lean
-- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py import smt system.io open tactic meta def script_check_id (n : name) : tactic unit := do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute") run_command script_check_id `abs run_command script_check_id `absurd run_command script_check_id `acc.cases_on run_command script_check_id `add run_command script_check_id `add_comm_group run_command script_check_id `add_comm_semigroup run_command script_check_id `add_group run_command script_check_id `add_monoid run_command script_check_id `and run_command script_check_id `and.elim_left run_command script_check_id `and.elim_right run_command script_check_id `and.intro run_command script_check_id `andthen run_command script_check_id `auto_param run_command script_check_id `bit0 run_command script_check_id `bit1 run_command script_check_id `bool run_command script_check_id `bool.ff run_command script_check_id `bool.tt run_command script_check_id `bind run_command script_check_id `combinator.K run_command script_check_id `caching_user_attribute run_command script_check_id `cast run_command script_check_id `cast_heq run_command script_check_id `char run_command script_check_id `char.of_nat run_command script_check_id `char.of_nat_ne_of_ne run_command script_check_id `classical.prop_decidable run_command script_check_id `classical.type_decidable_eq run_command script_check_id `coe run_command script_check_id `coe_fn run_command script_check_id `coe_sort run_command script_check_id `coe_to_lift run_command script_check_id `congr run_command script_check_id `congr_arg run_command script_check_id `congr_fun run_command script_check_id `decidable run_command script_check_id `decidable.to_bool run_command script_check_id `distrib run_command script_check_id `dite run_command script_check_id `div run_command script_check_id `id run_command script_check_id `empty run_command script_check_id `emptyc run_command script_check_id `Exists run_command script_check_id `eq run_command script_check_id `eq.cases_on run_command script_check_id `eq.drec run_command script_check_id `eq.mp run_command script_check_id `eq.mpr run_command script_check_id `eq.rec run_command script_check_id `eq.refl run_command script_check_id `eq.subst run_command script_check_id `eq.symm run_command script_check_id `eq.trans run_command script_check_id `eq_of_heq run_command script_check_id `eq_rec_heq run_command script_check_id `eq_true_intro run_command script_check_id `eq_false_intro run_command script_check_id `eq_self_iff_true run_command script_check_id `format run_command script_check_id `false run_command script_check_id `false_of_true_iff_false run_command script_check_id `false_of_true_eq_false run_command script_check_id `true_eq_false_of_false run_command script_check_id `false.rec run_command script_check_id `field run_command script_check_id `fin.mk run_command script_check_id `fin.ne_of_vne run_command script_check_id `forall_congr run_command script_check_id `forall_congr_eq run_command script_check_id `forall_not_of_not_exists run_command script_check_id `funext run_command script_check_id `ge run_command script_check_id `get_line run_command script_check_id `gt run_command script_check_id `has_add run_command script_check_id `has_div run_command script_check_id `has_mul run_command script_check_id `has_inv run_command script_check_id `has_le run_command script_check_id `has_lt run_command script_check_id `has_neg run_command script_check_id `has_one run_command script_check_id `has_one.one run_command script_check_id `has_sizeof run_command script_check_id `has_sizeof.mk run_command script_check_id `has_sub run_command script_check_id `has_to_format run_command script_check_id `has_to_string run_command script_check_id `has_zero run_command script_check_id `has_zero.zero run_command script_check_id `has_coe_t run_command script_check_id `heq run_command script_check_id `heq.refl run_command script_check_id `heq.symm run_command script_check_id `heq.trans run_command script_check_id `heq_of_eq run_command script_check_id `id_locked run_command script_check_id `if_neg run_command script_check_id `if_pos run_command script_check_id `iff run_command script_check_id `iff_false_intro run_command script_check_id `iff.intro run_command script_check_id `iff.mpr run_command script_check_id `iff.refl run_command script_check_id `iff.symm run_command script_check_id `iff.trans run_command script_check_id `iff_true_intro run_command script_check_id `imp_congr run_command script_check_id `imp_congr_eq run_command script_check_id `imp_congr_ctx run_command script_check_id `imp_congr_ctx_eq run_command script_check_id `implies run_command script_check_id `implies_of_if_neg run_command script_check_id `implies_of_if_pos run_command script_check_id `insert run_command script_check_id `int run_command script_check_id `int.has_add run_command script_check_id `int.has_mul run_command script_check_id `int.has_sub run_command script_check_id `int.has_div run_command script_check_id `int.has_le run_command script_check_id `int.has_lt run_command script_check_id `int.has_neg run_command script_check_id `int.has_mod run_command script_check_id `int.bit0_nonneg run_command script_check_id `int.bit1_nonneg run_command script_check_id `int.one_nonneg run_command script_check_id `int.zero_nonneg run_command script_check_id `int.bit0_pos run_command script_check_id `int.bit1_pos run_command script_check_id `int.one_pos run_command script_check_id `int.nat_abs_zero run_command script_check_id `int.nat_abs_one run_command script_check_id `int.nat_abs_bit0_step run_command script_check_id `int.nat_abs_bit1_nonneg_step run_command script_check_id `int.ne_of_nat_ne_nonneg_case run_command script_check_id `int.ne_neg_of_ne run_command script_check_id `int.neg_ne_of_pos run_command script_check_id `int.ne_neg_of_pos run_command script_check_id `int.neg_ne_zero_of_ne run_command script_check_id `int.zero_ne_neg_of_ne run_command script_check_id `int.decidable_linear_ordered_comm_group run_command script_check_id `interactive.parse run_command script_check_id `inv run_command script_check_id `io run_command script_check_id `io.functor run_command script_check_id `io.monad run_command script_check_id `is_associative run_command script_check_id `is_associative.assoc run_command script_check_id `is_commutative run_command script_check_id `is_commutative.comm run_command script_check_id `ite run_command script_check_id `left_distrib run_command script_check_id `left_comm run_command script_check_id `le run_command script_check_id `le_refl run_command script_check_id `linear_ordered_ring run_command script_check_id `linear_ordered_semiring run_command script_check_id `list run_command script_check_id `list.nil run_command script_check_id `list.cons run_command script_check_id `lt run_command script_check_id `match_failed run_command script_check_id `mod run_command script_check_id `monad run_command script_check_id `monad.bind run_command script_check_id `monad.ret run_command script_check_id `monad_fail run_command script_check_id `monoid run_command script_check_id `mul run_command script_check_id `mul_one run_command script_check_id `mul_zero run_command script_check_id `mul_zero_class run_command script_check_id `name.anonymous run_command script_check_id `name.mk_numeral run_command script_check_id `name.mk_string run_command script_check_id `nat run_command script_check_id `nat.of_num run_command script_check_id `nat.succ run_command script_check_id `nat.zero run_command script_check_id `nat.has_zero run_command script_check_id `nat.has_one run_command script_check_id `nat.has_add run_command script_check_id `nat.add run_command script_check_id `nat.cases_on run_command script_check_id `nat.bit0_ne run_command script_check_id `nat.bit0_ne_bit1 run_command script_check_id `nat.bit0_ne_zero run_command script_check_id `nat.bit0_ne_one run_command script_check_id `nat.bit1_ne run_command script_check_id `nat.bit1_ne_bit0 run_command script_check_id `nat.bit1_ne_zero run_command script_check_id `nat.bit1_ne_one run_command script_check_id `nat.zero_ne_one run_command script_check_id `nat.zero_ne_bit0 run_command script_check_id `nat.zero_ne_bit1 run_command script_check_id `nat.one_ne_zero run_command script_check_id `nat.one_ne_bit0 run_command script_check_id `nat.one_ne_bit1 run_command script_check_id `nat.bit0_lt run_command script_check_id `nat.bit1_lt run_command script_check_id `nat.bit0_lt_bit1 run_command script_check_id `nat.bit1_lt_bit0 run_command script_check_id `nat.zero_lt_one run_command script_check_id `nat.zero_lt_bit1 run_command script_check_id `nat.zero_lt_bit0 run_command script_check_id `nat.one_lt_bit0 run_command script_check_id `nat.one_lt_bit1 run_command script_check_id `nat.le_of_lt run_command script_check_id `nat.le_refl run_command script_check_id `ne run_command script_check_id `neg run_command script_check_id `neq_of_not_iff run_command script_check_id `norm_num.add1 run_command script_check_id `norm_num.add1_bit0 run_command script_check_id `norm_num.add1_bit1_helper run_command script_check_id `norm_num.add1_one run_command script_check_id `norm_num.add1_zero run_command script_check_id `norm_num.add_div_helper run_command script_check_id `norm_num.bin_add_zero run_command script_check_id `norm_num.bin_zero_add run_command script_check_id `norm_num.bit0_add_bit0_helper run_command script_check_id `norm_num.bit0_add_bit1_helper run_command script_check_id `norm_num.bit0_add_one run_command script_check_id `norm_num.bit1_add_bit0_helper run_command script_check_id `norm_num.bit1_add_bit1_helper run_command script_check_id `norm_num.bit1_add_one_helper run_command script_check_id `norm_num.div_add_helper run_command script_check_id `norm_num.div_eq_div_helper run_command script_check_id `norm_num.div_helper run_command script_check_id `norm_num.div_mul_helper run_command script_check_id `norm_num.mk_cong run_command script_check_id `norm_num.mul_bit0_helper run_command script_check_id `norm_num.mul_bit1_helper run_command script_check_id `norm_num.mul_div_helper run_command script_check_id `norm_num.neg_add_neg_helper run_command script_check_id `norm_num.neg_add_pos_helper1 run_command script_check_id `norm_num.neg_add_pos_helper2 run_command script_check_id `norm_num.neg_mul_neg_helper run_command script_check_id `norm_num.neg_mul_pos_helper run_command script_check_id `norm_num.neg_neg_helper run_command script_check_id `norm_num.neg_zero_helper run_command script_check_id `norm_num.nonneg_bit0_helper run_command script_check_id `norm_num.nonneg_bit1_helper run_command script_check_id `norm_num.nonzero_of_div_helper run_command script_check_id `norm_num.nonzero_of_neg_helper run_command script_check_id `norm_num.nonzero_of_pos_helper run_command script_check_id `norm_num.one_add_bit0 run_command script_check_id `norm_num.one_add_bit1_helper run_command script_check_id `norm_num.one_add_one run_command script_check_id `norm_num.pos_add_neg_helper run_command script_check_id `norm_num.pos_bit0_helper run_command script_check_id `norm_num.pos_bit1_helper run_command script_check_id `norm_num.pos_mul_neg_helper run_command script_check_id `norm_num.sub_nat_zero_helper run_command script_check_id `norm_num.sub_nat_pos_helper run_command script_check_id `norm_num.subst_into_div run_command script_check_id `norm_num.subst_into_prod run_command script_check_id `norm_num.subst_into_subtr run_command script_check_id `norm_num.subst_into_sum run_command script_check_id `not run_command script_check_id `not_of_iff_false run_command script_check_id `not_of_eq_false run_command script_check_id `num run_command script_check_id `num.pos run_command script_check_id `num.zero run_command script_check_id `of_eq_true run_command script_check_id `of_iff_true run_command script_check_id `one run_command script_check_id `opt_param run_command script_check_id `or run_command script_check_id `orelse run_command script_check_id `out_param run_command script_check_id `punit run_command script_check_id `punit.star run_command script_check_id `pos_num.bit0 run_command script_check_id `pos_num.bit1 run_command script_check_id `pos_num.one run_command script_check_id `prod.mk run_command script_check_id `pprod run_command script_check_id `pprod.mk run_command script_check_id `pprod.fst run_command script_check_id `pprod.snd run_command script_check_id `propext run_command script_check_id `pexpr run_command script_check_id `pexpr.subst run_command script_check_id `pre_monad.bind run_command script_check_id `pre_monad.and_then run_command script_check_id `pre_monad.seq run_command script_check_id `put_str run_command script_check_id `put_nat run_command script_check_id `to_pexpr run_command script_check_id `quot.mk run_command script_check_id `quot.lift run_command script_check_id `real run_command script_check_id `real.of_int run_command script_check_id `real.to_int run_command script_check_id `real.is_int run_command script_check_id `real.has_neg run_command script_check_id `real.has_div run_command script_check_id `real.has_add run_command script_check_id `real.has_mul run_command script_check_id `real.has_sub run_command script_check_id `real.has_lt run_command script_check_id `real.has_le run_command script_check_id `rfl run_command script_check_id `right_distrib run_command script_check_id `ring run_command script_check_id `scope_trace run_command script_check_id `set_of run_command script_check_id `sep run_command script_check_id `semiring run_command script_check_id `sigma run_command script_check_id `sigma.mk run_command script_check_id `sigma.fst run_command script_check_id `sigma.snd run_command script_check_id `psigma run_command script_check_id `psigma.cases_on run_command script_check_id `psigma.mk run_command script_check_id `singleton run_command script_check_id `sizeof run_command script_check_id `smt.array run_command script_check_id `smt.select run_command script_check_id `smt.store run_command script_check_id `smt.prove run_command script_check_id `string run_command script_check_id `string.empty run_command script_check_id `string.str run_command script_check_id `string.empty_ne_str run_command script_check_id `string.str_ne_empty run_command script_check_id `string.str_ne_str_left run_command script_check_id `string.str_ne_str_right run_command script_check_id `sub run_command script_check_id `subsingleton run_command script_check_id `subsingleton.elim run_command script_check_id `subsingleton.helim run_command script_check_id `subtype run_command script_check_id `subtype.tag run_command script_check_id `subtype.elt_of run_command script_check_id `subtype.rec run_command script_check_id `psum run_command script_check_id `psum.cases_on run_command script_check_id `psum.inl run_command script_check_id `psum.inr run_command script_check_id `tactic run_command script_check_id `tactic.eval_expr run_command script_check_id `tactic.try run_command script_check_id `tactic.triv run_command script_check_id `thunk run_command script_check_id `to_fmt run_command script_check_id `to_string run_command script_check_id `trans_rel_left run_command script_check_id `trans_rel_right run_command script_check_id `true run_command script_check_id `true.intro run_command script_check_id `unification_hint run_command script_check_id `unification_hint.mk run_command script_check_id `unit run_command script_check_id `unit.cases_on run_command script_check_id `unit.star run_command script_check_id `user_attribute run_command script_check_id `vm_monitor run_command script_check_id `weak_order run_command script_check_id `well_founded run_command script_check_id `xor run_command script_check_id `zero run_command script_check_id `zero_le_one run_command script_check_id `zero_lt_one run_command script_check_id `zero_mul
0c5ce596314e507b667a4b916d448d6595a7e03c
c6da0300d417abe3464e750ab51a63502b93acfa
/test/break_test.lean
4c80cf7c21d38dcb31a828a7efa7bccd24c8ef2f
[ "Apache-2.0" ]
permissive
uwplse/struct_tact
55bc1d260fac498cff83a4d71461041f8ed74bd6
22188ea2e97705d1185f75dde24e6bab88054ab0
refs/heads/master
1,630,670,592,496
1,515,453,299,000
1,515,453,299,000
104,603,771
0
0
null
null
null
null
UTF-8
Lean
false
false
411
lean
import ..src.struct_tact inductive t | C1 : int → t | C2 : int → t | C3 : int → t def foo (xs : t × t) : int := let z := 10 in match xs with | (t.C1 i, t.C2 j) := i + j + z | (t.C2 j, t.C1 i) := j + i + z | (_, _) := 0 end lemma nested_break_match : forall i j, foo (t.C2 i, t.C1 j) = foo (t.C1 j, t.C2 i) := begin -- intros, -- unfold foo, -- dsimp, -- break_match, admit, end
abcee0221ed615eeea23a0f62edb6d1065f7db2d
130c49f47783503e462c16b2eff31933442be6ff
/stage0/src/Lean/Data/Json/FromToJson.lean
70f7f6120d06dcd670231691132422a720d06ded
[ "Apache-2.0" ]
permissive
Hazel-Brown/lean4
8aa5860e282435ffc30dcdfccd34006c59d1d39c
79e6732fc6bbf5af831b76f310f9c488d44e7a16
refs/heads/master
1,689,218,208,951
1,629,736,869,000
1,629,736,896,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,510
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Marc Huisinga -/ import Lean.Data.Json.Basic import Lean.Data.Json.Printer namespace Lean universe u class FromJson (α : Type u) where fromJson? : Json → Except String α export FromJson (fromJson?) class ToJson (α : Type u) where toJson : α → Json export ToJson (toJson) instance : FromJson Json := ⟨Except.ok⟩ instance : ToJson Json := ⟨id⟩ instance : FromJson JsonNumber := ⟨Json.getNum?⟩ instance : ToJson JsonNumber := ⟨Json.num⟩ -- looks like id, but there are coercions happening instance : FromJson Bool := ⟨Json.getBool?⟩ instance : ToJson Bool := ⟨fun b => b⟩ instance : FromJson Nat := ⟨Json.getNat?⟩ instance : ToJson Nat := ⟨fun n => n⟩ instance : FromJson Int := ⟨Json.getInt?⟩ instance : ToJson Int := ⟨fun n => Json.num n⟩ instance : FromJson String := ⟨Json.getStr?⟩ instance : ToJson String := ⟨fun s => s⟩ instance [FromJson α] : FromJson (Array α) where fromJson? | Json.arr a => a.mapM fromJson? | _ => throw "JSON array expected" instance [ToJson α] : ToJson (Array α) := ⟨fun a => Json.arr (a.map toJson)⟩ instance [FromJson α] : FromJson (Option α) where fromJson? | Json.null => Except.ok none | j => some <$> fromJson? j instance [ToJson α] : ToJson (Option α) := ⟨fun | none => Json.null | some a => toJson a⟩ instance : FromJson Name where fromJson? j := do let s ← j.getStr? let some n ← Syntax.decodeNameLit s | throw s!"expected a `Name`, got '{j}'" return n instance : ToJson Name where toJson n := toString (repr n) /- Note that `USize`s and `UInt64`s are stored as strings because JavaScript cannot represent 64-bit numbers. -/ def bignumFromJson? (j : Json) : Except String Nat := do let s ← j.getStr? let some v ← Syntax.decodeNatLitVal? s -- TODO maybe this should be in Std | throw s!"expected a string-encoded number, got '{j}'" return v def bignumToJson (n : Nat) : Json := toString n instance : FromJson USize where fromJson? j := do let n ← bignumFromJson? j if n ≥ USize.size then throw "value '{j}' is too large for `USize`" return USize.ofNat n instance : ToJson USize where toJson v := bignumToJson (USize.toNat v) instance : FromJson UInt64 where fromJson? j := do let n ← bignumFromJson? j if n ≥ UInt64.size then throw "value '{j}' is too large for `UInt64`" return UInt64.ofNat n instance : ToJson UInt64 where toJson v := bignumToJson (UInt64.toNat v) namespace Json instance : FromJson Structured := ⟨fun | arr a => Structured.arr a | obj o => Structured.obj o | _ => throw "structured object expected"⟩ instance : ToJson Structured := ⟨fun | Structured.arr a => arr a | Structured.obj o => obj o⟩ def toStructured? [ToJson α] (v : α) : Except String Structured := fromJson? (toJson v) def getObjValAs? (j : Json) (α : Type u) [FromJson α] (k : String) : Except String α := fromJson? <| j.getObjValD k def opt [ToJson α] (k : String) : Option α → List (String × Json) | none => [] | some o => [⟨k, toJson o⟩] /-- Parses a JSON-encoded `structure` or `inductive` constructor. Used mostly by `deriving FromJson`. -/ def parseTagged (json : Json) (tag : String) (nFields : Nat) (fieldNames? : Option (Array Name)) : Except String (Array Json) := if nFields == 0 then match getStr? json with | Except.ok s => if s == tag then Except.ok #[] else throw s!"incorrect tag: {s} ≟ {tag}" | Except.error err => Except.error err else match getObjVal? json tag with | Except.ok payload => match fieldNames? with | some fieldNames => do let mut fields := #[] for fieldName in fieldNames do fields := fields.push (←getObjVal? payload fieldName.getString!) Except.ok fields | none => if nFields == 1 then Except.ok #[payload] else match getArr? payload with | Except.ok fields => if fields.size == nFields then Except.ok fields else Except.error s!"incorrect number of fields: {fields.size} ≟ {nFields}" | Except.error err => Except.error err | Except.error err => Except.error err end Json end Lean
56d71674c0e26d16b13f81d4be3910ec0092ed8a
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/algebra/geom_sum.lean
3b70678d8995df1cfdecfa9bccb9ae482408abe1
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,299
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland Sums of finite geometric series -/ import algebra.commute import algebra.group_with_zero_power universe u variable {α : Type u} open finset open_locale big_operators /-- Sum of the finite geometric series $\sum_{i=0}^{n-1} x^i$. -/ def geom_series [semiring α] (x : α) (n : ℕ) := ∑ i in range n, x ^ i theorem geom_series_def [semiring α] (x : α) (n : ℕ) : geom_series x n = ∑ i in range n, x ^ i := rfl @[simp] theorem geom_series_zero [semiring α] (x : α) : geom_series x 0 = 0 := rfl @[simp] theorem geom_series_one [semiring α] (x : α) : geom_series x 1 = 1 := by { rw [geom_series_def, sum_range_one, pow_zero] } /-- Sum of the finite geometric series $\sum_{i=0}^{n-1} x^i y^{n-1-i}$. -/ def geom_series₂ [semiring α] (x y : α) (n : ℕ) := ∑ i in range n, x ^ i * (y ^ (n - 1 - i)) theorem geom_series₂_def [semiring α] (x y : α) (n : ℕ) : geom_series₂ x y n = ∑ i in range n, x ^ i * y ^ (n - 1 - i) := rfl @[simp] theorem geom_series₂_zero [semiring α] (x y : α) : geom_series₂ x y 0 = 0 := rfl @[simp] theorem geom_series₂_one [semiring α] (x y : α) : geom_series₂ x y 1 = 1 := by { have : 1 - 1 - 0 = 0 := rfl, rw [geom_series₂_def, sum_range_one, this, pow_zero, pow_zero, mul_one] } @[simp] theorem geom_series₂_with_one [semiring α] (x : α) (n : ℕ) : geom_series₂ x 1 n = geom_series x n := sum_congr rfl (λ i _, by { rw [one_pow, mul_one] }) /-- $x^n-y^n = (x-y) \sum x^ky^{n-1-k}$ reformulated without `-` signs. -/ protected theorem commute.geom_sum₂_mul_add [semiring α] {x y : α} (h : commute x y) (n : ℕ) : (geom_series₂ (x + y) y n) * x + y ^ n = (x + y) ^ n := begin let f := λ (m i : ℕ), (x + y) ^ i * y ^ (m - 1 - i), change (∑ i in range n, (f n) i) * x + y ^ n = (x + y) ^ n, induction n with n ih, { rw [range_zero, sum_empty, zero_mul, zero_add, pow_zero, pow_zero] }, { have f_last : f (n + 1) n = (x + y) ^ n := by { dsimp [f], rw [nat.sub_sub, nat.add_comm, nat.sub_self, pow_zero, mul_one] }, have f_succ : ∀ i, i ∈ range n → f (n + 1) i = y * f n i := λ i hi, by { dsimp [f], have : commute y ((x + y) ^ i) := (h.symm.add_right (commute.refl y)).pow_right i, rw [← mul_assoc, this.eq, mul_assoc, ← pow_succ y (n - 1 - i)], congr' 2, rw [nat.add_sub_cancel, nat.sub_sub, add_comm 1 i], have : i + 1 + (n - (i + 1)) = n := nat.add_sub_of_le (mem_range.mp hi), rw [add_comm (i + 1)] at this, rw [← this, nat.add_sub_cancel, add_comm i 1, ← add_assoc, nat.add_sub_cancel] }, rw [pow_succ (x + y), add_mul, sum_range_succ, f_last, add_mul, add_assoc], rw [(((commute.refl x).add_right h).pow_right n).eq], congr' 1, rw[sum_congr rfl f_succ, ← mul_sum, pow_succ y], rw[mul_assoc, ← mul_add y, ih] } end /-- $x^n-y^n = (x-y) \sum x^ky^{n-1-k}$ reformulated without `-` signs. -/ theorem geom_sum₂_mul_add [comm_semiring α] (x y : α) (n : ℕ) : (geom_series₂ (x + y) y n) * x + y ^ n = (x + y) ^ n := (commute.all x y).geom_sum₂_mul_add n theorem geom_sum_mul_add [semiring α] (x : α) (n : ℕ) : (geom_series (x + 1) n) * x + 1 = (x + 1) ^ n := begin have := (commute.one_right x).geom_sum₂_mul_add n, rw [one_pow, geom_series₂_with_one] at this, exact this end theorem geom_sum₂_mul_comm [ring α] {x y : α} (h : commute x y) (n : ℕ) : (geom_series₂ x y n) * (x - y) = x ^ n - y ^ n := begin have := (h.sub_left (commute.refl y)).geom_sum₂_mul_add n, rw [sub_add_cancel] at this, rw [← this, add_sub_cancel] end theorem geom_sum₂_mul [comm_ring α] (x y : α) (n : ℕ) : (geom_series₂ x y n) * (x - y) = x ^ n - y ^ n := geom_sum₂_mul_comm (commute.all x y) n theorem geom_sum_mul [ring α] (x : α) (n : ℕ) : (geom_series x n) * (x - 1) = x ^ n - 1 := begin have := geom_sum₂_mul_comm (commute.one_right x) n, rw [one_pow, geom_series₂_with_one] at this, exact this end theorem geom_sum_mul_neg [ring α] (x : α) (n : ℕ) : (geom_series x n) * (1 - x) = 1 - x ^ n := begin have := congr_arg has_neg.neg (geom_sum_mul x n), rw [neg_sub, ← mul_neg_eq_neg_mul_symm, neg_sub] at this, exact this end theorem geom_sum [division_ring α] {x : α} (h : x ≠ 1) (n : ℕ) : (geom_series x n) = (x ^ n - 1) / (x - 1) := have x - 1 ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *, by rw [← geom_sum_mul, mul_div_cancel _ this] theorem geom_sum_Ico_mul [ring α] (x : α) {m n : ℕ} (hmn : m ≤ n) : ((finset.Ico m n).sum (pow x)) * (x - 1) = x^n - x^m := by rw [sum_Ico_eq_sub _ hmn, ← geom_series_def, ← geom_series_def, sub_mul, geom_sum_mul, geom_sum_mul, sub_sub_sub_cancel_right] theorem geom_sum_Ico_mul_neg [ring α] (x : α) {m n : ℕ} (hmn : m ≤ n) : ((finset.Ico m n).sum (pow x)) * (1 - x) = x^m - x^n := by rw [sum_Ico_eq_sub _ hmn, ← geom_series_def, ← geom_series_def, sub_mul, geom_sum_mul_neg, geom_sum_mul_neg, sub_sub_sub_cancel_left] theorem geom_sum_Ico [division_ring α] {x : α} (hx : x ≠ 1) {m n : ℕ} (hmn : m ≤ n) : (finset.Ico m n).sum (λ i, x ^ i) = (x ^ n - x ^ m) / (x - 1) := by simp only [sum_Ico_eq_sub _ hmn, (geom_series_def _ _).symm, geom_sum hx, div_sub_div_same, sub_sub_sub_cancel_right] lemma geom_sum_inv [division_ring α] {x : α} (hx1 : x ≠ 1) (hx0 : x ≠ 0) (n : ℕ) : (geom_series x⁻¹ n) = (x - 1)⁻¹ * (x - x⁻¹ ^ n * x) := have h₁ : x⁻¹ ≠ 1, by rwa [inv_eq_one_div, ne.def, div_eq_iff_mul_eq hx0, one_mul], have h₂ : x⁻¹ - 1 ≠ 0, from mt sub_eq_zero.1 h₁, have h₃ : x - 1 ≠ 0, from mt sub_eq_zero.1 hx1, have h₄ : x * (x ^ n)⁻¹ = (x ^ n)⁻¹ * x := nat.rec_on n (by simp) (λ n h, by rw [pow_succ, mul_inv', ←mul_assoc, h, mul_assoc, mul_inv_cancel hx0, mul_assoc, inv_mul_cancel hx0]), begin rw [geom_sum h₁, div_eq_iff_mul_eq h₂, ← domain.mul_left_inj h₃, ← mul_assoc, ← mul_assoc, mul_inv_cancel h₃], simp [mul_add, add_mul, mul_inv_cancel hx0, mul_assoc, h₄, sub_eq_add_neg, add_comm, add_left_comm], end
6a000689a7a99c00cb719919b3db8659f2a5545c
076f5040b63237c6dd928c6401329ed5adcb0e44
/assignments/hw1_up_and_running.lean
005384797b42d1b948788f3ebd0fb2191b4caa0c
[]
no_license
kevinsullivan/uva-cs-dm-f19
0f123689cf6cb078f263950b18382a7086bf30be
09a950752884bd7ade4be33e9e89a2c4b1927167
refs/heads/master
1,594,771,841,541
1,575,853,850,000
1,575,853,850,000
205,433,890
4
9
null
1,571,592,121,000
1,567,188,539,000
Lean
UTF-8
Lean
false
false
3,523
lean
/- This text isn't formal logic or code. It's natural language text embedded into a formal logic or code file, otherwise known as a "comment". This comment explains what to for this assignment. Do not make any changes to this file. Rather, make a copy of it in the "mywork" directory. The close this panel so that you're sure you're working on the copy in the mywork directory. To do this, right click on "hw1_up_and_running.lean" and select Copy. Then right click on "mywork" and select Paste. Each of the following lines of logic asserts that an identifer (such as n on the first line) can be found to a value of a specific type (such as natural number, written in our formal logic as nat or ℕ). Your job is to "prove" that each such assertion can be made true by filling in a specific value of the required type in place of the placeholders, denoted by underscores. These formal name for placeholders is meta-variables. In other words, fill in values of the right types in place of the specified metavariables. Then save your file and upload it as your submission for the Homework #1 assignment on Collab. The purpose of this assignment is to make sure that you've got everything working that you'll need to be able to get your work done in this class. This assignment is due by 9AM on Thursday, Sept. 5. Note, values of type ℕ include 0, 1, 2, 33, etc. Values of type string include "Hello, World!" and "This is really fun!" And the two values of type bool, in Lean, are tt (for true) and ff (for false). -/ /- Edit this code by replacing the placeholders with specific values. Note the red squiggly underlines under the underscores (placeholders). They indicate an error condition, specifically Lean doesn't know enough to fill in (synthesize) values. That's why we're asking you to fill them in. -/ def n : nat := _ def s : string := _ def b : bool := _ /- Have you succeeded? To know the answer, you should first check that the red underlines above have gone away. Second, both the #check and #eval commands below should be without errors. The #check command tells you the type of value that is expected to be bound to an identifier. The #eval command tells you what value is bound. If no value is bound because you've used a placeholder instead of a value, the #eval commands will fail (with a red squiggle) and an error message saying that you used "sorry" instead of a real value. In Lean "sorry" can be used instead of an underscore to represent a placeholder. Give it a try. -/ #check n #eval n #check s #eval s #check b #eval b /- Here's an important insight. In each of the three little problems above, you had to *prove* that a value of a specific type could be bound to a given identifier. The values that you picked can thus be though of as very simple proofs! Not every type can be proved. For example, there is a type called False that has n o proof (no values). You can declare an identifier f to be bound to a value of this type, and you can use a placeholder to tell Lean to accept your definition, but you'll never be able to fill in the placeholder because there is no proof of the type, False. For extra credit, declare the identifier F to be bound to a proof of False, using a placeholder where the proof (value) would go. -/ /- When you're done and there are no red underlines in the logic/code we've provided to you, you may submit your file. (If you did the extra credit of course there will be a red squiggle under where a value of type False is expected.) -/
498bb2a5937c7bacc260d1e8f337f734671215f5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/euclidean/angle/oriented/basic.lean
0ac7bef1188272cc1c92e36a65875fdf5f3f9674
[ "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
66,616
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Heather Macbeth -/ import analysis.inner_product_space.two_dim import analysis.special_functions.complex.circle import geometry.euclidean.angle.unoriented.basic /-! # Oriented angles. This file defines oriented angles in real inner product spaces. ## Main definitions * `orientation.oangle` is the oriented angle between two vectors with respect to an orientation. * `orientation.rotation` is the rotation by an oriented angle with respect to an orientation. ## Implementation notes The definitions here use the `real.angle` type, angles modulo `2 * π`. For some purposes, angles modulo `π` are more convenient, because results are true for such angles with less configuration dependence. Results that are only equalities modulo `π` can be represented modulo `2 * π` as equalities of `(2 : ℤ) • θ`. ## References * Evan Chen, Euclidean Geometry in Mathematical Olympiads. -/ noncomputable theory open finite_dimensional complex open_locale real real_inner_product_space complex_conjugate namespace orientation local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ local attribute [instance] complex.finrank_real_complex_fact variables {V V' : Type*} [inner_product_space ℝ V] [inner_product_space ℝ V'] variables [fact (finrank ℝ V = 2)] [fact (finrank ℝ V' = 2)] (o : orientation ℝ V (fin 2)) local notation `ω` := o.area_form local notation `J` := o.right_angle_rotation /-- The oriented angle from `x` to `y`, modulo `2 * π`. If either vector is 0, this is 0. See `inner_product_geometry.angle` for the corresponding unoriented angle definition. -/ def oangle (x y : V) : real.angle := complex.arg (o.kahler x y) /-- Oriented angles are continuous when the vectors involved are nonzero. -/ lemma continuous_at_oangle {x : V × V} (hx1 : x.1 ≠ 0) (hx2 : x.2 ≠ 0) : continuous_at (λ y : V × V, o.oangle y.1 y.2) x := begin refine (complex.continuous_at_arg_coe_angle _).comp _, { exact o.kahler_ne_zero hx1 hx2 }, exact ((continuous_of_real.comp continuous_inner).add ((continuous_of_real.comp o.area_form'.continuous₂).mul continuous_const)).continuous_at, end /-- If the first vector passed to `oangle` is 0, the result is 0. -/ @[simp] lemma oangle_zero_left (x : V) : o.oangle 0 x = 0 := by simp [oangle] /-- If the second vector passed to `oangle` is 0, the result is 0. -/ @[simp] lemma oangle_zero_right (x : V) : o.oangle x 0 = 0 := by simp [oangle] /-- If the two vectors passed to `oangle` are the same, the result is 0. -/ @[simp] lemma oangle_self (x : V) : o.oangle x x = 0 := begin simp only [oangle, kahler_apply_self, ← complex.of_real_pow], convert quotient_add_group.coe_zero _, apply arg_of_real_of_nonneg, positivity, end /-- If the angle between two vectors is nonzero, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : x ≠ 0 := by { rintro rfl, simpa using h } /-- If the angle between two vectors is nonzero, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : y ≠ 0 := by { rintro rfl, simpa using h } /-- If the angle between two vectors is nonzero, the vectors are not equal. -/ lemma ne_of_oangle_ne_zero {x y : V} (h : o.oangle x y ≠ 0) : x ≠ y := by { rintro rfl, simpa using h } /-- If the angle between two vectors is `π`, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ real.angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π`, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ real.angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π`, the vectors are not equal. -/ lemma ne_of_oangle_eq_pi {x y : V} (h : o.oangle x y = π) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ real.angle.pi_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ real.angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ real.angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `π / 2`, the vectors are not equal. -/ lemma ne_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ real.angle.pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (h.symm ▸ real.angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (h.symm ▸ real.angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the angle between two vectors is `-π / 2`, the vectors are not equal. -/ lemma ne_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : x ≠ y := o.ne_of_oangle_ne_zero (h.symm ▸ real.angle.neg_pi_div_two_ne_zero : o.oangle x y ≠ 0) /-- If the sign of the angle between two vectors is nonzero, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : x ≠ 0 := o.left_ne_zero_of_oangle_ne_zero (real.angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is nonzero, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : y ≠ 0 := o.right_ne_zero_of_oangle_ne_zero (real.angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is nonzero, the vectors are not equal. -/ lemma ne_of_oangle_sign_ne_zero {x y : V} (h : (o.oangle x y).sign ≠ 0) : x ≠ y := o.ne_of_oangle_ne_zero (real.angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between two vectors is positive, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : x ≠ 0 := o.left_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is positive, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : y ≠ 0 := o.right_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is positive, the vectors are not equal. -/ lemma ne_of_oangle_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : x ≠ y := o.ne_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the first vector is nonzero. -/ lemma left_ne_zero_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : x ≠ 0 := o.left_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the second vector is nonzero. -/ lemma right_ne_zero_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : y ≠ 0 := o.right_ne_zero_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (o.oangle x y).sign ≠ 0) /-- If the sign of the angle between two vectors is negative, the vectors are not equal. -/ lemma ne_of_oangle_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : x ≠ y := o.ne_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (o.oangle x y).sign ≠ 0) /-- Swapping the two vectors passed to `oangle` negates the angle. -/ lemma oangle_rev (x y : V) : o.oangle y x = -o.oangle x y := by simp only [oangle, o.kahler_swap y x, complex.arg_conj_coe_angle] /-- Adding the angles between two vectors in each order results in 0. -/ @[simp] lemma oangle_add_oangle_rev (x y : V) : o.oangle x y + o.oangle y x = 0 := by simp [o.oangle_rev y x] /-- Negating the first vector passed to `oangle` adds `π` to the angle. -/ lemma oangle_neg_left {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : o.oangle (-x) y = o.oangle x y + π := begin simp only [oangle, map_neg], convert complex.arg_neg_coe_angle _, exact o.kahler_ne_zero hx hy, end /-- Negating the second vector passed to `oangle` adds `π` to the angle. -/ lemma oangle_neg_right {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : o.oangle x (-y) = o.oangle x y + π := begin simp only [oangle, map_neg], convert complex.arg_neg_coe_angle _, exact o.kahler_ne_zero hx hy, end /-- Negating the first vector passed to `oangle` does not change twice the angle. -/ @[simp] lemma two_zsmul_oangle_neg_left (x y : V) : (2 : ℤ) • o.oangle (-x) y = (2 : ℤ) • o.oangle x y := begin by_cases hx : x = 0, { simp [hx] }, { by_cases hy : y = 0, { simp [hy] }, { simp [o.oangle_neg_left hx hy] } } end /-- Negating the second vector passed to `oangle` does not change twice the angle. -/ @[simp] lemma two_zsmul_oangle_neg_right (x y : V) : (2 : ℤ) • o.oangle x (-y) = (2 : ℤ) • o.oangle x y := begin by_cases hx : x = 0, { simp [hx] }, { by_cases hy : y = 0, { simp [hy] }, { simp [o.oangle_neg_right hx hy] } } end /-- Negating both vectors passed to `oangle` does not change the angle. -/ @[simp] lemma oangle_neg_neg (x y : V) : o.oangle (-x) (-y) = o.oangle x y := by simp [oangle] /-- Negating the first vector produces the same angle as negating the second vector. -/ lemma oangle_neg_left_eq_neg_right (x y : V) : o.oangle (-x) y = o.oangle x (-y) := by rw [←neg_neg y, oangle_neg_neg, neg_neg] /-- The angle between the negation of a nonzero vector and that vector is `π`. -/ @[simp] lemma oangle_neg_self_left {x : V} (hx : x ≠ 0) : o.oangle (-x) x = π := by simp [oangle_neg_left, hx] /-- The angle between a nonzero vector and its negation is `π`. -/ @[simp] lemma oangle_neg_self_right {x : V} (hx : x ≠ 0) : o.oangle x (-x) = π := by simp [oangle_neg_right, hx] /-- Twice the angle between the negation of a vector and that vector is 0. -/ @[simp] lemma two_zsmul_oangle_neg_self_left (x : V) : (2 : ℤ) • o.oangle (-x) x = 0 := begin by_cases hx : x = 0; simp [hx] end /-- Twice the angle between a vector and its negation is 0. -/ @[simp] lemma two_zsmul_oangle_neg_self_right (x : V) : (2 : ℤ) • o.oangle x (-x) = 0 := begin by_cases hx : x = 0; simp [hx] end /-- Adding the angles between two vectors in each order, with the first vector in each angle negated, results in 0. -/ @[simp] lemma oangle_add_oangle_rev_neg_left (x y : V) : o.oangle (-x) y + o.oangle (-y) x = 0 := by rw [oangle_neg_left_eq_neg_right, oangle_rev, add_left_neg] /-- Adding the angles between two vectors in each order, with the second vector in each angle negated, results in 0. -/ @[simp] lemma oangle_add_oangle_rev_neg_right (x y : V) : o.oangle x (-y) + o.oangle y (-x) = 0 := by rw [o.oangle_rev (-x), oangle_neg_left_eq_neg_right, add_neg_self] /-- Multiplying the first vector passed to `oangle` by a positive real does not change the angle. -/ @[simp] lemma oangle_smul_left_of_pos (x y : V) {r : ℝ} (hr : 0 < r) : o.oangle (r • x) y = o.oangle x y := by simp [oangle, complex.arg_real_mul _ hr] /-- Multiplying the second vector passed to `oangle` by a positive real does not change the angle. -/ @[simp] lemma oangle_smul_right_of_pos (x y : V) {r : ℝ} (hr : 0 < r) : o.oangle x (r • y) = o.oangle x y := by simp [oangle, complex.arg_real_mul _ hr] /-- Multiplying the first vector passed to `oangle` by a negative real produces the same angle as negating that vector. -/ @[simp] lemma oangle_smul_left_of_neg (x y : V) {r : ℝ} (hr : r < 0) : o.oangle (r • x) y = o.oangle (-x) y := by rw [←neg_neg r, neg_smul, ←smul_neg, o.oangle_smul_left_of_pos _ _ (neg_pos_of_neg hr)] /-- Multiplying the second vector passed to `oangle` by a negative real produces the same angle as negating that vector. -/ @[simp] lemma oangle_smul_right_of_neg (x y : V) {r : ℝ} (hr : r < 0) : o.oangle x (r • y) = o.oangle x (-y) := by rw [←neg_neg r, neg_smul, ←smul_neg, o.oangle_smul_right_of_pos _ _ (neg_pos_of_neg hr)] /-- The angle between a nonnegative multiple of a vector and that vector is 0. -/ @[simp] lemma oangle_smul_left_self_of_nonneg (x : V) {r : ℝ} (hr : 0 ≤ r) : o.oangle (r • x) x = 0 := begin rcases hr.lt_or_eq with (h|h), { simp [h] }, { simp [h.symm] } end /-- The angle between a vector and a nonnegative multiple of that vector is 0. -/ @[simp] lemma oangle_smul_right_self_of_nonneg (x : V) {r : ℝ} (hr : 0 ≤ r) : o.oangle x (r • x) = 0 := begin rcases hr.lt_or_eq with (h|h), { simp [h] }, { simp [h.symm] } end /-- The angle between two nonnegative multiples of the same vector is 0. -/ @[simp] lemma oangle_smul_smul_self_of_nonneg (x : V) {r₁ r₂ : ℝ} (hr₁ : 0 ≤ r₁) (hr₂ : 0 ≤ r₂) : o.oangle (r₁ • x) (r₂ • x) = 0 := begin rcases hr₁.lt_or_eq with (h|h), { simp [h, hr₂] }, { simp [h.symm] } end /-- Multiplying the first vector passed to `oangle` by a nonzero real does not change twice the angle. -/ @[simp] lemma two_zsmul_oangle_smul_left_of_ne_zero (x y : V) {r : ℝ} (hr : r ≠ 0) : (2 : ℤ) • o.oangle (r • x) y = (2 : ℤ) • o.oangle x y := begin rcases hr.lt_or_lt with (h|h); simp [h] end /-- Multiplying the second vector passed to `oangle` by a nonzero real does not change twice the angle. -/ @[simp] lemma two_zsmul_oangle_smul_right_of_ne_zero (x y : V) {r : ℝ} (hr : r ≠ 0) : (2 : ℤ) • o.oangle x (r • y) = (2 : ℤ) • o.oangle x y := begin rcases hr.lt_or_lt with (h|h); simp [h] end /-- Twice the angle between a multiple of a vector and that vector is 0. -/ @[simp] lemma two_zsmul_oangle_smul_left_self (x : V) {r : ℝ} : (2 : ℤ) • o.oangle (r • x) x = 0 := begin rcases lt_or_le r 0 with (h|h); simp [h] end /-- Twice the angle between a vector and a multiple of that vector is 0. -/ @[simp] lemma two_zsmul_oangle_smul_right_self (x : V) {r : ℝ} : (2 : ℤ) • o.oangle x (r • x) = 0 := begin rcases lt_or_le r 0 with (h|h); simp [h] end /-- Twice the angle between two multiples of a vector is 0. -/ @[simp] lemma two_zsmul_oangle_smul_smul_self (x : V) {r₁ r₂ : ℝ} : (2 : ℤ) • o.oangle (r₁ • x) (r₂ • x) = 0 := begin by_cases h : r₁ = 0; simp [h] end /-- If the spans of two vectors are equal, twice angles with those vectors on the left are equal. -/ lemma two_zsmul_oangle_left_of_span_eq {x y : V} (z : V) (h : (ℝ ∙ x) = ℝ ∙ y) : (2 : ℤ) • o.oangle x z = (2 : ℤ) • o.oangle y z := begin rw submodule.span_singleton_eq_span_singleton at h, rcases h with ⟨r, rfl⟩, exact (o.two_zsmul_oangle_smul_left_of_ne_zero _ _ (units.ne_zero _)).symm end /-- If the spans of two vectors are equal, twice angles with those vectors on the right are equal. -/ lemma two_zsmul_oangle_right_of_span_eq (x : V) {y z : V} (h : (ℝ ∙ y) = ℝ ∙ z) : (2 : ℤ) • o.oangle x y = (2 : ℤ) • o.oangle x z := begin rw submodule.span_singleton_eq_span_singleton at h, rcases h with ⟨r, rfl⟩, exact (o.two_zsmul_oangle_smul_right_of_ne_zero _ _ (units.ne_zero _)).symm end /-- If the spans of two pairs of vectors are equal, twice angles between those vectors are equal. -/ lemma two_zsmul_oangle_of_span_eq_of_span_eq {w x y z : V} (hwx : (ℝ ∙ w) = ℝ ∙ x) (hyz : (ℝ ∙ y) = ℝ ∙ z) : (2 : ℤ) • o.oangle w y = (2 : ℤ) • o.oangle x z := by rw [(o).two_zsmul_oangle_left_of_span_eq y hwx, (o).two_zsmul_oangle_right_of_span_eq x hyz] /-- The oriented angle between two vectors is zero if and only if the angle with the vectors swapped is zero. -/ lemma oangle_eq_zero_iff_oangle_rev_eq_zero {x y : V} : o.oangle x y = 0 ↔ o.oangle y x = 0 := by rw [oangle_rev, neg_eq_zero] /-- The oriented angle between two vectors is zero if and only if they are on the same ray. -/ lemma oangle_eq_zero_iff_same_ray {x y : V} : o.oangle x y = 0 ↔ same_ray ℝ x y := begin rw [oangle, kahler_apply_apply, complex.arg_coe_angle_eq_iff_eq_to_real, real.angle.to_real_zero, complex.arg_eq_zero_iff], simpa using o.nonneg_inner_and_area_form_eq_zero_iff_same_ray x y, end /-- The oriented angle between two vectors is `π` if and only if the angle with the vectors swapped is `π`. -/ lemma oangle_eq_pi_iff_oangle_rev_eq_pi {x y : V} : o.oangle x y = π ↔ o.oangle y x = π := by rw [oangle_rev, neg_eq_iff_neg_eq, eq_comm, real.angle.neg_coe_pi] /-- The oriented angle between two vectors is `π` if and only they are nonzero and the first is on the same ray as the negation of the second. -/ lemma oangle_eq_pi_iff_same_ray_neg {x y : V} : o.oangle x y = π ↔ x ≠ 0 ∧ y ≠ 0 ∧ same_ray ℝ x (-y) := begin rw [←o.oangle_eq_zero_iff_same_ray], split, { intro h, by_cases hx : x = 0, { simpa [hx, real.angle.pi_ne_zero.symm] using h }, by_cases hy : y = 0, { simpa [hy, real.angle.pi_ne_zero.symm] using h }, refine ⟨hx, hy, _⟩, rw [o.oangle_neg_right hx hy, h, real.angle.coe_pi_add_coe_pi] }, { rintro ⟨hx, hy, h⟩, rwa [o.oangle_neg_right hx hy, ←real.angle.sub_coe_pi_eq_add_coe_pi, sub_eq_zero] at h } end /-- The oriented angle between two vectors is zero or `π` if and only if those two vectors are not linearly independent. -/ lemma oangle_eq_zero_or_eq_pi_iff_not_linear_independent {x y : V} : (o.oangle x y = 0 ∨ o.oangle x y = π) ↔ ¬ linear_independent ℝ ![x, y] := by rw [oangle_eq_zero_iff_same_ray, oangle_eq_pi_iff_same_ray_neg, same_ray_or_ne_zero_and_same_ray_neg_iff_not_linear_independent] /-- The oriented angle between two vectors is zero or `π` if and only if the first vector is zero or the second is a multiple of the first. -/ lemma oangle_eq_zero_or_eq_pi_iff_right_eq_smul {x y : V} : (o.oangle x y = 0 ∨ o.oangle x y = π) ↔ (x = 0 ∨ ∃ r : ℝ, y = r • x) := begin rw [oangle_eq_zero_iff_same_ray, oangle_eq_pi_iff_same_ray_neg], refine ⟨λ h, _, λ h, _⟩, { rcases h with h|⟨-, -, h⟩, { by_cases hx : x = 0, { simp [hx] }, obtain ⟨r, -, rfl⟩ := h.exists_nonneg_left hx, exact or.inr ⟨r, rfl⟩ }, { by_cases hx : x = 0, { simp [hx] }, obtain ⟨r, -, hy⟩ := h.exists_nonneg_left hx, refine or.inr ⟨-r, _⟩, simp [hy] } }, { rcases h with rfl|⟨r, rfl⟩, { simp }, by_cases hx : x = 0, { simp [hx] }, rcases lt_trichotomy r 0 with hr|hr|hr, { rw ←neg_smul, exact or.inr ⟨hx, smul_ne_zero hr.ne hx, same_ray_pos_smul_right x (left.neg_pos_iff.2 hr)⟩ }, { simp [hr] }, { exact or.inl (same_ray_pos_smul_right x hr) } } end /-- The oriented angle between two vectors is not zero or `π` if and only if those two vectors are linearly independent. -/ lemma oangle_ne_zero_and_ne_pi_iff_linear_independent {x y : V} : (o.oangle x y ≠ 0 ∧ o.oangle x y ≠ π) ↔ linear_independent ℝ ![x, y] := by rw [←not_or_distrib, ←not_iff_not, not_not, oangle_eq_zero_or_eq_pi_iff_not_linear_independent] /-- Two vectors are equal if and only if they have equal norms and zero angle between them. -/ lemma eq_iff_norm_eq_and_oangle_eq_zero (x y : V) : x = y ↔ ‖x‖ = ‖y‖ ∧ o.oangle x y = 0 := begin rw oangle_eq_zero_iff_same_ray, split, { rintros rfl, simp }, { rcases eq_or_ne y 0 with rfl | hy, { simp }, rintros ⟨h₁, h₂⟩, obtain ⟨r, hr, rfl⟩ := h₂.exists_nonneg_right hy, have : ‖y‖ ≠ 0 := by simpa using hy, obtain rfl : r = 1, { apply mul_right_cancel₀ this, simpa [norm_smul, _root_.abs_of_nonneg hr] using h₁ }, simp }, end /-- Two vectors with equal norms are equal if and only if they have zero angle between them. -/ lemma eq_iff_oangle_eq_zero_of_norm_eq {x y : V} (h : ‖x‖ = ‖y‖) : x = y ↔ o.oangle x y = 0 := ⟨λ he, ((o.eq_iff_norm_eq_and_oangle_eq_zero x y).1 he).2, λ ha, (o.eq_iff_norm_eq_and_oangle_eq_zero x y).2 ⟨h, ha⟩⟩ /-- Two vectors with zero angle between them are equal if and only if they have equal norms. -/ lemma eq_iff_norm_eq_of_oangle_eq_zero {x y : V} (h : o.oangle x y = 0) : x = y ↔ ‖x‖ = ‖y‖ := ⟨λ he, ((o.eq_iff_norm_eq_and_oangle_eq_zero x y).1 he).1, λ hn, (o.eq_iff_norm_eq_and_oangle_eq_zero x y).2 ⟨hn, h⟩⟩ /-- Given three nonzero vectors, the angle between the first and the second plus the angle between the second and the third equals the angle between the first and the third. -/ @[simp] lemma oangle_add {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle x y + o.oangle y z = o.oangle x z := begin simp_rw [oangle], rw [←complex.arg_mul_coe_angle, o.kahler_mul y x z], congr' 1, convert complex.arg_real_mul _ (_ : 0 < ‖y‖ ^ 2) using 2, { norm_cast }, { have : 0 < ‖y‖ := by simpa using hy, positivity }, { exact o.kahler_ne_zero hx hy, }, { exact o.kahler_ne_zero hy hz } end /-- Given three nonzero vectors, the angle between the second and the third plus the angle between the first and the second equals the angle between the first and the third. -/ @[simp] lemma oangle_add_swap {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle y z + o.oangle x y = o.oangle x z := by rw [add_comm, o.oangle_add hx hy hz] /-- Given three nonzero vectors, the angle between the first and the third minus the angle between the first and the second equals the angle between the second and the third. -/ @[simp] lemma oangle_sub_left {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle x z - o.oangle x y = o.oangle y z := by rw [sub_eq_iff_eq_add, o.oangle_add_swap hx hy hz] /-- Given three nonzero vectors, the angle between the first and the third minus the angle between the second and the third equals the angle between the first and the second. -/ @[simp] lemma oangle_sub_right {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle x z - o.oangle y z = o.oangle x y := by rw [sub_eq_iff_eq_add, o.oangle_add hx hy hz] /-- Given three nonzero vectors, adding the angles between them in cyclic order results in 0. -/ @[simp] lemma oangle_add_cyc3 {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle x y + o.oangle y z + o.oangle z x = 0 := by simp [hx, hy, hz] /-- Given three nonzero vectors, adding the angles between them in cyclic order, with the first vector in each angle negated, results in π. If the vectors add to 0, this is a version of the sum of the angles of a triangle. -/ @[simp] lemma oangle_add_cyc3_neg_left {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle (-x) y + o.oangle (-y) z + o.oangle (-z) x = π := by rw [o.oangle_neg_left hx hy, o.oangle_neg_left hy hz, o.oangle_neg_left hz hx, (show o.oangle x y + π + (o.oangle y z + π) + (o.oangle z x + π) = o.oangle x y + o.oangle y z + o.oangle z x + (π + π + π : real.angle), by abel), o.oangle_add_cyc3 hx hy hz, real.angle.coe_pi_add_coe_pi, zero_add, zero_add] /-- Given three nonzero vectors, adding the angles between them in cyclic order, with the second vector in each angle negated, results in π. If the vectors add to 0, this is a version of the sum of the angles of a triangle. -/ @[simp] lemma oangle_add_cyc3_neg_right {x y z : V} (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) : o.oangle x (-y) + o.oangle y (-z) + o.oangle z (-x) = π := by simp_rw [←oangle_neg_left_eq_neg_right, o.oangle_add_cyc3_neg_left hx hy hz] /-- Pons asinorum, oriented vector angle form. -/ lemma oangle_sub_eq_oangle_sub_rev_of_norm_eq {x y : V} (h : ‖x‖ = ‖y‖) : o.oangle x (x - y) = o.oangle (y - x) y := by simp [oangle, h] /-- The angle at the apex of an isosceles triangle is `π` minus twice a base angle, oriented vector angle form. -/ lemma oangle_eq_pi_sub_two_zsmul_oangle_sub_of_norm_eq {x y : V} (hn : x ≠ y) (h : ‖x‖ = ‖y‖) : o.oangle y x = π - (2 : ℤ) • o.oangle (y - x) y := begin rw two_zsmul, rw [←o.oangle_sub_eq_oangle_sub_rev_of_norm_eq h] { occs := occurrences.pos [1] }, rw [eq_sub_iff_add_eq, ←oangle_neg_neg, ←add_assoc], have hy : y ≠ 0, { rintro rfl, rw [norm_zero, norm_eq_zero] at h, exact hn h }, have hx : x ≠ 0 := norm_ne_zero_iff.1 (h.symm ▸ norm_ne_zero_iff.2 hy), convert o.oangle_add_cyc3_neg_right (neg_ne_zero.2 hy) hx (sub_ne_zero_of_ne hn.symm); simp end /-- Auxiliary construction to build a rotation by the oriented angle `θ`. -/ def rotation_aux (θ : real.angle) : V →ₗᵢ[ℝ] V := linear_map.isometry_of_inner (real.angle.cos θ • linear_map.id + real.angle.sin θ • ↑(linear_isometry_equiv.to_linear_equiv J)) begin intros x y, simp only [is_R_or_C.conj_to_real, id.def, linear_map.smul_apply, linear_map.add_apply, linear_map.id_coe, linear_equiv.coe_coe, linear_isometry_equiv.coe_to_linear_equiv, orientation.area_form_right_angle_rotation_left, orientation.inner_right_angle_rotation_left, orientation.inner_right_angle_rotation_right, inner_add_left, inner_smul_left, inner_add_right, inner_smul_right], linear_combination inner x y * θ.cos_sq_add_sin_sq, end @[simp] lemma rotation_aux_apply (θ : real.angle) (x : V) : o.rotation_aux θ x = real.angle.cos θ • x + real.angle.sin θ • J x := rfl /-- A rotation by the oriented angle `θ`. -/ def rotation (θ : real.angle) : V ≃ₗᵢ[ℝ] V := linear_isometry_equiv.of_linear_isometry (o.rotation_aux θ) (real.angle.cos θ • linear_map.id - real.angle.sin θ • ↑(linear_isometry_equiv.to_linear_equiv J)) begin ext x, convert congr_arg (λ t : ℝ, t • x) θ.cos_sq_add_sin_sq using 1, { simp only [o.right_angle_rotation_right_angle_rotation, o.rotation_aux_apply, function.comp_app, id.def, linear_equiv.coe_coe, linear_isometry.coe_to_linear_map, linear_isometry_equiv.coe_to_linear_equiv, map_smul, map_sub, linear_map.coe_comp, linear_map.id_coe, linear_map.smul_apply, linear_map.sub_apply, ← mul_smul, add_smul, smul_add, smul_neg, smul_sub, mul_comm, sq], abel }, { simp }, end begin ext x, convert congr_arg (λ t : ℝ, t • x) θ.cos_sq_add_sin_sq using 1, { simp only [o.right_angle_rotation_right_angle_rotation, o.rotation_aux_apply, function.comp_app, id.def, linear_equiv.coe_coe, linear_isometry.coe_to_linear_map, linear_isometry_equiv.coe_to_linear_equiv, map_add, map_smul, linear_map.coe_comp, linear_map.id_coe, linear_map.smul_apply, linear_map.sub_apply, add_smul, ← mul_smul, mul_comm, smul_add, smul_neg, sq], abel }, { simp }, end lemma rotation_apply (θ : real.angle) (x : V) : o.rotation θ x = real.angle.cos θ • x + real.angle.sin θ • J x := rfl lemma rotation_symm_apply (θ : real.angle) (x : V) : (o.rotation θ).symm x = real.angle.cos θ • x - real.angle.sin θ • J x := rfl attribute [irreducible] rotation lemma rotation_eq_matrix_to_lin (θ : real.angle) {x : V} (hx : x ≠ 0) : (o.rotation θ).to_linear_map = matrix.to_lin (o.basis_right_angle_rotation x hx) (o.basis_right_angle_rotation x hx) !![θ.cos, -θ.sin; θ.sin, θ.cos] := begin apply (o.basis_right_angle_rotation x hx).ext, intros i, fin_cases i, { rw matrix.to_lin_self, simp [rotation_apply, fin.sum_univ_succ] }, { rw matrix.to_lin_self, simp [rotation_apply, fin.sum_univ_succ, add_comm] }, end /-- The determinant of `rotation` (as a linear map) is equal to `1`. -/ @[simp] lemma det_rotation (θ : real.angle) : (o.rotation θ).to_linear_map.det = 1 := begin haveI : nontrivial V := finite_dimensional.nontrivial_of_finrank_eq_succ (fact.out (finrank ℝ V = 2)), obtain ⟨x, hx⟩ : ∃ x, x ≠ (0:V) := exists_ne (0:V), rw o.rotation_eq_matrix_to_lin θ hx, simpa [sq] using θ.cos_sq_add_sin_sq, end /-- The determinant of `rotation` (as a linear equiv) is equal to `1`. -/ @[simp] lemma linear_equiv_det_rotation (θ : real.angle) : (o.rotation θ).to_linear_equiv.det = 1 := units.ext $ o.det_rotation θ /-- The inverse of `rotation` is rotation by the negation of the angle. -/ @[simp] lemma rotation_symm (θ : real.angle) : (o.rotation θ).symm = o.rotation (-θ) := by ext; simp [o.rotation_apply, o.rotation_symm_apply, sub_eq_add_neg] /-- Rotation by 0 is the identity. -/ @[simp] lemma rotation_zero : o.rotation 0 = linear_isometry_equiv.refl ℝ V := by ext; simp [rotation] /-- Rotation by π is negation. -/ @[simp] lemma rotation_pi : o.rotation π = linear_isometry_equiv.neg ℝ := begin ext x, simp [rotation] end /-- Rotation by π is negation. -/ lemma rotation_pi_apply (x : V) : o.rotation π x = -x := by simp /-- Rotation by π / 2 is the "right-angle-rotation" map `J`. -/ lemma rotation_pi_div_two : o.rotation (π / 2 : ℝ) = J := begin ext x, simp [rotation], end /-- Rotating twice is equivalent to rotating by the sum of the angles. -/ @[simp] lemma rotation_rotation (θ₁ θ₂ : real.angle) (x : V) : o.rotation θ₁ (o.rotation θ₂ x) = o.rotation (θ₁ + θ₂) x := begin simp only [o.rotation_apply, ←mul_smul, real.angle.cos_add, real.angle.sin_add, add_smul, sub_smul, linear_isometry_equiv.trans_apply, smul_add, linear_isometry_equiv.map_add, linear_isometry_equiv.map_smul, right_angle_rotation_right_angle_rotation, smul_neg], ring_nf, abel, end /-- Rotating twice is equivalent to rotating by the sum of the angles. -/ @[simp] lemma rotation_trans (θ₁ θ₂ : real.angle) : (o.rotation θ₁).trans (o.rotation θ₂) = o.rotation (θ₂ + θ₁) := linear_isometry_equiv.ext $ λ _, by rw [←rotation_rotation, linear_isometry_equiv.trans_apply] /-- Rotating the first of two vectors by `θ` scales their Kahler form by `cos θ - sin θ * I`. -/ @[simp] lemma kahler_rotation_left (x y : V) (θ : real.angle) : o.kahler (o.rotation θ x) y = conj (θ.exp_map_circle : ℂ) * o.kahler x y := begin simp only [o.rotation_apply, map_add, map_mul, linear_map.map_smulₛₗ, ring_hom.id_apply, linear_map.add_apply, linear_map.smul_apply, real_smul, kahler_right_angle_rotation_left, real.angle.coe_exp_map_circle, is_R_or_C.conj_of_real, conj_I], ring, end /-- Negating a rotation is equivalent to rotation by π plus the angle. -/ lemma neg_rotation (θ : real.angle) (x : V) : -o.rotation θ x = o.rotation (π + θ) x := by rw [←o.rotation_pi_apply, rotation_rotation] /-- Negating a rotation by -π / 2 is equivalent to rotation by π / 2. -/ @[simp] lemma neg_rotation_neg_pi_div_two (x : V) : -o.rotation (-π / 2 : ℝ) x = o.rotation (π / 2 : ℝ) x := by rw [neg_rotation, ←real.angle.coe_add, neg_div, ←sub_eq_add_neg, sub_half] /-- Negating a rotation by π / 2 is equivalent to rotation by -π / 2. -/ lemma neg_rotation_pi_div_two (x : V) : -o.rotation (π / 2 : ℝ) x = o.rotation (-π / 2 : ℝ) x := neg_eq_iff_neg_eq.1 $ o.neg_rotation_neg_pi_div_two _ /-- Rotating the first of two vectors by `θ` scales their Kahler form by `cos (-θ) + sin (-θ) * I`. -/ lemma kahler_rotation_left' (x y : V) (θ : real.angle) : o.kahler (o.rotation θ x) y = (-θ).exp_map_circle * o.kahler x y := by simpa [coe_inv_circle_eq_conj, -kahler_rotation_left] using o.kahler_rotation_left x y θ /-- Rotating the second of two vectors by `θ` scales their Kahler form by `cos θ + sin θ * I`. -/ @[simp] lemma kahler_rotation_right (x y : V) (θ : real.angle) : o.kahler x (o.rotation θ y) = θ.exp_map_circle * o.kahler x y := begin simp only [o.rotation_apply, map_add, linear_map.map_smulₛₗ, ring_hom.id_apply, real_smul, kahler_right_angle_rotation_right, real.angle.coe_exp_map_circle], ring, end /-- Rotating the first vector by `θ` subtracts `θ` from the angle between two vectors. -/ @[simp] lemma oangle_rotation_left {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) (θ : real.angle) : o.oangle (o.rotation θ x) y = o.oangle x y - θ := begin simp only [oangle, o.kahler_rotation_left'], rw [complex.arg_mul_coe_angle, real.angle.arg_exp_map_circle], { abel }, { exact ne_zero_of_mem_circle _ }, { exact o.kahler_ne_zero hx hy }, end /-- Rotating the second vector by `θ` adds `θ` to the angle between two vectors. -/ @[simp] lemma oangle_rotation_right {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) (θ : real.angle) : o.oangle x (o.rotation θ y) = o.oangle x y + θ := begin simp only [oangle, o.kahler_rotation_right], rw [complex.arg_mul_coe_angle, real.angle.arg_exp_map_circle], { abel }, { exact ne_zero_of_mem_circle _ }, { exact o.kahler_ne_zero hx hy }, end /-- The rotation of a vector by `θ` has an angle of `-θ` from that vector. -/ @[simp] lemma oangle_rotation_self_left {x : V} (hx : x ≠ 0) (θ : real.angle) : o.oangle (o.rotation θ x) x = -θ := by simp [hx] /-- A vector has an angle of `θ` from the rotation of that vector by `θ`. -/ @[simp] lemma oangle_rotation_self_right {x : V} (hx : x ≠ 0) (θ : real.angle) : o.oangle x (o.rotation θ x) = θ := by simp [hx] /-- Rotating the first vector by the angle between the two vectors results an an angle of 0. -/ @[simp] lemma oangle_rotation_oangle_left (x y : V) : o.oangle (o.rotation (o.oangle x y) x) y = 0 := begin by_cases hx : x = 0, { simp [hx] }, { by_cases hy : y = 0, { simp [hy] }, { simp [hx, hy] } } end /-- Rotating the first vector by the angle between the two vectors and swapping the vectors results an an angle of 0. -/ @[simp] lemma oangle_rotation_oangle_right (x y : V) : o.oangle y (o.rotation (o.oangle x y) x) = 0 := begin rw [oangle_rev], simp end /-- Rotating both vectors by the same angle does not change the angle between those vectors. -/ @[simp] lemma oangle_rotation (x y : V) (θ : real.angle) : o.oangle (o.rotation θ x) (o.rotation θ y) = o.oangle x y := begin by_cases hx : x = 0; by_cases hy : y = 0; simp [hx, hy] end /-- A rotation of a nonzero vector equals that vector if and only if the angle is zero. -/ @[simp] lemma rotation_eq_self_iff_angle_eq_zero {x : V} (hx : x ≠ 0) (θ : real.angle) : o.rotation θ x = x ↔ θ = 0 := begin split, { intro h, rw eq_comm, simpa [hx, h] using o.oangle_rotation_right hx hx θ }, { intro h, simp [h] } end /-- A nonzero vector equals a rotation of that vector if and only if the angle is zero. -/ @[simp] lemma eq_rotation_self_iff_angle_eq_zero {x : V} (hx : x ≠ 0) (θ : real.angle) : x = o.rotation θ x ↔ θ = 0 := by rw [←o.rotation_eq_self_iff_angle_eq_zero hx, eq_comm] /-- A rotation of a vector equals that vector if and only if the vector or the angle is zero. -/ lemma rotation_eq_self_iff (x : V) (θ : real.angle) : o.rotation θ x = x ↔ x = 0 ∨ θ = 0 := begin by_cases h : x = 0; simp [h] end /-- A vector equals a rotation of that vector if and only if the vector or the angle is zero. -/ lemma eq_rotation_self_iff (x : V) (θ : real.angle) : x = o.rotation θ x ↔ x = 0 ∨ θ = 0 := by rw [←rotation_eq_self_iff, eq_comm] /-- Rotating a vector by the angle to another vector gives the second vector if and only if the norms are equal. -/ @[simp] lemma rotation_oangle_eq_iff_norm_eq (x y : V) : o.rotation (o.oangle x y) x = y ↔ ‖x‖ = ‖y‖ := begin split, { intro h, rw [←h, linear_isometry_equiv.norm_map] }, { intro h, rw o.eq_iff_oangle_eq_zero_of_norm_eq; simp [h] } end /-- The angle between two nonzero vectors is `θ` if and only if the second vector is the first rotated by `θ` and scaled by the ratio of the norms. -/ lemma oangle_eq_iff_eq_norm_div_norm_smul_rotation_of_ne_zero {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) (θ : real.angle) : o.oangle x y = θ ↔ y = (‖y‖ / ‖x‖) • o.rotation θ x := begin have hp := div_pos (norm_pos_iff.2 hy) (norm_pos_iff.2 hx), split, { rintro rfl, rw [←linear_isometry_equiv.map_smul, ←o.oangle_smul_left_of_pos x y hp, eq_comm, rotation_oangle_eq_iff_norm_eq, norm_smul, real.norm_of_nonneg hp.le, div_mul_cancel _ (norm_ne_zero_iff.2 hx)] }, { intro hye, rw [hye, o.oangle_smul_right_of_pos _ _ hp, o.oangle_rotation_self_right hx] } end /-- The angle between two nonzero vectors is `θ` if and only if the second vector is the first rotated by `θ` and scaled by a positive real. -/ lemma oangle_eq_iff_eq_pos_smul_rotation_of_ne_zero {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) (θ : real.angle) : o.oangle x y = θ ↔ ∃ r : ℝ, 0 < r ∧ y = r • o.rotation θ x := begin split, { intro h, rw o.oangle_eq_iff_eq_norm_div_norm_smul_rotation_of_ne_zero hx hy at h, exact ⟨‖y‖ / ‖x‖, div_pos (norm_pos_iff.2 hy) (norm_pos_iff.2 hx), h⟩ }, { rintro ⟨r, hr, rfl⟩, rw [o.oangle_smul_right_of_pos _ _ hr, o.oangle_rotation_self_right hx] } end /-- The angle between two vectors is `θ` if and only if they are nonzero and the second vector is the first rotated by `θ` and scaled by the ratio of the norms, or `θ` and at least one of the vectors are zero. -/ lemma oangle_eq_iff_eq_norm_div_norm_smul_rotation_or_eq_zero {x y : V} (θ : real.angle) : o.oangle x y = θ ↔ (x ≠ 0 ∧ y ≠ 0 ∧ y = (‖y‖ / ‖x‖) • o.rotation θ x) ∨ (θ = 0 ∧ (x = 0 ∨ y = 0)) := begin by_cases hx : x = 0, { simp [hx, eq_comm] }, { by_cases hy : y = 0, { simp [hy, eq_comm] }, { rw o.oangle_eq_iff_eq_norm_div_norm_smul_rotation_of_ne_zero hx hy, simp [hx, hy] } } end /-- The angle between two vectors is `θ` if and only if they are nonzero and the second vector is the first rotated by `θ` and scaled by a positive real, or `θ` and at least one of the vectors are zero. -/ lemma oangle_eq_iff_eq_pos_smul_rotation_or_eq_zero {x y : V} (θ : real.angle) : o.oangle x y = θ ↔ (x ≠ 0 ∧ y ≠ 0 ∧ ∃ r : ℝ, 0 < r ∧ y = r • o.rotation θ x) ∨ (θ = 0 ∧ (x = 0 ∨ y = 0)) := begin by_cases hx : x = 0, { simp [hx, eq_comm] }, { by_cases hy : y = 0, { simp [hy, eq_comm] }, { rw o.oangle_eq_iff_eq_pos_smul_rotation_of_ne_zero hx hy, simp [hx, hy] } } end /-- Any linear isometric equivalence in `V` with positive determinant is `rotation`. -/ lemma exists_linear_isometry_equiv_eq_of_det_pos {f : V ≃ₗᵢ[ℝ] V} (hd : 0 < (f.to_linear_equiv : V →ₗ[ℝ] V).det) : ∃ θ : real.angle, f = o.rotation θ := begin haveI : nontrivial V := finite_dimensional.nontrivial_of_finrank_eq_succ (fact.out (finrank ℝ V = 2)), obtain ⟨x, hx⟩ : ∃ x, x ≠ (0:V) := exists_ne (0:V), use o.oangle x (f x), apply linear_isometry_equiv.to_linear_equiv_injective, apply linear_equiv.to_linear_map_injective, apply (o.basis_right_angle_rotation x hx).ext, intros i, symmetry, fin_cases i, { simp }, have : o.oangle (J x) (f (J x)) = o.oangle x (f x), { simp only [oangle, o.linear_isometry_equiv_comp_right_angle_rotation f hd, o.kahler_comp_right_angle_rotation] }, simp [← this], end /-- The angle between two vectors, with respect to an orientation given by `orientation.map` with a linear isometric equivalence, equals the angle between those two vectors, transformed by the inverse of that equivalence, with respect to the original orientation. -/ @[simp] lemma oangle_map (x y : V') (f : V ≃ₗᵢ[ℝ] V') : (orientation.map (fin 2) f.to_linear_equiv o).oangle x y = o.oangle (f.symm x) (f.symm y) := by simp [oangle, o.kahler_map] @[simp] protected lemma _root_.complex.oangle (w z : ℂ) : complex.orientation.oangle w z = complex.arg (conj w * z) := by simp [oangle] /-- The oriented angle on an oriented real inner product space of dimension 2 can be evaluated in terms of a complex-number representation of the space. -/ lemma oangle_map_complex (f : V ≃ₗᵢ[ℝ] ℂ) (hf : (orientation.map (fin 2) f.to_linear_equiv o) = complex.orientation) (x y : V) : o.oangle x y = complex.arg (conj (f x) * f y) := begin rw [← complex.oangle, ← hf, o.oangle_map], simp, end /-- Negating the orientation negates the value of `oangle`. -/ lemma oangle_neg_orientation_eq_neg (x y : V) : (-o).oangle x y = -(o.oangle x y) := by simp [oangle] lemma rotation_map (θ : real.angle) (f : V ≃ₗᵢ[ℝ] V') (x : V') : (orientation.map (fin 2) f.to_linear_equiv o).rotation θ x = f (o.rotation θ (f.symm x)) := by simp [rotation_apply, o.right_angle_rotation_map] @[simp] protected lemma _root_.complex.rotation (θ : real.angle) (z : ℂ) : complex.orientation.rotation θ z = θ.exp_map_circle * z := begin simp only [rotation_apply, complex.right_angle_rotation, real.angle.coe_exp_map_circle, real_smul], ring end /-- Rotation in an oriented real inner product space of dimension 2 can be evaluated in terms of a complex-number representation of the space. -/ lemma rotation_map_complex (θ : real.angle) (f : V ≃ₗᵢ[ℝ] ℂ) (hf : (orientation.map (fin 2) f.to_linear_equiv o) = complex.orientation) (x : V) : f (o.rotation θ x) = θ.exp_map_circle * f x := begin rw [← complex.rotation, ← hf, o.rotation_map], simp, end /-- Negating the orientation negates the angle in `rotation`. -/ lemma rotation_neg_orientation_eq_neg (θ : real.angle) : (-o).rotation θ = o.rotation (-θ) := linear_isometry_equiv.ext $ by simp [rotation_apply] /-- The inner product of two vectors is the product of the norms and the cosine of the oriented angle between the vectors. -/ lemma inner_eq_norm_mul_norm_mul_cos_oangle (x y : V) : ⟪x, y⟫ = ‖x‖ * ‖y‖ * real.angle.cos (o.oangle x y) := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, have : ‖x‖ ≠ 0 := by simpa using hx, have : ‖y‖ ≠ 0 := by simpa using hy, rw [oangle, real.angle.cos_coe, complex.cos_arg, o.abs_kahler], { simp only [kahler_apply_apply, real_smul, add_re, of_real_re, mul_re, I_re, of_real_im], field_simp, ring }, { exact o.kahler_ne_zero hx hy } end /-- The cosine of the oriented angle between two nonzero vectors is the inner product divided by the product of the norms. -/ lemma cos_oangle_eq_inner_div_norm_mul_norm {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : real.angle.cos (o.oangle x y) = ⟪x, y⟫ / (‖x‖ * ‖y‖) := begin rw o.inner_eq_norm_mul_norm_mul_cos_oangle, field_simp [norm_ne_zero_iff.2 hx, norm_ne_zero_iff.2 hy], ring end /-- The cosine of the oriented angle between two nonzero vectors equals that of the unoriented angle. -/ lemma cos_oangle_eq_cos_angle {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : real.angle.cos (o.oangle x y) = real.cos (inner_product_geometry.angle x y) := by rw [o.cos_oangle_eq_inner_div_norm_mul_norm hx hy, inner_product_geometry.cos_angle] /-- The oriented angle between two nonzero vectors is plus or minus the unoriented angle. -/ lemma oangle_eq_angle_or_eq_neg_angle {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : o.oangle x y = inner_product_geometry.angle x y ∨ o.oangle x y = -inner_product_geometry.angle x y := real.angle.cos_eq_real_cos_iff_eq_or_eq_neg.1 $ o.cos_oangle_eq_cos_angle hx hy /-- The unoriented angle between two nonzero vectors is the absolute value of the oriented angle, converted to a real. -/ lemma angle_eq_abs_oangle_to_real {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : inner_product_geometry.angle x y = |(o.oangle x y).to_real| := begin have h0 := inner_product_geometry.angle_nonneg x y, have hpi := inner_product_geometry.angle_le_pi x y, rcases o.oangle_eq_angle_or_eq_neg_angle hx hy with (h|h), { rw [h, eq_comm, real.angle.abs_to_real_coe_eq_self_iff], exact ⟨h0, hpi⟩ }, { rw [h, eq_comm, real.angle.abs_to_real_neg_coe_eq_self_iff], exact ⟨h0, hpi⟩ } end /-- If the sign of the oriented angle between two vectors is zero, either one of the vectors is zero or the unoriented angle is 0 or π. -/ lemma eq_zero_or_angle_eq_zero_or_pi_of_sign_oangle_eq_zero {x y : V} (h : (o.oangle x y).sign = 0) : x = 0 ∨ y = 0 ∨ inner_product_geometry.angle x y = 0 ∨ inner_product_geometry.angle x y = π := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw o.angle_eq_abs_oangle_to_real hx hy, rw real.angle.sign_eq_zero_iff at h, rcases h with h|h; simp [h, real.pi_pos.le] end /-- If two unoriented angles are equal, and the signs of the corresponding oriented angles are equal, then the oriented angles are equal (even in degenerate cases). -/ lemma oangle_eq_of_angle_eq_of_sign_eq {w x y z : V} (h : inner_product_geometry.angle w x = inner_product_geometry.angle y z) (hs : (o.oangle w x).sign = (o.oangle y z).sign) : o.oangle w x = o.oangle y z := begin by_cases h0 : (w = 0 ∨ x = 0) ∨ (y = 0 ∨ z = 0), { have hs' : (o.oangle w x).sign = 0 ∧ (o.oangle y z).sign = 0, { rcases h0 with (rfl|rfl)|rfl|rfl, { simpa using hs.symm }, { simpa using hs.symm }, { simpa using hs }, { simpa using hs } }, rcases hs' with ⟨hswx, hsyz⟩, have h' : inner_product_geometry.angle w x = π / 2 ∧ inner_product_geometry.angle y z = π / 2, { rcases h0 with (rfl|rfl)|rfl|rfl, { simpa using h.symm }, { simpa using h.symm }, { simpa using h }, { simpa using h } }, rcases h' with ⟨hwx, hyz⟩, have hpi : π / 2 ≠ π, { intro hpi, rw [div_eq_iff, eq_comm, ←sub_eq_zero, mul_two, add_sub_cancel] at hpi, { exact real.pi_pos.ne.symm hpi }, { exact two_ne_zero } }, have h0wx : (w = 0 ∨ x = 0), { have h0' := o.eq_zero_or_angle_eq_zero_or_pi_of_sign_oangle_eq_zero hswx, simpa [hwx, real.pi_pos.ne.symm, hpi] using h0' }, have h0yz : (y = 0 ∨ z = 0), { have h0' := o.eq_zero_or_angle_eq_zero_or_pi_of_sign_oangle_eq_zero hsyz, simpa [hyz, real.pi_pos.ne.symm, hpi] using h0' }, rcases h0wx with h0wx|h0wx; rcases h0yz with h0yz|h0yz; simp [h0wx, h0yz] }, { push_neg at h0, rw real.angle.eq_iff_abs_to_real_eq_of_sign_eq hs, rwa [o.angle_eq_abs_oangle_to_real h0.1.1 h0.1.2, o.angle_eq_abs_oangle_to_real h0.2.1 h0.2.2] at h } end /-- If the signs of two oriented angles between nonzero vectors are equal, the oriented angles are equal if and only if the unoriented angles are equal. -/ lemma angle_eq_iff_oangle_eq_of_sign_eq {w x y z : V} (hw : w ≠ 0) (hx : x ≠ 0) (hy : y ≠ 0) (hz : z ≠ 0) (hs : (o.oangle w x).sign = (o.oangle y z).sign) : inner_product_geometry.angle w x = inner_product_geometry.angle y z ↔ o.oangle w x = o.oangle y z := begin refine ⟨λ h, o.oangle_eq_of_angle_eq_of_sign_eq h hs, λ h, _⟩, rw [o.angle_eq_abs_oangle_to_real hw hx, o.angle_eq_abs_oangle_to_real hy hz, h] end /-- The oriented angle between two vectors equals the unoriented angle if the sign is positive. -/ lemma oangle_eq_angle_of_sign_eq_one {x y : V} (h : (o.oangle x y).sign = 1) : o.oangle x y = inner_product_geometry.angle x y := begin by_cases hx : x = 0, { exfalso, simpa [hx] using h }, by_cases hy : y = 0, { exfalso, simpa [hy] using h }, refine (o.oangle_eq_angle_or_eq_neg_angle hx hy).resolve_right _, intro hxy, rw [hxy, real.angle.sign_neg, neg_eq_iff_neg_eq, eq_comm, ←sign_type.neg_iff, ←not_le] at h, exact h (real.angle.sign_coe_nonneg_of_nonneg_of_le_pi (inner_product_geometry.angle_nonneg _ _) (inner_product_geometry.angle_le_pi _ _)) end /-- The oriented angle between two vectors equals minus the unoriented angle if the sign is negative. -/ lemma oangle_eq_neg_angle_of_sign_eq_neg_one {x y : V} (h : (o.oangle x y).sign = -1) : o.oangle x y = -inner_product_geometry.angle x y := begin by_cases hx : x = 0, { exfalso, simpa [hx] using h }, by_cases hy : y = 0, { exfalso, simpa [hy] using h }, refine (o.oangle_eq_angle_or_eq_neg_angle hx hy).resolve_left _, intro hxy, rw [hxy, ←sign_type.neg_iff, ←not_le] at h, exact h (real.angle.sign_coe_nonneg_of_nonneg_of_le_pi (inner_product_geometry.angle_nonneg _ _) (inner_product_geometry.angle_le_pi _ _)) end /-- The oriented angle between two nonzero vectors is zero if and only if the unoriented angle is zero. -/ lemma oangle_eq_zero_iff_angle_eq_zero {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) : o.oangle x y = 0 ↔ inner_product_geometry.angle x y = 0 := begin refine ⟨λ h, _, λ h, _⟩, { simpa [o.angle_eq_abs_oangle_to_real hx hy] }, { have ha := o.oangle_eq_angle_or_eq_neg_angle hx hy, rw h at ha, simpa using ha } end /-- The oriented angle between two vectors is `π` if and only if the unoriented angle is `π`. -/ lemma oangle_eq_pi_iff_angle_eq_pi {x y : V} : o.oangle x y = π ↔ inner_product_geometry.angle x y = π := begin by_cases hx : x = 0, { simp [hx, real.angle.pi_ne_zero.symm, div_eq_mul_inv, mul_right_eq_self₀, not_or_distrib, real.pi_ne_zero], norm_num }, by_cases hy : y = 0, { simp [hy, real.angle.pi_ne_zero.symm, div_eq_mul_inv, mul_right_eq_self₀, not_or_distrib, real.pi_ne_zero], norm_num }, refine ⟨λ h, _, λ h, _⟩, { rw [o.angle_eq_abs_oangle_to_real hx hy, h], simp [real.pi_pos.le] }, { have ha := o.oangle_eq_angle_or_eq_neg_angle hx hy, rw h at ha, simpa using ha } end /-- One of two vectors is zero or the oriented angle between them is plus or minus `π / 2` if and only if the inner product of those vectors is zero. -/ lemma eq_zero_or_oangle_eq_iff_inner_eq_zero {x y : V} : (x = 0 ∨ y = 0 ∨ o.oangle x y = (π / 2 : ℝ) ∨ o.oangle x y = (-π / 2 : ℝ)) ↔ ⟪x, y⟫ = 0 := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw [inner_product_geometry.inner_eq_zero_iff_angle_eq_pi_div_two, or_iff_right hx, or_iff_right hy], refine ⟨λ h, _, λ h, _⟩, { rwa [o.angle_eq_abs_oangle_to_real hx hy, real.angle.abs_to_real_eq_pi_div_two_iff] }, { convert o.oangle_eq_angle_or_eq_neg_angle hx hy; rw [h], exact neg_div _ _ } end /-- If the oriented angle between two vectors is `π / 2`, the inner product of those vectors is zero. -/ lemma inner_eq_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : ⟪x, y⟫ = 0 := o.eq_zero_or_oangle_eq_iff_inner_eq_zero.1 $ or.inr $ or.inr $ or.inl h /-- If the oriented angle between two vectors is `π / 2`, the inner product of those vectors (reversed) is zero. -/ lemma inner_rev_eq_zero_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = (π / 2 : ℝ)) : ⟪y, x⟫ = 0 := by rw [real_inner_comm, o.inner_eq_zero_of_oangle_eq_pi_div_two h] /-- If the oriented angle between two vectors is `-π / 2`, the inner product of those vectors is zero. -/ lemma inner_eq_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : ⟪x, y⟫ = 0 := o.eq_zero_or_oangle_eq_iff_inner_eq_zero.1 $ or.inr $ or.inr $ or.inr h /-- If the oriented angle between two vectors is `-π / 2`, the inner product of those vectors (reversed) is zero. -/ lemma inner_rev_eq_zero_of_oangle_eq_neg_pi_div_two {x y : V} (h : o.oangle x y = (-π / 2 : ℝ)) : ⟪y, x⟫ = 0 := by rw [real_inner_comm, o.inner_eq_zero_of_oangle_eq_neg_pi_div_two h] /-- The inner product between a `π / 2` rotation of a vector and that vector is zero. -/ @[simp] lemma inner_rotation_pi_div_two_left (x : V) : ⟪o.rotation (π / 2 : ℝ) x, x⟫ = 0 := by rw [rotation_pi_div_two, inner_right_angle_rotation_self] /-- The inner product between a vector and a `π / 2` rotation of that vector is zero. -/ @[simp] lemma inner_rotation_pi_div_two_right (x : V) : ⟪x, o.rotation (π / 2 : ℝ) x⟫ = 0 := by rw [real_inner_comm, inner_rotation_pi_div_two_left] /-- The inner product between a multiple of a `π / 2` rotation of a vector and that vector is zero. -/ @[simp] lemma inner_smul_rotation_pi_div_two_left (x : V) (r : ℝ) : ⟪r • o.rotation (π / 2 : ℝ) x, x⟫ = 0 := by rw [inner_smul_left, inner_rotation_pi_div_two_left, mul_zero] /-- The inner product between a vector and a multiple of a `π / 2` rotation of that vector is zero. -/ @[simp] lemma inner_smul_rotation_pi_div_two_right (x : V) (r : ℝ) : ⟪x, r • o.rotation (π / 2 : ℝ) x⟫ = 0 := by rw [real_inner_comm, inner_smul_rotation_pi_div_two_left] /-- The inner product between a `π / 2` rotation of a vector and a multiple of that vector is zero. -/ @[simp] lemma inner_rotation_pi_div_two_left_smul (x : V) (r : ℝ) : ⟪o.rotation (π / 2 : ℝ) x, r • x⟫ = 0 := by rw [inner_smul_right, inner_rotation_pi_div_two_left, mul_zero] /-- The inner product between a multiple of a vector and a `π / 2` rotation of that vector is zero. -/ @[simp] lemma inner_rotation_pi_div_two_right_smul (x : V) (r : ℝ) : ⟪r • x, o.rotation (π / 2 : ℝ) x⟫ = 0 := by rw [real_inner_comm, inner_rotation_pi_div_two_left_smul] /-- The inner product between a multiple of a `π / 2` rotation of a vector and a multiple of that vector is zero. -/ @[simp] lemma inner_smul_rotation_pi_div_two_smul_left (x : V) (r₁ r₂ : ℝ) : ⟪r₁ • o.rotation (π / 2 : ℝ) x, r₂ • x⟫ = 0 := by rw [inner_smul_right, inner_smul_rotation_pi_div_two_left, mul_zero] /-- The inner product between a multiple of a vector and a multiple of a `π / 2` rotation of that vector is zero. -/ @[simp] lemma inner_smul_rotation_pi_div_two_smul_right (x : V) (r₁ r₂ : ℝ) : ⟪r₂ • x, r₁ • o.rotation (π / 2 : ℝ) x⟫ = 0 := by rw [real_inner_comm, inner_smul_rotation_pi_div_two_smul_left] /-- The inner product between two vectors is zero if and only if the first vector is zero or the second is a multiple of a `π / 2` rotation of that vector. -/ lemma inner_eq_zero_iff_eq_zero_or_eq_smul_rotation_pi_div_two {x y : V} : ⟪x, y⟫ = 0 ↔ (x = 0 ∨ ∃ r : ℝ, r • o.rotation (π / 2 : ℝ) x = y) := begin rw ←o.eq_zero_or_oangle_eq_iff_inner_eq_zero, refine ⟨λ h, _, λ h, _⟩, { rcases h with rfl | rfl | h | h, { exact or.inl rfl }, { exact or.inr ⟨0, zero_smul _ _⟩ }, { obtain ⟨r, hr, rfl⟩ := (o.oangle_eq_iff_eq_pos_smul_rotation_of_ne_zero (o.left_ne_zero_of_oangle_eq_pi_div_two h) (o.right_ne_zero_of_oangle_eq_pi_div_two h) _).1 h, exact or.inr ⟨r, rfl⟩ }, { obtain ⟨r, hr, rfl⟩ := (o.oangle_eq_iff_eq_pos_smul_rotation_of_ne_zero (o.left_ne_zero_of_oangle_eq_neg_pi_div_two h) (o.right_ne_zero_of_oangle_eq_neg_pi_div_two h) _).1 h, refine or.inr ⟨-r, _⟩, rw [neg_smul, ←smul_neg, o.neg_rotation_pi_div_two] } }, { rcases h with rfl | ⟨r, rfl⟩, { exact or.inl rfl }, { by_cases hx : x = 0, { exact or.inl hx }, rcases lt_trichotomy r 0 with hr | rfl | hr, { refine or.inr (or.inr (or.inr _)), rw [o.oangle_smul_right_of_neg _ _ hr, o.neg_rotation_pi_div_two, o.oangle_rotation_self_right hx] }, { exact or.inr (or.inl (zero_smul _ _)) }, { refine or.inr (or.inr (or.inl _)), rw [o.oangle_smul_right_of_pos _ _ hr, o.oangle_rotation_self_right hx] } } } end /-- Negating the first vector passed to `oangle` negates the sign of the angle. -/ @[simp] lemma oangle_sign_neg_left (x y : V) : (o.oangle (-x) y).sign = -((o.oangle x y).sign) := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw [o.oangle_neg_left hx hy, real.angle.sign_add_pi] end /-- Negating the second vector passed to `oangle` negates the sign of the angle. -/ @[simp] lemma oangle_sign_neg_right (x y : V) : (o.oangle x (-y)).sign = -((o.oangle x y).sign) := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw [o.oangle_neg_right hx hy, real.angle.sign_add_pi] end /-- Multiplying the first vector passed to `oangle` by a real multiplies the sign of the angle by the sign of the real. -/ @[simp] lemma oangle_sign_smul_left (x y : V) (r : ℝ) : (o.oangle (r • x) y).sign = sign r * (o.oangle x y).sign := begin rcases lt_trichotomy r 0 with h|h|h; simp [h] end /-- Multiplying the second vector passed to `oangle` by a real multiplies the sign of the angle by the sign of the real. -/ @[simp] lemma oangle_sign_smul_right (x y : V) (r : ℝ) : (o.oangle x (r • y)).sign = sign r * (o.oangle x y).sign := begin rcases lt_trichotomy r 0 with h|h|h; simp [h] end /-- Auxiliary lemma for the proof of `oangle_sign_smul_add_right`; not intended to be used outside of that proof. -/ lemma oangle_smul_add_right_eq_zero_or_eq_pi_iff {x y : V} (r : ℝ) : (o.oangle x (r • x + y) = 0 ∨ o.oangle x (r • x + y) = π) ↔ (o.oangle x y = 0 ∨ o.oangle x y = π) := begin simp_rw [oangle_eq_zero_or_eq_pi_iff_not_linear_independent, fintype.not_linear_independent_iff, fin.sum_univ_two, fin.exists_fin_two], refine ⟨λ h, _, λ h, _⟩, { rcases h with ⟨m, h, hm⟩, change m 0 • x + m 1 • (r • x + y) = 0 at h, refine ⟨![m 0 + m 1 * r, m 1], _⟩, change (m 0 + m 1 * r) • x + m 1 • y = 0 ∧ (m 0 + m 1 * r ≠ 0 ∨ m 1 ≠ 0), rw [smul_add, smul_smul, ←add_assoc, ←add_smul] at h, refine ⟨h, not_and_distrib.1 (λ h0, _)⟩, obtain ⟨h0, h1⟩ := h0, rw h1 at h0 hm, rw [zero_mul, add_zero] at h0, simpa [h0] using hm }, { rcases h with ⟨m, h, hm⟩, change m 0 • x + m 1 • y = 0 at h, refine ⟨![m 0 - m 1 * r, m 1], _⟩, change (m 0 - m 1 * r) • x + m 1 • (r • x + y) = 0 ∧ (m 0 - m 1 * r ≠ 0 ∨ m 1 ≠ 0), rw [sub_smul, smul_add, smul_smul, ←add_assoc, sub_add_cancel], refine ⟨h, not_and_distrib.1 (λ h0, _)⟩, obtain ⟨h0, h1⟩ := h0, rw h1 at h0 hm, rw [zero_mul, sub_zero] at h0, simpa [h0] using hm } end /-- Adding a multiple of the first vector passed to `oangle` to the second vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_smul_add_right (x y : V) (r : ℝ) : (o.oangle x (r • x + y)).sign = (o.oangle x y).sign := begin by_cases h : o.oangle x y = 0 ∨ o.oangle x y = π, { rwa [real.angle.sign_eq_zero_iff.2 h, real.angle.sign_eq_zero_iff, oangle_smul_add_right_eq_zero_or_eq_pi_iff] }, have h' : ∀ r' : ℝ, o.oangle x (r' • x + y) ≠ 0 ∧ o.oangle x (r' • x + y) ≠ π, { intro r', rwa [←o.oangle_smul_add_right_eq_zero_or_eq_pi_iff r', not_or_distrib] at h }, let s : set (V × V) := (λ r' : ℝ, (x, r' • x + y)) '' set.univ, have hc : is_connected s := is_connected_univ.image _ ((continuous_const.prod_mk ((continuous_id.smul continuous_const).add continuous_const)).continuous_on), have hf : continuous_on (λ z : V × V, o.oangle z.1 z.2) s, { refine continuous_at.continuous_on (λ z hz, o.continuous_at_oangle _ _), all_goals { simp_rw [s, set.mem_image] at hz, obtain ⟨r', -, rfl⟩ := hz, simp only [prod.fst, prod.snd], intro hz }, { simpa [hz] using (h' 0).1 }, { simpa [hz] using (h' r').1 } }, have hs : ∀ z : V × V, z ∈ s → o.oangle z.1 z.2 ≠ 0 ∧ o.oangle z.1 z.2 ≠ π, { intros z hz, simp_rw [s, set.mem_image] at hz, obtain ⟨r', -, rfl⟩ := hz, exact h' r' }, have hx : (x, y) ∈ s, { convert set.mem_image_of_mem (λ r' : ℝ, (x, r' • x + y)) (set.mem_univ 0), simp }, have hy : (x, r • x + y) ∈ s := set.mem_image_of_mem _ (set.mem_univ _), convert real.angle.sign_eq_of_continuous_on hc hf hs hx hy end /-- Adding a multiple of the second vector passed to `oangle` to the first vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_add_smul_left (x y : V) (r : ℝ) : (o.oangle (x + r • y) y).sign = (o.oangle x y).sign := by simp_rw [o.oangle_rev y, real.angle.sign_neg, add_comm x, oangle_sign_smul_add_right] /-- Subtracting a multiple of the first vector passed to `oangle` from the second vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_sub_smul_right (x y : V) (r : ℝ) : (o.oangle x (y - r • x)).sign = (o.oangle x y).sign := by rw [sub_eq_add_neg, ←neg_smul, add_comm, oangle_sign_smul_add_right] /-- Subtracting a multiple of the second vector passed to `oangle` from the first vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_sub_smul_left (x y : V) (r : ℝ) : (o.oangle (x - r • y) y).sign = (o.oangle x y).sign := by rw [sub_eq_add_neg, ←neg_smul, oangle_sign_add_smul_left] /-- Adding the first vector passed to `oangle` to the second vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_add_right (x y : V) : (o.oangle x (x + y)).sign = (o.oangle x y).sign := by rw [←o.oangle_sign_smul_add_right x y 1, one_smul] /-- Adding the second vector passed to `oangle` to the first vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_add_left (x y : V) : (o.oangle (x + y) y).sign = (o.oangle x y).sign := by rw [←o.oangle_sign_add_smul_left x y 1, one_smul] /-- Subtracting the first vector passed to `oangle` from the second vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_sub_right (x y : V) : (o.oangle x (y - x)).sign = (o.oangle x y).sign := by rw [←o.oangle_sign_sub_smul_right x y 1, one_smul] /-- Subtracting the second vector passed to `oangle` from the first vector does not change the sign of the angle. -/ @[simp] lemma oangle_sign_sub_left (x y : V) : (o.oangle (x - y) y).sign = (o.oangle x y).sign := by rw [←o.oangle_sign_sub_smul_left x y 1, one_smul] /-- Subtracting the second vector passed to `oangle` from a multiple of the first vector negates the sign of the angle. -/ @[simp] lemma oangle_sign_smul_sub_right (x y : V) (r : ℝ) : (o.oangle x (r • x - y)).sign = -(o.oangle x y).sign := by rw [←oangle_sign_neg_right, sub_eq_add_neg, oangle_sign_smul_add_right] /-- Subtracting the first vector passed to `oangle` from a multiple of the second vector negates the sign of the angle. -/ @[simp] lemma oangle_sign_smul_sub_left (x y : V) (r : ℝ) : (o.oangle (r • y - x) y).sign = -(o.oangle x y).sign := by rw [←oangle_sign_neg_left, sub_eq_neg_add, oangle_sign_add_smul_left] /-- Subtracting the second vector passed to `oangle` from the first vector negates the sign of the angle. -/ lemma oangle_sign_sub_right_eq_neg (x y : V) : (o.oangle x (x - y)).sign = -(o.oangle x y).sign := by rw [←o.oangle_sign_smul_sub_right x y 1, one_smul] /-- Subtracting the first vector passed to `oangle` from the second vector negates the sign of the angle. -/ lemma oangle_sign_sub_left_eq_neg (x y : V) : (o.oangle (y - x) y).sign = -(o.oangle x y).sign := by rw [←o.oangle_sign_smul_sub_left x y 1, one_smul] /-- Subtracting the first vector passed to `oangle` from the second vector then swapping the vectors does not change the sign of the angle. -/ @[simp] lemma oangle_sign_sub_right_swap (x y : V) : (o.oangle y (y - x)).sign = (o.oangle x y).sign := by rw [oangle_sign_sub_right_eq_neg, o.oangle_rev y x, real.angle.sign_neg] /-- Subtracting the second vector passed to `oangle` from the first vector then swapping the vectors does not change the sign of the angle. -/ @[simp] lemma oangle_sign_sub_left_swap (x y : V) : (o.oangle (x - y) x).sign = (o.oangle x y).sign := by rw [oangle_sign_sub_left_eq_neg, o.oangle_rev y x, real.angle.sign_neg] /-- The sign of the angle between a vector, and a linear combination of that vector with a second vector, is the sign of the factor by which the second vector is multiplied in that combination multiplied by the sign of the angle between the two vectors. -/ @[simp] lemma oangle_sign_smul_add_smul_right (x y : V) (r₁ r₂ : ℝ) : (o.oangle x (r₁ • x + r₂ • y)).sign = sign r₂ * (o.oangle x y).sign := begin rw ←o.oangle_sign_smul_add_right x (r₁ • x + r₂ • y) (-r₁), simp end /-- The sign of the angle between a linear combination of two vectors and the second vector is the sign of the factor by which the first vector is multiplied in that combination multiplied by the sign of the angle between the two vectors. -/ @[simp] lemma oangle_sign_smul_add_smul_left (x y : V) (r₁ r₂ : ℝ) : (o.oangle (r₁ • x + r₂ • y) y).sign = sign r₁ * (o.oangle x y).sign := by simp_rw [o.oangle_rev y, real.angle.sign_neg, add_comm (r₁ • x), oangle_sign_smul_add_smul_right, mul_neg] /-- The sign of the angle between two linear combinations of two vectors is the sign of the determinant of the factors in those combinations multiplied by the sign of the angle between the two vectors. -/ lemma oangle_sign_smul_add_smul_smul_add_smul (x y : V) (r₁ r₂ r₃ r₄ : ℝ) : (o.oangle (r₁ • x + r₂ • y) (r₃ • x + r₄ • y)).sign = sign (r₁ * r₄ - r₂ * r₃) * (o.oangle x y).sign := begin by_cases hr₁ : r₁ = 0, { rw [hr₁, zero_smul, zero_mul, zero_add, zero_sub, left.sign_neg, oangle_sign_smul_left, add_comm, oangle_sign_smul_add_smul_right, oangle_rev, real.angle.sign_neg, sign_mul, mul_neg, mul_neg, neg_mul, mul_assoc] }, { rw [←o.oangle_sign_smul_add_right (r₁ • x + r₂ • y) (r₃ • x + r₄ • y) (-r₃ / r₁), smul_add, smul_smul, smul_smul, div_mul_cancel _ hr₁, neg_smul, ←add_assoc, add_comm (-(r₃ • x)), ←sub_eq_add_neg, sub_add_cancel, ←add_smul, oangle_sign_smul_right, oangle_sign_smul_add_smul_left, ←mul_assoc, ←sign_mul, add_mul, mul_assoc, mul_comm r₂ r₁, ←mul_assoc, div_mul_cancel _ hr₁, add_comm, neg_mul, ←sub_eq_add_neg, mul_comm r₄, mul_comm r₃] } end end orientation
f766e8ee5e6dc80fe58c0b6fd5729ddfbebf1de4
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/data/list/min_max.lean
42d6a75382b89c1f7f00c3398947821c9be2f335
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
11,239
lean
/- Copyright (c) 2019 Minchao Wu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Minchao Wu, Chris Hughes -/ import data.list.basic /- # Minimum and maximum of lists ## Main definitions The main definition are `argmax`, `argmin`, `minimum` and `maximum` for lists. `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ namespace list variables {α : Type*} {β : Type*} [decidable_linear_order β] /-- Auxiliary definition to define `argmax` -/ def argmax₂ (f : α → β) (a : option α) (b : α) : option α := option.cases_on a (some b) (λ c, if f b ≤ f c then some c else some b) /-- `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` -/ def argmax (f : α → β) (l : list α) : option α := l.foldl (argmax₂ f) none /-- `argmin f l` returns `some a`, where `a` of `l` that minimises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmin f []` = none` -/ def argmin (f : α → β) (l : list α) := @argmax _ (order_dual β) _ f l @[simp] lemma argmax_two_self (f : α → β) (a : α) : argmax₂ f (some a) a = a := if_pos (le_refl _) @[simp] lemma argmax_nil (f : α → β) : argmax f [] = none := rfl @[simp] lemma argmin_nil (f : α → β) : argmin f [] = none := rfl @[simp] lemma argmax_singleton {f : α → β} {a : α} : argmax f [a] = some a := rfl @[simp] lemma argmin_singleton {f : α → β} {a : α} : argmin f [a] = a := rfl @[simp] lemma foldl_argmax₂_eq_none {f : α → β} {l : list α} {o : option α} : l.foldl (argmax₂ f) o = none ↔ l = [] ∧ o = none := list.reverse_rec_on l (by simp) $ (assume tl hd, by simp [argmax₂]; cases foldl (argmax₂ f) o tl; simp; try {split_ifs}; simp) private theorem le_of_foldl_argmax₂ {f : α → β} {l} : Π {a m : α} {o : option α}, a ∈ l → m ∈ foldl (argmax₂ f) o l → f a ≤ f m := list.reverse_rec_on l (λ _ _ _ h, absurd h $ not_mem_nil _) begin intros tl _ ih _ _ _ h ho, rw [foldl_append, foldl_cons, foldl_nil, argmax₂] at ho, cases hf : foldl (argmax₂ f) o tl, { rw [hf] at ho, rw [foldl_argmax₂_eq_none] at hf, simp [hf.1, hf.2, *] at * }, rw [hf, option.mem_def] at ho, dsimp only at ho, cases mem_append.1 h with h h, { refine le_trans (ih h hf) _, have := @le_of_lt _ _ (f val) (f m), split_ifs at ho; simp * at * }, { split_ifs at ho; simp * at * } end private theorem foldl_argmax₂_mem (f : α → β) (l) : Π (a m : α), m ∈ foldl (argmax₂ f) (some a) l → m ∈ a :: l := list.reverse_rec_on l (by simp [eq_comm]) begin assume tl hd ih a m, simp only [foldl_append, foldl_cons, foldl_nil, argmax₂], cases hf : foldl (argmax₂ f) (some a) tl, { simp {contextual := tt} }, { dsimp only, split_ifs, { finish [ih _ _ hf] }, { simp {contextual := tt} } } end theorem argmax_mem {f : α → β} : Π {l : list α} {m : α}, m ∈ argmax f l → m ∈ l | [] m := by simp | (hd::tl) m := by simpa [argmax, argmax₂] using foldl_argmax₂_mem f tl hd m theorem argmin_mem {f : α → β} : Π {l : list α} {m : α}, m ∈ argmin f l → m ∈ l := @argmax_mem _ (order_dual β) _ _ @[simp] theorem argmax_eq_none {f : α → β} {l : list α} : l.argmax f = none ↔ l = [] := by simp [argmax] @[simp] theorem argmin_eq_none {f : α → β} {l : list α} : l.argmin f = none ↔ l = [] := @argmax_eq_none _ (order_dual β) _ _ _ theorem le_argmax_of_mem {f : α → β} {a m : α} {l : list α} : a ∈ l → m ∈ argmax f l → f a ≤ f m := le_of_foldl_argmax₂ theorem argmin_le_of_mem {f : α → β} {a m : α} {l : list α} : a ∈ l → m ∈ argmin f l → f m ≤ f a:= @le_argmax_of_mem _ (order_dual β) _ _ _ _ _ theorem argmax_concat (f : α → β) (a : α) (l : list α) : argmax f (l ++ [a]) = option.cases_on (argmax f l) (some a) (λ c, if f a ≤ f c then some c else some a) := by rw [argmax, argmax]; simp [argmax₂] theorem argmin_concat (f : α → β) (a : α) (l : list α) : argmin f (l ++ [a]) = option.cases_on (argmin f l) (some a) (λ c, if f c ≤ f a then some c else some a) := @argmax_concat _ (order_dual β) _ _ _ _ theorem argmax_cons (f : α → β) (a : α) (l : list α) : argmax f (a :: l) = option.cases_on (argmax f l) (some a) (λ c, if f c ≤ f a then some a else some c) := list.reverse_rec_on l rfl $ assume hd tl ih, begin rw [← cons_append, argmax_concat, ih, argmax_concat], cases h : argmax f hd with m, { simp [h] }, { simp [h], dsimp, by_cases ham : f m ≤ f a, { rw if_pos ham, dsimp, by_cases htlm : f tl ≤ f m, { rw if_pos htlm, dsimp, rw [if_pos (le_trans htlm ham), if_pos ham] }, { rw if_neg htlm } }, { rw if_neg ham, dsimp, by_cases htlm : f tl ≤ f m, { rw if_pos htlm, dsimp, rw if_neg ham }, { rw if_neg htlm, dsimp, rw [if_neg (not_le_of_gt (lt_trans (lt_of_not_ge ham) (lt_of_not_ge htlm)))] } } } end theorem argmin_cons (f : α → β) (a : α) (l : list α) : argmin f (a :: l) = option.cases_on (argmin f l) (some a) (λ c, if f a ≤ f c then some a else some c) := @argmax_cons _ (order_dual β) _ _ _ _ theorem index_of_argmax [decidable_eq α] {f : α → β} : Π {l : list α} {m : α}, m ∈ argmax f l → ∀ {a}, a ∈ l → f m ≤ f a → l.index_of m ≤ l.index_of a | [] m _ _ _ _ := by simp | (hd::tl) m hm a ha ham := begin simp only [index_of_cons, argmax_cons, option.mem_def] at ⊢ hm, cases h : argmax f tl, { rw h at hm, simp * at * }, { rw h at hm, dsimp only at hm, cases ha with hahd hatl, { clear index_of_argmax, subst hahd, split_ifs at hm, { subst hm }, { subst hm, contradiction } }, { have := index_of_argmax h hatl, clear index_of_argmax, split_ifs at *; refl <|> exact nat.zero_le _ <|> simp [*, nat.succ_le_succ_iff, -not_le] at * } } end theorem index_of_argmin [decidable_eq α] {f : α → β} : Π {l : list α} {m : α}, m ∈ argmin f l → ∀ {a}, a ∈ l → f a ≤ f m → l.index_of m ≤ l.index_of a := @index_of_argmax _ (order_dual β) _ _ _ theorem mem_argmax_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : m ∈ argmax f l ↔ m ∈ l ∧ (∀ a ∈ l, f a ≤ f m) ∧ (∀ a ∈ l, f m ≤ f a → l.index_of m ≤ l.index_of a) := ⟨λ hm, ⟨argmax_mem hm, λ a ha, le_argmax_of_mem ha hm, λ _, index_of_argmax hm⟩, begin rintros ⟨hml, ham, hma⟩, cases harg : argmax f l with n, { simp * at * }, { have := le_antisymm (hma n (argmax_mem harg) (le_argmax_of_mem hml harg)) (index_of_argmax harg hml (ham _ (argmax_mem harg))), rw [(index_of_inj hml (argmax_mem harg)).1 this, option.mem_def] } end⟩ theorem argmax_eq_some_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : argmax f l = some m ↔ m ∈ l ∧ (∀ a ∈ l, f a ≤ f m) ∧ (∀ a ∈ l, f m ≤ f a → l.index_of m ≤ l.index_of a) := mem_argmax_iff theorem mem_argmin_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : m ∈ argmin f l ↔ m ∈ l ∧ (∀ a ∈ l, f m ≤ f a) ∧ (∀ a ∈ l, f a ≤ f m → l.index_of m ≤ l.index_of a) := @mem_argmax_iff _ (order_dual β) _ _ _ _ _ theorem argmin_eq_some_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : argmin f l = some m ↔ m ∈ l ∧ (∀ a ∈ l, f m ≤ f a) ∧ (∀ a ∈ l, f a ≤ f m → l.index_of m ≤ l.index_of a) := mem_argmin_iff variable [decidable_linear_order α] /-- `maximum l` returns an `with_bot α`, the largest element of `l` for nonempty lists, and `⊥` for `[]` -/ def maximum (l : list α) : with_bot α := argmax id l /-- `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ def minimum (l : list α) : with_top α := argmin id l @[simp] lemma maximum_nil : maximum ([] : list α) = ⊥ := rfl @[simp] lemma minimum_nil : minimum ([] : list α) = ⊤ := rfl @[simp] lemma maximum_singleton (a : α) : maximum [a] = a := rfl @[simp] lemma minimum_singleton (a : α) : minimum [a] = a := rfl theorem maximum_mem {l : list α} {m : α} : (maximum l : with_top α) = m → m ∈ l := argmax_mem theorem minimum_mem {l : list α} {m : α} : (minimum l : with_bot α) = m → m ∈ l := argmin_mem @[simp] theorem maximum_eq_none {l : list α} : l.maximum = none ↔ l = [] := argmax_eq_none @[simp] theorem minimum_eq_none {l : list α} : l.minimum = none ↔ l = [] := argmin_eq_none theorem le_maximum_of_mem {a m : α} {l : list α} : a ∈ l → (maximum l : with_bot α) = m → a ≤ m := le_argmax_of_mem theorem minimum_le_of_mem {a m : α} {l : list α} : a ∈ l → (minimum l : with_top α) = m → m ≤ a := argmin_le_of_mem theorem le_maximum_of_mem' {a : α} {l : list α} (ha : a ∈ l) : (a : with_bot α) ≤ maximum l := option.cases_on (maximum l) (λ _ h, absurd ha ((h rfl).symm ▸ not_mem_nil _)) (λ m hm _, with_bot.coe_le_coe.2 $ hm _ rfl) (λ m, @le_maximum_of_mem _ _ _ m _ ha) (@maximum_eq_none _ _ l).1 theorem le_minimum_of_mem' {a : α} {l : list α} (ha : a ∈ l) : minimum l ≤ (a : with_top α) := @le_maximum_of_mem' (order_dual α) _ _ _ ha theorem maximum_concat (a : α) (l : list α) : maximum (l ++ [a]) = max (maximum l) a := begin rw max_comm, simp only [maximum, argmax_concat, id, max], cases h : argmax id l, { rw [if_neg], refl, exact not_le_of_gt (with_bot.bot_lt_some _) }, change (coe : α → with_bot α) with some, simp, congr end theorem minimum_concat (a : α) (l : list α) : minimum (l ++ [a]) = min (minimum l) a := by simp only [min_comm _ (a : with_top α)]; exact @maximum_concat (order_dual α) _ _ _ theorem maximum_cons (a : α) (l : list α) : maximum (a :: l) = max a (maximum l) := list.reverse_rec_on l (by simp [@max_eq_left (with_bot α) _ _ _ lattice.bot_le]) (λ tl hd ih, by rw [← cons_append, maximum_concat, ih, maximum_concat, max_assoc]) theorem minimum_cons (a : α) (l : list α) : minimum (a :: l) = min a (minimum l) := min_comm (minimum l) a ▸ @maximum_cons (order_dual α) _ _ _ theorem maximum_eq_coe_iff {m : α} {l : list α} : maximum l = m ↔ m ∈ l ∧ (∀ a ∈ l, a ≤ m) := begin unfold_coes, simp only [maximum, argmax_eq_some_iff, id], split, { simp only [true_and, forall_true_iff] {contextual := tt} }, { simp only [true_and, forall_true_iff] {contextual := tt}, intros h a hal hma, rw [le_antisymm hma (h.2 a hal)] } end theorem minimum_eq_coe_iff {m : α} {l : list α} : minimum l = m ↔ m ∈ l ∧ (∀ a ∈ l, m ≤ a) := @maximum_eq_coe_iff (order_dual α) _ _ _ end list
13394c5e1d0ba4652cd156f1f0647ac0485a49d3
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/measure_theory/outer_measure.lean
9bce4dca37970cb693ce1afc68162e0bf5a4d510
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
19,883
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 Outer measures -- overapproximations of measures -/ import algebra.big_operators algebra.module topology.instances.ennreal analysis.specific_limits measure_theory.measurable_space noncomputable theory open set finset function filter encodable open_locale classical namespace measure_theory structure outer_measure (α : Type*) := (measure_of : set α → ennreal) (empty : measure_of ∅ = 0) (mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂) (Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ (∑i, measure_of (s i))) namespace outer_measure instance {α} : has_coe_to_fun (outer_measure α) := ⟨_, λ m, m.measure_of⟩ section basic variables {α : Type*} {ms : set (outer_measure α)} {m : outer_measure α} @[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty theorem mono' (m : outer_measure α) {s₁ s₂} (h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h theorem Union_aux (m : set α → ennreal) (m0 : m ∅ = 0) {β} [encodable β] (s : β → set α) : (∑ b, m (s b)) = ∑ i, m (⋃ b ∈ decode2 β i, s b) := begin have H : ∀ n, m (⋃ b ∈ decode2 β n, s b) ≠ 0 → (decode2 β n).is_some, { intros n h, cases decode2 β n with b, { exact (h (by simp [m0])).elim }, { exact rfl } }, refine tsum_eq_tsum_of_ne_zero_bij (λ n h, option.get (H n h)) _ _ _, { intros m n hm hn e, have := mem_decode2.1 (option.get_mem (H n hn)), rwa [← e, mem_decode2.1 (option.get_mem (H m hm))] at this }, { intros b h, refine ⟨encode b, _, _⟩, { convert h, simp [ext_iff, encodek2] }, { exact option.get_of_mem _ (encodek2 _) } }, { intros n h, transitivity, swap, rw [show decode2 β n = _, from option.get_mem (H n h)], congr, simp [ext_iff, -option.some_get] } end protected theorem Union (m : outer_measure α) {β} [encodable β] (s : β → set α) : m (⋃i, s i) ≤ (∑i, m (s i)) := by rw [Union_decode2, Union_aux _ m.empty' s]; exact m.Union_nat _ lemma Union_null (m : outer_measure α) {β} [encodable β] {s : β → set α} (h : ∀ i, m (s i) = 0) : m (⋃i, s i) = 0 := by simpa [h] using m.Union s protected lemma union (m : outer_measure α) (s₁ s₂ : set α) : m (s₁ ∪ s₂) ≤ m s₁ + m s₂ := begin convert m.Union (λ b, cond b s₁ s₂), { simp [union_eq_Union] }, { rw tsum_fintype, change _ = _ + _, simp } end lemma union_null (m : outer_measure α) {s₁ s₂ : set α} (h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 := by simpa [h₁, h₂] using m.union s₁ s₂ @[ext] lemma ext : ∀{μ₁ μ₂ : outer_measure α}, (∀s, μ₁ s = μ₂ s) → μ₁ = μ₂ | ⟨m₁, e₁, _, u₁⟩ ⟨m₂, e₂, _, u₂⟩ h := by congr; exact funext h instance : has_zero (outer_measure α) := ⟨{ measure_of := λ_, 0, empty := rfl, mono := assume _ _ _, le_refl 0, Union_nat := assume s, zero_le _ }⟩ @[simp] theorem zero_apply (s : set α) : (0 : outer_measure α) s = 0 := rfl instance : inhabited (outer_measure α) := ⟨0⟩ instance : has_add (outer_measure α) := ⟨λm₁ m₂, { measure_of := λs, m₁ s + m₂ s, empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty], mono := assume s₁ s₂ h, add_le_add' (m₁.mono h) (m₂.mono h), Union_nat := assume s, calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤ (∑i, m₁ (s i)) + (∑i, m₂ (s i)) : add_le_add' (m₁.Union_nat s) (m₂.Union_nat s) ... = _ : ennreal.tsum_add.symm}⟩ @[simp] theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ + m₂) s = m₁ s + m₂ s := rfl instance add_comm_monoid : add_comm_monoid (outer_measure α) := { zero := 0, add := (+), add_comm := assume a b, ext $ assume s, add_comm _ _, add_assoc := assume a b c, ext $ assume s, add_assoc _ _ _, add_zero := assume a, ext $ assume s, add_zero _, zero_add := assume a, ext $ assume s, zero_add _ } instance : has_bot (outer_measure α) := ⟨0⟩ instance outer_measure.order_bot : order_bot (outer_measure α) := { le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s, bot := 0, le_refl := assume a s, le_refl _, le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s), le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s), bot_le := assume a s, zero_le _ } section supremum instance : has_Sup (outer_measure α) := ⟨λms, { measure_of := λs, ⨆m:ms, m.val s, empty := le_zero_iff_eq.1 $ supr_le $ λ ⟨m, h⟩, le_of_eq m.empty, mono := assume s₁ s₂ hs, supr_le_supr $ assume ⟨m, hm⟩, m.mono hs, Union_nat := assume f, supr_le $ assume m, calc m.val (⋃i, f i) ≤ (∑ (i : ℕ), m.val (f i)) : m.val.Union_nat _ ... ≤ (∑i, ⨆m:ms, m.val (f i)) : ennreal.tsum_le_tsum $ assume i, le_supr (λm:ms, m.val (f i)) m }⟩ protected lemma le_Sup (hm : m ∈ ms) : m ≤ Sup ms := λ s, le_supr (λm:ms, m.val s) ⟨m, hm⟩ protected lemma Sup_le (hm : ∀m' ∈ ms, m' ≤ m) : Sup ms ≤ m := λ s, (supr_le $ assume ⟨m', h'⟩, (hm m' h') s) instance : has_Inf (outer_measure α) := ⟨λs, Sup {m | ∀m'∈s, m ≤ m'}⟩ protected lemma Inf_le (hm : m ∈ ms) : Inf ms ≤ m := outer_measure.Sup_le $ assume m' h', h' _ hm protected lemma le_Inf (hm : ∀m' ∈ ms, m ≤ m') : m ≤ Inf ms := outer_measure.le_Sup hm instance : complete_lattice (outer_measure α) := { top := Sup univ, le_top := assume a, outer_measure.le_Sup (mem_univ a), Sup := Sup, Sup_le := assume s m, outer_measure.Sup_le, le_Sup := assume s m, outer_measure.le_Sup, Inf := Inf, Inf_le := assume s m, outer_measure.Inf_le, le_Inf := assume s m, outer_measure.le_Inf, sup := λa b, Sup {a, b}, le_sup_left := assume a b, outer_measure.le_Sup $ by simp, le_sup_right := assume a b, outer_measure.le_Sup $ by simp, sup_le := assume a b c ha hb, outer_measure.Sup_le $ by simp [or_imp_distrib, ha, hb] {contextual:=tt}, inf := λa b, Inf {a, b}, inf_le_left := assume a b, outer_measure.Inf_le $ by simp, inf_le_right := assume a b, outer_measure.Inf_le $ by simp, le_inf := assume a b c ha hb, outer_measure.le_Inf $ by simp [or_imp_distrib, ha, hb] {contextual:=tt}, .. outer_measure.order_bot } @[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) : (Sup ms) s = ⨆ m : ms, m s := rfl @[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) : (⨆ i : ι, f i) s = ⨆ i, f i s := le_antisymm (supr_le $ λ ⟨_, i, rfl⟩, le_supr _ i) (supr_le $ λ i, le_supr (λ (m : {a : outer_measure α // ∃ i, f i = a}), m.1 s) ⟨f i, i, rfl⟩) @[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s := by have := supr_apply (λ b, cond b m₁ m₂) s; rwa [supr_bool_eq, supr_bool_eq] at this end supremum def map {β} (f : α → β) (m : outer_measure α) : outer_measure β := { measure_of := λs, m (f ⁻¹' s), empty := m.empty, mono := λ s t h, m.mono (preimage_mono h), Union_nat := λ s, by rw [preimage_Union]; exact m.Union_nat (λ i, f ⁻¹' s i) } @[simp] theorem map_apply {β} (f : α → β) (m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl @[simp] theorem map_id (m : outer_measure α) : map id m = m := ext $ λ s, rfl @[simp] theorem map_map {β γ} (f : α → β) (g : β → γ) (m : outer_measure α) : map g (map f m) = map (g ∘ f) m := ext $ λ s, rfl instance : functor outer_measure := {map := λ α β, map} instance : is_lawful_functor outer_measure := { id_map := λ α, map_id, comp_map := λ α β γ f g m, (map_map f g m).symm } /-- The dirac outer measure. -/ def dirac (a : α) : outer_measure α := { measure_of := λs, ⨆ h : a ∈ s, 1, empty := by simp, mono := λ s t h, supr_le_supr2 (λ h', ⟨h h', le_refl _⟩), Union_nat := λ s, supr_le $ λ h, let ⟨i, h⟩ := mem_Union.1 h in le_trans (by exact le_supr _ h) (ennreal.le_tsum i) } @[simp] theorem dirac_apply (a : α) (s : set α) : dirac a s = ⨆ h : a ∈ s, 1 := rfl def sum {ι} (f : ι → outer_measure α) : outer_measure α := { measure_of := λs, ∑ i, f i s, empty := by simp, mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h), Union_nat := λ s, by rw ennreal.tsum_comm; exact ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) } @[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) : sum f s = ∑ i, f i s := rfl instance : has_scalar ennreal (outer_measure α) := ⟨λ a m, { measure_of := λs, a * m s, empty := by simp, mono := λ s t h, canonically_ordered_semiring.mul_le_mul (le_refl _) (m.mono' h), Union_nat := λ s, by rw ennreal.tsum_mul_left; exact canonically_ordered_semiring.mul_le_mul (le_refl _) (m.Union_nat _) }⟩ @[simp] theorem smul_apply (a : ennreal) (m : outer_measure α) (s : set α) : (a • m) s = a * m s := rfl instance : semimodule ennreal (outer_measure α) := { smul_add := λ a m₁ m₂, ext $ λ s, mul_add _ _ _, add_smul := λ a b m, ext $ λ s, add_mul _ _ _, mul_smul := λ a b m, ext $ λ s, mul_assoc _ _ _, one_smul := λ m, ext $ λ s, one_mul _, zero_smul := λ m, ext $ λ s, zero_mul _, smul_zero := λ a, ext $ λ s, mul_zero _, ..outer_measure.has_scalar } theorem smul_dirac_apply (a : ennreal) (b : α) (s : set α) : (a • dirac b) s = ⨆ h : b ∈ s, a := by by_cases b ∈ s; simp [h] theorem top_apply {s : set α} (h : s.nonempty) : (⊤ : outer_measure α) s = ⊤ := let ⟨a, as⟩ := h in top_unique $ le_supr_of_le ⟨(⊤ : ennreal) • dirac a, trivial⟩ $ by simp [smul_dirac_apply, as] end basic section of_function set_option eqn_compiler.zeta true /-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/ protected def of_function {α : Type*} (m : set α → ennreal) (m_empty : m ∅ = 0) : outer_measure α := let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑i, m (f i) in { measure_of := μ, empty := le_antisymm (infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty]) (zero_le _), mono := assume s₁ s₂ hs, infi_le_infi $ assume f, infi_le_infi2 $ assume hb, ⟨subset.trans hs hb, le_refl _⟩, Union_nat := assume s, ennreal.le_of_forall_epsilon_le $ begin assume ε hε (hb : (∑i, μ (s i)) < ⊤), rcases ennreal.exists_pos_sum_of_encodable (ennreal.coe_lt_coe.2 hε) ℕ with ⟨ε', hε', hl⟩, refine le_trans _ (add_le_add_left' (le_of_lt hl)), rw ← ennreal.tsum_add, choose f hf using show ∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ (∑i, m (f i)) < μ (s i) + ε' i, { intro, have : μ (s i) < μ (s i) + ε' i := ennreal.lt_add_right (lt_of_le_of_lt (by apply ennreal.le_tsum) hb) (by simpa using hε' i), simpa [μ, infi_lt_iff] }, refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2), rw [← ennreal.tsum_prod, ← tsum_equiv equiv.nat_prod_nat_equiv_nat.symm], swap, {apply_instance}, refine infi_le_of_le _ (infi_le _ _), exact Union_subset (λ i, subset.trans (hf i).1 $ Union_subset $ λ j, subset.trans (by simp) $ subset_Union _ $ equiv.nat_prod_nat_equiv_nat (i, j)), end } theorem of_function_le {α : Type*} (m : set α → ennreal) (m_empty s) : outer_measure.of_function m m_empty s ≤ m s := let f : ℕ → set α := λi, nat.rec_on i s (λn s, ∅) in infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $ calc (∑i, m (f i)) = ({0} : finset ℕ).sum (λi, m (f i)) : tsum_eq_sum $ by intro i; cases i; simp [m_empty] ... = m s : by simp; refl theorem le_of_function {α : Type*} {m m_empty} {μ : outer_measure α} : μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s := ⟨λ H s, le_trans (H _) (of_function_le _ _ _), λ H s, le_infi $ λ f, le_infi $ λ hs, le_trans (μ.mono hs) $ le_trans (μ.Union f) $ ennreal.tsum_le_tsum $ λ i, H _⟩ end of_function section caratheodory_measurable universe u parameters {α : Type u} (m : outer_measure α) include m local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc variables {s s₁ s₂ : set α} private def C (s : set α) := ∀t, m t = m (t ∩ s) + m (t \ s) private lemma C_iff_le {s : set α} : C s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $ by convert m.union _ _; rw inter_union_diff t s @[simp] private lemma C_empty : C ∅ := by simp [C, m.empty, diff_empty] private lemma C_compl : C s₁ → C (- s₁) := by simp [C, diff_eq, add_comm] @[simp] private lemma C_compl_iff : C (- s) ↔ C s := ⟨λ h, by simpa using C_compl m h, C_compl⟩ private lemma C_union (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∪ s₂) := λ t, begin rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)), inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁, inter_eq_self_of_subset_right (set.subset_union_left _ _), union_diff_left, h₂ (t ∩ s₁)], simp [diff_eq] end private lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : C s₁) {t : set α} : m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) := by rw [h₁, set.inter_assoc, set.union_inter_cancel_left, inter_diff_assoc, union_diff_cancel_left h] private lemma C_Union_lt {s : ℕ → set α} : ∀{n:ℕ}, (∀i<n, C (s i)) → C (⋃i<n, s i) | 0 h := by simp [nat.not_lt_zero] | (n + 1) h := by rw Union_lt_succ; exact C_union m (h n (le_refl (n + 1))) (C_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _) private lemma C_inter (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∩ s₂) := by rw [← C_compl_iff, compl_inter]; from C_union _ (C_compl _ h₁) (C_compl _ h₂) private lemma C_sum {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) {t : set α} : ∀ {n}, (finset.range n).sum (λi, m (t ∩ s i)) = m (t ∩ ⋃i<n, s i) | 0 := by simp [nat.not_lt_zero, m.empty] | (nat.succ n) := begin simp [Union_lt_succ, range_succ], rw [measure_inter_union m _ (h n), C_sum], intro a, simpa [range_succ] using λ h₁ i hi h₂, hd _ _ (ne_of_gt hi) ⟨h₁, h₂⟩ end private lemma C_Union_nat {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) : C (⋃i, s i) := C_iff_le.2 $ λ t, begin have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)), { convert m.Union (λ i, t ∩ s i), { rw inter_Union }, { simp [ennreal.tsum_eq_supr_nat, C_sum m h hd] } }, refine le_trans (add_le_add_right' hp) _, rw ennreal.supr_add, refine supr_le (λ n, le_trans (add_le_add_left' _) (ge_of_eq (C_Union_lt m (λ i _, h i) _))), refine m.mono (diff_subset_diff_right _), exact bUnion_subset (λ i _, subset_Union _ i), end private lemma f_Union {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑i, m (s i) := begin refine le_antisymm (m.Union_nat s) _, rw ennreal.tsum_eq_supr_nat, refine supr_le (λ n, _), have := @C_sum _ m _ h hd univ n, simp at this, simp [this], exact m.mono (bUnion_subset (λ i _, subset_Union _ i)), end private def caratheodory_dynkin : measurable_space.dynkin_system α := { has := C, has_empty := C_empty, has_compl := assume s, C_compl, has_Union_nat := assume f hf hn, C_Union_nat hn hf } /-- Given an outer measure `μ`, the Caratheodory measurable space is defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/ protected def caratheodory : measurable_space α := caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, C_inter lemma is_caratheodory {s : set α} : caratheodory.is_measurable s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) := iff.rfl lemma is_caratheodory_le {s : set α} : caratheodory.is_measurable s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := C_iff_le protected lemma Union_eq_of_caratheodory {s : ℕ → set α} (h : ∀i, caratheodory.is_measurable (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑i, m (s i) := f_Union h hd end caratheodory_measurable variables {α : Type*} lemma caratheodory_is_measurable {m : set α → ennreal} {s : set α} {h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (outer_measure.of_function m h₀).caratheodory.is_measurable s := let o := (outer_measure.of_function m h₀) in (is_caratheodory_le o).2 $ λ t, le_infi $ λ f, le_infi $ λ hf, begin refine le_trans (add_le_add' (infi_le_of_le (λi, f i ∩ s) $ infi_le _ _) (infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _, { rw ← Union_inter, exact inter_subset_inter_left _ hf }, { rw ← Union_diff, exact diff_subset_diff_left hf }, { rw ← ennreal.tsum_add, exact ennreal.tsum_le_tsum (λ i, hs _) } end @[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ := top_unique $ λ s _ t, (add_zero _).symm theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ := top_unique $ assume s hs, (is_caratheodory_le _).2 $ assume t, t.eq_empty_or_nonempty.elim (λ ht, by simp [ht]) (λ ht, by simp only [ht, top_apply, le_top]) theorem le_add_caratheodory (m₁ m₂ : outer_measure α) : m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory := λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t, add_left_comm] theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) : (⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory := λ s h t, by simp [λ i, measurable_space.is_measurable_infi.1 h i t, ennreal.tsum_add] theorem le_smul_caratheodory (a : ennreal) (m : outer_measure α) : m.caratheodory ≤ (a • m).caratheodory := λ s h t, by simp [h t, mul_add] @[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ := top_unique $ λ s _ t, begin by_cases a ∈ t; simp [h], by_cases a ∈ s; simp [h] end section Inf_gen def Inf_gen (m : set (outer_measure α)) (s : set α) : ennreal := ⨆(h : s.nonempty), ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s @[simp] lemma Inf_gen_empty (m : set (outer_measure α)) : Inf_gen m ∅ = 0 := by simp [Inf_gen, empty_not_nonempty] lemma Inf_gen_nonempty1 (m : set (outer_measure α)) (t : set α) (h : t.nonempty) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := by rw [Inf_gen, supr_pos h] lemma Inf_gen_nonempty2 (m : set (outer_measure α)) (μ) (h : μ ∈ m) (t) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := begin cases t.eq_empty_or_nonempty with ht ht, { simp [ht], refine (bot_unique $ infi_le_of_le μ $ _).symm, refine infi_le_of_le h (le_refl ⊥) }, { exact Inf_gen_nonempty1 m t ht } end lemma Inf_eq_of_function_Inf_gen (m : set (outer_measure α)) : Inf m = outer_measure.of_function (Inf_gen m) (Inf_gen_empty m) := begin refine le_antisymm (assume t', le_of_function.2 (assume t, _) _) (_root_.le_Inf $ assume μ hμ t, le_trans (outer_measure.of_function_le _ _ _) _); cases t.eq_empty_or_nonempty with ht ht; simp [ht, Inf_gen_nonempty1], { assume μ hμ, exact (show Inf m ≤ μ, from _root_.Inf_le hμ) t }, { exact infi_le_of_le μ (infi_le _ hμ) } end end Inf_gen end outer_measure end measure_theory
2527908281d1f172f2abf28ecb0757d98b1d0f9a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/combinatorics/set_family/intersecting.lean
63fb5939370aeb25680537677df2b50a166b3825
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
7,606
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.fintype.card import order.upper_lower /-! # Intersecting families This file defines intersecting families and proves their basic properties. ## Main declarations * `set.intersecting`: Predicate for a set of elements in a generalized boolean algebra to be an intersecting family. * `set.intersecting.card_le`: An intersecting family can only take up to half the elements, because `a` and `aᶜ` cannot simultaneously be in it. * `set.intersecting.is_max_iff_card_eq`: Any maximal intersecting family takes up half the elements. ## References * [D. J. Kleitman, *Families of non-disjoint subsets*][kleitman1966] -/ open finset variables {α : Type*} namespace set section semilattice_inf variables [semilattice_inf α] [order_bot α] {s t : set α} {a b c : α} /-- A set family is intersecting if every pair of elements is non-disjoint. -/ def intersecting (s : set α) : Prop := ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → ¬ disjoint a b @[mono] lemma intersecting.mono (h : t ⊆ s) (hs : s.intersecting) : t.intersecting := λ a ha b hb, hs (h ha) (h hb) lemma intersecting.not_bot_mem (hs : s.intersecting) : ⊥ ∉ s := λ h, hs h h disjoint_bot_left lemma intersecting.ne_bot (hs : s.intersecting) (ha : a ∈ s) : a ≠ ⊥ := ne_of_mem_of_not_mem ha hs.not_bot_mem lemma intersecting_empty : (∅ : set α).intersecting := λ _, false.elim @[simp] lemma intersecting_singleton : ({a} : set α).intersecting ↔ a ≠ ⊥ := by simp [intersecting] lemma intersecting.insert (hs : s.intersecting) (ha : a ≠ ⊥) (h : ∀ b ∈ s, ¬ disjoint a b) : (insert a s).intersecting := begin rintro b (rfl | hb) c (rfl | hc), { rwa disjoint_self }, { exact h _ hc }, { exact λ H, h _ hb H.symm }, { exact hs hb hc } end lemma intersecting_insert : (insert a s).intersecting ↔ s.intersecting ∧ a ≠ ⊥ ∧ ∀ b ∈ s, ¬ disjoint a b := ⟨λ h, ⟨h.mono $ subset_insert _ _, h.ne_bot $ mem_insert _ _, λ b hb, h (mem_insert _ _) $ mem_insert_of_mem _ hb⟩, λ h, h.1.insert h.2.1 h.2.2⟩ lemma intersecting_iff_pairwise_not_disjoint : s.intersecting ↔ s.pairwise (λ a b, ¬ disjoint a b) ∧ s ≠ {⊥} := begin refine ⟨λ h, ⟨λ a ha b hb _, h ha hb, _⟩, λ h a ha b hb hab, _⟩, { rintro rfl, exact intersecting_singleton.1 h rfl }, { have := h.1.eq ha hb (not_not.2 hab), rw [this, disjoint_self] at hab, rw hab at hb, exact h.2 (eq_singleton_iff_unique_mem.2 ⟨hb, λ c hc, not_ne_iff.1 $ λ H, h.1 hb hc H.symm disjoint_bot_left⟩) } end protected lemma subsingleton.intersecting (hs : s.subsingleton) : s.intersecting ↔ s ≠ {⊥} := intersecting_iff_pairwise_not_disjoint.trans $ and_iff_right $ hs.pairwise _ lemma intersecting_iff_eq_empty_of_subsingleton [subsingleton α] (s : set α) : s.intersecting ↔ s = ∅ := begin refine subsingleton_of_subsingleton.intersecting.trans ⟨not_imp_comm.2 $ λ h, subsingleton_of_subsingleton.eq_singleton_of_mem _, _⟩, { obtain ⟨a, ha⟩ := ne_empty_iff_nonempty.1 h, rwa subsingleton.elim ⊥ a }, { rintro rfl, exact (set.singleton_nonempty _).ne_empty.symm } end /-- Maximal intersecting families are upper sets. -/ protected lemma intersecting.is_upper_set (hs : s.intersecting) (h : ∀ t : set α, t.intersecting → s ⊆ t → s = t) : is_upper_set s := begin classical, rintro a b hab ha, rw h (insert b s) _ (subset_insert _ _), { exact mem_insert _ _ }, exact hs.insert (mt (eq_bot_mono hab) $ hs.ne_bot ha) (λ c hc hbc, hs ha hc $ hbc.mono_left hab), end /-- Maximal intersecting families are upper sets. Finset version. -/ lemma intersecting.is_upper_set' {s : finset α} (hs : (s : set α).intersecting) (h : ∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t) : is_upper_set (s : set α) := begin classical, rintro a b hab ha, rw h (insert b s) _ (finset.subset_insert _ _), { exact mem_insert_self _ _ }, rw coe_insert, exact hs.insert (mt (eq_bot_mono hab) $ hs.ne_bot ha) (λ c hc hbc, hs ha hc $ hbc.mono_left hab), end end semilattice_inf lemma intersecting.exists_mem_set {𝒜 : set (set α)} (h𝒜 : 𝒜.intersecting) {s t : set α} (hs : s ∈ 𝒜) (ht : t ∈ 𝒜) : ∃ a, a ∈ s ∧ a ∈ t := not_disjoint_iff.1 $ h𝒜 hs ht lemma intersecting.exists_mem_finset [decidable_eq α] {𝒜 : set (finset α)} (h𝒜 : 𝒜.intersecting) {s t : finset α} (hs : s ∈ 𝒜) (ht : t ∈ 𝒜) : ∃ a, a ∈ s ∧ a ∈ t := not_disjoint_iff.1 $ disjoint_coe.not.2 $ h𝒜 hs ht variables [boolean_algebra α] lemma intersecting.not_compl_mem {s : set α} (hs : s.intersecting) {a : α} (ha : a ∈ s) : aᶜ ∉ s := λ h, hs ha h disjoint_compl_right lemma intersecting.not_mem {s : set α} (hs : s.intersecting) {a : α} (ha : aᶜ ∈ s) : a ∉ s := λ h, hs ha h disjoint_compl_left lemma intersecting.disjoint_map_compl {s : finset α} (hs : (s : set α).intersecting) : disjoint s (s.map ⟨compl, compl_injective⟩) := begin rw finset.disjoint_left, rintro x hx hxc, obtain ⟨x, hx', rfl⟩ := mem_map.mp hxc, exact hs.not_compl_mem hx' hx, end lemma intersecting.card_le [fintype α] {s : finset α} (hs : (s : set α).intersecting) : 2 * s.card ≤ fintype.card α := begin classical, refine (s.disj_union _ hs.disjoint_map_compl).card_le_univ.trans_eq' _, rw [two_mul, card_disj_union, card_map], end variables [nontrivial α] [fintype α] {s : finset α} -- Note, this lemma is false when `α` has exactly one element and boring when `α` is empty. lemma intersecting.is_max_iff_card_eq (hs : (s : set α).intersecting) : (∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t) ↔ 2 * s.card = fintype.card α := begin classical, refine ⟨λ h, _, λ h t ht hst, finset.eq_of_subset_of_card_le hst $ le_of_mul_le_mul_left (ht.card_le.trans_eq h.symm) two_pos⟩, suffices : s.disj_union (s.map ⟨compl, compl_injective⟩) (hs.disjoint_map_compl) = finset.univ, { rw [fintype.card, ←this, two_mul, card_disj_union, card_map] }, rw [←coe_eq_univ, disj_union_eq_union, coe_union, coe_map, function.embedding.coe_fn_mk, image_eq_preimage_of_inverse compl_compl compl_compl], refine eq_univ_of_forall (λ a, _), simp_rw [mem_union, mem_preimage], by_contra' ha, refine s.ne_insert_of_not_mem _ ha.1 (h _ _ $ s.subset_insert _), rw coe_insert, refine hs.insert _ (λ b hb hab, ha.2 $ (hs.is_upper_set' h) hab.le_compl_left hb), rintro rfl, have := h {⊤} (by { rw coe_singleton, exact intersecting_singleton.2 top_ne_bot }), rw compl_bot at ha, rw coe_eq_empty.1 ((hs.is_upper_set' h).not_top_mem.1 ha.2) at this, exact singleton_ne_empty _ (this $ empty_subset _).symm, end lemma intersecting.exists_card_eq (hs : (s : set α).intersecting) : ∃ t, s ⊆ t ∧ 2 * t.card = fintype.card α ∧ (t : set α).intersecting := begin have := hs.card_le, rw [mul_comm, ←nat.le_div_iff_mul_le' two_pos] at this, revert hs, refine s.strong_downward_induction_on _ this, rintro s ih hcard hs, by_cases ∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t, { exact ⟨s, subset.rfl, hs.is_max_iff_card_eq.1 h, hs⟩ }, push_neg at h, obtain ⟨t, ht, hst⟩ := h, refine (ih _ (_root_.ssubset_iff_subset_ne.2 hst) ht).imp (λ u, and.imp_left hst.1.trans), rw [nat.le_div_iff_mul_le' two_pos, mul_comm], exact ht.card_le, end end set
cc36abe215e7a8b70e4a33b33f8a824cc536f5ba
c777c32c8e484e195053731103c5e52af26a25d1
/test/matrix_reflection.lean
331d0a5573bfc227b77292b0adc42322b1f3261c
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
1,110
lean
import data.matrix.reflection variables {α: Type*} open_locale matrix open matrix -- This one is too long for the docstring, but is nice to have tested example [add_comm_monoid α] [has_mul α] (a₁₁ a₁₂ a₁₃ a₂₁ a₂₂ a₂₃ a₃₁ a₃₂ a₃₃ b₁₁ b₁₂ b₁₃ b₂₁ b₂₂ b₂₃ b₃₁ b₃₂ b₃₃ : α) : !![a₁₁, a₁₂, a₁₃; a₂₁, a₂₂, a₂₃; a₃₁, a₃₂, a₃₃] ⬝ !![b₁₁, b₁₂, b₁₃; b₂₁, b₂₂, b₂₃; b₃₁, b₃₂, b₃₃] = !![a₁₁*b₁₁ + a₁₂*b₂₁ + a₁₃*b₃₁, a₁₁*b₁₂ + a₁₂*b₂₂ + a₁₃*b₃₂, a₁₁*b₁₃ + a₁₂*b₂₃ + a₁₃*b₃₃; a₂₁*b₁₁ + a₂₂*b₂₁ + a₂₃*b₃₁, a₂₁*b₁₂ + a₂₂*b₂₂ + a₂₃*b₃₂, a₂₁*b₁₃ + a₂₂*b₂₃ + a₂₃*b₃₃; a₃₁*b₁₁ + a₃₂*b₂₁ + a₃₃*b₃₁, a₃₁*b₁₂ + a₃₂*b₂₂ + a₃₃*b₃₂, a₃₁*b₁₃ + a₃₂*b₂₃ + a₃₃*b₃₃] := (matrix.mulᵣ_eq _ _).symm
4e2558f20b2d3e4f7dd33507e5837f6abd9f5ea4
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/affine_space/independent.lean
d78a866495336a8dedc30fa4ab38f6ff62b63deb
[ "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
32,154
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 data.finset.sort import data.fin.vec_notation import linear_algebra.affine_space.combination import linear_algebra.affine_space.affine_equiv import linear_algebra.basis /-! # Affine independence This file defines affinely independent families of points. ## Main definitions * `affine_independent` defines affinely independent families of points as those where no nontrivial weighted subtraction is `0`. This is proved equivalent to two other formulations: linear independence of the results of subtracting a base point in the family from the other points in the family, or any equal affine combinations having the same weights. A bundled type `simplex` is provided for finite affinely independent families of points, with an abbreviation `triangle` for the case of three points. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable theory open_locale big_operators classical affine open function section affine_independent variables (k : Type*) {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V /-- An indexed family is said to be affinely independent if no nontrivial weighted subtractions (where the sum of weights is 0) are 0. -/ def affine_independent (p : ι → P) : Prop := ∀ (s : finset ι) (w : ι → k), ∑ i in s, w i = 0 → s.weighted_vsub p w = (0:V) → ∀ i ∈ s, w i = 0 /-- The definition of `affine_independent`. -/ lemma affine_independent_def (p : ι → P) : affine_independent k p ↔ ∀ (s : finset ι) (w : ι → k), ∑ i in s, w i = 0 → s.weighted_vsub p w = (0 : V) → ∀ i ∈ s, w i = 0 := iff.rfl /-- A family with at most one point is affinely independent. -/ lemma affine_independent_of_subsingleton [subsingleton ι] (p : ι → P) : affine_independent k p := λ s w h hs i hi, fintype.eq_of_subsingleton_of_sum_eq h i hi /-- A family indexed by a `fintype` is affinely independent if and only if no nontrivial weighted subtractions over `finset.univ` (where the sum of the weights is 0) are 0. -/ lemma affine_independent_iff_of_fintype [fintype ι] (p : ι → P) : affine_independent k p ↔ ∀ w : ι → k, ∑ i, w i = 0 → finset.univ.weighted_vsub p w = (0 : V) → ∀ i, w i = 0 := begin split, { exact λ h w hw hs i, h finset.univ w hw hs i (finset.mem_univ _) }, { intros h s w hw hs i hi, rw finset.weighted_vsub_indicator_subset _ _ (finset.subset_univ s) at hs, rw set.sum_indicator_subset _ (finset.subset_univ s) at hw, replace h := h ((↑s : set ι).indicator w) hw hs i, simpa [hi] using h } end /-- A family is affinely independent if and only if the differences from a base point in that family are linearly independent. -/ lemma affine_independent_iff_linear_independent_vsub (p : ι → P) (i1 : ι) : affine_independent k p ↔ linear_independent k (λ i : {x // x ≠ i1}, (p i -ᵥ p i1 : V)) := begin split, { intro h, rw linear_independent_iff', intros s g hg i hi, set f : ι → k := λ x, if hx : x = i1 then -∑ y in s, g y else g ⟨x, hx⟩ with hfdef, let s2 : finset ι := insert i1 (s.map (embedding.subtype _)), have hfg : ∀ x : {x // x ≠ i1}, g x = f x, { intro x, rw hfdef, dsimp only [], erw [dif_neg x.property, subtype.coe_eta] }, rw hfg, have hf : ∑ ι in s2, f ι = 0, { rw [finset.sum_insert (finset.not_mem_map_subtype_of_not_property s (not_not.2 rfl)), finset.sum_subtype_map_embedding (λ x hx, (hfg x).symm)], rw hfdef, dsimp only [], rw dif_pos rfl, exact neg_add_self _ }, have hs2 : s2.weighted_vsub p f = (0:V), { set f2 : ι → V := λ x, f x • (p x -ᵥ p i1) with hf2def, set g2 : {x // x ≠ i1} → V := λ x, g x • (p x -ᵥ p i1) with hg2def, have hf2g2 : ∀ x : {x // x ≠ i1}, f2 x = g2 x, { simp_rw [hf2def, hg2def, hfg], exact λ x, rfl }, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s2 f p hf (p i1), finset.weighted_vsub_of_point_insert, finset.weighted_vsub_of_point_apply, finset.sum_subtype_map_embedding (λ x hx, hf2g2 x)], exact hg }, exact h s2 f hf hs2 i (finset.mem_insert_of_mem (finset.mem_map.2 ⟨i, hi, rfl⟩)) }, { intro h, rw linear_independent_iff' at h, intros s w hw hs i hi, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s w p hw (p i1), ←s.weighted_vsub_of_point_erase w p i1, finset.weighted_vsub_of_point_apply] at hs, let f : ι → V := λ i, w i • (p i -ᵥ p i1), have hs2 : ∑ i in (s.erase i1).subtype (λ i, i ≠ i1), f i = 0, { rw [←hs], convert finset.sum_subtype_of_mem f (λ x, finset.ne_of_mem_erase) }, have h2 := h ((s.erase i1).subtype (λ i, i ≠ i1)) (λ x, w x) hs2, simp_rw [finset.mem_subtype] at h2, have h2b : ∀ i ∈ s, i ≠ i1 → w i = 0 := λ i his hi, h2 ⟨i, hi⟩ (finset.mem_erase_of_ne_of_mem hi his), exact finset.eq_zero_of_sum_eq_zero hw h2b i hi } end /-- A set is affinely independent if and only if the differences from a base point in that set are linearly independent. -/ lemma affine_independent_set_iff_linear_independent_vsub {s : set P} {p₁ : P} (hp₁ : p₁ ∈ s) : affine_independent k (λ p, p : s → P) ↔ linear_independent k (λ v, v : (λ p, (p -ᵥ p₁ : V)) '' (s \ {p₁}) → V) := begin rw affine_independent_iff_linear_independent_vsub k (λ p, p : s → P) ⟨p₁, hp₁⟩, split, { intro h, have hv : ∀ v : (λ p, (p -ᵥ p₁ : V)) '' (s \ {p₁}), (v : V) +ᵥ p₁ ∈ s \ {p₁} := λ v, (vsub_left_injective p₁).mem_set_image.1 ((vadd_vsub (v : V) p₁).symm ▸ v.property), let f : (λ p : P, (p -ᵥ p₁ : V)) '' (s \ {p₁}) → {x : s // x ≠ ⟨p₁, hp₁⟩} := λ x, ⟨⟨(x : V) +ᵥ p₁, set.mem_of_mem_diff (hv x)⟩, λ hx, set.not_mem_of_mem_diff (hv x) (subtype.ext_iff.1 hx)⟩, convert h.comp f (λ x1 x2 hx, (subtype.ext (vadd_right_cancel p₁ (subtype.ext_iff.1 (subtype.ext_iff.1 hx))))), ext v, exact (vadd_vsub (v : V) p₁).symm }, { intro h, let f : {x : s // x ≠ ⟨p₁, hp₁⟩} → (λ p : P, (p -ᵥ p₁ : V)) '' (s \ {p₁}) := λ x, ⟨((x : s) : P) -ᵥ p₁, ⟨x, ⟨⟨(x : s).property, λ hx, x.property (subtype.ext hx)⟩, rfl⟩⟩⟩, convert h.comp f (λ x1 x2 hx, subtype.ext (subtype.ext (vsub_left_cancel (subtype.ext_iff.1 hx)))) } end /-- A set of nonzero vectors is linearly independent if and only if, given a point `p₁`, the vectors added to `p₁` and `p₁` itself are affinely independent. -/ lemma linear_independent_set_iff_affine_independent_vadd_union_singleton {s : set V} (hs : ∀ v ∈ s, v ≠ (0 : V)) (p₁ : P) : linear_independent k (λ v, v : s → V) ↔ affine_independent k (λ p, p : {p₁} ∪ ((λ v, v +ᵥ p₁) '' s) → P) := begin rw affine_independent_set_iff_linear_independent_vsub k (set.mem_union_left _ (set.mem_singleton p₁)), have h : (λ p, (p -ᵥ p₁ : V)) '' (({p₁} ∪ (λ v, v +ᵥ p₁) '' s) \ {p₁}) = s, { simp_rw [set.union_diff_left, set.image_diff (vsub_left_injective p₁), set.image_image, set.image_singleton, vsub_self, vadd_vsub, set.image_id'], exact set.diff_singleton_eq_self (λ h, hs 0 h rfl) }, rw h end /-- A family is affinely independent if and only if any affine combinations (with sum of weights 1) that evaluate to the same point have equal `set.indicator`. -/ lemma affine_independent_iff_indicator_eq_of_affine_combination_eq (p : ι → P) : affine_independent k p ↔ ∀ (s1 s2 : finset ι) (w1 w2 : ι → k), ∑ i in s1, w1 i = 1 → ∑ i in s2, w2 i = 1 → s1.affine_combination p w1 = s2.affine_combination p w2 → set.indicator ↑s1 w1 = set.indicator ↑s2 w2 := begin split, { intros ha s1 s2 w1 w2 hw1 hw2 heq, ext i, by_cases hi : i ∈ (s1 ∪ s2), { rw ←sub_eq_zero, rw set.sum_indicator_subset _ (finset.subset_union_left s1 s2) at hw1, rw set.sum_indicator_subset _ (finset.subset_union_right s1 s2) at hw2, have hws : ∑ i in s1 ∪ s2, (set.indicator ↑s1 w1 - set.indicator ↑s2 w2) i = 0, { simp [hw1, hw2] }, rw [finset.affine_combination_indicator_subset _ _ (finset.subset_union_left s1 s2), finset.affine_combination_indicator_subset _ _ (finset.subset_union_right s1 s2), ←@vsub_eq_zero_iff_eq V, finset.affine_combination_vsub] at heq, exact ha (s1 ∪ s2) (set.indicator ↑s1 w1 - set.indicator ↑s2 w2) hws heq i hi }, { rw [←finset.mem_coe, finset.coe_union] at hi, simp [mt (set.mem_union_left ↑s2) hi, mt (set.mem_union_right ↑s1) hi] } }, { intros ha s w hw hs i0 hi0, let w1 : ι → k := function.update (function.const ι 0) i0 1, have hw1 : ∑ i in s, w1 i = 1, { rw [finset.sum_update_of_mem hi0, finset.sum_const_zero, add_zero] }, have hw1s : s.affine_combination p w1 = p i0 := s.affine_combination_of_eq_one_of_eq_zero w1 p hi0 (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), let w2 := w + w1, have hw2 : ∑ i in s, w2 i = 1, { simp [w2, finset.sum_add_distrib, hw, hw1] }, have hw2s : s.affine_combination p w2 = p i0, { simp [w2, ←finset.weighted_vsub_vadd_affine_combination, hs, hw1s] }, replace ha := ha s s w2 w1 hw2 hw1 (hw1s.symm ▸ hw2s), have hws : w2 i0 - w1 i0 = 0, { rw ←finset.mem_coe at hi0, rw [←set.indicator_of_mem hi0 w2, ←set.indicator_of_mem hi0 w1, ha, sub_self] }, simpa [w2] using hws } end /-- A finite family is affinely independent if and only if any affine combinations (with sum of weights 1) that evaluate to the same point are equal. -/ lemma affine_independent_iff_eq_of_fintype_affine_combination_eq [fintype ι] (p : ι → P) : affine_independent k p ↔ ∀ (w1 w2 : ι → k), ∑ i, w1 i = 1 → ∑ i, w2 i = 1 → finset.univ.affine_combination p w1 = finset.univ.affine_combination p w2 → w1 = w2 := begin rw affine_independent_iff_indicator_eq_of_affine_combination_eq, split, { intros h w1 w2 hw1 hw2 hweq, simpa only [set.indicator_univ, finset.coe_univ] using h _ _ w1 w2 hw1 hw2 hweq, }, { intros h s1 s2 w1 w2 hw1 hw2 hweq, have hw1' : ∑ i, (s1 : set ι).indicator w1 i = 1, { rwa set.sum_indicator_subset _ (finset.subset_univ s1) at hw1, }, have hw2' : ∑ i, (s2 : set ι).indicator w2 i = 1, { rwa set.sum_indicator_subset _ (finset.subset_univ s2) at hw2, }, rw [finset.affine_combination_indicator_subset w1 p (finset.subset_univ s1), finset.affine_combination_indicator_subset w2 p (finset.subset_univ s2)] at hweq, exact h _ _ hw1' hw2' hweq, }, end variables {k} /-- If we single out one member of an affine-independent family of points and affinely transport all others along the line joining them to this member, the resulting new family of points is affine- independent. This is the affine version of `linear_independent.units_smul`. -/ lemma affine_independent.units_line_map {p : ι → P} (hp : affine_independent k p) (j : ι) (w : ι → units k) : affine_independent k (λ i, affine_map.line_map (p j) (p i) (w i : k)) := begin rw affine_independent_iff_linear_independent_vsub k _ j at hp ⊢, simp only [affine_map.line_map_vsub_left, affine_map.coe_const, affine_map.line_map_same], exact hp.units_smul (λ i, w i), end lemma affine_independent.indicator_eq_of_affine_combination_eq {p : ι → P} (ha : affine_independent k p) (s₁ s₂ : finset ι) (w₁ w₂ : ι → k) (hw₁ : ∑ i in s₁, w₁ i = 1) (hw₂ : ∑ i in s₂, w₂ i = 1) (h : s₁.affine_combination p w₁ = s₂.affine_combination p w₂) : set.indicator ↑s₁ w₁ = set.indicator ↑s₂ w₂ := (affine_independent_iff_indicator_eq_of_affine_combination_eq k p).1 ha s₁ s₂ w₁ w₂ hw₁ hw₂ h /-- An affinely independent family is injective, if the underlying ring is nontrivial. -/ protected lemma affine_independent.injective [nontrivial k] {p : ι → P} (ha : affine_independent k p) : function.injective p := begin intros i j hij, rw affine_independent_iff_linear_independent_vsub _ _ j at ha, by_contra hij', exact ha.ne_zero ⟨i, hij'⟩ (vsub_eq_zero_iff_eq.mpr hij) end /-- If a family is affinely independent, so is any subfamily given by composition of an embedding into index type with the original family. -/ lemma affine_independent.comp_embedding {ι2 : Type*} (f : ι2 ↪ ι) {p : ι → P} (ha : affine_independent k p) : affine_independent k (p ∘ f) := begin intros fs w hw hs i0 hi0, let fs' := fs.map f, let w' := λ i, if h : ∃ i2, f i2 = i then w h.some else 0, have hw' : ∀ i2 : ι2, w' (f i2) = w i2, { intro i2, have h : ∃ i : ι2, f i = f i2 := ⟨i2, rfl⟩, have hs : h.some = i2 := f.injective h.some_spec, simp_rw [w', dif_pos h, hs] }, have hw's : ∑ i in fs', w' i = 0, { rw [←hw, finset.sum_map], simp [hw'] }, have hs' : fs'.weighted_vsub p w' = (0:V), { rw [←hs, finset.weighted_vsub_map], congr' with i, simp [hw'] }, rw [←ha fs' w' hw's hs' (f i0) ((finset.mem_map' _).2 hi0), hw'] end /-- If a family is affinely independent, so is any subfamily indexed by a subtype of the index type. -/ protected lemma affine_independent.subtype {p : ι → P} (ha : affine_independent k p) (s : set ι) : affine_independent k (λ i : s, p i) := ha.comp_embedding (embedding.subtype _) /-- If an indexed family of points is affinely independent, so is the corresponding set of points. -/ protected lemma affine_independent.range {p : ι → P} (ha : affine_independent k p) : affine_independent k (λ x, x : set.range p → P) := begin let f : set.range p → ι := λ x, x.property.some, have hf : ∀ x, p (f x) = x := λ x, x.property.some_spec, let fe : set.range p ↪ ι := ⟨f, λ x₁ x₂ he, subtype.ext (hf x₁ ▸ hf x₂ ▸ he ▸ rfl)⟩, convert ha.comp_embedding fe, ext, simp [hf] end lemma affine_independent_equiv {ι' : Type*} (e : ι ≃ ι') {p : ι' → P} : affine_independent k (p ∘ e) ↔ affine_independent k p := begin refine ⟨_, affine_independent.comp_embedding e.to_embedding⟩, intros h, have : p = p ∘ e ∘ e.symm.to_embedding, { ext, simp, }, rw this, exact h.comp_embedding e.symm.to_embedding, end /-- If a set of points is affinely independent, so is any subset. -/ protected lemma affine_independent.mono {s t : set P} (ha : affine_independent k (λ x, x : t → P)) (hs : s ⊆ t) : affine_independent k (λ x, x : s → P) := ha.comp_embedding (s.embedding_of_subset t hs) /-- If the range of an injective indexed family of points is affinely independent, so is that family. -/ lemma affine_independent.of_set_of_injective {p : ι → P} (ha : affine_independent k (λ x, x : set.range p → P)) (hi : function.injective p) : affine_independent k p := ha.comp_embedding (⟨λ i, ⟨p i, set.mem_range_self _⟩, λ x y h, hi (subtype.mk_eq_mk.1 h)⟩ : ι ↪ set.range p) section composition variables {V₂ P₂ : Type*} [add_comm_group V₂] [module k V₂] [affine_space V₂ P₂] include V₂ /-- If the image of a family of points in affine space under an affine transformation is affine- independent, then the original family of points is also affine-independent. -/ lemma affine_independent.of_comp {p : ι → P} (f : P →ᵃ[k] P₂) (hai : affine_independent k (f ∘ p)) : affine_independent k p := begin cases is_empty_or_nonempty ι with h h, { haveI := h, apply affine_independent_of_subsingleton, }, obtain ⟨i⟩ := h, rw affine_independent_iff_linear_independent_vsub k p i, simp_rw [affine_independent_iff_linear_independent_vsub k (f ∘ p) i, function.comp_app, ← f.linear_map_vsub] at hai, exact linear_independent.of_comp f.linear hai, end /-- The image of a family of points in affine space, under an injective affine transformation, is affine-independent. -/ lemma affine_independent.map' {p : ι → P} (hai : affine_independent k p) (f : P →ᵃ[k] P₂) (hf : function.injective f) : affine_independent k (f ∘ p) := begin cases is_empty_or_nonempty ι with h h, { haveI := h, apply affine_independent_of_subsingleton, }, obtain ⟨i⟩ := h, rw affine_independent_iff_linear_independent_vsub k p i at hai, simp_rw [affine_independent_iff_linear_independent_vsub k (f ∘ p) i, function.comp_app, ← f.linear_map_vsub], have hf' : f.linear.ker = ⊥, { rwa [linear_map.ker_eq_bot, f.injective_iff_linear_injective], }, exact linear_independent.map' hai f.linear hf', end /-- Injective affine maps preserve affine independence. -/ lemma affine_map.affine_independent_iff {p : ι → P} (f : P →ᵃ[k] P₂) (hf : function.injective f) : affine_independent k (f ∘ p) ↔ affine_independent k p := ⟨affine_independent.of_comp f, λ hai, affine_independent.map' hai f hf⟩ /-- Affine equivalences preserve affine independence of families of points. -/ lemma affine_equiv.affine_independent_iff {p : ι → P} (e : P ≃ᵃ[k] P₂) : affine_independent k (e ∘ p) ↔ affine_independent k p := e.to_affine_map.affine_independent_iff e.to_equiv.injective /-- Affine equivalences preserve affine independence of subsets. -/ lemma affine_equiv.affine_independent_set_of_eq_iff {s : set P} (e : P ≃ᵃ[k] P₂) : affine_independent k (coe : (e '' s) → P₂) ↔ affine_independent k (coe : s → P) := begin have : e ∘ (coe : s → P) = (coe : e '' s → P₂) ∘ ((e : P ≃ P₂).image s) := rfl, rw [← e.affine_independent_iff, this, affine_independent_equiv], end end composition /-- If a family is affinely independent, and the spans of points indexed by two subsets of the index type have a point in common, those subsets of the index type have an element in common, if the underlying ring is nontrivial. -/ lemma affine_independent.exists_mem_inter_of_exists_mem_inter_affine_span [nontrivial k] {p : ι → P} (ha : affine_independent k p) {s1 s2 : set ι} {p0 : P} (hp0s1 : p0 ∈ affine_span k (p '' s1)) (hp0s2 : p0 ∈ affine_span k (p '' s2)): ∃ (i : ι), i ∈ s1 ∩ s2 := begin rw set.image_eq_range at hp0s1 hp0s2, rw [mem_affine_span_iff_eq_affine_combination, ←finset.eq_affine_combination_subset_iff_eq_affine_combination_subtype] at hp0s1 hp0s2, rcases hp0s1 with ⟨fs1, hfs1, w1, hw1, hp0s1⟩, rcases hp0s2 with ⟨fs2, hfs2, w2, hw2, hp0s2⟩, rw affine_independent_iff_indicator_eq_of_affine_combination_eq at ha, replace ha := ha fs1 fs2 w1 w2 hw1 hw2 (hp0s1 ▸ hp0s2), have hnz : ∑ i in fs1, w1 i ≠ 0 := hw1.symm ▸ one_ne_zero, rcases finset.exists_ne_zero_of_sum_ne_zero hnz with ⟨i, hifs1, hinz⟩, simp_rw [←set.indicator_of_mem (finset.mem_coe.2 hifs1) w1, ha] at hinz, use [i, hfs1 hifs1, hfs2 (set.mem_of_indicator_ne_zero hinz)] end /-- If a family is affinely independent, the spans of points indexed by disjoint subsets of the index type are disjoint, if the underlying ring is nontrivial. -/ lemma affine_independent.affine_span_disjoint_of_disjoint [nontrivial k] {p : ι → P} (ha : affine_independent k p) {s1 s2 : set ι} (hd : s1 ∩ s2 = ∅) : (affine_span k (p '' s1) : set P) ∩ affine_span k (p '' s2) = ∅ := begin by_contradiction hne, change (affine_span k (p '' s1) : set P) ∩ affine_span k (p '' s2) ≠ ∅ at hne, rw set.ne_empty_iff_nonempty at hne, rcases hne with ⟨p0, hp0s1, hp0s2⟩, cases ha.exists_mem_inter_of_exists_mem_inter_affine_span hp0s1 hp0s2 with i hi, exact set.not_mem_empty i (hd ▸ hi) end /-- If a family is affinely independent, a point in the family is in the span of some of the points given by a subset of the index type if and only if that point's index is in the subset, if the underlying ring is nontrivial. -/ @[simp] protected lemma affine_independent.mem_affine_span_iff [nontrivial k] {p : ι → P} (ha : affine_independent k p) (i : ι) (s : set ι) : p i ∈ affine_span k (p '' s) ↔ i ∈ s := begin split, { intro hs, have h := affine_independent.exists_mem_inter_of_exists_mem_inter_affine_span ha hs (mem_affine_span k (set.mem_image_of_mem _ (set.mem_singleton _))), rwa [←set.nonempty_def, set.inter_singleton_nonempty] at h }, { exact λ h, mem_affine_span k (set.mem_image_of_mem p h) } end /-- If a family is affinely independent, a point in the family is not in the affine span of the other points, if the underlying ring is nontrivial. -/ lemma affine_independent.not_mem_affine_span_diff [nontrivial k] {p : ι → P} (ha : affine_independent k p) (i : ι) (s : set ι) : p i ∉ affine_span k (p '' (s \ {i})) := by simp [ha] lemma exists_nontrivial_relation_sum_zero_of_not_affine_ind {t : finset V} (h : ¬ affine_independent k (coe : t → V)) : ∃ f : V → k, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin classical, rw affine_independent_iff_of_fintype at h, simp only [exists_prop, not_forall] at h, obtain ⟨w, hw, hwt, i, hi⟩ := h, simp only [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero _ w (coe : t → V) hw 0, vsub_eq_sub, finset.weighted_vsub_of_point_apply, sub_zero] at hwt, let f : Π (x : V), x ∈ t → k := λ x hx, w ⟨x, hx⟩, refine ⟨λ x, if hx : x ∈ t then f x hx else (0 : k), _, _, by { use i, simp [hi, f], }⟩, suffices : ∑ (e : V) in t, dite (e ∈ t) (λ hx, (f e hx) • e) (λ hx, 0) = 0, { convert this, ext, by_cases hx : x ∈ t; simp [hx], }, all_goals { simp only [finset.sum_dite_of_true (λx h, h), subtype.val_eq_coe, finset.mk_coe, f, hwt, hw], }, end end affine_independent 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 /-- An affinely independent set of points can be extended to such a set that spans the whole space. -/ lemma exists_subset_affine_independent_affine_span_eq_top {s : set P} (h : affine_independent k (λ p, p : s → P)) : ∃ t : set P, s ⊆ t ∧ affine_independent k (λ p, p : t → P) ∧ affine_span k t = ⊤ := begin rcases s.eq_empty_or_nonempty with rfl | ⟨p₁, hp₁⟩, { have p₁ : P := add_torsor.nonempty.some, let hsv := basis.of_vector_space k V, have hsvi := hsv.linear_independent, have hsvt := hsv.span_eq, rw basis.coe_of_vector_space at hsvi hsvt, have h0 : ∀ v : V, v ∈ (basis.of_vector_space_index _ _) → v ≠ 0, { intros v hv, simpa using hsv.ne_zero ⟨v, hv⟩ }, rw linear_independent_set_iff_affine_independent_vadd_union_singleton k h0 p₁ at hsvi, exact ⟨{p₁} ∪ (λ v, v +ᵥ p₁) '' _, set.empty_subset _, hsvi, affine_span_singleton_union_vadd_eq_top_of_span_eq_top p₁ hsvt⟩ }, { rw affine_independent_set_iff_linear_independent_vsub k hp₁ at h, let bsv := basis.extend h, have hsvi := bsv.linear_independent, have hsvt := bsv.span_eq, rw basis.coe_extend at hsvi hsvt, have hsv := h.subset_extend (set.subset_univ _), have h0 : ∀ v : V, v ∈ (h.extend _) → v ≠ 0, { intros v hv, simpa using bsv.ne_zero ⟨v, hv⟩ }, rw linear_independent_set_iff_affine_independent_vadd_union_singleton k h0 p₁ at hsvi, refine ⟨{p₁} ∪ (λ v, v +ᵥ p₁) '' h.extend (set.subset_univ _), _, _⟩, { refine set.subset.trans _ (set.union_subset_union_right _ (set.image_subset _ hsv)), simp [set.image_image] }, { use [hsvi, affine_span_singleton_union_vadd_eq_top_of_span_eq_top p₁ hsvt] } } end variables (k V) lemma exists_affine_independent (s : set P) : ∃ t ⊆ s, affine_span k t = affine_span k s ∧ affine_independent k (coe : t → P) := begin rcases s.eq_empty_or_nonempty with rfl | ⟨p, hp⟩, { exact ⟨∅, set.empty_subset ∅, rfl, affine_independent_of_subsingleton k _⟩, }, obtain ⟨b, hb₁, hb₂, hb₃⟩ := exists_linear_independent k ((equiv.vadd_const p).symm '' s), have hb₀ : ∀ (v : V), v ∈ b → v ≠ 0, { exact λ v hv, hb₃.ne_zero (⟨v, hv⟩ : b), }, rw linear_independent_set_iff_affine_independent_vadd_union_singleton k hb₀ p at hb₃, refine ⟨{p} ∪ (equiv.vadd_const p) '' b, _, _, hb₃⟩, { apply set.union_subset (set.singleton_subset_iff.mpr hp), rwa ← (equiv.vadd_const p).subset_image' b s, }, { rw [equiv.coe_vadd_const_symm, ← vector_span_eq_span_vsub_set_right k hp] at hb₂, apply affine_subspace.ext_of_direction_eq, { have : submodule.span k b = submodule.span k (insert 0 b), { by simp, }, simp only [direction_affine_span, ← hb₂, equiv.coe_vadd_const, set.singleton_union, vector_span_eq_span_vsub_set_right k (set.mem_insert p _), this], congr, change (equiv.vadd_const p).symm '' insert p ((equiv.vadd_const p) '' b) = _, rw [set.image_insert_eq, ← set.image_comp], simp, }, { use p, simp only [equiv.coe_vadd_const, set.singleton_union, set.mem_inter_eq, coe_affine_span], exact ⟨mem_span_points k _ _ (set.mem_insert p _), mem_span_points k _ _ hp⟩, }, }, end variables (k) {V P} /-- Two different points are affinely independent. -/ lemma affine_independent_of_ne {p₁ p₂ : P} (h : p₁ ≠ p₂) : affine_independent k ![p₁, p₂] := begin rw affine_independent_iff_linear_independent_vsub k ![p₁, p₂] 0, let i₁ : {x // x ≠ (0 : fin 2)} := ⟨1, by norm_num⟩, have he' : ∀ i, i = i₁, { rintro ⟨i, hi⟩, ext, fin_cases i, { simpa using hi } }, haveI : unique {x // x ≠ (0 : fin 2)} := ⟨⟨i₁⟩, he'⟩, have hz : (![p₁, p₂] ↑default -ᵥ ![p₁, p₂] 0 : V) ≠ 0, { rw he' default, simpa using h.symm }, exact linear_independent_unique _ hz end end division_ring namespace affine variables (k : Type*) {V : Type*} (P : Type*) [ring k] [add_comm_group V] [module k V] variables [affine_space V P] include V /-- A `simplex k P n` is a collection of `n + 1` affinely independent points. -/ structure simplex (n : ℕ) := (points : fin (n + 1) → P) (independent : affine_independent k points) /-- A `triangle k P` is a collection of three affinely independent points. -/ abbreviation triangle := simplex k P 2 namespace simplex variables {P} /-- Construct a 0-simplex from a point. -/ def mk_of_point (p : P) : simplex k P 0 := ⟨λ _, p, affine_independent_of_subsingleton k _⟩ /-- The point in a simplex constructed with `mk_of_point`. -/ @[simp] lemma mk_of_point_points (p : P) (i : fin 1) : (mk_of_point k p).points i = p := rfl instance [inhabited P] : inhabited (simplex k P 0) := ⟨mk_of_point k default⟩ instance nonempty : nonempty (simplex k P 0) := ⟨mk_of_point k $ add_torsor.nonempty.some⟩ variables {k V} /-- Two simplices are equal if they have the same points. -/ @[ext] lemma ext {n : ℕ} {s1 s2 : simplex k P n} (h : ∀ i, s1.points i = s2.points i) : s1 = s2 := begin cases s1, cases s2, congr' with i, exact h i end /-- Two simplices are equal if and only if they have the same points. -/ lemma ext_iff {n : ℕ} (s1 s2 : simplex k P n): s1 = s2 ↔ ∀ i, s1.points i = s2.points i := ⟨λ h _, h ▸ rfl, ext⟩ /-- A face of a simplex is a simplex with the given subset of points. -/ def face {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : simplex k P m := ⟨s.points ∘ fs.order_emb_of_fin h, s.independent.comp_embedding (fs.order_emb_of_fin h).to_embedding⟩ /-- The points of a face of a simplex are given by `mono_of_fin`. -/ lemma face_points {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) (i : fin (m + 1)) : (s.face h).points i = s.points (fs.order_emb_of_fin h i) := rfl /-- The points of a face of a simplex are given by `mono_of_fin`. -/ lemma face_points' {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : (s.face h).points = s.points ∘ (fs.order_emb_of_fin h) := rfl /-- A single-point face equals the 0-simplex constructed with `mk_of_point`. -/ @[simp] lemma face_eq_mk_of_point {n : ℕ} (s : simplex k P n) (i : fin (n + 1)) : s.face (finset.card_singleton i) = mk_of_point k (s.points i) := by { ext, simp [face_points] } /-- The set of points of a face. -/ @[simp] lemma range_face_points {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : set.range (s.face h).points = s.points '' ↑fs := by rw [face_points', set.range_comp, finset.range_order_emb_of_fin] end simplex end affine namespace affine namespace simplex variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] [affine_space V P] include V /-- The centroid of a face of a simplex as the centroid of a subset of the points. -/ @[simp] lemma face_centroid_eq_centroid {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : finset.univ.centroid k (s.face h).points = fs.centroid k s.points := begin convert (finset.univ.centroid_map k (fs.order_emb_of_fin h).to_embedding s.points).symm, rw [← finset.coe_inj, finset.coe_map, finset.coe_univ, set.image_univ], simp end /-- Over a characteristic-zero division ring, the centroids given by two subsets of the points of a simplex are equal if and only if those faces are given by the same subset of points. -/ @[simp] lemma centroid_eq_iff [char_zero k] {n : ℕ} (s : simplex k P n) {fs₁ fs₂ : finset (fin (n + 1))} {m₁ m₂ : ℕ} (h₁ : fs₁.card = m₁ + 1) (h₂ : fs₂.card = m₂ + 1) : fs₁.centroid k s.points = fs₂.centroid k s.points ↔ fs₁ = fs₂ := begin split, { intro h, rw [finset.centroid_eq_affine_combination_fintype, finset.centroid_eq_affine_combination_fintype] at h, have ha := (affine_independent_iff_indicator_eq_of_affine_combination_eq k s.points).1 s.independent _ _ _ _ (fs₁.sum_centroid_weights_indicator_eq_one_of_card_eq_add_one k h₁) (fs₂.sum_centroid_weights_indicator_eq_one_of_card_eq_add_one k h₂) h, simp_rw [finset.coe_univ, set.indicator_univ, function.funext_iff, finset.centroid_weights_indicator_def, finset.centroid_weights, h₁, h₂] at ha, ext i, replace ha := ha i, split, all_goals { intro hi, by_contradiction hni, simp [hi, hni] at ha, norm_cast at ha } }, { intro h, have hm : m₁ = m₂, { subst h, simpa [h₁] using h₂ }, subst hm, congr, exact h } end /-- Over a characteristic-zero division ring, the centroids of two faces of a simplex are equal if and only if those faces are given by the same subset of points. -/ lemma face_centroid_eq_iff [char_zero k] {n : ℕ} (s : simplex k P n) {fs₁ fs₂ : finset (fin (n + 1))} {m₁ m₂ : ℕ} (h₁ : fs₁.card = m₁ + 1) (h₂ : fs₂.card = m₂ + 1) : finset.univ.centroid k (s.face h₁).points = finset.univ.centroid k (s.face h₂).points ↔ fs₁ = fs₂ := begin rw [face_centroid_eq_centroid, face_centroid_eq_centroid], exact s.centroid_eq_iff h₁ h₂ end /-- Two simplices with the same points have the same centroid. -/ lemma centroid_eq_of_range_eq {n : ℕ} {s₁ s₂ : simplex k P n} (h : set.range s₁.points = set.range s₂.points) : finset.univ.centroid k s₁.points = finset.univ.centroid k s₂.points := begin rw [←set.image_univ, ←set.image_univ, ←finset.coe_univ] at h, exact finset.univ.centroid_eq_of_inj_on_of_image_eq k _ (λ _ _ _ _ he, affine_independent.injective s₁.independent he) (λ _ _ _ _ he, affine_independent.injective s₂.independent he) h end end simplex end affine
245ee8705e8cb498e99ce9b836ca2765c08b7674
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/algebra/module.lean
59a5037a3068abf0caaa81bf12011d518026cafc
[ "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
30,142
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import group_theory.group_action /-! # Modules over a ring In this file we define * `semimodule R M` : an additive commutative monoid `M` is a `semimodule` over a `semiring` `R` if for `r : R` and `x : M` their "scalar multiplication `r • x : M` is defined, and the operation `•` satisfies some natural associativity and distributivity axioms similar to those on a ring. * `module R M` : same as `semimodule R M` but assumes that `R` is a `ring` and `M` is an additive commutative group. * `vector_space k M` : same as `semimodule k M` and `module k M` but assumes that `k` is a `field` and `M` is an additive commutative group. * `linear_map R M M₂`, `M →ₗ[R] M₂` : a linear map between two R-`semimodule`s. * `is_linear_map R f` : predicate saying that `f : M → M₂` is a linear map. * `submodule R M` : a subset of `M` that contains zero and is closed with respect to addition and scalar multiplication. * `subspace k M` : an abbreviation for `submodule` assuming that `k` is a `field`. ## Implementation notes * `vector_space` and `module` are abbreviations for `semimodule R M`. ## TODO * `submodule R M` was written before bundled `submonoid`s, so it does not extend it. ## Tags semimodule, module, vector space, submodule, subspace, linear map -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type u} {k : Type u'} {S : Type v} {M : Type w} {M₂ : Type x} {M₃ : Type y} {ι : Type z} section prio set_option default_priority 100 -- see Note [default priority] /-- A semimodule is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`, connected by a "scalar multiplication" operation `r • x : M` (where `r : R` and `x : M`) with some natural associativity and distributivity axioms similar to those on a ring. -/ @[protect_proj] class semimodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] extends distrib_mul_action R M := (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (zero_smul : ∀x : M, (0 : R) • x = 0) end prio section add_comm_monoid variables [semiring R] [add_comm_monoid M] [semimodule R M] (r s : R) (x y : M) theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x variables (R) @[simp] theorem zero_smul : (0 : R) • x = 0 := semimodule.zero_smul x theorem two_smul : (2 : R) • x = x + x := by rw [bit0, add_smul, one_smul] /-- Pullback a `semimodule` structure along an injective additive monoid homomorphism. -/ protected def function.injective.semimodule [add_comm_monoid M₂] [has_scalar R M₂] (f : M₂ →+ M) (hf : injective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : semimodule R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, hf $ by simp only [smul, f.map_add, add_smul], zero_smul := λ x, hf $ by simp only [smul, zero_smul, f.map_zero], .. hf.distrib_mul_action f smul } /-- Pushforward a `semimodule` structure along a surjective additive monoid homomorphism. -/ protected def function.surjective.semimodule [add_comm_monoid M₂] [has_scalar R M₂] (f : M →+ M₂) (hf : surjective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : semimodule R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, by { rcases hf x with ⟨x, rfl⟩, simp only [add_smul, ← smul, ← f.map_add] }, zero_smul := λ x, by { rcases hf x with ⟨x, rfl⟩, simp only [← f.map_zero, ← smul, zero_smul] }, .. hf.distrib_mul_action f smul } variable (M) /-- `(•)` as an `add_monoid_hom`. -/ def smul_add_hom : R →+ M →+ M := { to_fun := const_smul_hom M, map_zero' := add_monoid_hom.ext $ λ r, by simp, map_add' := λ x y, add_monoid_hom.ext $ λ r, by simp [add_smul] } variables {R M} @[simp] lemma smul_add_hom_apply (r : R) (x : M) : smul_add_hom R M r x = r • x := rfl lemma semimodule.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by rw [←one_smul R x, ←zero_eq_one, zero_smul] lemma list.sum_smul {l : list R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_list_sum l lemma multiset.sum_smul {l : multiset R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_multiset_sum l lemma finset.sum_smul {f : ι → R} {s : finset ι} {x : M} : (∑ i in s, f i) • x = (∑ i in s, (f i) • x) := ((smul_add_hom R M).flip x).map_sum f s end add_comm_monoid section add_comm_group variables (R M) [semiring R] [add_comm_group M] /-- A structure containing most informations as in a semimodule, except the fields `zero_smul` and `smul_zero`. As these fields can be deduced from the other ones when `M` is an `add_comm_group`, this provides a way to construct a semimodule structure by checking less properties, in `semimodule.of_core`. -/ @[nolint has_inhabited_instance] structure semimodule.core extends has_scalar R M := (smul_add : ∀(r : R) (x y : M), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : R) (x : M), (r * s) • x = r • s • x) (one_smul : ∀x : M, (1 : R) • x = x) variables {R M} /-- Define `semimodule` without proving `zero_smul` and `smul_zero` by using an auxiliary structure `semimodule.core`, when the underlying space is an `add_comm_group`. -/ def semimodule.of_core (H : semimodule.core R M) : semimodule R M := by letI := H.to_has_scalar; exact { zero_smul := λ x, (add_monoid_hom.mk' (λ r : R, r • x) (λ r s, H.add_smul r s x)).map_zero, smul_zero := λ r, (add_monoid_hom.mk' ((•) r) (H.smul_add r)).map_zero, ..H } variable [semimodule R M] @[simp] theorem smul_neg (r : R) (x : M) : r • (-x) = -(r • x) := eq_neg_of_add_eq_zero (by simp [← smul_add]) theorem smul_sub (r : R) (x y : M) : r • (x - y) = r • x - r • y := by simp [smul_add, sub_eq_add_neg]; rw smul_neg end add_comm_group /-- Modules are defined as an `abbreviation` for semimodules, if the base semiring is a ring. (A previous definition made `module` a structure defined to be `semimodule`.) This has as advantage that modules are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for modules as well. A cosmetic disadvantage is that one can not extend modules as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "module definition" /-- A module is the same as a semimodule, except the scalar semiring is actually a ring. This is the traditional generalization of spaces like `ℤ^n`, which have a natural addition operation and a way to multiply them by elements of a ring, but no multiplication operation between vectors. -/ abbreviation module (R : Type u) (M : Type v) [ring R] [add_comm_group M] := semimodule R M /-- To prove two module structures on a fixed `add_comm_group` agree, it suffices to check the scalar multiplications agree. -/ -- We'll later use this to show `module ℤ M` is a subsingleton. @[ext] lemma module_ext {R : Type*} [ring R] {M : Type*} [add_comm_group M] (P Q : module R M) (w : ∀ (r : R) (m : M), by { haveI := P, exact r • m } = by { haveI := Q, exact r • m }) : P = Q := begin unfreezingI { rcases P with ⟨⟨⟨⟨P⟩⟩⟩⟩, rcases Q with ⟨⟨⟨⟨Q⟩⟩⟩⟩ }, congr, funext r m, exact w r m, all_goals { apply proof_irrel_heq }, end section module variables [ring R] [add_comm_group M] [module R M] (r s : R) (x y : M) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) variables (R) theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp variables {R} theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by simp [add_smul, sub_eq_add_neg] theorem smul_eq_zero {R E : Type*} [division_ring R] [add_comm_group E] [module R E] {c : R} {x : E} : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨λ h, classical.or_iff_not_imp_left.2 $ λ hc, (units.mk0 c hc).smul_eq_zero.1 h, λ h, h.elim (λ hc, hc.symm ▸ zero_smul R x) (λ hx, hx.symm ▸ smul_zero c)⟩ end module section set_option default_priority 910 instance semiring.to_semimodule [semiring R] : semimodule R R := { smul := (*), smul_add := mul_add, add_smul := add_mul, mul_smul := mul_assoc, one_smul := one_mul, zero_smul := zero_mul, smul_zero := mul_zero } end @[simp] lemma smul_eq_mul [semiring R] {a a' : R} : a • a' = a * a' := rfl /-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`. -/ def ring_hom.to_semimodule [semiring R] [semiring S] (f : R →+* S) : semimodule R S := { smul := λ r x, f r * x, smul_add := λ r x y, by unfold has_scalar.smul; rw [mul_add], add_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_add, add_mul], mul_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_mul, mul_assoc], one_smul := λ x, show f 1 * x = _, by rw [f.map_one, one_mul], zero_smul := λ x, show f 0 * x = 0, by rw [f.map_zero, zero_mul], smul_zero := λ r, mul_zero (f r) } /-- A map `f` between semimodules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. The predicate `is_linear_map R f` asserts this property. A bundled version is available with `linear_map`, and should be favored over `is_linear_map` most of the time. -/ structure is_linear_map (R : Type u) {M : Type v} {M₂ : Type w} [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M → M₂) : Prop := (map_add : ∀ x y, f (x + y) = f x + f y) (map_smul : ∀ (c : R) x, f (c • x) = c • f x) /-- A map `f` between semimodules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. Elements of `linear_map R M M₂` (available under the notation `M →ₗ[R] M₂`) are bundled versions of such maps. An unbundled version is available with the predicate `is_linear_map`, but it should be avoided most of the time. -/ structure linear_map (R : Type u) (M : Type v) (M₂ : Type w) [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] := (to_fun : M → M₂) (map_add' : ∀x y, to_fun (x + y) = to_fun x + to_fun y) (map_smul' : ∀(c : R) x, to_fun (c • x) = c • to_fun x) infixr ` →ₗ `:25 := linear_map _ notation M ` →ₗ[`:25 R:25 `] `:0 M₂:0 := linear_map R M M₂ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] section variables [semimodule R M] [semimodule R M₂] instance : has_coe_to_fun (M →ₗ[R] M₂) := ⟨_, to_fun⟩ @[simp] lemma coe_mk (f : M → M₂) (h₁ h₂) : ((linear_map.mk f h₁ h₂ : M →ₗ[R] M₂) : M → M₂) = f := rfl /-- Identity map as a `linear_map` -/ def id : M →ₗ[R] M := ⟨id, λ _ _, rfl, λ _ _, rfl⟩ @[simp] lemma id_apply (x : M) : @id R M _ _ _ x = x := rfl end section -- We can infer the module structure implicitly from the linear maps, -- rather than via typeclass resolution. variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f g : M →ₗ[R] M₂) @[simp] lemma to_fun_eq_coe : f.to_fun = ⇑f := rfl theorem is_linear : is_linear_map R f := ⟨f.2, f.3⟩ variables {f g} @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H lemma coe_fn_congr : Π {x x' : M}, x = x' → f x = f x' | _ _ rfl := rfl theorem ext_iff : f = g ↔ ∀ x, f x = g x := ⟨by { rintro rfl x, refl } , ext⟩ variables (f g) @[simp] lemma map_add (x y : M) : f (x + y) = f x + f y := f.map_add' x y @[simp] lemma map_smul (c : R) (x : M) : f (c • x) = c • f x := f.map_smul' c x @[simp] lemma map_zero : f 0 = 0 := by rw [← zero_smul R, map_smul f 0 0, zero_smul] instance : is_add_monoid_hom f := { map_add := map_add f, map_zero := map_zero f } /-- convert a linear map to an additive map -/ def to_add_monoid_hom : M →+ M₂ := { to_fun := f, map_zero' := f.map_zero, map_add' := f.map_add } @[simp] lemma to_add_monoid_hom_coe : ((f.to_add_monoid_hom) : M → M₂) = f := rfl @[simp] lemma map_sum {ι} {t : finset ι} {g : ι → M} : f (∑ i in t, g i) = (∑ i in t, f (g i)) := f.to_add_monoid_hom.map_sum _ _ end section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} {semimodule_M₃ : semimodule R M₃} variables (f : M₂ →ₗ[R] M₃) (g : M →ₗ[R] M₂) /-- Composition of two linear maps is a linear map -/ def comp : M →ₗ[R] M₃ := ⟨f ∘ g, by simp, by simp⟩ @[simp] lemma comp_apply (x : M) : f.comp g x = f (g x) := rfl end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) @[simp] lemma map_neg (x : M) : f (- x) = - f x := f.to_add_monoid_hom.map_neg x @[simp] lemma map_sub (x y : M) : f (x - y) = f x - f y := f.to_add_monoid_hom.map_sub x y instance : is_add_group_hom f := { map_add := map_add f} end add_comm_group end linear_map namespace is_linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] variables [semimodule R M] [semimodule R M₂] include R /-- Convert an `is_linear_map` predicate to a `linear_map` -/ def mk' (f : M → M₂) (H : is_linear_map R f) : M →ₗ M₂ := ⟨f, H.1, H.2⟩ @[simp] theorem mk'_apply {f : M → M₂} (H : is_linear_map R f) (x : M) : mk' f H x = f x := rfl lemma is_linear_map_smul {R M : Type*} [comm_semiring R] [add_comm_monoid M] [semimodule R M] (c : R) : is_linear_map R (λ (z : M), c • z) := begin refine is_linear_map.mk (smul_add c) _, intros _ _, simp only [smul_smul, mul_comm] end --TODO: move lemma is_linear_map_smul' {R M : Type*} [semiring R] [add_comm_monoid M] [semimodule R M] (a : M) : is_linear_map R (λ (c : R), c • a) := is_linear_map.mk (λ x y, add_smul x y a) (λ x y, mul_smul x y a) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_zero : f (0 : M) = (0 : M₂) := (lin.mk' f).map_zero end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] variables [semimodule R M] [semimodule R M₂] include R lemma is_linear_map_neg : is_linear_map R (λ (z : M), -z) := is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_neg (x : M) : f (- x) = - f x := (lin.mk' f).map_neg x lemma map_sub (x y) : f (x - y) = f x - f y := (lin.mk' f).map_sub x y end add_comm_group end is_linear_map /-- Ring of linear endomorphismsms of a module. -/ abbreviation module.End (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [semimodule R M] := M →ₗ[R] M set_option old_structure_cmd true /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [semimodule R M] extends add_submonoid M : Type v := (smul_mem' : ∀ (c:R) {x}, x ∈ carrier → c • x ∈ carrier) /-- Reinterpret a `submodule` as an `add_submonoid`. -/ add_decl_doc submodule.to_add_submonoid namespace submodule variables [semiring R] [add_comm_monoid M] [semimodule R M] instance : has_coe_t (submodule R M) (set M) := ⟨λ s, s.carrier⟩ instance : has_mem M (submodule R M) := ⟨λ x p, x ∈ (p : set M)⟩ instance : has_coe_to_sort (submodule R M) := ⟨_, λ p, {x : M // x ∈ p}⟩ variables (p q : submodule R M) @[simp, norm_cast] theorem coe_sort_coe : ↥(p : set M) = p := rfl variables {p q} protected theorem «exists» {q : p → Prop} : (∃ x, q x) ↔ (∃ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.exists protected theorem «forall» {q : p → Prop} : (∀ x, q x) ↔ (∀ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.forall theorem coe_injective : injective (coe : submodule R M → set M) := λ p q h, by cases p; cases q; congr' @[simp, norm_cast] theorem coe_set_eq : (p : set M) = q ↔ p = q := coe_injective.eq_iff theorem ext'_iff : p = q ↔ (p : set M) = q := coe_set_eq.symm @[ext] theorem ext (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := coe_injective $ set.ext h theorem to_add_submonoid_injective : injective (to_add_submonoid : submodule R M → add_submonoid M) := λ p q h, ext'_iff.2 $ add_submonoid.ext'_iff.1 h @[simp] theorem to_add_submonoid_eq : p.to_add_submonoid = q.to_add_submonoid ↔ p = q := to_add_submonoid_injective.eq_iff end submodule namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] -- We can infer the module structure implicitly from the bundled submodule, -- rather than via typeclass resolution. variables {semimodule_M : semimodule R M} variables {p q : submodule R M} variables {r : R} {x y : M} variables (p) @[simp] theorem mem_coe : x ∈ (p : set M) ↔ x ∈ p := iff.rfl @[simp] lemma zero_mem : (0 : M) ∈ p := p.zero_mem' lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := p.add_mem' h₁ h₂ lemma smul_mem (r : R) (h : x ∈ p) : r • x ∈ p := p.smul_mem' r h lemma sum_mem {t : finset ι} {f : ι → M} : (∀c∈t, f c ∈ p) → (∑ i in t, f i) ∈ p := p.to_add_submonoid.sum_mem @[simp] lemma smul_mem_iff' (u : units R) : (u:R) • x ∈ p ↔ x ∈ p := ⟨λ h, by simpa only [smul_smul, u.inv_mul, one_smul] using p.smul_mem ↑u⁻¹ h, p.smul_mem u⟩ instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem _ x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : inhabited p := ⟨0⟩ instance : has_scalar R p := ⟨λ c x, ⟨c • x.1, smul_mem _ c x.2⟩⟩ @[simp] lemma mk_eq_zero {x} (h : x ∈ p) : (⟨x, h⟩ : p) = 0 ↔ x = 0 := subtype.ext_iff_val variables {p} @[simp, norm_cast] lemma coe_eq_coe {x y : p} : (x : M) = y ↔ x = y := subtype.ext_iff_val.symm @[simp, norm_cast] lemma coe_eq_zero {x : p} : (x : M) = 0 ↔ x = 0 := @coe_eq_coe _ _ _ _ _ _ x 0 @[simp, norm_cast] lemma coe_add (x y : p) : (↑(x + y) : M) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : p) : M) = 0 := rfl @[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 @[simp] lemma coe_mem (x : p) : (x : M) ∈ p := x.2 @[simp] protected lemma eta (x : p) (hx : (x : M) ∈ p) : (⟨x, hx⟩ : p) = x := subtype.eta x hx variables (p) instance : add_comm_monoid p := { add := (+), zero := 0, .. p.to_add_submonoid.to_add_comm_monoid } instance : semimodule R p := by refine {smul := (•), ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } /-- 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 : ((submodule.subtype p) : p → M) = subtype.val := rfl end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] variables {semimodule_M : semimodule R M} variables (p p' : submodule 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 /-- Reinterpret a submodule as an additive subgroup. -/ def to_add_subgroup : add_subgroup M := { neg_mem' := λ _, p.neg_mem , .. p.to_add_submonoid } @[simp] lemma coe_to_add_subgroup : (p.to_add_subgroup : set M) = p := rfl lemma sub_mem (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := p.add_mem hx (p.neg_mem hy) @[simp] lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := p.to_add_subgroup.neg_mem_iff lemma add_mem_iff_left (h₁ : y ∈ p) : x + y ∈ p ↔ x ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, λ h₂, add_mem _ h₂ h₁⟩ lemma add_mem_iff_right (h₁ : x ∈ p) : x + y ∈ p ↔ y ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, add_mem _ h₁⟩ instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩ @[simp, norm_cast] lemma coe_neg (x : p) : ((-x : p) : M) = -x := rfl instance : add_comm_group p := { add := (+), zero := 0, neg := has_neg.neg, ..p.to_add_subgroup.to_add_comm_group } @[simp, norm_cast] lemma coe_sub (x y : p) : (↑(x - y) : M) = ↑x - ↑y := rfl end add_comm_group end submodule -- TODO: Do we want one-sided ideals? /-- Ideal in a commutative ring is an additive subgroup `s` such that `a * b ∈ s` whenever `b ∈ s`. We define `ideal R` as `submodule R R`. -/ @[reducible] def ideal (R : Type u) [comm_ring R] := submodule R R namespace ideal variables [comm_ring R] (I : ideal R) {a b : R} protected lemma zero_mem : (0 : R) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _ lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h end ideal /-- Vector spaces are defined as an `abbreviation` for semimodules, if the base ring is a field. (A previous definition made `vector_space` a structure defined to be `module`.) This has as advantage that vector spaces are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for vector spaces as well. A cosmetic disadvantage is that one can not extend vector spaces as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "vector space definition" /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ abbreviation vector_space (R : Type u) (M : Type v) [field R] [add_comm_group M] := semimodule R M /-- Subspace of a vector space. Defined to equal `submodule`. -/ abbreviation subspace (R : Type u) (M : Type v) [field R] [add_comm_group M] [vector_space R M] := submodule R M namespace submodule variables [division_ring R] [add_comm_group M] [module R M] variables (p : submodule R M) {r : R} {x y : M} theorem smul_mem_iff (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p := p.smul_mem_iff' (units.mk0 r r0) end submodule namespace add_comm_monoid open add_monoid variables [add_comm_monoid M] /-- The natural ℕ-semimodule structure on any `add_comm_monoid`. -/ -- We don't make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℕ`. def nat_semimodule : semimodule ℕ M := { smul := nsmul, smul_add := λ _ _ _, nsmul_add _ _ _, add_smul := λ _ _ _, add_nsmul _ _ _, mul_smul := λ _ _ _, mul_nsmul _ _ _, one_smul := one_nsmul, zero_smul := zero_nsmul, smul_zero := nsmul_zero } end add_comm_monoid namespace add_comm_group variables [add_comm_group M] /-- The natural ℤ-module structure on any `add_comm_group`. -/ -- We don't immediately make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℤ`. -- We do turn it into a global instance, but only at the end of this file, -- and I remain dubious whether this is a good idea. def int_module : module ℤ M := { smul := gsmul, smul_add := λ _ _ _, gsmul_add _ _ _, add_smul := λ _ _ _, add_gsmul _ _ _, mul_smul := λ _ _ _, gsmul_mul _ _ _, one_smul := one_gsmul, zero_smul := zero_gsmul, smul_zero := gsmul_zero } instance : subsingleton (module ℤ M) := begin split, intros P Q, ext, -- isn't that lovely: `r • m = r • m` have one_smul : by { haveI := P, exact (1 : ℤ) • m } = by { haveI := Q, exact (1 : ℤ) • m }, begin rw [@one_smul ℤ _ _ (by { haveI := P, apply_instance, }) m], rw [@one_smul ℤ _ _ (by { haveI := Q, apply_instance, }) m], end, have nat_smul : ∀ n : ℕ, by { haveI := P, exact (n : ℤ) • m } = by { haveI := Q, exact (n : ℤ) • m }, begin intro n, induction n with n ih, { erw [zero_smul, zero_smul], }, { rw [int.coe_nat_succ, add_smul, add_smul], erw ih, rw [one_smul], } end, cases r, { rw [int.of_nat_eq_coe, nat_smul], }, { rw [int.neg_succ_of_nat_coe, neg_smul, neg_smul, nat_smul], } end end add_comm_group section local attribute [instance] add_comm_monoid.nat_semimodule lemma semimodule.smul_eq_smul (R : Type*) [semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M] (n : ℕ) (b : M) : n • b = (n : R) • b := begin induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { change (n + 1) • b = (n + 1 : R) • b, rw [add_smul, add_smul, one_smul, ih, one_smul] } end lemma semimodule.nsmul_eq_smul (R : Type*) [semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M] (n : ℕ) (b : M) : n •ℕ b = (n : R) • b := semimodule.smul_eq_smul R n b lemma nat.smul_def {M : Type*} [add_comm_monoid M] (n : ℕ) (x : M) : n • x = n •ℕ x := rfl end section local attribute [instance] add_comm_group.int_module lemma gsmul_eq_smul {M : Type*} [add_comm_group M] (n : ℤ) (x : M) : gsmul n x = n • x := rfl lemma module.gsmul_eq_smul_cast (R : Type*) [ring R] {M : Type*} [add_comm_group M] [module R M] (n : ℤ) (b : M) : gsmul n b = (n : R) • b := begin cases n, { apply semimodule.nsmul_eq_smul, }, { dsimp, rw semimodule.nsmul_eq_smul R, push_cast, rw neg_smul, } end lemma module.gsmul_eq_smul {M : Type*} [add_comm_group M] [module ℤ M] (n : ℤ) (b : M) : gsmul n b = n • b := by rw [module.gsmul_eq_smul_cast ℤ, int.cast_id] end -- We prove this without using the `add_comm_group.int_module` instance, so the `•`s here -- come from whatever the local `module ℤ` structure actually is. lemma add_monoid_hom.map_int_module_smul [add_comm_group M] [add_comm_group M₂] [module ℤ M] [module ℤ M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f (x • a) = x • f a := by simp only [← module.gsmul_eq_smul, f.map_gsmul] lemma add_monoid_hom.map_int_cast_smul [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [← module.gsmul_eq_smul_cast, f.map_gsmul] lemma add_monoid_hom.map_nat_cast_smul [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →+ M₂) (x : ℕ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [← semimodule.nsmul_eq_smul, f.map_nsmul] lemma add_monoid_hom.map_rat_cast_smul {R : Type*} [division_ring R] [char_zero R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] (f : E →+ F) (c : ℚ) (x : E) : f ((c : R) • x) = (c : R) • f x := begin have : ∀ (x : E) (n : ℕ), 0 < n → f (((n⁻¹ : ℚ) : R) • x) = ((n⁻¹ : ℚ) : R) • f x, { intros x n hn, replace hn : (n : R) ≠ 0 := nat.cast_ne_zero.2 (ne_of_gt hn), conv_rhs { congr, skip, rw [← one_smul R x, ← mul_inv_cancel hn, mul_smul] }, rw [f.map_nat_cast_smul, smul_smul, rat.cast_inv, rat.cast_coe_nat, inv_mul_cancel hn, one_smul] }, refine c.num_denom_cases_on (λ m n hn hmn, _), rw [rat.mk_eq_div, div_eq_mul_inv, rat.cast_mul, int.cast_coe_nat, mul_smul, mul_smul, rat.cast_coe_int, f.map_int_cast_smul, this _ n hn] end lemma add_monoid_hom.map_rat_module_smul {E : Type*} [add_comm_group E] [vector_space ℚ E] {F : Type*} [add_comm_group F] [module ℚ F] (f : E →+ F) (c : ℚ) (x : E) : f (c • x) = c • f x := rat.cast_id c ▸ f.map_rat_cast_smul c x -- We finally turn on these instances globally: attribute [instance] add_comm_monoid.nat_semimodule add_comm_group.int_module /-- Reinterpret an additive homomorphism as a `ℤ`-linear map. -/ def add_monoid_hom.to_int_linear_map [add_comm_group M] [add_comm_group M₂] (f : M →+ M₂) : M →ₗ[ℤ] M₂ := ⟨f, f.map_add, f.map_int_module_smul⟩ /-- Reinterpret an additive homomorphism as a `ℚ`-linear map. -/ def add_monoid_hom.to_rat_linear_map [add_comm_group M] [vector_space ℚ M] [add_comm_group M₂] [vector_space ℚ M₂] (f : M →+ M₂) : M →ₗ[ℚ] M₂ := ⟨f, f.map_add, f.map_rat_module_smul⟩ namespace finset variable (R) lemma sum_const' [semiring R] [add_comm_monoid M] [semimodule R M] {s : finset ι} (b : M) : (∑ i in s, b) = (finset.card s : R) • b := by rw [finset.sum_const, ← semimodule.smul_eq_smul]; refl variables {R} [decidable_linear_ordered_cancel_add_comm_monoid M] {s : finset ι} (f : ι → M) theorem exists_card_smul_le_sum (hs : s.nonempty) : ∃ i ∈ s, s.card • f i ≤ (∑ i in s, f i) := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] theorem exists_card_smul_ge_sum (hs : s.nonempty) : ∃ i ∈ s, (∑ i in s, f i) ≤ s.card • f i := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] end finset
18895338d734a51cfccff5b76c3e17a8683408a2
42c01158c2730cc6ac3e058c1339c18cb90366e2
/group_projects/lagrange.lean
2a2d78b23714bcdf73248e3d36e74e4720c6e0c7
[]
no_license
ChrisHughes24/xena
c80d94355d0c2ae8deddda9d01e6d31bc21c30ae
337a0d7c9f0e255e08d6d0a383e303c080c6ec0c
refs/heads/master
1,631,059,898,392
1,511,200,551,000
1,511,200,551,000
111,468,589
1
0
null
null
null
null
UTF-8
Lean
false
false
3,844
lean
import tactic.norm_num -- if that line above doesn't work then you don't have mathlib -- so just comment it out and maybe some stuff won't work. -- If you think you do have mathlib, then try upgrading -- Lean (to nightly) and then try upgrading mathlib. -- If you have Lean running and want to get mathlib working -- then you could ask how to do it in the comments of this -- page on wordpress.com. When Kevin has the time he will -- try and help, and perhaps others will help sooner. -- all that should be on blog ^^^ -- Ignore these lines if you son't understand them -- -- they're just to get things up and running namespace xena universe u variables {α β γ : Type u} variables a1 a2 a : α variable b : β variables X Y : Type u -- a set variables m n : ℕ -- This is where the maths starts definition is_injective (f : α → β) : Prop := ∀ {a1 a2 : α}, f a1 = f a2 → a1 = a2 #print is_injective definition is_surjective (f : α → β) : Prop := ∀ b, ∃ a, f a = b definition is_bijective (f : α → β) : Prop := is_injective f ∧ is_surjective f definition bijects_with (X) (Y) : Prop := ∃ f : X → Y, is_bijective f -- "fin n" means the finite set {0,1,...,n-1} of size n definition has_size (X) (n) : Prop := bijects_with (fin n) X -- if Kenny cares about constructive maths he can -- prove that this statement is decidable -- and give it a decidable instance. -- set_option pp.implicit true theorem inv_of_bij (f : α → β) : is_bijective f → exists g : β → α, is_bijective g := begin intros H_f_bijective, have H_f_surjective := H_f_bijective.right, have H_f_injective := H_f_bijective.left, have surj_proof := λ b, classical.some_spec (H_f_surjective b), let g := λ b, classical.some (H_f_surjective b), have H_right_inverse : ∀ b : β, f (g b) = b, intro b, exact surj_proof b, clear surj_proof, existsi g, split, -- injectivity intros b1 b2 H_g_b1_eq_g_b2, rw [←H_right_inverse b1,H_g_b1_eq_g_b2,H_right_inverse b2], -- surjectivity intro a, existsi (f a), apply H_f_injective, apply H_right_inverse, end -- Example of usage: theorem inj_of_inj_inj {f : α → β} {g : β → γ} : is_injective f → is_injective g → is_injective (g ∘ f) := begin assume H_f_inj : is_injective f, assume H_g_inj : is_injective g, intros a1 a2, assume H1 : g (f a1) = g (f a2), have H2 : f a1 = f a2, from H_g_inj H1, exact H_f_inj H2, end theorem surj_of_surj_surj {f : α → β} {g : β → γ} : is_surjective f → is_surjective g → is_surjective (g ∘ f) := begin intros Hf Hg c, have : ∃ b, g b = c, by apply Hg, cases this with b Hb, have : ∃ a, f a = b, by apply Hf, cases this with a Ha, existsi a, simp [function.comp,Ha,Hb] end theorem bij_of_bij_bij {f : α → β} {g : β → γ} : is_bijective f → is_bijective g → is_bijective (g ∘ f) := -- λ ⟨Hfi,Hfs⟩ ⟨Hgi,Hgs⟩, ⟨inj_of_inj_inj Hfi Hgi,surj_of_surj_surj⟩ begin intro Hf, cases Hf with Hinjf Hsurf, intro Hg, cases Hg with Hinjg Hsurg, have Hinjgf : is_injective (g ∘ f), apply inj_of_inj_inj;assumption, have Hsurgf : is_surjective (g ∘ f), apply surj_of_surj_surj;assumption, split;assumption, -- ask Mario why I couldn't use functions end theorem only_one_size' (X) {m n} : has_size X m ∧ has_size X n → m = n := begin assume X_size_m_and_n, have X_size_m, from X_size_m_and_n.left, have X_size_n, from X_size_m_and_n.right, end definition subset (s : α → Prop) := { a : α // s a } definition complement (s : α → Prop) := λ a, ¬ (s a) example (s : α → Prop) (m n : ℕ) : has_size (subset s) m ∧ has_size (subset (complement s)) n → has_size α (m+n) := begin assume H : has_size (subset s) m ∧ has_size (subset (complement s)) n, cases H with H_s_m H_nots_n, --: has_size (subset s) m) admit end end xena
4731661a5a603f30c6c45326d41db1bcfdb51bad
a047a4718edfa935d17231e9e6ecec8c7b701e05
/src/group_theory/order_of_element.lean
cab0c91ce158d26679e9d17d62fefd594be6a94d
[ "Apache-2.0" ]
permissive
utensil-contrib/mathlib
bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767
b91909e77e219098a2f8cc031f89d595fe274bd2
refs/heads/master
1,668,048,976,965
1,592,442,701,000
1,592,442,701,000
273,197,855
0
0
null
1,592,472,812,000
1,592,472,811,000
null
UTF-8
Lean
false
false
24,271
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 group_theory.coset import data.nat.totient import data.set.finite open function open_locale big_operators variables {α : Type*} {s : set α} {a a₁ a₂ b c: α} -- TODO this lemma isn't used anywhere in this file, and should be moved elsewhere. namespace finset open finset lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) end finset lemma conj_inj [group α] {x : α} : function.injective (λ (g : α), x * g * x⁻¹) := λ a b h, by simpa [mul_left_inj, mul_right_inj] using h lemma mem_normalizer_fintype [group α] {s : set α} [fintype s] {x : α} (h : ∀ n, n ∈ s → x * n * x⁻¹ ∈ s) : x ∈ is_subgroup.normalizer s := by haveI := classical.prop_decidable; haveI := set.fintype_image s (λ n, x * n * x⁻¹); exact λ n, ⟨h n, λ h₁, have heq : (λ n, x * n * x⁻¹) '' s = s := set.eq_of_subset_of_card_le (λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective s conj_inj), have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' s := heq.symm ▸ h₁, let ⟨y, hy⟩ := this in conj_inj hy.2 ▸ hy.1⟩ section order_of variable [group α] open quotient_group set @[simp] lemma card_trivial [fintype (is_subgroup.trivial α)] : fintype.card (is_subgroup.trivial α) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : α), by simp⟩, λ ⟨y, hy⟩, subtype.eq $ is_subgroup.mem_trivial.1 hy⟩ variables [fintype α] [dec : decidable_eq α] instance quotient_group.fintype (s : set α) [is_subgroup s] [d : decidable_pred s] : fintype (quotient s) := @quotient.fintype _ _ (left_rel s) (λ _ _, d _) lemma card_eq_card_quotient_mul_card_subgroup (s : set α) [hs : is_subgroup s] [fintype s] [decidable_pred s] : fintype.card α = fintype.card (quotient s) * fintype.card s := by rw ← fintype.card_prod; exact fintype.card_congr (is_subgroup.group_equiv_quotient_times_subgroup hs) lemma card_subgroup_dvd_card (s : set α) [is_subgroup s] [fintype s] : fintype.card s ∣ fintype.card α := by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s] lemma card_quotient_dvd_card (s : set α) [is_subgroup s] [decidable_pred s] [fintype s] : fintype.card (quotient s) ∣ fintype.card α := by simp [card_eq_card_quotient_mul_card_subgroup s] lemma exists_gpow_eq_one (a : α) : ∃i≠0, a ^ (i:ℤ) = 1 := have ¬ injective (λi:ℤ, a ^ i), from not_injective_infinite_fintype _, let ⟨i, j, a_eq, ne⟩ := show ∃(i j : ℤ), a ^ i = a ^ j ∧ i ≠ j, by rw [injective] at this; simpa [classical.not_forall] in have a ^ (i - j) = 1, by simp [sub_eq_add_neg, gpow_add, gpow_neg, a_eq], ⟨i - j, sub_ne_zero.mpr ne, this⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_pow_eq_one (a : α) : ∃i > 0, a ^ i = 1 := let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in begin cases i, { exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ }, { exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ } end include dec /-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/ def order_of (a : α) : ℕ := nat.find (exists_pow_eq_one a) lemma pow_order_of_eq_one (a : α) : a ^ order_of a = 1 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₂ lemma order_of_pos (a : α) : 0 < order_of a := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₁ private lemma pow_injective_aux {n m : ℕ} (a : α) (h : n ≤ m) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := decidable.by_contradiction $ assume ne : n ≠ m, have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]), have h₂ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq], have le : order_of a ≤ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, h₂⟩, have lt : m - n < order_of a, from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm, lt_irrefl _ (lt_of_le_of_lt le lt) lemma pow_injective_of_lt_order_of {n m : ℕ} (a : α) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := (le_total n m).elim (assume h, pow_injective_aux a h hn hm eq) (assume h, (pow_injective_aux a h hm hn eq.symm).symm) lemma order_of_le_card_univ : order_of a ≤ fintype.card α := finset.card_le_of_inj_on ((^) a) (assume n _, fintype.complete _) (assume i j, pow_injective_of_lt_order_of a) lemma pow_eq_mod_order_of {n : ℕ} : a ^ n = a ^ (n % order_of a) := calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) : by rw [nat.mod_add_div] ... = a ^ (n % order_of a) : by simp [pow_add, pow_mul, pow_order_of_eq_one] lemma gpow_eq_mod_order_of {i : ℤ} : a ^ i = a ^ (i % order_of a) := calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) : by rw [int.mod_add_div] ... = a ^ (i % order_of a) : by simp [gpow_add, gpow_mul, pow_order_of_eq_one] lemma mem_gpowers_iff_mem_range_order_of {a a' : α} : a' ∈ gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : ℕ → α) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) (assume i, gpow_eq_mod_order_of.symm) instance decidable_gpowers : decidable_pred (gpowers a) := assume a', decidable_of_iff' (a' ∈ (finset.range (order_of a)).image ((^) a)) mem_gpowers_iff_mem_range_order_of lemma order_of_dvd_of_pow_eq_one {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n := by_contradiction (λ h₁, nat.find_min _ (show n % order_of a < order_of a, from nat.mod_lt _ (order_of_pos _)) ⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩) lemma order_of_dvd_iff_pow_eq_one {n : ℕ} : order_of a ∣ n ↔ a ^ n = 1 := ⟨λ h, by rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd h, pow_zero], order_of_dvd_of_pow_eq_one⟩ lemma order_of_le_of_pow_eq_one {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n := nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩ lemma sum_card_order_of_eq_card_pow_eq_one {n : ℕ} (hn : 0 < n) : ∑ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (λ a : α, order_of a = m)).card = (finset.univ.filter (λ a : α, a ^ n = 1)).card := calc ∑ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (λ a : α, order_of a = m)).card = _ : (finset.card_bind (by { intros, apply finset.disjoint_filter.2, cc })).symm ... = _ : congr_arg finset.card (finset.ext (begin assume a, suffices : order_of a ≤ n ∧ order_of a ∣ n ↔ a ^ n = 1, { simpa [nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) section local attribute [instance] set_fintype lemma order_eq_card_gpowers : order_of a = fintype.card (gpowers a) := begin refine (finset.card_eq_of_bijective _ _ _ _).symm, { exact λn hn, ⟨gpow a n, ⟨n, rfl⟩⟩ }, { exact assume ⟨_, i, rfl⟩ _, have pos: (0:int) < order_of a, from int.coe_nat_lt.mpr $ order_of_pos a, have 0 ≤ i % (order_of a), from int.mod_nonneg _ $ ne_of_gt pos, ⟨int.to_nat (i % order_of a), by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ }, { intros, exact finset.mem_univ _ }, { exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq } end @[simp] lemma order_of_one : order_of (1 : α) = 1 := by rw [order_eq_card_gpowers, fintype.card_eq_one_iff]; exact ⟨⟨1, 0, rfl⟩, λ ⟨a, i, ha⟩, by simp [ha.symm]⟩ @[simp] lemma order_of_eq_one_iff : order_of a = 1 ↔ a = 1 := ⟨λ h, by conv { to_lhs, rw [← pow_one a, ← h, pow_order_of_eq_one] }, λ h, by simp [h]⟩ lemma order_of_eq_prime {p : ℕ} [hp : fact p.prime] (hg : a^p = 1) (hg1 : a ≠ 1) : order_of a = p := (hp.2 _ (order_of_dvd_of_pow_eq_one hg)).resolve_left (mt order_of_eq_one_iff.1 hg1) section classical open_locale classical open quotient_group /- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/ lemma order_of_dvd_card_univ : order_of a ∣ fintype.card α := have ft_prod : fintype (quotient (gpowers a) × (gpowers a)), from fintype.of_equiv α (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup, have ft_s : fintype (gpowers a), from @fintype.fintype_prod_right _ _ _ ft_prod _, have ft_cosets : fintype (quotient (gpowers a)), from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, is_submonoid.one_mem⟩⟩, have ft : fintype (quotient (gpowers a) × (gpowers a)), from @prod.fintype _ _ ft_cosets ft_s, have eq₁ : fintype.card α = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card α = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eq₂ : order_of a = @fintype.card _ ft_s, from calc order_of a = _ : order_eq_card_gpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, dvd.intro (@fintype.card (quotient (gpowers a)) ft_cosets) $ by rw [eq₁, eq₂, mul_comm] omit dec @[simp] lemma pow_card_eq_one (a : α) : a ^ fintype.card α = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] lemma powers_eq_gpowers (a : α) : powers a = gpowers a := set.ext (λ x, ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, λ ⟨i, hi⟩, ⟨(i % order_of a).nat_abs, by rwa [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of]⟩⟩) end classical open nat lemma order_of_pow (a : α) (n : ℕ) : order_of (a ^ n) = order_of a / gcd (order_of a) n := dvd_antisymm (order_of_dvd_of_pow_eq_one (by rw [← pow_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), pow_mul, pow_order_of_eq_one, _root_.one_pow])) (have gcd_pos : 0 < gcd (order_of a) n, from gcd_pos_of_pos_left n (order_of_pos a), have hdvd : order_of a ∣ n * order_of (a ^ n), from order_of_dvd_of_pow_eq_one (by rw [pow_mul, pow_order_of_eq_one]), coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd gcd_pos) (dvd_of_mul_dvd_mul_right gcd_pos (by rwa [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm]))) lemma image_range_order_of (a : α) : finset.image (λ i, a ^ i) (finset.range (order_of a)) = (gpowers a).to_finset := by { ext x, rw [set.mem_to_finset, mem_gpowers_iff_mem_range_order_of] } omit dec open_locale classical lemma pow_gcd_card_eq_one_iff {n : ℕ} {a : α} : a ^ n = 1 ↔ a ^ (gcd n (fintype.card α)) = 1 := ⟨λ h, have hn : order_of a ∣ n, from dvd_of_mod_eq_zero $ by_contradiction (λ ha, by rw pow_eq_mod_order_of at h; exact (not_le_of_gt (nat.mod_lt n (order_of_pos a))) (order_of_le_of_pow_eq_one (nat.pos_of_ne_zero ha) h)), let ⟨m, hm⟩ := dvd_gcd hn order_of_dvd_card_univ in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card α) in by rw [hm, pow_mul, h, _root_.one_pow]⟩ end end order_of section cyclic local attribute [instance] set_fintype /-- A group is called *cyclic* if it is generated by a single element. -/ class is_cyclic (α : Type*) [group α] : Prop := (exists_generator [] : ∃ g : α, ∀ x, x ∈ gpowers g) /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α := { mul_comm := λ x y, show x * y = y * x, from let ⟨g, hg⟩ := is_cyclic.exists_generator α in let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in hm ▸ hn ▸ gpow_mul_comm _ _ _, ..hg } lemma is_cyclic_of_order_of_eq_card [group α] [fintype α] [decidable_eq α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := ⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le (set.subset_univ _) (by rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers])⟩⟩ lemma order_of_eq_card_of_forall_mem_gpowers [group α] [fintype α] [decidable_eq α] {g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α := by rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers]; simp [hx]; congr instance [group α] : is_cyclic (is_subgroup.trivial α) := ⟨⟨(1 : is_subgroup.trivial α), λ x, ⟨0, subtype.eq $ eq.symm (is_subgroup.mem_trivial.1 x.2)⟩⟩⟩ instance is_subgroup.is_cyclic [group α] [is_cyclic α] (H : set α) [is_subgroup H] : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator α in if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx in let ⟨k, hk⟩ := hg x in have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]), match k, hk with | (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← is_subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex), from ⟨k / nat.find hex, eq.symm $ gpow_mul _ _ _⟩, have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H, by rw gpow_mul; exact is_subgroup.gpow_mem (nat.find_spec hex).2, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (is_subgroup.mul_mem_cancel_right H hk₂).1 $ by rw [← gpow_add, int.mod_add_div, hk]; exact hx, have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← gpow_coe_nat, ← hk₄], have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0, from by_contradiction (λ h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hk₅⟩), ⟨k / (nat.find hex : ℤ), subtype.coe_ext.2 begin suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x, { simpa [gpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = is_subgroup.trivial α, from set.ext $ λ x, ⟨λ h, by simp at *; tauto, λ h, by rw [is_subgroup.mem_trivial.1 h]; exact is_submonoid.one_mem⟩, by clear _let_match; subst this; apply_instance open finset nat lemma is_cyclic.card_pow_eq_one_le [group α] [fintype α] [decidable_eq α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n := let ⟨g, hg⟩ := is_cyclic.exists_generator α in calc (univ.filter (λ a : α, a ^ n = 1)).card ≤ (gpowers (g ^ (fintype.card α / (gcd n (fintype.card α))))).to_finset.card : card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ powers g, from (powers_eq_gpowers g).symm ▸ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card α / (gcd n (fintype.card α))) : ℕ), have hgmn : g ^ (m * gcd n (fintype.card α)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _, conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]}, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≤ n : let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card α) in have hm0 : 0 < m, from nat.pos_of_ne_zero (λ hm0, (by rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm; exact hm 1)), begin rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_gpowers, order_of_pow, order_of_eq_card_of_forall_mem_gpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (gcd_dvd_left _ _) end lemma is_cyclic.exists_monoid_generator (α : Type*) [group α] [fintype α] [is_cyclic α] : ∃ x : α, ∀ y : α, y ∈ powers x := by simp only [powers_eq_gpowers]; exact is_cyclic.exists_generator α section variables [group α] [fintype α] [decidable_eq α] lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (order_of a)) = univ := begin simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha], convert set.to_finset_univ end lemma is_cyclic.image_range_card (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (fintype.card α)) = univ := by rw [← order_of_eq_card_of_forall_mem_gpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [group α] [fintype α] [decidable_eq α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n) include hn lemma card_pow_eq_one_eq_order_of_aux (a : α) : (finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos _)) (calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers ... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (fintype.of_finset _ (λ _, iff.rfl)) : @fintype.card_le_of_injective (gpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat, pow_order_of_eq_one, one_gpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use φ for nat.totient private lemma card_order_of_eq_totient_aux₁ : ∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card → (univ.filter (λ a : α, order_of a = d)).card = φ d | 0 := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in absurd (mem_filter.1 ha).2 $ ne_of_gt $ order_of_pos a | (d+1) := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in have ha : order_of a = d.succ, from (mem_filter.1 ha).2, have h : ∑ m in (range d.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card = ∑ m in (range d.succ).filter (∣ d.succ), φ m, from finset.sum_congr rfl (λ m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1, have hm : m ∣ d.succ, from (mem_filter.1 hm).2, card_order_of_eq_totient_aux₁ (dvd.trans hm hd) (finset.card_pos.2 ⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _, by rw [order_of_pow, ha, gcd_eq_right (div_dvd_of_dvd hm), nat.div_div_self hm (succ_pos _)]⟩⟩)), have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ)) = (range d.succ.succ).filter (∣ d.succ), from (finset.ext $ λ x, ⟨λ h, (mem_insert.1 h).elim (λ h, by simp [h, range_succ]) (by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩), have hinsert₁ : d.succ ∉ (range d.succ).filter (∣ d.succ), by simp [mem_range, zero_le_one, le_succ], (add_left_inj (∑ m in (range d.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card)).1 (calc _ = ∑ m in insert d.succ (filter (∣ d.succ) (range d.succ)), (univ.filter (λ a : α, order_of a = m)).card : eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ])) ... = ∑ m in (range d.succ.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card : sum_congr hinsert (λ _ _, rfl) ... = (univ.filter (λ a : α, a ^ d.succ = 1)).card : sum_card_order_of_eq_card_pow_eq_one (succ_pos d) ... = ∑ m in (range d.succ.succ).filter (∣ d.succ), φ m : ha ▸ (card_pow_eq_one_eq_order_of_aux hn a).symm ▸ (sum_totient _).symm ... = _ : by rw [h, ← sum_insert hinsert₁]; exact finset.sum_congr hinsert.symm (λ _ _, rfl)) lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = φ d := by_contradiction $ λ h, have h0 : (univ.filter (λ a : α , order_of a = d)).card = 0 := not_not.1 (mt nat.pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)), let c := fintype.card α in have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩, lt_irrefl c $ calc c = (univ.filter (λ a : α, a ^ c = 1)).card : congr_arg card $ by simp [finset.ext_iff, c] ... = ∑ m in (range c.succ).filter (∣ c), (univ.filter (λ a : α, order_of a = m)).card : (sum_card_order_of_eq_card_pow_eq_one hc0).symm ... = ∑ m in ((range c.succ).filter (∣ c)).erase d, (univ.filter (λ a : α, order_of a = m)).card : eq.symm (sum_subset (erase_subset _ _) (λ m hm₁ hm₂, have m = d, by simp at *; cc, by simp [*, finset.ext_iff] at *; exact h0)) ... ≤ ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m : sum_le_sum (λ m hm, have hmc : m ∣ c, by simp at hm; tauto, (imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim (λ h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le]) (λ h, by rw h)) ... < φ d + ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m : lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero (λ h, nat.pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h ▸ hd)))) ... = ∑ m in insert d (((range c.succ).filter (∣ c)).erase d), φ m : eq.symm (sum_insert (by simp)) ... = ∑ m in (range c.succ).filter (∣ c), φ m : finset.sum_congr (finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (λ _ _, rfl) ... = c : sum_totient _ lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α := have (univ.filter (λ a : α, order_of a = fintype.card α)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_aux₂ hn (dvd_refl _)]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 end totient lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [fintype α] [decidable_eq α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d := card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd end cyclic
ccd6ca2c5204f37547a6f1ddd6c882a4c53ef05b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finsupp/big_operators.lean
9f0051a7f7e10a8b78c516c75d85c29a6918a962
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
5,020
lean
/- Copyright (c) 2022 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.finsupp.defs import data.finset.pairwise /-! # Sums of collections of finsupp, and their support > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides results about the `finsupp.support` of sums of collections of `finsupp`, including sums of `list`, `multiset`, and `finset`. The support of the sum is a subset of the union of the supports: * `list.support_sum_subset` * `multiset.support_sum_subset` * `finset.support_sum_subset` The support of the sum of pairwise disjoint finsupps is equal to the union of the supports * `list.support_sum_eq` * `multiset.support_sum_eq` * `finset.support_sum_eq` Member in the support of the indexed union over a collection iff it is a member of the support of a member of the collection: * `list.mem_foldr_sup_support_iff` * `multiset.mem_sup_map_support_iff` * `finset.mem_sup_support_iff` -/ variables {ι M : Type*} [decidable_eq ι] lemma list.support_sum_subset [add_monoid M] (l : list (ι →₀ M)) : l.sum.support ⊆ l.foldr ((⊔) ∘ finsupp.support) ∅ := begin induction l with hd tl IH, { simp }, { simp only [list.sum_cons, finset.union_comm], refine finsupp.support_add.trans (finset.union_subset_union _ IH), refl } end lemma multiset.support_sum_subset [add_comm_monoid M] (s : multiset (ι →₀ M)) : s.sum.support ⊆ (s.map (finsupp.support)).sup := begin induction s using quot.induction_on, simpa using list.support_sum_subset _ end lemma finset.support_sum_subset [add_comm_monoid M] (s : finset (ι →₀ M)) : (s.sum id).support ⊆ finset.sup s finsupp.support := by { classical, convert multiset.support_sum_subset s.1; simp } lemma list.mem_foldr_sup_support_iff [has_zero M] {l : list (ι →₀ M)} {x : ι} : x ∈ l.foldr ((⊔) ∘ finsupp.support) ∅ ↔ ∃ (f : ι →₀ M) (hf : f ∈ l), x ∈ f.support := begin simp only [finset.sup_eq_union, list.foldr_map, finsupp.mem_support_iff, exists_prop], induction l with hd tl IH, { simp }, { simp only [IH, list.foldr_cons, finset.mem_union, finsupp.mem_support_iff, list.mem_cons_iff], split, { rintro (h|h), { exact ⟨hd, or.inl rfl, h⟩ }, { exact h.imp (λ f hf, hf.imp_left or.inr) } }, { rintro ⟨f, rfl|hf, h⟩, { exact or.inl h }, { exact or.inr ⟨f, hf, h⟩ } } } end lemma multiset.mem_sup_map_support_iff [has_zero M] {s : multiset (ι →₀ M)} {x : ι} : x ∈ (s.map (finsupp.support)).sup ↔ ∃ (f : ι →₀ M) (hf : f ∈ s), x ∈ f.support := quot.induction_on s $ λ _, by simpa using list.mem_foldr_sup_support_iff lemma finset.mem_sup_support_iff [has_zero M] {s : finset (ι →₀ M)} {x : ι} : x ∈ s.sup finsupp.support ↔ ∃ (f : ι →₀ M) (hf : f ∈ s), x ∈ f.support := multiset.mem_sup_map_support_iff lemma list.support_sum_eq [add_monoid M] (l : list (ι →₀ M)) (hl : l.pairwise (disjoint on finsupp.support)) : l.sum.support = l.foldr ((⊔) ∘ finsupp.support) ∅ := begin induction l with hd tl IH, { simp }, { simp only [list.pairwise_cons] at hl, simp only [list.sum_cons, list.foldr_cons, function.comp_app], rw [finsupp.support_add_eq, IH hl.right, finset.sup_eq_union], suffices : disjoint hd.support (tl.foldr ((⊔) ∘ finsupp.support) ∅), { exact finset.disjoint_of_subset_right (list.support_sum_subset _) this }, { rw [←list.foldr_map, ←finset.bot_eq_empty, list.foldr_sup_eq_sup_to_finset], rw finset.disjoint_sup_right, intros f hf, simp only [list.mem_to_finset, list.mem_map] at hf, obtain ⟨f, hf, rfl⟩ := hf, exact hl.left _ hf } } end lemma multiset.support_sum_eq [add_comm_monoid M] (s : multiset (ι →₀ M)) (hs : s.pairwise (disjoint on finsupp.support)) : s.sum.support = (s.map finsupp.support).sup := begin induction s using quot.induction_on, obtain ⟨l, hl, hd⟩ := hs, convert list.support_sum_eq _ _, { simp }, { simp }, { simp only [multiset.quot_mk_to_coe'', multiset.coe_map, multiset.coe_eq_coe] at hl, exact hl.symm.pairwise hd (λ _ _ h, disjoint.symm h) } end lemma finset.support_sum_eq [add_comm_monoid M] (s : finset (ι →₀ M)) (hs : (s : set (ι →₀ M)).pairwise_disjoint finsupp.support) : (s.sum id).support = finset.sup s finsupp.support := begin classical, convert multiset.support_sum_eq s.1 _, { exact (finset.sum_val _).symm }, { obtain ⟨l, hl, hn⟩ : ∃ (l : list (ι →₀ M)), l.to_finset = s ∧ l.nodup, { refine ⟨s.to_list, _, finset.nodup_to_list _⟩, simp }, subst hl, rwa [list.to_finset_val, list.dedup_eq_self.mpr hn, multiset.pairwise_coe_iff_pairwise, ←list.pairwise_disjoint_iff_coe_to_finset_pairwise_disjoint hn], intros x y hxy, exact symmetric_disjoint hxy } end
0ccecf84c82ae60416ecef78191a1f7126294534
f3849be5d845a1cb97680f0bbbe03b85518312f0
/tests/lean/nat_pp.lean
f7180002fa01f11a7425b458fbfde12422fc0851
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
121
lean
#reduce nat.add (nat.of_num 3) (nat.of_num 6) open nat #reduce nat.add (nat.of_num 3) (nat.of_num 6) #reduce (3:nat) + 6
33cb7411873442697ddcfc508427d875dd4ce005
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/387.lean
24b6c66cdbd1b55a94dd3f666e0ca75fd7cdd917
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
567
lean
-- axiom p {α β} : α → β → Prop axiom foo {α β} (a : α) (b : β) : p a b example : p 0 0 := by simp [foo] example (a : Nat) : p a a := by simp [foo a] example : p 0 0 := by simp [foo 0] example : p 0 0 := by simp [foo 0 0] example : p 0 0 := by simp [foo 1] -- will not simplify simp [foo 0] example : p 0 0 ∧ p 1 1 := by simp [foo 1] trace_state simp [foo 0] namespace Foo axiom p {α} : α → Prop axiom foo {α} [ToString α] (n : Nat) (a : α) : p a example : p 0 := by simp [foo 0] example : p 0 ∧ True := by simp [foo 0] end Foo
c805a1b03d3667c888dc673da791e45642537fa4
a46270e2f76a375564f3b3e9c1bf7b635edc1f2c
/7.10.4.lean
e8683be51c8593897e8a016202ad983a1b292315
[ "CC0-1.0" ]
permissive
wudcscheme/lean-exercise
88ea2506714eac343de2a294d1132ee8ee6d3a20
5b23b9be3d361fff5e981d5be3a0a1175504b9f6
refs/heads/master
1,678,958,930,293
1,583,197,205,000
1,583,197,205,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,879
lean
inductive t: Type | const : bool -> t | var : ℕ -> t | and : t -> t -> t | or : t -> t -> t | not : t -> t def bn := ℕ × t. def env := list bn. def lookup: env -> nat -> t | [] x := t.var x | ((y,v)::ys) x := if x = y then v else lookup ys x def t_and: t -> t -> t | (t.const a) (t.const b) := t.const (a ∧ b) | u v := t.and u v def t_or: t -> t -> t | (t.const a) (t.const b) := t.const (a ∨ b) | u v := t.or u v def t_not: t -> t | (t.const a) := t.const (¬ a) | u := t.not u infix `<|>`:50 := t_or infix `<&>`:70 := t_and def subst : env -> t -> t | _ (t.const v) := t.const v | e (t.var x) := lookup e x | e (t.and u v) := t.and (subst e u) (subst e v) | e (t.or u v) := t.or (subst e u) (subst e v) | e (t.not u) := t_not (subst e u) . def atomic : t -> t | (t.const v) := t.const v | (t.not v) := t_not v | u := u def reduce : env -> t -> t | _ (t.const v) := t.const v | e (t.var x) := atomic (lookup e x) | e (t.and u v) := (reduce e u) <&> (reduce e v) | e (t.or u v) := (reduce e u) <|> (reduce e v) | e (t.not u) := t_not (reduce e u) . def T := t.const tt def F := t.const ff def x₁ := t.var 1 def x₂ := t.var 2 def x₃ := t.var 3 def x₄ := t.var 4 #reduce reduce [] (t.const tt) #reduce reduce [] (t.var 1) #reduce reduce [(1, T)] (t.var 1) #reduce subst [(1, T)] (t.var 1) #reduce reduce [(1, T), (2, T)] (x₁ <&> x₂) #reduce reduce [(1, F), (2, T)] (x₁ <&> x₂) #reduce reduce [(1, F), (2, T)] (x₁ <|> x₂) #reduce reduce [(1, F), (2, F)] (x₁ <|> x₂) #reduce reduce [(1, x₃ <|> x₄), (2, T)] (x₁ <&> x₂) #reduce subst [(1, x₃ <|> x₄), (2, T)] (x₁ <&> x₂) #reduce reduce [(3, F), (4, F)] (subst [(1, x₃ <|> x₄), (2, T)] (x₁ <&> x₂)) #reduce reduce [(3, F), (4, t.not F)] (subst [(1, x₃ <|> x₄), (2, T)] (x₁ <&> x₂))
ed38b36d3ffdd8a655060cfbc0d3dc85551c61d0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/homotopy/fundamental_groupoid.lean
82e786297e174e45432c05248749c93718d08601
[ "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
12,537
lean
/- Copyright (c) 2021 Shing Tak Lam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam -/ import category_theory.category.Groupoid import category_theory.groupoid import topology.category.Top.basic import topology.homotopy.path /-! # Fundamental groupoid of a space Given a topological space `X`, we can define the fundamental groupoid of `X` to be the category with objects being points of `X`, and morphisms `x ⟶ y` being paths from `x` to `y`, quotiented by homotopy equivalence. With this, the fundamental group of `X` based at `x` is just the automorphism group of `x`. -/ universes u v variables {X : Type u} {Y : Type v} [topological_space X] [topological_space Y] variables {x₀ x₁ : X} noncomputable theory open_locale unit_interval namespace path namespace homotopy section /-- Auxilliary function for `refl_trans_symm` -/ def refl_trans_symm_aux (x : I × I) : ℝ := if (x.2 : ℝ) ≤ 1/2 then x.1 * 2 * x.2 else x.1 * (2 - 2 * x.2) @[continuity] lemma continuous_refl_trans_symm_aux : continuous refl_trans_symm_aux := begin refine continuous_if_le _ _ (continuous.continuous_on _) (continuous.continuous_on _) _, { continuity }, { continuity }, { continuity }, { continuity }, intros x hx, norm_num [hx, mul_assoc], end lemma refl_trans_symm_aux_mem_I (x : I × I) : refl_trans_symm_aux x ∈ I := begin dsimp only [refl_trans_symm_aux], split_ifs, { split, { apply mul_nonneg, { apply mul_nonneg, { unit_interval }, { norm_num } }, { unit_interval } }, { rw [mul_assoc], apply mul_le_one, { unit_interval }, { apply mul_nonneg, { norm_num }, { unit_interval } }, { linarith } } }, { split, { apply mul_nonneg, { unit_interval }, linarith [unit_interval.nonneg x.2, unit_interval.le_one x.2] }, { apply mul_le_one, { unit_interval }, { linarith [unit_interval.nonneg x.2, unit_interval.le_one x.2] }, { linarith [unit_interval.nonneg x.2, unit_interval.le_one x.2] } } } end /-- For any path `p` from `x₀` to `x₁`, we have a homotopy from the constant path based at `x₀` to `p.trans p.symm`. -/ def refl_trans_symm (p : path x₀ x₁) : homotopy (path.refl x₀) (p.trans p.symm) := { to_fun := λ x, p ⟨refl_trans_symm_aux x, refl_trans_symm_aux_mem_I x⟩, continuous_to_fun := by continuity, to_fun_zero := by norm_num [refl_trans_symm_aux], to_fun_one := λ x, begin dsimp only [refl_trans_symm_aux, path.coe_to_continuous_map, path.trans], change _ = ite _ _ _, split_ifs, { rw [path.extend, set.Icc_extend_of_mem], { norm_num }, { rw unit_interval.mul_pos_mem_iff zero_lt_two, exact ⟨unit_interval.nonneg x, h⟩ } }, { rw [path.symm, path.extend, set.Icc_extend_of_mem], { congr' 1, ext, norm_num [sub_sub_assoc_swap] }, { rw unit_interval.two_mul_sub_one_mem_iff, exact ⟨(not_le.1 h).le, unit_interval.le_one x⟩ } } end, prop' := λ t x hx, begin cases hx, { rw hx, simp [refl_trans_symm_aux] }, { rw set.mem_singleton_iff at hx, rw hx, norm_num [refl_trans_symm_aux] } end } /-- For any path `p` from `x₀` to `x₁`, we have a homotopy from the constant path based at `x₁` to `p.symm.trans p`. -/ def refl_symm_trans (p : path x₀ x₁) : homotopy (path.refl x₁) (p.symm.trans p) := (refl_trans_symm p.symm).cast rfl $ congr_arg _ path.symm_symm end section trans_refl /-- Auxilliary function for `trans_refl_reparam` -/ def trans_refl_reparam_aux (t : I) : ℝ := if (t : ℝ) ≤ 1/2 then 2 * t else 1 @[continuity] lemma continuous_trans_refl_reparam_aux : continuous trans_refl_reparam_aux := begin refine continuous_if_le _ _ (continuous.continuous_on _) (continuous.continuous_on _) _; [continuity, continuity, continuity, continuity, skip], intros x hx, norm_num [hx] end lemma trans_refl_reparam_aux_mem_I (t : I) : trans_refl_reparam_aux t ∈ I := begin unfold trans_refl_reparam_aux, split_ifs; split; linarith [unit_interval.le_one t, unit_interval.nonneg t] end lemma trans_refl_reparam_aux_zero : trans_refl_reparam_aux 0 = 0 := by norm_num [trans_refl_reparam_aux] lemma trans_refl_reparam_aux_one : trans_refl_reparam_aux 1 = 1 := by norm_num [trans_refl_reparam_aux] lemma trans_refl_reparam (p : path x₀ x₁) : p.trans (path.refl x₁) = p.reparam (λ t, ⟨trans_refl_reparam_aux t, trans_refl_reparam_aux_mem_I t⟩) (by continuity) (subtype.ext trans_refl_reparam_aux_zero) (subtype.ext trans_refl_reparam_aux_one) := begin ext, unfold trans_refl_reparam_aux, simp only [path.trans_apply, not_le, coe_to_fun, function.comp_app], split_ifs, { refl }, { simp } end /-- For any path `p` from `x₀` to `x₁`, we have a homotopy from `p.trans (path.refl x₁)` to `p`. -/ def trans_refl (p : path x₀ x₁) : homotopy (p.trans (path.refl x₁)) p := ((homotopy.reparam p (λ t, ⟨trans_refl_reparam_aux t, trans_refl_reparam_aux_mem_I t⟩) (by continuity) (subtype.ext trans_refl_reparam_aux_zero) (subtype.ext trans_refl_reparam_aux_one)).cast rfl (trans_refl_reparam p).symm).symm /-- For any path `p` from `x₀` to `x₁`, we have a homotopy from `(path.refl x₀).trans p` to `p`. -/ def refl_trans (p : path x₀ x₁) : homotopy ((path.refl x₀).trans p) p := (trans_refl p.symm).symm₂.cast (by simp) (by simp) end trans_refl section assoc /-- Auxilliary function for `trans_assoc_reparam`. -/ def trans_assoc_reparam_aux (t : I) : ℝ := if (t : ℝ) ≤ 1/4 then 2 * t else if (t : ℝ) ≤ 1/2 then t + 1/4 else 1/2 * (t + 1) @[continuity] lemma continuous_trans_assoc_reparam_aux : continuous trans_assoc_reparam_aux := begin refine continuous_if_le _ _ (continuous.continuous_on _) (continuous_if_le _ _ (continuous.continuous_on _) (continuous.continuous_on _) _).continuous_on _; [continuity, continuity, continuity, continuity, continuity, continuity, continuity, skip, skip]; { intros x hx, norm_num [hx], } end lemma trans_assoc_reparam_aux_mem_I (t : I) : trans_assoc_reparam_aux t ∈ I := begin unfold trans_assoc_reparam_aux, split_ifs; split; linarith [unit_interval.le_one t, unit_interval.nonneg t] end lemma trans_assoc_reparam_aux_zero : trans_assoc_reparam_aux 0 = 0 := by norm_num [trans_assoc_reparam_aux] lemma trans_assoc_reparam_aux_one : trans_assoc_reparam_aux 1 = 1 := by norm_num [trans_assoc_reparam_aux] lemma trans_assoc_reparam {x₀ x₁ x₂ x₃ : X} (p : path x₀ x₁) (q : path x₁ x₂) (r : path x₂ x₃) : (p.trans q).trans r = (p.trans (q.trans r)).reparam (λ t, ⟨trans_assoc_reparam_aux t, trans_assoc_reparam_aux_mem_I t⟩) (by continuity) (subtype.ext trans_assoc_reparam_aux_zero) (subtype.ext trans_assoc_reparam_aux_one) := begin ext, simp only [trans_assoc_reparam_aux, path.trans_apply, mul_inv_cancel_left₀, not_le, function.comp_app, ne.def, not_false_iff, bit0_eq_zero, one_ne_zero, mul_ite, subtype.coe_mk, path.coe_to_fun], -- TODO: why does split_ifs not reduce the ifs?????? split_ifs with h₁ h₂ h₃ h₄ h₅, { simp [h₂, h₃, -one_div] }, { exfalso, linarith }, { exfalso, linarith }, { have h : ¬ (x : ℝ) + 1/4 ≤ 1/2, by linarith, have h' : 2 * ((x : ℝ) + 1/4) - 1 ≤ 1/2, by linarith, have h'' : 2 * (2 * (x : ℝ)) - 1 = 2 * (2 * (↑x + 1/4) - 1), by linarith, simp only [h₄, h₁, h, h', h'', dif_neg (show ¬ false, from id), dif_pos true.intro, if_false, if_true] }, { exfalso, linarith }, { have h : ¬ (1 / 2 : ℝ) * (x + 1) ≤ 1/2, by linarith, have h' : ¬ 2 * ((1 / 2 : ℝ) * (x + 1)) - 1 ≤ 1/2, by linarith, simp only [h₁, h₅, h, h', if_false, dif_neg (show ¬ false, from id)], congr, ring } end /-- For paths `p q r`, we have a homotopy from `(p.trans q).trans r` to `p.trans (q.trans r)`. -/ def trans_assoc {x₀ x₁ x₂ x₃ : X} (p : path x₀ x₁) (q : path x₁ x₂) (r : path x₂ x₃) : homotopy ((p.trans q).trans r) (p.trans (q.trans r)) := ((homotopy.reparam (p.trans (q.trans r)) (λ t, ⟨trans_assoc_reparam_aux t, trans_assoc_reparam_aux_mem_I t⟩) (by continuity) (subtype.ext trans_assoc_reparam_aux_zero) (subtype.ext trans_assoc_reparam_aux_one)).cast rfl (trans_assoc_reparam p q r).symm).symm end assoc end homotopy end path /-- The fundamental groupoid of a space `X` is defined to be a type synonym for `X`, and we subsequently put a `category_theory.groupoid` structure on it. -/ def fundamental_groupoid (X : Type u) := X namespace fundamental_groupoid instance {X : Type u} [h : inhabited X] : inhabited (fundamental_groupoid X) := h local attribute [reducible] fundamental_groupoid local attribute [instance] path.homotopic.setoid instance : category_theory.groupoid (fundamental_groupoid X) := { hom := λ x y, path.homotopic.quotient x y, id := λ x, ⟦path.refl x⟧, comp := λ x y z, path.homotopic.quotient.comp, id_comp' := λ x y f, quotient.induction_on f (λ a, show ⟦(path.refl x).trans a⟧ = ⟦a⟧, from quotient.sound ⟨path.homotopy.refl_trans a⟩ ), comp_id' := λ x y f, quotient.induction_on f (λ a, show ⟦a.trans (path.refl y)⟧ = ⟦a⟧, from quotient.sound ⟨path.homotopy.trans_refl a⟩), assoc' := λ w x y z f g h, quotient.induction_on₃ f g h (λ p q r, show ⟦(p.trans q).trans r⟧ = ⟦p.trans (q.trans r)⟧, from quotient.sound ⟨path.homotopy.trans_assoc p q r⟩), inv := λ x y p, quotient.lift (λ l : path x y, ⟦l.symm⟧) begin rintros a b ⟨h⟩, rw quotient.eq, exact ⟨h.symm₂⟩, end p, inv_comp' := λ x y f, quotient.induction_on f (λ a, show ⟦a.symm.trans a⟧ = ⟦path.refl y⟧, from quotient.sound ⟨(path.homotopy.refl_symm_trans a).symm⟩), comp_inv' := λ x y f, quotient.induction_on f (λ a, show ⟦a.trans a.symm⟧ = ⟦path.refl x⟧, from quotient.sound ⟨(path.homotopy.refl_trans_symm a).symm⟩) } lemma comp_eq (x y z : fundamental_groupoid X) (p : x ⟶ y) (q : y ⟶ z) : p ≫ q = p.comp q := rfl lemma id_eq_path_refl (x : fundamental_groupoid X) : 𝟙 x = ⟦path.refl x⟧ := rfl /-- The functor sending a topological space `X` to its fundamental groupoid. -/ def fundamental_groupoid_functor : Top ⥤ category_theory.Groupoid := { obj := λ X, { α := fundamental_groupoid X }, map := λ X Y f, { obj := f, map := λ x y p, p.map_fn f, map_id' := λ X, rfl, map_comp' := λ x y z p q, quotient.induction_on₂ p q $ λ a b, by simp [comp_eq, ← path.homotopic.map_lift, ← path.homotopic.comp_lift] }, map_id' := begin intro X, change _ = (⟨_, _, _, _⟩ : fundamental_groupoid X ⥤ fundamental_groupoid X), congr', ext x y p, refine quotient.induction_on p (λ q, _), rw [← path.homotopic.map_lift], conv_rhs { rw [←q.map_id] }, refl, end, map_comp' := begin intros X Y Z f g, congr', ext x y p, refine quotient.induction_on p (λ q, _), simp only [quotient.map_mk, path.map_map, quotient.eq], refl, end } localized "notation `π` := fundamental_groupoid.fundamental_groupoid_functor" in fundamental_groupoid localized "notation `πₓ` := fundamental_groupoid.fundamental_groupoid_functor.obj" in fundamental_groupoid localized "notation `πₘ` := fundamental_groupoid.fundamental_groupoid_functor.map" in fundamental_groupoid /-- Help the typechecker by converting a point in a groupoid back to a point in the underlying topological space. -/ @[reducible] def to_top {X : Top} (x : (πₓ X).α) : X := x /-- Help the typechecker by converting a point in a topological space to a point in the fundamental groupoid of that space -/ @[reducible] def from_top {X : Top} (x : X) : (πₓ X).α := x /-- Help the typechecker by converting an arrow in the fundamental groupoid of a topological space back to a path in that space (i.e., `path.homotopic.quotient`). -/ @[reducible] def to_path {X : Top} {x₀ x₁ : (πₓ X).α} (p : x₀ ⟶ x₁) : path.homotopic.quotient x₀ x₁ := p /-- Help the typechecker by convering a path in a topological space to an arrow in the fundamental groupoid of that space. -/ @[reducible] def from_path {X : Top} {x₀ x₁ : X} (p : path.homotopic.quotient x₀ x₁) : (x₀ ⟶ x₁) := p end fundamental_groupoid
314b95495e88a10b084dc39c7b4a1d82a939d746
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/min_max.lean
f9220ce2e46c034bcb8d5cd48a793d75b4369788
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
9,066
lean
/- Copyright (c) 2019 Minchao Wu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Minchao Wu, Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Minimum and maximum of lists ## Main definitions The main definitions are `argmax`, `argmin`, `minimum` and `maximum` for lists. `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ namespace list /-- Auxiliary definition to define `argmax` -/ def argmax₂ {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (a : Option α) (b : α) : Option α := option.cases_on a (some b) fun (c : α) => ite (f b ≤ f c) (some c) (some b) /-- `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` -/ def argmax {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (l : List α) : Option α := foldl (argmax₂ f) none l /-- `argmin f l` returns `some a`, where `a` of `l` that minimises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmin f []` = none` -/ def argmin {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (l : List α) : Option α := argmax f l @[simp] theorem argmax_two_self {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (a : α) : argmax₂ f (some a) a = ↑a := if_pos (le_refl (f a)) @[simp] theorem argmax_nil {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) : argmax f [] = none := rfl @[simp] theorem argmin_nil {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) : argmin f [] = none := rfl @[simp] theorem argmax_singleton {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {a : α} : argmax f [a] = some a := rfl @[simp] theorem argmin_singleton {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {a : α} : argmin f [a] = ↑a := rfl @[simp] theorem foldl_argmax₂_eq_none {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {l : List α} {o : Option α} : foldl (argmax₂ f) o l = none ↔ l = [] ∧ o = none := sorry theorem argmax_mem {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {l : List α} {m : α} : m ∈ argmax f l → m ∈ l := sorry theorem argmin_mem {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {l : List α} {m : α} : m ∈ argmin f l → m ∈ l := argmax_mem @[simp] theorem argmax_eq_none {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {l : List α} : argmax f l = none ↔ l = [] := sorry @[simp] theorem argmin_eq_none {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {l : List α} : argmin f l = none ↔ l = [] := argmax_eq_none theorem le_argmax_of_mem {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {a : α} {m : α} {l : List α} : a ∈ l → m ∈ argmax f l → f a ≤ f m := le_of_foldl_argmax₂ theorem argmin_le_of_mem {α : Type u_1} {β : Type u_2} [linear_order β] {f : α → β} {a : α} {m : α} {l : List α} : a ∈ l → m ∈ argmin f l → f m ≤ f a := le_argmax_of_mem theorem argmax_concat {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (a : α) (l : List α) : argmax f (l ++ [a]) = option.cases_on (argmax f l) (some a) fun (c : α) => ite (f a ≤ f c) (some c) (some a) := sorry theorem argmin_concat {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (a : α) (l : List α) : argmin f (l ++ [a]) = option.cases_on (argmin f l) (some a) fun (c : α) => ite (f c ≤ f a) (some c) (some a) := argmax_concat f a l theorem argmax_cons {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (a : α) (l : List α) : argmax f (a :: l) = option.cases_on (argmax f l) (some a) fun (c : α) => ite (f c ≤ f a) (some a) (some c) := sorry theorem argmin_cons {α : Type u_1} {β : Type u_2} [linear_order β] (f : α → β) (a : α) (l : List α) : argmin f (a :: l) = option.cases_on (argmin f l) (some a) fun (c : α) => ite (f a ≤ f c) (some a) (some c) := argmax_cons f a l theorem index_of_argmax {α : Type u_1} {β : Type u_2} [linear_order β] [DecidableEq α] {f : α → β} {l : List α} {m : α} : m ∈ argmax f l → ∀ {a : α}, a ∈ l → f m ≤ f a → index_of m l ≤ index_of a l := sorry theorem index_of_argmin {α : Type u_1} {β : Type u_2} [linear_order β] [DecidableEq α] {f : α → β} {l : List α} {m : α} : m ∈ argmin f l → ∀ {a : α}, a ∈ l → f a ≤ f m → index_of m l ≤ index_of a l := index_of_argmax theorem mem_argmax_iff {α : Type u_1} {β : Type u_2} [linear_order β] [DecidableEq α] {f : α → β} {m : α} {l : List α} : m ∈ argmax f l ↔ m ∈ l ∧ (∀ (a : α), a ∈ l → f a ≤ f m) ∧ ∀ (a : α), a ∈ l → f m ≤ f a → index_of m l ≤ index_of a l := sorry theorem argmax_eq_some_iff {α : Type u_1} {β : Type u_2} [linear_order β] [DecidableEq α] {f : α → β} {m : α} {l : List α} : argmax f l = some m ↔ m ∈ l ∧ (∀ (a : α), a ∈ l → f a ≤ f m) ∧ ∀ (a : α), a ∈ l → f m ≤ f a → index_of m l ≤ index_of a l := mem_argmax_iff theorem mem_argmin_iff {α : Type u_1} {β : Type u_2} [linear_order β] [DecidableEq α] {f : α → β} {m : α} {l : List α} : m ∈ argmin f l ↔ m ∈ l ∧ (∀ (a : α), a ∈ l → f m ≤ f a) ∧ ∀ (a : α), a ∈ l → f a ≤ f m → index_of m l ≤ index_of a l := mem_argmax_iff theorem argmin_eq_some_iff {α : Type u_1} {β : Type u_2} [linear_order β] [DecidableEq α] {f : α → β} {m : α} {l : List α} : argmin f l = some m ↔ m ∈ l ∧ (∀ (a : α), a ∈ l → f m ≤ f a) ∧ ∀ (a : α), a ∈ l → f a ≤ f m → index_of m l ≤ index_of a l := mem_argmin_iff /-- `maximum l` returns an `with_bot α`, the largest element of `l` for nonempty lists, and `⊥` for `[]` -/ def maximum {α : Type u_1} [linear_order α] (l : List α) : with_bot α := argmax id l /-- `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ def minimum {α : Type u_1} [linear_order α] (l : List α) : with_top α := argmin id l @[simp] theorem maximum_nil {α : Type u_1} [linear_order α] : maximum [] = ⊥ := rfl @[simp] theorem minimum_nil {α : Type u_1} [linear_order α] : minimum [] = ⊤ := rfl @[simp] theorem maximum_singleton {α : Type u_1} [linear_order α] (a : α) : maximum [a] = ↑a := rfl @[simp] theorem minimum_singleton {α : Type u_1} [linear_order α] (a : α) : minimum [a] = ↑a := rfl theorem maximum_mem {α : Type u_1} [linear_order α] {l : List α} {m : α} : maximum l = ↑m → m ∈ l := argmax_mem theorem minimum_mem {α : Type u_1} [linear_order α] {l : List α} {m : α} : minimum l = ↑m → m ∈ l := argmin_mem @[simp] theorem maximum_eq_none {α : Type u_1} [linear_order α] {l : List α} : maximum l = none ↔ l = [] := argmax_eq_none @[simp] theorem minimum_eq_none {α : Type u_1} [linear_order α] {l : List α} : minimum l = none ↔ l = [] := argmin_eq_none theorem le_maximum_of_mem {α : Type u_1} [linear_order α] {a : α} {m : α} {l : List α} : a ∈ l → maximum l = ↑m → a ≤ m := le_argmax_of_mem theorem minimum_le_of_mem {α : Type u_1} [linear_order α] {a : α} {m : α} {l : List α} : a ∈ l → minimum l = ↑m → m ≤ a := argmin_le_of_mem theorem le_maximum_of_mem' {α : Type u_1} [linear_order α] {a : α} {l : List α} (ha : a ∈ l) : ↑a ≤ maximum l := sorry theorem le_minimum_of_mem' {α : Type u_1} [linear_order α] {a : α} {l : List α} (ha : a ∈ l) : minimum l ≤ ↑a := le_maximum_of_mem' ha theorem maximum_concat {α : Type u_1} [linear_order α] (a : α) (l : List α) : maximum (l ++ [a]) = max (maximum l) ↑a := sorry theorem minimum_concat {α : Type u_1} [linear_order α] (a : α) (l : List α) : minimum (l ++ [a]) = min (minimum l) ↑a := maximum_concat a l theorem maximum_cons {α : Type u_1} [linear_order α] (a : α) (l : List α) : maximum (a :: l) = max (↑a) (maximum l) := sorry theorem minimum_cons {α : Type u_1} [linear_order α] (a : α) (l : List α) : minimum (a :: l) = min (↑a) (minimum l) := maximum_cons a l theorem maximum_eq_coe_iff {α : Type u_1} [linear_order α] {m : α} {l : List α} : maximum l = ↑m ↔ m ∈ l ∧ ∀ (a : α), a ∈ l → a ≤ m := sorry theorem minimum_eq_coe_iff {α : Type u_1} [linear_order α] {m : α} {l : List α} : minimum l = ↑m ↔ m ∈ l ∧ ∀ (a : α), a ∈ l → m ≤ a := maximum_eq_coe_iff
4a8859b5e129c527419643faec3bb786853ef135
43390109ab88557e6090f3245c47479c123ee500
/src/xenalib/Ellen_Arlt_matrix_rings.lean
c2852bb479362abbe48322e9559db488f2b6dcf9
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,631
lean
-- copyright 2017/18 Ellen Arlt -- August 2018, Proved module R (matrix R n m) by Blair Shi import algebra.big_operators data.set.finite import algebra.module definition matrix (R: Type) (n m : nat)[ring R] := fin n →( fin m → R ) namespace matrix definition add ( R : Type) [ring R] {n m: nat }(A:matrix R m n) ( B : matrix R m n): (matrix R m n):= λ I J, A I J + B I J definition neg ( R : Type) [ring R] {n m: nat } (A:matrix R m n) : (matrix R m n):= λ I J, - A I J definition zero ( R : Type) [ring R] {m n: nat }: (matrix R m n):= λ I J , 0 definition sub ( R : Type) [ring R] {n m: nat }(A:matrix R m n) ( B : matrix R m n): (matrix R m n):= λ I J, A I J - B I J theorem add_assoc1 ( R : Type) [ring R] {m n: nat }(A:matrix R m n) ( B : matrix R m n) ( C : matrix R m n) : add R A (add R B C) = add R (add R A B) C := begin apply funext, intro, apply funext, intro y, unfold add, show A x y + (B x y + C x y) = ( A x y + B x y) + C x y, rw [add_assoc], end theorem add_assoc2 ( R : Type) [ring R] {m n: nat }(A:matrix R m n) ( B : matrix R m n) ( C : matrix R m n) : add R (add R A B) C = add R A (add R B C) := begin apply funext, intro, apply funext, intro y, show ( A x y + B x y) + C x y = A x y + (B x y + C x y) , rw[add_assoc], end theorem zero_add ( R : Type) [ring R] {m n: nat }(A:matrix R m n): add R (zero R) A = A:= begin apply funext, intro, apply funext, intro y, show 0 + A x y = A x y, rw[zero_add] end theorem add_zero (R : Type) [ring R] {m n: nat }(A:matrix R m n): add R A (zero R) = A:= begin apply funext, intro, apply funext, intro y, show A x y + 0 = A x y, rw[add_zero] end theorem add_left_neg (R : Type) [ring R] {m n: nat }(A:matrix R m n) : add R ( neg R A) A = zero R := begin apply funext, intro, apply funext, intro y, show - A x y + A x y = 0 , rw[add_left_neg] end theorem add_comm (R : Type) [ring R] {m n: nat }(A:matrix R m n)( B : matrix R m n): add R A B = add R B A := begin apply funext, intro, apply funext, intro y, show A x y + B x y = B x y + A x y , rw [add_comm] end instance matrices_add_comm_group ( R : Type) [ring R] {m n: nat }: add_comm_group (matrix R m n):={ add:= matrix.add R , add_assoc := @matrix.add_assoc2 R _ m n, zero := matrix.zero R , neg := matrix.neg R, zero_add := @matrix.zero_add R _ m n, add_zero := @matrix.add_zero R _ m n, add_left_neg := @matrix.add_left_neg R _ m n, add_comm := @matrix.add_comm R _ m n } definition mul ( R : Type) [ring R] {n m l: nat }(A:matrix R m n) ( B : matrix R n l): (matrix R m l ):= λ I J, finset.sum finset.univ (λ K, A I K * B K J) theorem mul_assoc ( R : Type) [ring R] {m n l o: nat }(A:matrix R m n) ( B : matrix R n l) ( C : matrix R l o) : mul R A (mul R B C) = mul R (mul R A B) C := begin unfold mul, apply funext, intro x, apply funext, intro y, -- This next line just rewrites the goal so that the variables we're summing over -- are the same on both sides. show finset.sum finset.univ (λ (K : fin n), A x K * finset.sum finset.univ (λ (J : fin l), B K J * C J y)) = finset.sum finset.univ (λ (J : fin l), finset.sum finset.univ (λ (K : fin n), A x K * B K J) * C J y), -- So the key things we now need to use are: -- finset.mul_sum (which says c * sum_n a_n = sum_n (c*a_n)) -- finset.sum_mul (which says the same but multiplication on the right) -- and finset.sum_comm (which says that two finite sums can be commuted). -- However, rw finset.sum_mul doesn't work -- and neither does rw finset.mul_sum, -- and until we apply these we can't commute the sums. -- I think the reasons they don't work are that C J y, which we need to move, depends on J, -- which is not one of our variables. -- So here's a trick. We prove an intermediate lemma which says we can -- move C J y into the sum, by checking the things we're summing over are the same. have H2 : (λ (J : fin l), finset.sum finset.univ (λ (K : fin n), A x K * B K J) * C J y) = (λ (J : fin l), finset.sum finset.univ (λ (K : fin n), A x K * B K J * C J y)), { apply funext, intro J, exact finset.sum_mul, }, -- Now we can commute the sums after using this lemma. rw [H2], clear H2, rw [finset.sum_comm], -- Now I just rewrite the goal to show that both things are a sum over K in fin n. show finset.sum finset.univ (λ (K : fin n), A x K * finset.sum finset.univ (λ (K_1 : fin l), B K K_1 * C K_1 y)) = finset.sum finset.univ (λ (K : fin n), finset.sum finset.univ (λ (x_1 : fin l), A x K * B K x_1 * C x_1 y)), -- Now we can cancel the first sum and it's all downhill from here. apply congr_arg _, apply funext, intro z, rw finset.mul_sum, -- c*sum a_n = sum c*a_n apply congr_arg, apply funext, intro w, rw mul_assoc, end theorem left_distrib (R:Type) [ring R] {m n l : nat} (A : matrix R m n) (B : matrix R n l) (C : matrix R n l ) : mul R A (add R B C) = add R (mul R A B) (mul R A C) := begin unfold mul, unfold add, apply funext, intro x, apply funext, intro y, have H3 : finset.sum finset.univ (λ (K : fin n), A x K * B K y) + finset.sum finset.univ (λ (K : fin n), A x K * C K y) = finset.sum finset.univ (λ (K : fin n), A x K * B K y + A x K * C K y), {simp[finset.sum_add_distrib]}, rw[H3], clear H3, apply congr_arg _, apply funext, intro z, rw [left_distrib] end theorem right_distrib (R:Type) [ring R] {m n l : nat} (A : matrix R m n) (B : matrix R m n) (C : matrix R n l ) : mul R (add R A B) C = add R (mul R A C) (mul R B C) := begin unfold mul, unfold add, apply funext, intro x, apply funext, intro y, have H4 : finset.sum finset.univ (λ (K : fin n), A x K * C K y) + finset.sum finset.univ (λ (K : fin n), B x K * C K y) = finset.sum finset.univ (λ (K : fin n), A x K * C K y + B x K * C K y), {simp[finset.sum_add_distrib]}, rw[H4], clear H4, apply congr_arg _, apply funext, intro z, rw [right_distrib] end theorem mul_sub_mul {R : Type} [comm_ring R] (a b c : ℕ) : ∀ (A B: matrix R a b), ∀ C : matrix R b c, matrix.sub R (matrix.mul R A C) (matrix.mul R B C) = matrix.mul R (matrix.sub R A B) C := begin intros A B C, unfold matrix.sub, unfold matrix.mul, funext, conv in ((A _ _ - B _ _) * C _ _) begin rw [sub_mul], end, {simp[finset.sum_add_distrib]}, end definition identity_matrix ( R : Type) [ring R] { n: nat }: (matrix R n n):= λ I J , if I=J then 1 else 0 --set_option pp.all true --set_option pp.notation false theorem one_mul ( R : Type) [ring R] {n: nat }(A:matrix R n n): mul R (identity_matrix R) A = A:= begin unfold mul, unfold identity_matrix, apply funext, intro x, apply funext, intro y, let xfinset : finset (fin n) := finset.singleton x, suffices : finset.sum finset.univ (λ (K : fin n), ite (x = K) 1 0 * A K y) = finset.sum xfinset (λ t, A t y), simp [this], -- exact finset.sum_singleton, apply eq.symm, have H1 : finset.sum xfinset (λ (t : fin n), A t y) = finset.sum xfinset (λ (K : fin n), ite (x = K) 1 0 * A K y), rw finset.sum_singleton, rw finset.sum_singleton, simp, rw H1, refine (finset.sum_subset (_ : xfinset ⊆ finset.univ) _), exact finset.subset_univ xfinset, intros K H H2, have H3 : ¬ (x = K), intro H4, apply H2, rw ←H4, apply finset.mem_singleton.2, refl, simp [H3], end --#check @finset.sum_subset --finset.sum_subset : -- ∀ {α : Type u_1} {β : Type u_2} {s₁ s₂ : finset α} {f : α → β} [_inst_1 : add_comm_monoid β], -- s₁ ⊆ s₂ → (∀ (x : α), x ∈ s₂ → x ∉ s₁ → f x = 0) → finset.sum s₁ f = finset.sum s₂ f theorem mul_one ( R : Type) [ring R] {n: nat }(A:matrix R n n): mul R A (identity_matrix R) = A:= begin unfold mul, unfold identity_matrix, apply funext, intro x, apply funext, intro y, let yfinset : finset (fin n) := finset.singleton y, suffices : finset.sum finset.univ (λ (K : fin n), A x K * ite (K = y) 1 0 ) = finset.sum yfinset (λ t, A x t), simp [this], apply eq.symm, have H1 : finset.sum yfinset (λ (t : fin n), A x t) = finset.sum yfinset (λ (K : fin n), A x K * ite (K = y) 1 0 ), rw finset.sum_singleton, rw finset.sum_singleton, simp, rw H1, refine (finset.sum_subset (_ : yfinset ⊆ finset.univ) _), exact finset.subset_univ yfinset, intros K H H2, have H3 : ¬ (K = y), intro H4, apply H2, rw H4, apply finset.mem_singleton.2, refl, simp[H3], end instance ring ( R : Type) [ring R] { n: nat }: ring (matrix R n n):={ add:= matrix.add R , add_assoc := @matrix.add_assoc2 R _ n n, zero := matrix.zero R , neg := matrix.neg R, zero_add := @matrix.zero_add R _ n n, add_zero := @matrix.add_zero R _ n n, add_left_neg := @matrix.add_left_neg R _ n n, add_comm := @matrix.add_comm R _ n n, mul := matrix.mul R, mul_assoc := λ A B C, eq.symm $ @matrix.mul_assoc R _ n n n n A B C, mul_one := @matrix.mul_one R _ n , one := matrix.identity_matrix R, one_mul := @matrix.one_mul R _ n , left_distrib := @matrix.left_distrib R _ n n n, right_distrib := @matrix.right_distrib R _ n n n } end matrix namespace M_module def smul_M {F : Type} {n m : ℕ} [ring F] (a : F) (M : matrix F n m) : matrix F n m := λ I, λ J, a * (M I J) instance (F : Type) [ring F] (n m : ℕ) : has_scalar F (matrix F n m) := { smul := smul_M } theorem smul_add' {F : Type} {n m : ℕ} [ring F] (s : F) (m1 m2 : matrix F n m) : smul_M s (matrix.add F m1 m2) = matrix.add F (smul_M s m1) (smul_M s m2) := begin unfold smul_M, funext, unfold matrix.add, rw [mul_add], end theorem add_smul' {F : Type} {n m : ℕ} [ring F] (s t : F) (M : matrix F n m) : smul_M (s + t) M = (smul_M s M) + (smul_M t M) := begin unfold smul_M, simp only [add_mul], funext, congr, end theorem mul_smul' {F : Type} {n m : ℕ} [ring F] (s t : F) (M : matrix F n m) : smul_M (s * t) M = smul_M s (smul_M t M) := begin unfold smul_M, funext, rw [mul_assoc], end theorem one_smul' {F : Type} {n m : ℕ} [ring F] (M : matrix F n m) : smul_M (1 : F) M = M := begin unfold smul_M, funext, rw [one_mul], end instance {R : Type} {n m : ℕ} [ring R] : module R (matrix R n m) := { smul_add := smul_add', add_smul := add_smul', mul_smul := mul_smul', one_smul := one_smul', } end M_module /- dec_trivial finset.range I.val -/
8958fa2348da8953c174c49537d1d8f7571327b6
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/13_More_Tactics.org.10.lean
5d8b9b818eb8a1c8e507f71f3dfda8bfd8ae861d
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
93
lean
import standard example (p q : Prop) (Hq : q) : p ∨ q := begin constructor; assumption end
94720ad360ba398f0bd6eaf51d638f36c4583d50
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/group/arithmetic.lean
9fd1d770b65aad4d73f6efe0ac449aa034992756
[ "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
33,098
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.measure.ae_measurable /-! # Typeclasses for measurability of operations In this file we define classes `has_measurable_mul` etc and prove dot-style lemmas (`measurable.mul`, `ae_measurable.mul` etc). For binary operations we define two typeclasses: - `has_measurable_mul` says that both left and right multiplication are measurable; - `has_measurable_mul₂` says that `λ p : α × α, p.1 * p.2` is measurable, and similarly for other binary operations. The reason for introducing these classes is that in case of topological space `α` equipped with the Borel `σ`-algebra, instances for `has_measurable_mul₂` etc require `α` to have a second countable topology. We define separate classes for `has_measurable_div`/`has_measurable_sub` because on some types (e.g., `ℕ`, `ℝ≥0∞`) division and/or subtraction are not defined as `a * b⁻¹` / `a + (-b)`. For instances relating, e.g., `has_continuous_mul` to `has_measurable_mul` see file `measure_theory.borel_space`. ## Implementation notes For the heuristics of `@[to_additive]` it is important that the type with a multiplication (or another multiplicative operations) is the first (implicit) argument of all declarations. ## Tags measurable function, arithmetic operator ## Todo * Uniformize the treatment of `pow` and `smul`. * Use `@[to_additive]` to send `has_measurable_pow` to `has_measurable_smul₂`. * This might require changing the definition (swapping the arguments in the function that is in the conclusion of `measurable_smul`.) -/ universes u v open_locale big_operators pointwise measure_theory open measure_theory /-! ### Binary operations: `(+)`, `(*)`, `(-)`, `(/)` -/ /-- We say that a type `has_measurable_add` if `((+) c)` and `(+ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (+)` see `has_measurable_add₂`. -/ class has_measurable_add (M : Type*) [measurable_space M] [has_add M] : Prop := (measurable_const_add : ∀ c : M, measurable ((+) c)) (measurable_add_const : ∀ c : M, measurable (+ c)) export has_measurable_add (measurable_const_add measurable_add_const) /-- We say that a type `has_measurable_add` if `uncurry (+)` is a measurable functions. For a typeclass assuming measurability of `((+) c)` and `(+ c)` see `has_measurable_add`. -/ class has_measurable_add₂ (M : Type*) [measurable_space M] [has_add M] : Prop := (measurable_add : measurable (λ p : M × M, p.1 + p.2)) export has_measurable_add₂ (measurable_add) has_measurable_add (measurable_const_add measurable_add_const) /-- We say that a type `has_measurable_mul` if `((*) c)` and `(* c)` are measurable functions. For a typeclass assuming measurability of `uncurry (*)` see `has_measurable_mul₂`. -/ @[to_additive] class has_measurable_mul (M : Type*) [measurable_space M] [has_mul M] : Prop := (measurable_const_mul : ∀ c : M, measurable ((*) c)) (measurable_mul_const : ∀ c : M, measurable (* c)) export has_measurable_mul (measurable_const_mul measurable_mul_const) /-- We say that a type `has_measurable_mul` if `uncurry (*)` is a measurable functions. For a typeclass assuming measurability of `((*) c)` and `(* c)` see `has_measurable_mul`. -/ @[to_additive has_measurable_add₂] class has_measurable_mul₂ (M : Type*) [measurable_space M] [has_mul M] : Prop := (measurable_mul : measurable (λ p : M × M, p.1 * p.2)) export has_measurable_mul₂ (measurable_mul) section mul variables {M α : Type*} [measurable_space M] [has_mul M] {m : measurable_space α} {f g : α → M} {μ : measure α} include m @[measurability, to_additive] lemma measurable.const_mul [has_measurable_mul M] (hf : measurable f) (c : M) : measurable (λ x, c * f x) := (measurable_const_mul c).comp hf @[measurability, to_additive] lemma ae_measurable.const_mul [has_measurable_mul M] (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c * f x) μ := (has_measurable_mul.measurable_const_mul c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.mul_const [has_measurable_mul M] (hf : measurable f) (c : M) : measurable (λ x, f x * c) := (measurable_mul_const c).comp hf @[measurability, to_additive] lemma ae_measurable.mul_const [has_measurable_mul M] (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x * c) μ := (measurable_mul_const c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.mul' [has_measurable_mul₂ M] (hf : measurable f) (hg : measurable g) : measurable (f * g) := measurable_mul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma measurable.mul [has_measurable_mul₂ M] (hf : measurable f) (hg : measurable g) : measurable (λ a, f a * g a) := measurable_mul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.mul' [has_measurable_mul₂ M] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f * g) μ := measurable_mul.comp_ae_measurable (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.mul [has_measurable_mul₂ M] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a * g a) μ := measurable_mul.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100, to_additive] instance has_measurable_mul₂.to_has_measurable_mul [has_measurable_mul₂ M] : has_measurable_mul M := ⟨λ c, measurable_const.mul measurable_id, λ c, measurable_id.mul measurable_const⟩ @[to_additive] instance pi.has_measurable_mul {ι : Type*} {α : ι → Type*} [∀ i, has_mul (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_mul (α i)] : has_measurable_mul (Π i, α i) := ⟨λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).const_mul _, λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).mul_const _⟩ @[to_additive pi.has_measurable_add₂] instance pi.has_measurable_mul₂ {ι : Type*} {α : ι → Type*} [∀ i, has_mul (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_mul₂ (α i)] : has_measurable_mul₂ (Π i, α i) := ⟨measurable_pi_iff.mpr $ λ i, measurable_fst.eval.mul measurable_snd.eval⟩ attribute [measurability] measurable.add' measurable.add ae_measurable.add ae_measurable.add' measurable.const_add ae_measurable.const_add measurable.add_const ae_measurable.add_const end mul /-- A version of `measurable_div_const` that assumes `has_measurable_mul` instead of `has_measurable_div`. This can be nice to avoid unnecessary type-class assumptions. -/ @[to_additive /-" A version of `measurable_sub_const` that assumes `has_measurable_add` instead of `has_measurable_sub`. This can be nice to avoid unnecessary type-class assumptions. "-/] lemma measurable_div_const' {G : Type*} [div_inv_monoid G] [measurable_space G] [has_measurable_mul G] (g : G) : measurable (λ h, h / g) := by simp_rw [div_eq_mul_inv, measurable_mul_const] /-- This class assumes that the map `β × γ → β` given by `(x, y) ↦ x ^ y` is measurable. -/ class has_measurable_pow (β γ : Type*) [measurable_space β] [measurable_space γ] [has_pow β γ] := (measurable_pow : measurable (λ p : β × γ, p.1 ^ p.2)) export has_measurable_pow (measurable_pow) /-- `monoid.has_pow` is measurable. -/ instance monoid.has_measurable_pow (M : Type*) [monoid M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_pow M ℕ := ⟨measurable_from_prod_encodable $ λ n, begin induction n with n ih, { simp only [pow_zero, ←pi.one_def, measurable_one] }, { simp only [pow_succ], exact measurable_id.mul ih } end⟩ section pow variables {β γ α : Type*} [measurable_space β] [measurable_space γ] [has_pow β γ] [has_measurable_pow β γ] {m : measurable_space α} {μ : measure α} {f : α → β} {g : α → γ} include m @[measurability] lemma measurable.pow (hf : measurable f) (hg : measurable g) : measurable (λ x, f x ^ g x) := measurable_pow.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.pow (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x ^ g x) μ := measurable_pow.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma measurable.pow_const (hf : measurable f) (c : γ) : measurable (λ x, f x ^ c) := hf.pow measurable_const @[measurability] lemma ae_measurable.pow_const (hf : ae_measurable f μ) (c : γ) : ae_measurable (λ x, f x ^ c) μ := hf.pow ae_measurable_const @[measurability] lemma measurable.const_pow (hg : measurable g) (c : β) : measurable (λ x, c ^ g x) := measurable_const.pow hg @[measurability] lemma ae_measurable.const_pow (hg : ae_measurable g μ) (c : β) : ae_measurable (λ x, c ^ g x) μ := ae_measurable_const.pow hg omit m end pow /-- We say that a type `has_measurable_sub` if `(λ x, c - x)` and `(λ x, x - c)` are measurable functions. For a typeclass assuming measurability of `uncurry (-)` see `has_measurable_sub₂`. -/ class has_measurable_sub (G : Type*) [measurable_space G] [has_sub G] : Prop := (measurable_const_sub : ∀ c : G, measurable (λ x, c - x)) (measurable_sub_const : ∀ c : G, measurable (λ x, x - c)) export has_measurable_sub (measurable_const_sub measurable_sub_const) /-- We say that a type `has_measurable_sub` if `uncurry (-)` is a measurable functions. For a typeclass assuming measurability of `((-) c)` and `(- c)` see `has_measurable_sub`. -/ class has_measurable_sub₂ (G : Type*) [measurable_space G] [has_sub G] : Prop := (measurable_sub : measurable (λ p : G × G, p.1 - p.2)) export has_measurable_sub₂ (measurable_sub) /-- We say that a type `has_measurable_div` if `((/) c)` and `(/ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (/)` see `has_measurable_div₂`. -/ @[to_additive] class has_measurable_div (G₀: Type*) [measurable_space G₀] [has_div G₀] : Prop := (measurable_const_div : ∀ c : G₀, measurable ((/) c)) (measurable_div_const : ∀ c : G₀, measurable (/ c)) export has_measurable_div (measurable_const_div measurable_div_const) /-- We say that a type `has_measurable_div` if `uncurry (/)` is a measurable functions. For a typeclass assuming measurability of `((/) c)` and `(/ c)` see `has_measurable_div`. -/ @[to_additive has_measurable_sub₂] class has_measurable_div₂ (G₀: Type*) [measurable_space G₀] [has_div G₀] : Prop := (measurable_div : measurable (λ p : G₀× G₀, p.1 / p.2)) export has_measurable_div₂ (measurable_div) section div variables {G α : Type*} [measurable_space G] [has_div G] {m : measurable_space α} {f g : α → G} {μ : measure α} include m @[measurability, to_additive] lemma measurable.const_div [has_measurable_div G] (hf : measurable f) (c : G) : measurable (λ x, c / f x) := (has_measurable_div.measurable_const_div c).comp hf @[measurability, to_additive] lemma ae_measurable.const_div [has_measurable_div G] (hf : ae_measurable f μ) (c : G) : ae_measurable (λ x, c / f x) μ := (has_measurable_div.measurable_const_div c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.div_const [has_measurable_div G] (hf : measurable f) (c : G) : measurable (λ x, f x / c) := (has_measurable_div.measurable_div_const c).comp hf @[measurability, to_additive] lemma ae_measurable.div_const [has_measurable_div G] (hf : ae_measurable f μ) (c : G) : ae_measurable (λ x, f x / c) μ := (has_measurable_div.measurable_div_const c).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.div' [has_measurable_div₂ G] (hf : measurable f) (hg : measurable g) : measurable (f / g) := measurable_div.comp (hf.prod_mk hg) @[measurability, to_additive] lemma measurable.div [has_measurable_div₂ G] (hf : measurable f) (hg : measurable g) : measurable (λ a, f a / g a) := measurable_div.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.div' [has_measurable_div₂ G] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f / g) μ := measurable_div.comp_ae_measurable (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.div [has_measurable_div₂ G] (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a / g a) μ := measurable_div.comp_ae_measurable (hf.prod_mk hg) attribute [measurability] measurable.sub measurable.sub' ae_measurable.sub ae_measurable.sub' measurable.const_sub ae_measurable.const_sub measurable.sub_const ae_measurable.sub_const omit m @[priority 100, to_additive] instance has_measurable_div₂.to_has_measurable_div [has_measurable_div₂ G] : has_measurable_div G := ⟨λ c, measurable_const.div measurable_id, λ c, measurable_id.div measurable_const⟩ @[to_additive] instance pi.has_measurable_div {ι : Type*} {α : ι → Type*} [∀ i, has_div (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_div (α i)] : has_measurable_div (Π i, α i) := ⟨λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).const_div _, λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).div_const _⟩ @[to_additive pi.has_measurable_sub₂] instance pi.has_measurable_div₂ {ι : Type*} {α : ι → Type*} [∀ i, has_div (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_div₂ (α i)] : has_measurable_div₂ (Π i, α i) := ⟨measurable_pi_iff.mpr $ λ i, measurable_fst.eval.div measurable_snd.eval⟩ @[measurability] lemma measurable_set_eq_fun {m : measurable_space α} {E} [measurable_space E] [add_group E] [measurable_singleton_class E] [has_measurable_sub₂ E] {f g : α → E} (hf : measurable f) (hg : measurable g) : measurable_set {x | f x = g x} := begin suffices h_set_eq : {x : α | f x = g x} = {x | (f-g) x = (0 : E)}, { rw h_set_eq, exact (hf.sub hg) measurable_set_eq, }, ext, simp_rw [set.mem_set_of_eq, pi.sub_apply, sub_eq_zero], end lemma measurable_set_eq_fun_of_encodable {m : measurable_space α} {E} [measurable_space E] [measurable_singleton_class E] [encodable E] {f g : α → E} (hf : measurable f) (hg : measurable g) : measurable_set {x | f x = g x} := begin have : {x | f x = g x} = ⋃ j, {x | f x = j} ∩ {x | g x = j}, { ext1 x, simp only [set.mem_set_of_eq, set.mem_Union, set.mem_inter_eq, exists_eq_right'], }, rw this, refine measurable_set.Union (λ j, measurable_set.inter _ _), { exact hf (measurable_set_singleton j), }, { exact hg (measurable_set_singleton j), }, end lemma ae_eq_trim_of_measurable {α E} {m m0 : measurable_space α} {μ : measure α} [measurable_space E] [add_group E] [measurable_singleton_class E] [has_measurable_sub₂ E] (hm : m ≤ m0) {f g : α → E} (hf : measurable[m] f) (hg : measurable[m] g) (hfg : f =ᵐ[μ] g) : f =ᶠ[@measure.ae α m (μ.trim hm)] g := begin rwa [filter.eventually_eq, ae_iff, trim_measurable_set_eq hm _], exact (@measurable_set.compl α _ m (@measurable_set_eq_fun α m E _ _ _ _ _ _ hf hg)), end end div /-- We say that a type `has_measurable_neg` if `x ↦ -x` is a measurable function. -/ class has_measurable_neg (G : Type*) [has_neg G] [measurable_space G] : Prop := (measurable_neg : measurable (has_neg.neg : G → G)) /-- We say that a type `has_measurable_inv` if `x ↦ x⁻¹` is a measurable function. -/ @[to_additive] class has_measurable_inv (G : Type*) [has_inv G] [measurable_space G] : Prop := (measurable_inv : measurable (has_inv.inv : G → G)) export has_measurable_inv (measurable_inv) has_measurable_neg (measurable_neg) @[priority 100, to_additive] instance has_measurable_div_of_mul_inv (G : Type*) [measurable_space G] [div_inv_monoid G] [has_measurable_mul G] [has_measurable_inv G] : has_measurable_div G := { measurable_const_div := λ c, by { convert (measurable_inv.const_mul c), ext1, apply div_eq_mul_inv }, measurable_div_const := λ c, by { convert (measurable_id.mul_const c⁻¹), ext1, apply div_eq_mul_inv } } section inv variables {G α : Type*} [has_inv G] [measurable_space G] [has_measurable_inv G] {m : measurable_space α} {f : α → G} {μ : measure α} include m @[measurability, to_additive] lemma measurable.inv (hf : measurable f) : measurable (λ x, (f x)⁻¹) := measurable_inv.comp hf @[measurability, to_additive] lemma ae_measurable.inv (hf : ae_measurable f μ) : ae_measurable (λ x, (f x)⁻¹) μ := measurable_inv.comp_ae_measurable hf attribute [measurability] measurable.neg ae_measurable.neg @[simp, to_additive] lemma measurable_inv_iff {G : Type*} [group G] [measurable_space G] [has_measurable_inv G] {f : α → G} : measurable (λ x, (f x)⁻¹) ↔ measurable f := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp, to_additive] lemma ae_measurable_inv_iff {G : Type*} [group G] [measurable_space G] [has_measurable_inv G] {f : α → G} : ae_measurable (λ x, (f x)⁻¹) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp] lemma measurable_inv_iff₀ {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [has_measurable_inv G₀] {f : α → G₀} : measurable (λ x, (f x)⁻¹) ↔ measurable f := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp] lemma ae_measurable_inv_iff₀ {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [has_measurable_inv G₀] {f : α → G₀} : ae_measurable (λ x, (f x)⁻¹) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ omit m @[to_additive] instance pi.has_measurable_inv {ι : Type*} {α : ι → Type*} [∀ i, has_inv (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_inv (α i)] : has_measurable_inv (Π i, α i) := ⟨measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).inv⟩ @[to_additive] lemma measurable_set.inv {s : set G} (hs : measurable_set s) : measurable_set s⁻¹ := measurable_inv hs end inv /-- `div_inv_monoid.has_pow` is measurable. -/ instance div_inv_monoid.has_measurable_zpow (G : Type u) [div_inv_monoid G] [measurable_space G] [has_measurable_mul₂ G] [has_measurable_inv G] : has_measurable_pow G ℤ := ⟨measurable_from_prod_encodable $ λ n, begin cases n with n n, { simp_rw zpow_of_nat, exact measurable_id.pow_const _ }, { simp_rw zpow_neg_succ_of_nat, exact (measurable_id.pow_const (n + 1)).inv } end⟩ @[priority 100, to_additive] instance has_measurable_div₂_of_mul_inv (G : Type*) [measurable_space G] [div_inv_monoid G] [has_measurable_mul₂ G] [has_measurable_inv G] : has_measurable_div₂ G := ⟨by { simp only [div_eq_mul_inv], exact measurable_fst.mul measurable_snd.inv }⟩ /-- We say that the action of `M` on `α` `has_measurable_vadd` if for each `c` the map `x ↦ c +ᵥ x` is a measurable function and for each `x` the map `c ↦ c +ᵥ x` is a measurable function. -/ class has_measurable_vadd (M α : Type*) [has_vadd M α] [measurable_space M] [measurable_space α] : Prop := (measurable_const_vadd : ∀ c : M, measurable ((+ᵥ) c : α → α)) (measurable_vadd_const : ∀ x : α, measurable (λ c : M, c +ᵥ x)) /-- We say that the action of `M` on `α` `has_measurable_smul` if for each `c` the map `x ↦ c • x` is a measurable function and for each `x` the map `c ↦ c • x` is a measurable function. -/ @[to_additive] class has_measurable_smul (M α : Type*) [has_smul M α] [measurable_space M] [measurable_space α] : Prop := (measurable_const_smul : ∀ c : M, measurable ((•) c : α → α)) (measurable_smul_const : ∀ x : α, measurable (λ c : M, c • x)) /-- We say that the action of `M` on `α` `has_measurable_vadd₂` if the map `(c, x) ↦ c +ᵥ x` is a measurable function. -/ class has_measurable_vadd₂ (M α : Type*) [has_vadd M α] [measurable_space M] [measurable_space α] : Prop := (measurable_vadd : measurable (function.uncurry (+ᵥ) : M × α → α)) /-- We say that the action of `M` on `α` `has_measurable_smul₂` if the map `(c, x) ↦ c • x` is a measurable function. -/ @[to_additive has_measurable_vadd₂] class has_measurable_smul₂ (M α : Type*) [has_smul M α] [measurable_space M] [measurable_space α] : Prop := (measurable_smul : measurable (function.uncurry (•) : M × α → α)) export has_measurable_smul (measurable_const_smul measurable_smul_const) export has_measurable_smul₂ (measurable_smul) export has_measurable_vadd (measurable_const_vadd measurable_vadd_const) export has_measurable_vadd₂ (measurable_vadd) @[to_additive] instance has_measurable_smul_of_mul (M : Type*) [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_smul M M := ⟨measurable_id.const_mul, measurable_id.mul_const⟩ @[to_additive] instance has_measurable_smul₂_of_mul (M : Type*) [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_smul₂ M M := ⟨measurable_mul⟩ @[to_additive] instance submonoid.has_measurable_smul {M α} [measurable_space M] [measurable_space α] [monoid M] [mul_action M α] [has_measurable_smul M α] (s : submonoid M) : has_measurable_smul s α := ⟨λ c, by simpa only using measurable_const_smul (c : M), λ x, (measurable_smul_const x : measurable (λ c : M, c • x)).comp measurable_subtype_coe⟩ @[to_additive] instance subgroup.has_measurable_smul {G α} [measurable_space G] [measurable_space α] [group G] [mul_action G α] [has_measurable_smul G α] (s : subgroup G) : has_measurable_smul s α := s.to_submonoid.has_measurable_smul section smul variables {M β α : Type*} [measurable_space M] [measurable_space β] [has_smul M β] {m : measurable_space α} {f : α → M} {g : α → β} include m @[measurability, to_additive] lemma measurable.smul [has_measurable_smul₂ M β] (hf : measurable f) (hg : measurable g) : measurable (λ x, f x • g x) := measurable_smul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.smul [has_measurable_smul₂ M β] {μ : measure α} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x • g x) μ := has_measurable_smul₂.measurable_smul.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100, to_additive] instance has_measurable_smul₂.to_has_measurable_smul [has_measurable_smul₂ M β] : has_measurable_smul M β := ⟨λ c, measurable_const.smul measurable_id, λ y, measurable_id.smul measurable_const⟩ include m variables [has_measurable_smul M β] {μ : measure α} @[measurability, to_additive] lemma measurable.smul_const (hf : measurable f) (y : β) : measurable (λ x, f x • y) := (has_measurable_smul.measurable_smul_const y).comp hf @[measurability, to_additive] lemma ae_measurable.smul_const (hf : ae_measurable f μ) (y : β) : ae_measurable (λ x, f x • y) μ := (has_measurable_smul.measurable_smul_const y).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.const_smul' (hg : measurable g) (c : M) : measurable (λ x, c • g x) := (has_measurable_smul.measurable_const_smul c).comp hg @[measurability, to_additive] lemma measurable.const_smul (hg : measurable g) (c : M) : measurable (c • g) := hg.const_smul' c @[measurability, to_additive] lemma ae_measurable.const_smul' (hg : ae_measurable g μ) (c : M) : ae_measurable (λ x, c • g x) μ := (has_measurable_smul.measurable_const_smul c).comp_ae_measurable hg @[measurability, to_additive] lemma ae_measurable.const_smul (hf : ae_measurable g μ) (c : M) : ae_measurable (c • g) μ := hf.const_smul' c omit m @[to_additive] instance pi.has_measurable_smul {ι : Type*} {α : ι → Type*} [∀ i, has_smul M (α i)] [∀ i, measurable_space (α i)] [∀ i, has_measurable_smul M (α i)] : has_measurable_smul M (Π i, α i) := ⟨λ g, measurable_pi_iff.mpr $ λ i, (measurable_pi_apply i).const_smul _, λ g, measurable_pi_iff.mpr $ λ i, measurable_smul_const _⟩ /-- `add_monoid.has_smul_nat` is measurable. -/ instance add_monoid.has_measurable_smul_nat₂ (M : Type*) [add_monoid M] [measurable_space M] [has_measurable_add₂ M] : has_measurable_smul₂ ℕ M := ⟨begin suffices : measurable (λ p : M × ℕ, p.2 • p.1), { apply this.comp measurable_swap, }, refine measurable_from_prod_encodable (λ n, _), induction n with n ih, { simp only [zero_smul, ←pi.zero_def, measurable_zero] }, { simp only [succ_nsmul], exact measurable_id.add ih } end⟩ /-- `sub_neg_monoid.has_smul_int` is measurable. -/ instance sub_neg_monoid.has_measurable_smul_int₂ (M : Type*) [sub_neg_monoid M] [measurable_space M] [has_measurable_add₂ M] [has_measurable_neg M] : has_measurable_smul₂ ℤ M := ⟨begin suffices : measurable (λ p : M × ℤ, p.2 • p.1), { apply this.comp measurable_swap, }, refine measurable_from_prod_encodable (λ n, _), induction n with n n ih, { simp only [of_nat_zsmul], exact measurable_const_smul _, }, { simp only [zsmul_neg_succ_of_nat], exact (measurable_const_smul _).neg } end⟩ end smul section mul_action variables {M β α : Type*} [measurable_space M] [measurable_space β] [monoid M] [mul_action M β] [has_measurable_smul M β] [measurable_space α] {f : α → β} {μ : measure α} variables {G : Type*} [group G] [measurable_space G] [mul_action G β] [has_measurable_smul G β] @[to_additive] lemma measurable_const_smul_iff (c : G) : measurable (λ x, c • f x) ↔ measurable f := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ @[to_additive] lemma ae_measurable_const_smul_iff (c : G) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ @[to_additive] instance : measurable_space Mˣ := measurable_space.comap (coe : Mˣ → M) ‹_› @[to_additive] instance units.has_measurable_smul : has_measurable_smul Mˣ β := { measurable_const_smul := λ c, (measurable_const_smul (c : M) : _), measurable_smul_const := λ x, (measurable_smul_const x : measurable (λ c : M, c • x)).comp measurable_space.le_map_comap, } @[to_additive] lemma is_unit.measurable_const_smul_iff {c : M} (hc : is_unit c) : measurable (λ x, c • f x) ↔ measurable f := let ⟨u, hu⟩ := hc in hu ▸ measurable_const_smul_iff u @[to_additive] lemma is_unit.ae_measurable_const_smul_iff {c : M} (hc : is_unit c) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := let ⟨u, hu⟩ := hc in hu ▸ ae_measurable_const_smul_iff u variables {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [mul_action G₀ β] [has_measurable_smul G₀ β] lemma measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : measurable (λ x, c • f x) ↔ measurable f := (is_unit.mk0 c hc).measurable_const_smul_iff lemma ae_measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := (is_unit.mk0 c hc).ae_measurable_const_smul_iff end mul_action /-! ### Opposite monoid -/ section opposite open mul_opposite @[to_additive] instance {α : Type*} [h : measurable_space α] : measurable_space αᵐᵒᵖ := measurable_space.map op h @[to_additive] lemma measurable_mul_op {α : Type*} [measurable_space α] : measurable (op : α → αᵐᵒᵖ) := λ s, id @[to_additive] lemma measurable_mul_unop {α : Type*} [measurable_space α] : measurable (unop : αᵐᵒᵖ → α) := λ s, id @[to_additive] instance {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_mul Mᵐᵒᵖ := ⟨λ c, measurable_mul_op.comp (measurable_mul_unop.mul_const _), λ c, measurable_mul_op.comp (measurable_mul_unop.const_mul _)⟩ @[to_additive] instance {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_mul₂ Mᵐᵒᵖ := ⟨measurable_mul_op.comp ((measurable_mul_unop.comp measurable_snd).mul (measurable_mul_unop.comp measurable_fst))⟩ /-- If a scalar is central, then its right action is measurable when its left action is. -/ instance has_measurable_smul.op {M α} [measurable_space M] [measurable_space α] [has_smul M α] [has_smul Mᵐᵒᵖ α] [is_central_scalar M α] [has_measurable_smul M α] : has_measurable_smul Mᵐᵒᵖ α := ⟨ mul_opposite.rec $ λ c, show measurable (λ x, op c • x), by simpa only [op_smul_eq_smul] using measurable_const_smul c, λ x, show measurable (λ c, op (unop c) • x), by simpa only [op_smul_eq_smul] using (measurable_smul_const x).comp measurable_mul_unop⟩ /-- If a scalar is central, then its right action is measurable when its left action is. -/ instance has_measurable_smul₂.op {M α} [measurable_space M] [measurable_space α] [has_smul M α] [has_smul Mᵐᵒᵖ α] [is_central_scalar M α] [has_measurable_smul₂ M α] : has_measurable_smul₂ Mᵐᵒᵖ α := ⟨show measurable (λ x : Mᵐᵒᵖ × α, op (unop x.1) • x.2), begin simp_rw op_smul_eq_smul, refine (measurable_mul_unop.comp measurable_fst).smul measurable_snd, end⟩ @[to_additive] instance has_measurable_smul_opposite_of_mul {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_smul Mᵐᵒᵖ M := ⟨λ c, measurable_mul_const (unop c), λ x, measurable_mul_unop.const_mul x⟩ @[to_additive] instance has_measurable_smul₂_opposite_of_mul {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_smul₂ Mᵐᵒᵖ M := ⟨measurable_snd.mul (measurable_mul_unop.comp measurable_fst)⟩ end opposite /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M α : Type*} [monoid M] [measurable_space M] [has_measurable_mul₂ M] {m : measurable_space α} {μ : measure α} include m @[measurability, to_additive] lemma list.measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable l.prod := begin induction l with f l ihl, { exact measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[measurability, to_additive] lemma list.ae_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable l.prod μ := begin induction l with f l ihl, { exact ae_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[measurability, to_additive] lemma list.measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable (λ x, (l.map (λ f : α → M, f x)).prod) := by simpa only [← pi.list_prod_apply] using l.measurable_prod' hl @[measurability, to_additive] lemma list.ae_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable (λ x, (l.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.list_prod_apply] using l.ae_measurable_prod' hl omit m end monoid section comm_monoid variables {M ι α : Type*} [comm_monoid M] [measurable_space M] [has_measurable_mul₂ M] {m : measurable_space α} {μ : measure α} {f : ι → α → M} include m @[measurability, to_additive] lemma multiset.measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable l.prod := by { rcases l with ⟨l⟩, simpa using l.measurable_prod' (by simpa using hl) } @[measurability, to_additive] lemma multiset.ae_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable l.prod μ := by { rcases l with ⟨l⟩, simpa using l.ae_measurable_prod' (by simpa using hl) } @[measurability, to_additive] lemma multiset.measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, measurable f) : measurable (λ x, (s.map (λ f : α → M, f x)).prod) := by simpa only [← pi.multiset_prod_apply] using s.measurable_prod' hs @[measurability, to_additive] lemma multiset.ae_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, ae_measurable f μ) : ae_measurable (λ x, (s.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.multiset_prod_apply] using s.ae_measurable_prod' hs @[measurability, to_additive] lemma finset.measurable_prod' (s : finset ι) (hf : ∀i ∈ s, measurable (f i)) : measurable (∏ i in s, f i) := finset.prod_induction _ _ (λ _ _, measurable.mul) (@measurable_one M _ _ _ _) hf @[measurability, to_additive] lemma finset.measurable_prod (s : finset ι) (hf : ∀i ∈ s, measurable (f i)) : measurable (λ a, ∏ i in s, f i a) := by simpa only [← finset.prod_apply] using s.measurable_prod' hf @[measurability, to_additive] lemma finset.ae_measurable_prod' (s : finset ι) (hf : ∀i ∈ s, ae_measurable (f i) μ) : ae_measurable (∏ i in s, f i) μ := multiset.ae_measurable_prod' _ $ λ g hg, let ⟨i, hi, hg⟩ := multiset.mem_map.1 hg in (hg ▸ hf _ hi) @[measurability, to_additive] lemma finset.ae_measurable_prod (s : finset ι) (hf : ∀i ∈ s, ae_measurable (f i) μ) : ae_measurable (λ a, ∏ i in s, f i a) μ := by simpa only [← finset.prod_apply] using s.ae_measurable_prod' hf omit m end comm_monoid
883195365247c4c4f18eeb7b86dbc62ae74d23b8
e898bfefd5cb60a60220830c5eba68cab8d02c79
/uexp/src/uexp/rules/commonexp.lean
1fbea54df0473aafbbdfac10f4c2f0a2a0715b6e
[ "BSD-2-Clause" ]
permissive
kkpapa/Cosette
9ed09e2dc4c1ecdef815c30b5501f64a7383a2ce
fda8fdbbf0de6c1be9b4104b87bbb06cede46329
refs/heads/master
1,584,573,128,049
1,526,370,422,000
1,526,370,422,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,166
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..ucongr import ..TDP import ..cosette_tactics -- NOTE: this one cannot solved by ucongr, need to be revisited open Expr open Proj open Pred open SQL open binary_operators theorem rule : forall (Γ s1 : Schema) (a : relation s1) (x : Column datatypes.int s1) (y : Column datatypes.int s1), denoteSQL (SELECT1 (e2p (binaryExpr add_ (uvariable (right⋅x)) (uvariable (right⋅x)))) FROM1 (table a WHERE (equal (uvariable (right⋅x)) (uvariable (right⋅y)))) : SQL Γ _) = denoteSQL (SELECT1 (e2p (binaryExpr add_ (uvariable (right⋅x)) (uvariable (right⋅y)))) FROM1 (table a WHERE (equal (uvariable (right⋅x)) (uvariable (right⋅y)))) : SQL Γ _) := begin intros, funext, unfold_all_denotations, simp, congr, funext, apply congr_arg _, have eq_subst_l' : ∀ {s: Schema} (t₁ t₂: Tuple s) (R: Tuple s → Tuple s) (e : Tuple s), (t₁ ≃ t₂) * (e ≃ R t₁) = (t₁ ≃ t₂) * (e ≃ R t₂), { intros, rw eq_subst_l }, rewrite eq_subst_l', end
9e8c1210e43a807b4b2cffc56b5ca0d5732e061e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/probability/probability_mass_function/constructions.lean
4d3a42989695dc701da964c94998dd776c3910af
[ "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
9,769
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, Devon Tuma -/ import probability.probability_mass_function.monad /-! # Specific Constructions of Probability Mass Functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file gives a number of different `pmf` constructions for common probability distributions. `map` and `seq` allow pushing a `pmf α` along a function `f : α → β` (or distribution of functions `f : pmf (α → β)`) to get a `pmf β` `of_finset` and `of_fintype` simplify the construction of a `pmf α` from a function `f : α → ℝ≥0∞`, by allowing the "sum equals 1" constraint to be in terms of `finset.sum` instead of `tsum`. `normalize` constructs a `pmf α` by normalizing a function `f : α → ℝ≥0∞` by its sum, and `filter` uses this to filter the support of a `pmf` and re-normalize the new distribution. `bernoulli` represents the bernoulli distribution on `bool` -/ namespace pmf noncomputable theory variables {α β γ : Type*} open_locale classical big_operators nnreal ennreal section map /-- The functorial action of a function on a `pmf`. -/ def map (f : α → β) (p : pmf α) : pmf β := bind p (pure ∘ f) variables (f : α → β) (p : pmf α) (b : β) lemma monad_map_eq_map {α β : Type*} (f : α → β) (p : pmf α) : f <$> p = p.map f := rfl @[simp] lemma map_apply : (map f p) b = ∑' a, if b = f a then p a else 0 := by simp [map] @[simp] lemma support_map : (map f p).support = f '' p.support := set.ext (λ b, by simp [map, @eq_comm β b]) lemma mem_support_map_iff : b ∈ (map f p).support ↔ ∃ a ∈ p.support, f a = b := by simp lemma bind_pure_comp : bind p (pure ∘ f) = map f p := rfl lemma map_id : map id p = p := bind_pure _ lemma map_comp (g : β → γ) : (p.map f).map g = p.map (g ∘ f) := by simp [map] lemma pure_map (a : α) : (pure a).map f = pure (f a) := pure_bind _ _ lemma map_bind (q : α → pmf β) (f : β → γ) : (p.bind q).map f = p.bind (λ a, (q a).map f) := bind_bind _ _ _ @[simp] lemma bind_map (p : pmf α) (f : α → β) (q : β → pmf γ) : (p.map f).bind q = p.bind (q ∘ f) := (bind_bind _ _ _).trans (congr_arg _ (funext (λ a, pure_bind _ _))) @[simp] lemma map_const : p.map (function.const α b) = pure b := by simp only [map, bind_const, function.comp_const] section measure variable (s : set β) @[simp] lemma to_outer_measure_map_apply : (p.map f).to_outer_measure s = p.to_outer_measure (f ⁻¹' s) := by simp [map, set.indicator, to_outer_measure_apply p (f ⁻¹' s)] @[simp] lemma to_measure_map_apply [measurable_space α] [measurable_space β] (hf : measurable f) (hs : measurable_set s) : (p.map f).to_measure s = p.to_measure (f ⁻¹' s) := begin rw [to_measure_apply_eq_to_outer_measure_apply _ s hs, to_measure_apply_eq_to_outer_measure_apply _ (f ⁻¹' s) (measurable_set_preimage hf hs)], exact to_outer_measure_map_apply f p s, end end measure end map section seq /-- The monadic sequencing operation for `pmf`. -/ def seq (q : pmf (α → β)) (p : pmf α) : pmf β := q.bind (λ m, p.bind $ λ a, pure (m a)) variables (q : pmf (α → β)) (p : pmf α) (b : β) lemma monad_seq_eq_seq {α β : Type*} (q : pmf (α → β)) (p : pmf α) : q <*> p = q.seq p := rfl @[simp] lemma seq_apply : (seq q p) b = ∑' (f : α → β) (a : α), if b = f a then q f * p a else 0 := begin simp only [seq, mul_boole, bind_apply, pure_apply], refine tsum_congr (λ f, (ennreal.tsum_mul_left).symm.trans (tsum_congr (λ a, _))), simpa only [mul_zero] using mul_ite (b = f a) (q f) (p a) 0 end @[simp] lemma support_seq : (seq q p).support = ⋃ f ∈ q.support, f '' p.support := set.ext (λ b, by simp [-mem_support_iff, seq, @eq_comm β b]) lemma mem_support_seq_iff : b ∈ (seq q p).support ↔ ∃ (f ∈ q.support), b ∈ f '' p.support := by simp end seq instance : is_lawful_functor pmf := { map_const_eq := λ α β, rfl, id_map := λ α, bind_pure, comp_map := λ α β γ g h x, (map_comp _ _ _).symm } instance : is_lawful_monad pmf := { bind_pure_comp_eq_map := λ α β f x, rfl, bind_map_eq_seq := λ α β f x, rfl, pure_bind := λ α β, pure_bind, bind_assoc := λ α β γ, bind_bind } section of_finset /-- Given a finset `s` and a function `f : α → ℝ≥0∞` with sum `1` on `s`, such that `f a = 0` for `a ∉ s`, we get a `pmf` -/ def of_finset (f : α → ℝ≥0∞) (s : finset α) (h : ∑ a in s, f a = 1) (h' : ∀ a ∉ s, f a = 0) : pmf α := ⟨f, h ▸ has_sum_sum_of_ne_finset_zero h'⟩ variables {f : α → ℝ≥0∞} {s : finset α} (h : ∑ a in s, f a = 1) (h' : ∀ a ∉ s, f a = 0) @[simp] lemma of_finset_apply (a : α) : of_finset f s h h' a = f a := rfl @[simp] lemma support_of_finset : (of_finset f s h h').support = s ∩ (function.support f) := set.ext (λ a, by simpa [mem_support_iff] using mt (h' a)) lemma mem_support_of_finset_iff (a : α) : a ∈ (of_finset f s h h').support ↔ a ∈ s ∧ f a ≠ 0 := by simp lemma of_finset_apply_of_not_mem {a : α} (ha : a ∉ s) : of_finset f s h h' a = 0 := h' a ha section measure variable (t : set α) @[simp] lemma to_outer_measure_of_finset_apply : (of_finset f s h h').to_outer_measure t = ∑' x, t.indicator f x := to_outer_measure_apply (of_finset f s h h') t @[simp] lemma to_measure_of_finset_apply [measurable_space α] (ht : measurable_set t) : (of_finset f s h h').to_measure t = ∑' x, t.indicator f x := (to_measure_apply_eq_to_outer_measure_apply _ t ht).trans (to_outer_measure_of_finset_apply h h' t) end measure end of_finset section of_fintype /-- Given a finite type `α` and a function `f : α → ℝ≥0∞` with sum 1, we get a `pmf`. -/ def of_fintype [fintype α] (f : α → ℝ≥0∞) (h : ∑ a, f a = 1) : pmf α := of_finset f finset.univ h (λ a ha, absurd (finset.mem_univ a) ha) variables [fintype α] {f : α → ℝ≥0∞} (h : ∑ a, f a = 1) @[simp] lemma of_fintype_apply (a : α) : of_fintype f h a = f a := rfl @[simp] lemma support_of_fintype : (of_fintype f h).support = function.support f := rfl lemma mem_support_of_fintype_iff (a : α) : a ∈ (of_fintype f h).support ↔ f a ≠ 0 := iff.rfl section measure variable (s : set α) @[simp] lemma to_outer_measure_of_fintype_apply : (of_fintype f h).to_outer_measure s = ∑' x, s.indicator f x := to_outer_measure_apply (of_fintype f h) s @[simp] lemma to_measure_of_fintype_apply [measurable_space α] (hs : measurable_set s) : (of_fintype f h).to_measure s = ∑' x, s.indicator f x := (to_measure_apply_eq_to_outer_measure_apply _ s hs).trans (to_outer_measure_of_fintype_apply h s) end measure end of_fintype section normalize /-- Given a `f` with non-zero and non-infinite sum, get a `pmf` by normalizing `f` by its `tsum` -/ def normalize (f : α → ℝ≥0∞) (hf0 : tsum f ≠ 0) (hf : tsum f ≠ ∞) : pmf α := ⟨λ a, f a * (∑' x, f x)⁻¹, ennreal.summable.has_sum_iff.2 (ennreal.tsum_mul_right.trans (ennreal.mul_inv_cancel hf0 hf))⟩ variables {f : α → ℝ≥0∞} (hf0 : tsum f ≠ 0) (hf : tsum f ≠ ∞) @[simp] lemma normalize_apply (a : α) : (normalize f hf0 hf) a = f a * (∑' x, f x)⁻¹ := rfl @[simp] lemma support_normalize : (normalize f hf0 hf).support = function.support f := set.ext (λ a, by simp [hf, mem_support_iff]) lemma mem_support_normalize_iff (a : α) : a ∈ (normalize f hf0 hf).support ↔ f a ≠ 0 := by simp end normalize section filter /-- Create new `pmf` by filtering on a set with non-zero measure and normalizing -/ def filter (p : pmf α) (s : set α) (h : ∃ a ∈ s, a ∈ p.support) : pmf α := pmf.normalize (s.indicator p) (by simpa using h) (p.tsum_coe_indicator_ne_top s) variables {p : pmf α} {s : set α} (h : ∃ a ∈ s, a ∈ p.support) @[simp] lemma filter_apply (a : α) : (p.filter s h) a = (s.indicator p a) * (∑' a', (s.indicator p) a')⁻¹ := by rw [filter, normalize_apply] lemma filter_apply_eq_zero_of_not_mem {a : α} (ha : a ∉ s) : (p.filter s h) a = 0 := by rw [filter_apply, set.indicator_apply_eq_zero.mpr (λ ha', absurd ha' ha), zero_mul] lemma mem_support_filter_iff {a : α} : a ∈ (p.filter s h).support ↔ a ∈ s ∧ a ∈ p.support := (mem_support_normalize_iff _ _ _).trans set.indicator_apply_ne_zero @[simp] lemma support_filter : (p.filter s h).support = s ∩ p.support:= set.ext $ λ x, (mem_support_filter_iff _) lemma filter_apply_eq_zero_iff (a : α) : (p.filter s h) a = 0 ↔ a ∉ s ∨ a ∉ p.support := by erw [apply_eq_zero_iff, support_filter, set.mem_inter_iff, not_and_distrib] lemma filter_apply_ne_zero_iff (a : α) : (p.filter s h) a ≠ 0 ↔ a ∈ s ∧ a ∈ p.support := by rw [ne.def, filter_apply_eq_zero_iff, not_or_distrib, not_not, not_not] end filter section bernoulli /-- A `pmf` which assigns probability `p` to `tt` and `1 - p` to `ff`. -/ def bernoulli (p : ℝ≥0∞) (h : p ≤ 1) : pmf bool := of_fintype (λ b, cond b p (1 - p)) (by simp [h]) variables {p : ℝ≥0∞} (h : p ≤ 1) (b : bool) @[simp] lemma bernoulli_apply : bernoulli p h b = cond b p (1 - p) := rfl @[simp] lemma support_bernoulli : (bernoulli p h).support = {b | cond b (p ≠ 0) (p ≠ 1)} := begin refine set.ext (λ b, _), induction b, { simp_rw [mem_support_iff, bernoulli_apply, bool.cond_ff, ne.def, tsub_eq_zero_iff_le, not_le], exact ⟨ne_of_lt, lt_of_le_of_ne h⟩ }, { simp only [mem_support_iff, bernoulli_apply, bool.cond_tt, set.mem_set_of_eq], } end lemma mem_support_bernoulli_iff : b ∈ (bernoulli p h).support ↔ cond b (p ≠ 0) (p ≠ 1) := by simp end bernoulli end pmf
4c2f68466c5d9b04930f4dcbe4d175558517ef1d
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/number_theory/quadratic_reciprocity.lean
c09371ecd062ad86f8c072148cea5870c35760b1
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
25,029
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 import data.zmod.basic import data.nat.parity /-! # Quadratic reciprocity. This file contains results about quadratic residues modulo a prime number. The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the interpretations in terms of existence of square roots depending on the congruence mod 4, `exists_pow_two_eq_prime_iff_of_mod_four_eq_one`, and `exists_pow_two_eq_prime_iff_of_mod_four_eq_three`. Also proven are conditions for `-1` and `2` to be a square modulo a prime, `exists_pow_two_eq_neg_one_iff_mod_four_ne_three` and `exists_pow_two_eq_two_iff` ## Implementation notes The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma -/ open function finset nat finite_field zmod open_locale big_operators namespace zmod variables (p q : ℕ) [fact p.prime] [fact q.prime] /-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion_units (x : units (zmod p)) : (∃ y : units (zmod p), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, refine iff_of_true ⟨1, _⟩ _; apply subsingleton.elim }, obtain ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmod p)), obtain ⟨n, hn⟩ : x ∈ submonoid.powers g, { rw mem_powers_iff_mem_gpowers, apply hg }, split, { rintro ⟨y, rfl⟩, rw [← pow_mul, two_mul_odd_div_two hp_odd, units_pow_card_sub_one_eq_one], }, { subst x, assume h, have key : 2 * (p / 2) ∣ n * (p / 2), { rw [← pow_mul] at h, rw [two_mul_odd_div_two hp_odd, ← card_units, ← order_of_eq_card_of_forall_mem_gpowers hg], apply order_of_dvd_of_pow_eq_one h }, have : 0 < p / 2 := nat.div_pos (show fact (1 < p), by apply_instance) dec_trivial, obtain ⟨m, rfl⟩ := dvd_of_mul_dvd_mul_right this key, refine ⟨g ^ m, _⟩, rw [mul_comm, pow_mul], }, end /-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion {a : zmod p} (ha : a ≠ 0) : (∃ y : zmod p, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := begin apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)), simp only [units.ext_iff, pow_two, units.coe_mk0, units.coe_mul], split, { rintro ⟨y, hy⟩, exact ⟨y, hy⟩ }, { rintro ⟨y, rfl⟩, have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, }, refine ⟨units.mk0 y hy, _⟩, simp, } end lemma exists_pow_two_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmod p, y ^ 2 = -1) ↔ p % 4 ≠ 3 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, exact dec_trivial }, change fact (p % 2 = 1) at hp_odd, resetI, have neg_one_ne_zero : (-1 : zmod p) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, rw [euler_criterion p neg_one_ne_zero, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one (p / 2) with p_half_even p_half_odd, { rw [p_half_even, pow_zero, eq_self_iff_true, true_iff], contrapose! p_half_even with hp, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp], exact dec_trivial }, { rw [p_half_odd, pow_one, iff_false_intro (ne_neg_self p one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at p_half_odd, rw [_root_.fact, ← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp_odd, have hp : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp hp_odd p_half_odd, generalize : p % 4 = k, dec_trivial! } end lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, revert a ha, exact dec_trivial }, rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd], exact pow_card_sub_one_eq_one ha end /-- Wilson's Lemma: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : (nat.fact (p - 1) : zmod p) = -1 := begin refine calc (nat.fact (p - 1) : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_fact, prod_nat_cast] ... = (∏ x : units (zmod p), x) : _ ... = -1 : by rw [prod_hom _ (coe : units (zmod p) → zmod p), prod_univ_units_id_eq_neg_one, units.coe_neg, units.coe_one], have hp : 0 < p := nat.prime.pos ‹p.prime›, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [Ico.mem, ← 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 [Ico.mem, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, nat.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 (nat.prime.pos ‹p.prime›)] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_fact, wilsons_lemma] end end zmod /-- 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, nat.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.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, nat.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, Ico.mem.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 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 (Ico.mem.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg_eq_neg_mul_symm, 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 (Ico.mem.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_refl _)) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2).fact : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2).fact := calc (a ^ (p / 2) * (p / 2).fact : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_fact, prod_const, Ico.card, 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 [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).fact : 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_fact] private lemma gauss_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [hp2 : 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).fact : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.dvd_fact, not_le]; exact nat.div_lt_self hp.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [hp : 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, 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_cast_nat]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, sum_nat_cast, 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 [sum_nat_cast] ... = ((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, sum_nat_cast], } ... = _ : 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 [sum_nat_cast]) rfl private lemma eisenstein_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [hp2 : 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, sum_nat_cast, 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 $ by finish) (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. -/ private 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, Ico.mem, mem_product] at *; tauto) (nat.div_lt_self hp.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, le_zero_iff_eq, Ico.mem, 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, Ico.mem, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [Ico.card, nat.sub_zero, succ_sub_succ_eq_sub] end variables (p q : ℕ) [fact p.prime] [fact q.prime] namespace zmod /-- The Legendre symbol of `a` and `p` is an integer defined as * `0` if `a` is `0` modulo `p`; * `1` if `a ^ (p / 2)` is `1` modulo `p` (by `euler_criterion` this is equivalent to “`a` is a square modulo `p`”); * `-1` otherwise. -/ def legendre_sym (a p : ℕ) : ℤ := if (a : zmod p) = 0 then 0 else if (a : zmod p) ^ (p / 2) = 1 then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) [hp : fact p.prime] : (legendre_sym a p : zmod p) = (a ^ (p / 2)) := begin rw legendre_sym, by_cases ha : (a : zmod p) = 0, { simp only [if_pos, ha, zero_pow (nat.div_pos (hp.two_le) (succ_pos 1)), int.cast_zero] }, cases hp.eq_two_or_odd with hp2 hp_odd, { substI p, generalize : (a : (zmod 2)) = b, revert b, dec_trivial, }, { change fact (p % 2 = 1) at hp_odd, resetI, rw if_neg ha, have : (-1 : zmod p) ≠ 1, from (ne_neg_self p one_ne_zero).symm, cases pow_div_two_eq_neg_one_or_one p ha with h h, { rw [if_pos h, h, int.cast_one], }, { rw [h, if_neg this, int.cast_neg, int.cast_one], } } end lemma legendre_sym_eq_one_or_neg_one (a p : ℕ) (ha : (a : zmod p) ≠ 0) : legendre_sym a p = -1 ∨ legendre_sym a p = 1 := by unfold legendre_sym; split_ifs; simp only [*, eq_self_iff_true, or_true, true_or] at * lemma legendre_sym_eq_zero_iff (a p : ℕ) : legendre_sym a p = 0 ↔ (a : zmod p) = 0 := begin split, { classical, contrapose, assume ha, cases legendre_sym_eq_one_or_neg_one a p ha with h h, all_goals { rw h, norm_num } }, { assume ha, rw [legendre_sym, if_pos ha] } end /-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less than `p/2` such that `(a * x) % p > p / 2` -/ lemma gauss_lemma {a : ℕ} [hp1 : fact (p % 2 = 1)] (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := have (legendre_sym a p : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p), by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ p ha0]; simp, begin cases legendre_sym_eq_one_or_neg_one a p ha0; cases @neg_one_pow_eq_or ℤ _ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card; simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at * end lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = 1 ↔ (∃ b : zmod p, b ^ 2 = a) := begin rw [euler_criterion p ha0, legendre_sym, if_neg ha0], split_ifs, { simp only [h, eq_self_iff_true] }, finish -- this is quite slow. I'm actually surprised that it can close the goal at all! end lemma eisenstein_lemma [hp1 : fact (p % 2 = 1)] {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p := by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p ha0, neg_one_pow_eq_pow_mod_two, show _ = _, from eisenstein_lemma_aux₂ p ha1 ha0] theorem quadratic_reciprocity [hp1 : fact (p % 2 = 1)] [hq1 : fact (q % 2 = 1)] (hpq : p ≠ q) : legendre_sym p q * legendre_sym q p = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm, have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq, by rw [eisenstein_lemma q hp1 hpq0, eisenstein_lemma p hq1 hqp0, ← pow_add, sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm] -- move this instance fact_prime_two : fact (nat.prime 2) := nat.prime_two lemma legendre_sym_two [hp1 : fact (p % 2 = 1)] : legendre_sym 2 p = (-1) ^ (p / 4 + p / 2) := have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simpa using hp1), have hp22 : p / 2 / 2 = _ := div_eq_filter_card (show 0 < 2, from dec_trivial) (nat.div_le_self (p / 2) 2), have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp, have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x, from λ x hx, have h2xp : 2 * x < p, from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left (le_of_lt_succ $ by finish) dec_trivial ... < _ : by conv_rhs {rw [← mod_add_div p 2, add_comm, show p % 2 = 1, from hp1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)), from disjoint_filter.2 (λ x hx, by simp [hx2 _ hx, mul_comm]), have hunion : ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪ ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) = Ico 1 (p / 2).succ, begin rw [filter_union_right], conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]}, exact filter_congr (λ x hx, by simp [hx2 _ hx, lt_or_le, mul_comm]) end, begin rw [gauss_lemma p (prime_ne_zero p 2 hp2), neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _), rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add, ← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_pow_two_eq_two_iff [hp1 : fact (p % 2 = 1)] : (∃ a : zmod p, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 := have hp2 : ((2 : ℕ) : zmod p) ≠ 0, from prime_ne_zero p 2 (λ h, by simpa [h] using hp1), have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm, have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm, begin rw [show (2 : zmod p) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff p hp2, legendre_sym_two p, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial), even_add, even_div, even_div], have := nat.mod_lt p (show 0 < 8, from dec_trivial), resetI, rw _root_.fact at hp1, revert this hp1, erw [hpm4, hpm2], generalize hm : p % 8 = m, unfreezingI {clear_dependent p}, dec_trivial!, end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) [hq1 : fact (q % 2 = 1)] : (∃ a : zmod p, a ^ 2 = q) ↔ ∃ b : zmod q, b ^ 2 = p := if hpq : p = q then by substI hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from (dvd_iff_mod_eq_zero _ _).1 (dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin haveI hp_odd : fact (p % 2 = 1) := odd_of_mod_four_eq_one hp1, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hqp0, if_neg hpq0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction, end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmod p, a ^ 2 = q) ↔ ¬∃ b : zmod q, b ^ 2 = p := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin haveI hp_odd : fact (p % 2 = 1) := odd_of_mod_four_eq_three hp3, haveI hq_odd : fact (q % 2 = 1) := odd_of_mod_four_eq_three hq3, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hpq0, if_neg hqp0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction end end zmod
531eec0b33c46d5be16186ff05d16854d987bc79
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/sup_indep.lean
572799ddcd38e93f00f4eb526277bf9223a6de1e
[ "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
18,335
lean
/- Copyright (c) 2021 Aaron Anderson, Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Kevin Buzzard, Yaël Dillies, Eric Wieser -/ import data.finset.sigma import data.finset.pairwise import data.finset.powerset import data.fintype.basic /-! # Supremum independence > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we define supremum independence of indexed sets. An indexed family `f : ι → α` is sup-independent if, for all `a`, `f a` and the supremum of the rest are disjoint. ## Main definitions * `finset.sup_indep s f`: a family of elements `f` are supremum independent on the finite set `s`. * `complete_lattice.set_independent s`: a set of elements are supremum independent. * `complete_lattice.independent f`: a family of elements are supremum independent. ## Main statements * In a distributive lattice, supremum independence is equivalent to pairwise disjointness: * `finset.sup_indep_iff_pairwise_disjoint` * `complete_lattice.set_independent_iff_pairwise_disjoint` * `complete_lattice.independent_iff_pairwise_disjoint` * Otherwise, supremum independence is stronger than pairwise disjointness: * `finset.sup_indep.pairwise_disjoint` * `complete_lattice.set_independent.pairwise_disjoint` * `complete_lattice.independent.pairwise_disjoint` ## Implementation notes For the finite version, we avoid the "obvious" definition `∀ i ∈ s, disjoint (f i) ((s.erase i).sup f)` because `erase` would require decidable equality on `ι`. -/ variables {α β ι ι' : Type*} /-! ### On lattices with a bottom element, via `finset.sup` -/ namespace finset section lattice variables [lattice α] [order_bot α] /-- Supremum independence of finite sets. We avoid the "obvious" definition using `s.erase i` because `erase` would require decidable equality on `ι`. -/ def sup_indep (s : finset ι) (f : ι → α) : Prop := ∀ ⦃t⦄, t ⊆ s → ∀ ⦃i⦄, i ∈ s → i ∉ t → disjoint (f i) (t.sup f) variables {s t : finset ι} {f : ι → α} {i : ι} instance [decidable_eq ι] [decidable_eq α] : decidable (sup_indep s f) := begin apply @finset.decidable_forall_of_decidable_subsets _ _ _ _, intros t ht, apply @finset.decidable_dforall_finset _ _ _ _, exact λ i hi, @implies.decidable _ _ _ (decidable_of_iff' (_ = ⊥) disjoint_iff), end lemma sup_indep.subset (ht : t.sup_indep f) (h : s ⊆ t) : s.sup_indep f := λ u hu i hi, ht (hu.trans h) (h hi) lemma sup_indep_empty (f : ι → α) : (∅ : finset ι).sup_indep f := λ _ _ a ha, ha.elim lemma sup_indep_singleton (i : ι) (f : ι → α) : ({i} : finset ι).sup_indep f := λ s hs j hji hj, begin rw [eq_empty_of_ssubset_singleton ⟨hs, λ h, hj (h hji)⟩, sup_empty], exact disjoint_bot_right, end lemma sup_indep.pairwise_disjoint (hs : s.sup_indep f) : (s : set ι).pairwise_disjoint f := λ a ha b hb hab, sup_singleton.subst $ hs (singleton_subset_iff.2 hb) ha $ not_mem_singleton.2 hab lemma sup_indep.le_sup_iff (hs : s.sup_indep f) (hts : t ⊆ s) (hi : i ∈ s) (hf : ∀ i, f i ≠ ⊥) : f i ≤ t.sup f ↔ i ∈ t := begin refine ⟨λ h, _, le_sup⟩, by_contra hit, exact hf i (disjoint_self.1 $ (hs hts hi hit).mono_right h), end /-- The RHS looks like the definition of `complete_lattice.independent`. -/ lemma sup_indep_iff_disjoint_erase [decidable_eq ι] : s.sup_indep f ↔ ∀ i ∈ s, disjoint (f i) ((s.erase i).sup f) := ⟨λ hs i hi, hs (erase_subset _ _) hi (not_mem_erase _ _), λ hs t ht i hi hit, (hs i hi).mono_right (sup_mono $ λ j hj, mem_erase.2 ⟨ne_of_mem_of_not_mem hj hit, ht hj⟩)⟩ lemma sup_indep.image [decidable_eq ι] {s : finset ι'} {g : ι' → ι} (hs : s.sup_indep (f ∘ g)) : (s.image g).sup_indep f := begin intros t ht i hi hit, rw mem_image at hi, obtain ⟨i, hi, rfl⟩ := hi, haveI : decidable_eq ι' := classical.dec_eq _, suffices hts : t ⊆ (s.erase i).image g, { refine (sup_indep_iff_disjoint_erase.1 hs i hi).mono_right ((sup_mono hts).trans _), rw sup_image }, rintro j hjt, obtain ⟨j, hj, rfl⟩ := mem_image.1 (ht hjt), exact mem_image_of_mem _ (mem_erase.2 ⟨ne_of_apply_ne g (ne_of_mem_of_not_mem hjt hit), hj⟩), end lemma sup_indep_map {s : finset ι'} {g : ι' ↪ ι} : (s.map g).sup_indep f ↔ s.sup_indep (f ∘ g) := begin refine ⟨λ hs t ht i hi hit, _, λ hs, _⟩, { rw ←sup_map, exact hs (map_subset_map.2 ht) ((mem_map' _).2 hi) (by rwa mem_map') }, { classical, rw map_eq_image, exact hs.image } end @[simp] lemma sup_indep_pair [decidable_eq ι] {i j : ι} (hij : i ≠ j) : ({i, j} : finset ι).sup_indep f ↔ disjoint (f i) (f j) := ⟨λ h, h.pairwise_disjoint (by simp) (by simp) hij, λ h, begin rw sup_indep_iff_disjoint_erase, intros k hk, rw [finset.mem_insert, finset.mem_singleton] at hk, obtain rfl | rfl := hk, { convert h using 1, rw [finset.erase_insert, finset.sup_singleton], simpa using hij }, { convert h.symm using 1, have : ({i, k} : finset ι).erase k = {i}, { ext, rw [mem_erase, mem_insert, mem_singleton, mem_singleton, and_or_distrib_left, ne.def, not_and_self, or_false, and_iff_right_of_imp], rintro rfl, exact hij }, rw [this, finset.sup_singleton] } end⟩ lemma sup_indep_univ_bool (f : bool → α) : (finset.univ : finset bool).sup_indep f ↔ disjoint (f ff) (f tt) := begin have : tt ≠ ff := by simp only [ne.def, not_false_iff], exact (sup_indep_pair this).trans disjoint.comm, end @[simp] lemma sup_indep_univ_fin_two (f : fin 2 → α) : (finset.univ : finset (fin 2)).sup_indep f ↔ disjoint (f 0) (f 1) := begin have : (0 : fin 2) ≠ 1 := by simp, exact sup_indep_pair this, end lemma sup_indep.attach (hs : s.sup_indep f) : s.attach.sup_indep (λ a, f a) := begin intros t ht i _ hi, classical, rw ←finset.sup_image, refine hs (image_subset_iff.2 $ λ (j : {x // x ∈ s}) _, j.2) i.2 (λ hi', hi _), rw mem_image at hi', obtain ⟨j, hj, hji⟩ := hi', rwa subtype.ext hji at hj, end @[simp] lemma sup_indep_attach : s.attach.sup_indep (λ a, f a) ↔ s.sup_indep f := begin refine ⟨λ h t ht i his hit, _, sup_indep.attach⟩, classical, convert h (filter_subset (λ i, (i : ι) ∈ t) _) (mem_attach _ ⟨i, ‹_›⟩) (λ hi, hit $ by simpa using hi) using 1, refine eq_of_forall_ge_iff _, simp only [finset.sup_le_iff, mem_filter, mem_attach, true_and, function.comp_app, subtype.forall, subtype.coe_mk], exact λ a, forall_congr (λ j, ⟨λ h _, h, λ h hj, h (ht hj) hj⟩), end end lattice section distrib_lattice variables [distrib_lattice α] [order_bot α] {s : finset ι} {f : ι → α} lemma sup_indep_iff_pairwise_disjoint : s.sup_indep f ↔ (s : set ι).pairwise_disjoint f := ⟨sup_indep.pairwise_disjoint, λ hs t ht i hi hit, finset.disjoint_sup_right.2 $ λ j hj, hs hi (ht hj) (ne_of_mem_of_not_mem hj hit).symm⟩ alias sup_indep_iff_pairwise_disjoint ↔ sup_indep.pairwise_disjoint _root_.set.pairwise_disjoint.sup_indep /-- Bind operation for `sup_indep`. -/ lemma sup_indep.sup [decidable_eq ι] {s : finset ι'} {g : ι' → finset ι} {f : ι → α} (hs : s.sup_indep (λ i, (g i).sup f)) (hg : ∀ i' ∈ s, (g i').sup_indep f) : (s.sup g).sup_indep f := begin simp_rw sup_indep_iff_pairwise_disjoint at ⊢ hs hg, rw [sup_eq_bUnion, coe_bUnion], exact hs.bUnion_finset hg, end /-- Bind operation for `sup_indep`. -/ lemma sup_indep.bUnion [decidable_eq ι] {s : finset ι'} {g : ι' → finset ι} {f : ι → α} (hs : s.sup_indep (λ i, (g i).sup f)) (hg : ∀ i' ∈ s, (g i').sup_indep f) : (s.bUnion g).sup_indep f := by { rw ←sup_eq_bUnion, exact hs.sup hg } /-- Bind operation for `sup_indep`. -/ lemma sup_indep.sigma {β : ι → Type*} {s : finset ι} {g : Π i, finset (β i)} {f : sigma β → α} (hs : s.sup_indep $ λ i, (g i).sup $ λ b, f ⟨i, b⟩) (hg : ∀ i ∈ s, (g i).sup_indep $ λ b, f ⟨i, b⟩) : (s.sigma g).sup_indep f := begin rintro t ht ⟨i, b⟩ hi hit, rw finset.disjoint_sup_right, rintro ⟨j, c⟩ hj, have hbc := (ne_of_mem_of_not_mem hj hit).symm, replace hj := ht hj, rw mem_sigma at hi hj, obtain rfl | hij := eq_or_ne i j, { exact (hg _ hj.1).pairwise_disjoint hi.2 hj.2 (sigma_mk_injective.ne_iff.1 hbc) }, { refine (hs.pairwise_disjoint hi.1 hj.1 hij).mono _ _, { convert le_sup hi.2 }, { convert le_sup hj.2 } } end lemma sup_indep.product {s : finset ι} {t : finset ι'} {f : ι × ι' → α} (hs : s.sup_indep $ λ i, t.sup $ λ i', f (i, i')) (ht : t.sup_indep $ λ i', s.sup $ λ i, f (i, i')) : (s.product t).sup_indep f := begin rintro u hu ⟨i, i'⟩ hi hiu, rw finset.disjoint_sup_right, rintro ⟨j, j'⟩ hj, have hij := (ne_of_mem_of_not_mem hj hiu).symm, replace hj := hu hj, rw mem_product at hi hj, obtain rfl | hij := eq_or_ne i j, { refine (ht.pairwise_disjoint hi.2 hj.2 $ (prod.mk.inj_left _).ne_iff.1 hij).mono _ _, { convert le_sup hi.1 }, { convert le_sup hj.1 } }, { refine (hs.pairwise_disjoint hi.1 hj.1 hij).mono _ _, { convert le_sup hi.2 }, { convert le_sup hj.2 } } end lemma sup_indep_product_iff {s : finset ι} {t : finset ι'} {f : ι × ι' → α} : (s.product t).sup_indep f ↔ s.sup_indep (λ i, t.sup $ λ i', f (i, i')) ∧ t.sup_indep (λ i', s.sup $ λ i, f (i, i')) := begin refine ⟨_, λ h, h.1.product h.2⟩, simp_rw sup_indep_iff_pairwise_disjoint, refine (λ h, ⟨λ i hi j hj hij, _, λ i hi j hj hij, _⟩); simp_rw [function.on_fun, finset.disjoint_sup_left, finset.disjoint_sup_right]; intros i' hi' j' hj', { exact h (mk_mem_product hi hi') (mk_mem_product hj hj') (ne_of_apply_ne prod.fst hij) }, { exact h (mk_mem_product hi' hi) (mk_mem_product hj' hj) (ne_of_apply_ne prod.snd hij) } end end distrib_lattice end finset /-! ### On complete lattices via `has_Sup.Sup` -/ namespace complete_lattice variables [complete_lattice α] open set function /-- An independent set of elements in a complete lattice is one in which every element is disjoint from the `Sup` of the rest. -/ def set_independent (s : set α) : Prop := ∀ ⦃a⦄, a ∈ s → disjoint a (Sup (s \ {a})) variables {s : set α} (hs : set_independent s) @[simp] lemma set_independent_empty : set_independent (∅ : set α) := λ x hx, (set.not_mem_empty x hx).elim theorem set_independent.mono {t : set α} (hst : t ⊆ s) : set_independent t := λ a ha, (hs (hst ha)).mono_right (Sup_le_Sup (diff_subset_diff_left hst)) /-- If the elements of a set are independent, then any pair within that set is disjoint. -/ lemma set_independent.pairwise_disjoint : s.pairwise_disjoint id := λ x hx y hy h, disjoint_Sup_right (hs hx) ((mem_diff y).mpr ⟨hy, h.symm⟩) lemma set_independent_pair {a b : α} (hab : a ≠ b) : set_independent ({a, b} : set α) ↔ disjoint a b := begin split, { intro h, exact h.pairwise_disjoint (mem_insert _ _) (mem_insert_of_mem _ (mem_singleton _)) hab, }, { rintros h c ((rfl : c = a) | (rfl : c = b)), { convert h using 1, simp [hab, Sup_singleton] }, { convert h.symm using 1, simp [hab, Sup_singleton] }, }, end include hs /-- If the elements of a set are independent, then any element is disjoint from the `Sup` of some subset of the rest. -/ lemma set_independent.disjoint_Sup {x : α} {y : set α} (hx : x ∈ s) (hy : y ⊆ s) (hxy : x ∉ y) : disjoint x (Sup y) := begin have := (hs.mono $ insert_subset.mpr ⟨hx, hy⟩) (mem_insert x _), rw [insert_diff_of_mem _ (mem_singleton _), diff_singleton_eq_self hxy] at this, exact this, end omit hs /-- An independent indexed family of elements in a complete lattice is one in which every element is disjoint from the `supr` of the rest. Example: an indexed family of non-zero elements in a vector space is linearly independent iff the indexed family of subspaces they generate is independent in this sense. Example: an indexed family of submodules of a module is independent in this sense if and only the natural map from the direct sum of the submodules to the module is injective. -/ def independent {ι : Sort*} {α : Type*} [complete_lattice α] (t : ι → α) : Prop := ∀ i : ι, disjoint (t i) (⨆ (j ≠ i), t j) lemma set_independent_iff {α : Type*} [complete_lattice α] (s : set α) : set_independent s ↔ independent (coe : s → α) := begin simp_rw [independent, set_independent, set_coe.forall, Sup_eq_supr], refine forall₂_congr (λ a ha, _), congr' 2, convert supr_subtype.symm, simp [supr_and], end variables {t : ι → α} (ht : independent t) theorem independent_def : independent t ↔ ∀ i : ι, disjoint (t i) (⨆ (j ≠ i), t j) := iff.rfl theorem independent_def' : independent t ↔ ∀ i, disjoint (t i) (Sup (t '' {j | j ≠ i})) := by {simp_rw Sup_image, refl} theorem independent_def'' : independent t ↔ ∀ i, disjoint (t i) (Sup {a | ∃ j ≠ i, t j = a}) := by {rw independent_def', tidy} @[simp] lemma independent_empty (t : empty → α) : independent t. @[simp] lemma independent_pempty (t : pempty → α) : independent t. /-- If the elements of a set are independent, then any pair within that set is disjoint. -/ lemma independent.pairwise_disjoint : pairwise (disjoint on t) := λ x y h, disjoint_Sup_right (ht x) ⟨y, supr_pos h.symm⟩ lemma independent.mono {s t : ι → α} (hs : independent s) (hst : t ≤ s) : independent t := λ i, (hs i).mono (hst i) $ supr₂_mono $ λ j _, hst j /-- Composing an independent indexed family with an injective function on the index results in another indepedendent indexed family. -/ lemma independent.comp {ι ι' : Sort*} {t : ι → α} {f : ι' → ι} (ht : independent t) (hf : injective f) : independent (t ∘ f) := λ i, (ht (f i)).mono_right $ begin refine (supr_mono $ λ i, _).trans (supr_comp_le _ f), exact supr_const_mono hf.ne, end lemma independent.comp' {ι ι' : Sort*} {t : ι → α} {f : ι' → ι} (ht : independent $ t ∘ f) (hf : surjective f) : independent t := begin intros i, obtain ⟨i', rfl⟩ := hf i, rw ← hf.supr_comp, exact (ht i').mono_right (bsupr_mono $ λ j' hij, mt (congr_arg f) hij), end lemma independent.set_independent_range (ht : independent t) : set_independent $ range t := begin rw set_independent_iff, rw ← coe_comp_range_factorization t at ht, exact ht.comp' surjective_onto_range, end lemma independent.injective (ht : independent t) (h_ne_bot : ∀ i, t i ≠ ⊥) : injective t := begin intros i j h, by_contra' contra, apply h_ne_bot j, suffices : t j ≤ ⨆ k (hk : k ≠ i), t k, { replace ht := (ht i).mono_right this, rwa [h, disjoint_self] at ht, }, replace contra : j ≠ i, { exact ne.symm contra, }, exact le_supr₂ j contra, end lemma independent_pair {i j : ι} (hij : i ≠ j) (huniv : ∀ k, k = i ∨ k = j): independent t ↔ disjoint (t i) (t j) := begin split, { exact λ h, h.pairwise_disjoint hij }, { rintros h k, obtain rfl | rfl := huniv k, { refine h.mono_right (supr_le $ λ i, supr_le $ λ hi, eq.le _), rw (huniv i).resolve_left hi }, { refine h.symm.mono_right (supr_le $ λ j, supr_le $ λ hj, eq.le _), rw (huniv j).resolve_right hj } }, end /-- Composing an indepedent indexed family with an order isomorphism on the elements results in another indepedendent indexed family. -/ lemma independent.map_order_iso {ι : Sort*} {α β : Type*} [complete_lattice α] [complete_lattice β] (f : α ≃o β) {a : ι → α} (ha : independent a) : independent (f ∘ a) := λ i, ((ha i).map_order_iso f).mono_right (f.monotone.le_map_supr₂ _) @[simp] lemma independent_map_order_iso_iff {ι : Sort*} {α β : Type*} [complete_lattice α] [complete_lattice β] (f : α ≃o β) {a : ι → α} : independent (f ∘ a) ↔ independent a := ⟨ λ h, have hf : f.symm ∘ f ∘ a = a := congr_arg (∘ a) f.left_inv.comp_eq_id, hf ▸ h.map_order_iso f.symm, λ h, h.map_order_iso f⟩ /-- If the elements of a set are independent, then any element is disjoint from the `supr` of some subset of the rest. -/ lemma independent.disjoint_bsupr {ι : Type*} {α : Type*} [complete_lattice α] {t : ι → α} (ht : independent t) {x : ι} {y : set ι} (hx : x ∉ y) : disjoint (t x) (⨆ i ∈ y, t i) := disjoint.mono_right (bsupr_mono $ λ i hi, (ne_of_mem_of_not_mem hi hx : _)) (ht x) end complete_lattice lemma complete_lattice.independent_iff_sup_indep [complete_lattice α] {s : finset ι} {f : ι → α} : complete_lattice.independent (f ∘ (coe : s → ι)) ↔ s.sup_indep f := begin classical, rw finset.sup_indep_iff_disjoint_erase, refine subtype.forall.trans (forall₂_congr $ λ a b, _), rw finset.sup_eq_supr, congr' 2, refine supr_subtype.trans _, congr' 1 with x, simp [supr_and, @supr_comm _ (x ∈ s)], end alias complete_lattice.independent_iff_sup_indep ↔ complete_lattice.independent.sup_indep finset.sup_indep.independent /-- A variant of `complete_lattice.independent_iff_sup_indep` for `fintype`s. -/ lemma complete_lattice.independent_iff_sup_indep_univ [complete_lattice α] [fintype ι] {f : ι → α} : complete_lattice.independent f ↔ finset.univ.sup_indep f := begin classical, simp [finset.sup_indep_iff_disjoint_erase, complete_lattice.independent, finset.sup_eq_supr], end alias complete_lattice.independent_iff_sup_indep_univ ↔ complete_lattice.independent.sup_indep_univ finset.sup_indep.independent_of_univ section frame namespace complete_lattice variables [order.frame α] lemma set_independent_iff_pairwise_disjoint {s : set α} : set_independent s ↔ s.pairwise_disjoint id := ⟨set_independent.pairwise_disjoint, λ hs i hi, disjoint_Sup_iff.2 $ λ j hj, hs hi hj.1 $ ne.symm hj.2⟩ alias set_independent_iff_pairwise_disjoint ↔ _ _root_.set.pairwise_disjoint.set_independent lemma independent_iff_pairwise_disjoint {f : ι → α} : independent f ↔ pairwise (disjoint on f) := ⟨independent.pairwise_disjoint, λ hs i, disjoint_supr_iff.2 $ λ j, disjoint_supr_iff.2 $ λ hij, hs hij.symm⟩ end complete_lattice end frame
04bac5259199bee643f218d882e2039c11fa7093
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/back4.lean
3f75eaeb23cbba586a42307c2eb3a0f94f660236
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,596
lean
/- Lean also support E-matching: a heuristic lemma instantiation procedure that is available in many SMT solvers. Goals such as mk_mem_list can also be discharged using heuristic instantiation. -/ open list tactic universe variable u lemma in_tail {α : Type u} {a : α} (b : α) {l : list α} : a ∈ l → a ∈ b::l := mem_cons_of_mem _ lemma in_head {α : Type u} (a : α) (l : list α) : a ∈ a::l := mem_cons_self _ _ lemma in_left {α : Type u} {a : α} {l : list α} (r : list α) : a ∈ l → a ∈ l ++ r := mem_append_left _ lemma in_right {α : Type u} {a : α} (l : list α) {r : list α} : a ∈ r → a ∈ l ++ r := mem_append_right _ /- Using ematching -/ example (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] intros, iterate { ematch_using [in_left, in_right, in_head, in_tail], try {close} } end /- We mark lemmas for ematching. -/ attribute [ematch] in_left in_right in_head in_tail example (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] intros, iterate {ematch, try {close}} end example (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] intros, eblast end /- The following example is not provable -/ example (a b c : nat) (l₁ l₂ : list nat) : a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] intros, iterate {ematch, try {close}}, admit /- finish the proof admiting the goal -/ end
3df49c7a4424df71a2e8d7ebda3b4963561aa661
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/library/data/finset/basic.lean
6bacb0425f4b17063bc349a7f79127fdf291e689
[ "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
28,388
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad Finite sets. -/ import data.fintype.basic data.nat data.list.perm algebra.binary open nat quot list subtype binary function eq.ops open [decl] perm definition nodup_list (A : Type) := {l : list A | nodup l} variable {A : Type} definition to_nodup_list_of_nodup {l : list A} (n : nodup l) : nodup_list A := tag l n definition to_nodup_list [decidable_eq A] (l : list A) : nodup_list A := @to_nodup_list_of_nodup A (erase_dup l) (nodup_erase_dup l) private definition eqv (l₁ l₂ : nodup_list A) := perm (elt_of l₁) (elt_of l₂) local infix ~ := eqv private definition eqv.refl (l : nodup_list A) : l ~ l := !perm.refl private definition eqv.symm {l₁ l₂ : nodup_list A} : l₁ ~ l₂ → l₂ ~ l₁ := perm.symm private definition eqv.trans {l₁ l₂ l₃ : nodup_list A} : l₁ ~ l₂ → l₂ ~ l₃ → l₁ ~ l₃ := perm.trans definition finset.nodup_list_setoid [instance] (A : Type) : setoid (nodup_list A) := setoid.mk (@eqv A) (mk_equivalence (@eqv A) (@eqv.refl A) (@eqv.symm A) (@eqv.trans A)) definition finset (A : Type) : Type := quot (finset.nodup_list_setoid A) namespace finset -- give finset notation higher priority than set notation, so that it is tried first protected definition prio : num := num.succ std.priority.default definition to_finset_of_nodup (l : list A) (n : nodup l) : finset A := ⟦to_nodup_list_of_nodup n⟧ definition to_finset [decidable_eq A] (l : list A) : finset A := ⟦to_nodup_list l⟧ lemma to_finset_eq_of_nodup [decidable_eq A] {l : list A} (n : nodup l) : to_finset_of_nodup l n = to_finset l := assert P : to_nodup_list_of_nodup n = to_nodup_list l, from begin rewrite [↑to_nodup_list, ↑to_nodup_list_of_nodup], congruence, rewrite [erase_dup_eq_of_nodup n] end, quot.sound (eq.subst P !setoid.refl) definition has_decidable_eq [instance] [decidable_eq A] : decidable_eq (finset A) := λ s₁ s₂, quot.rec_on_subsingleton₂ s₁ s₂ (λ l₁ l₂, match decidable_perm (elt_of l₁) (elt_of l₂) with | decidable.inl e := decidable.inl (quot.sound e) | decidable.inr n := decidable.inr (λ e : ⟦l₁⟧ = ⟦l₂⟧, absurd (quot.exact e) n) end) definition mem (a : A) (s : finset A) : Prop := quot.lift_on s (λ l, a ∈ elt_of l) (λ l₁ l₂ (e : l₁ ~ l₂), propext (iff.intro (λ ainl₁, mem_perm e ainl₁) (λ ainl₂, mem_perm (perm.symm e) ainl₂))) infix [priority finset.prio] ∈ := mem notation [priority finset.prio] a ∉ b := ¬ mem a b theorem mem_of_mem_list {a : A} {l : nodup_list A} : a ∈ elt_of l → a ∈ ⟦l⟧ := λ ainl, ainl theorem mem_list_of_mem {a : A} {l : nodup_list A} : a ∈ ⟦l⟧ → a ∈ elt_of l := λ ainl, ainl /- singleton -/ definition singleton (a : A) : finset A := to_finset_of_nodup [a] !nodup_singleton theorem mem_singleton [simp] (a : A) : a ∈ singleton a := mem_of_mem_list !mem_cons theorem eq_of_mem_singleton {x a : A} : x ∈ singleton a → x = a := list.mem_singleton theorem mem_singleton_eq (x a : A) : (x ∈ singleton a) = (x = a) := propext (iff.intro eq_of_mem_singleton (assume H, eq.subst H !mem_singleton)) lemma eq_of_singleton_eq {a b : A} : singleton a = singleton b → a = b := assume Pseq, eq_of_mem_singleton (Pseq ▸ mem_singleton a) definition decidable_mem [instance] [h : decidable_eq A] : ∀ (a : A) (s : finset A), decidable (a ∈ s) := λ a s, quot.rec_on_subsingleton s (λ l, match list.decidable_mem a (elt_of l) with | decidable.inl p := decidable.inl (mem_of_mem_list p) | decidable.inr n := decidable.inr (λ p, absurd (mem_list_of_mem p) n) end) theorem mem_to_finset [decidable_eq A] {a : A} {l : list A} : a ∈ l → a ∈ to_finset l := λ ainl, mem_erase_dup ainl theorem mem_to_finset_of_nodup {a : A} {l : list A} (n : nodup l) : a ∈ l → a ∈ to_finset_of_nodup l n := λ ainl, ainl /- extensionality -/ theorem ext {s₁ s₂ : finset A} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ e, quot.sound (perm_ext (has_property l₁) (has_property l₂) e)) /- empty -/ definition empty : finset A := to_finset_of_nodup [] nodup_nil notation [priority finset.prio] `∅` := !empty theorem not_mem_empty [simp] (a : A) : a ∉ ∅ := λ aine : a ∈ ∅, aine theorem mem_empty_iff [simp] (x : A) : x ∈ ∅ ↔ false := iff_false_intro !not_mem_empty theorem mem_empty_eq (x : A) : x ∈ ∅ = false := propext !mem_empty_iff theorem eq_empty_of_forall_not_mem {s : finset A} (H : ∀x, ¬ x ∈ s) : s = ∅ := ext (take x, iff_false_intro (H x)) /- universe -/ definition univ [h : fintype A] : finset A := to_finset_of_nodup (@fintype.elems A h) (@fintype.unique A h) theorem mem_univ [fintype A] (x : A) : x ∈ univ := fintype.complete x theorem mem_univ_eq [fintype A] (x : A) : x ∈ univ = true := propext (iff_true_intro !mem_univ) /- card -/ definition card (s : finset A) : nat := quot.lift_on s (λ l, length (elt_of l)) (λ l₁ l₂ p, length_eq_length_of_perm p) theorem card_empty : card (@empty A) = 0 := rfl theorem card_singleton (a : A) : card (singleton a) = 1 := rfl lemma ne_empty_of_card_eq_succ {s : finset A} {n : nat} : card s = succ n → s ≠ ∅ := by intros; substvars; contradiction /- insert -/ section insert variable [h : decidable_eq A] include h definition insert (a : A) (s : finset A) : finset A := quot.lift_on s (λ l, to_finset_of_nodup (insert a (elt_of l)) (nodup_insert a (has_property l))) (λ (l₁ l₂ : nodup_list A) (p : l₁ ~ l₂), quot.sound (perm_insert a p)) -- set builder notation notation [priority finset.prio] `'{`:max a:(foldr `, ` (x b, insert x b) ∅) `}`:0 := a theorem mem_insert (a : A) (s : finset A) : a ∈ insert a s := quot.induction_on s (λ l : nodup_list A, mem_to_finset_of_nodup _ !list.mem_insert) theorem mem_insert_of_mem {a : A} {s : finset A} (b : A) : a ∈ s → a ∈ insert b s := quot.induction_on s (λ (l : nodup_list A) (ainl : a ∈ ⟦l⟧), mem_to_finset_of_nodup _ (list.mem_insert_of_mem _ ainl)) theorem eq_or_mem_of_mem_insert {x a : A} {s : finset A} : x ∈ insert a s → x = a ∨ x ∈ s := quot.induction_on s (λ l : nodup_list A, λ H, list.eq_or_mem_of_mem_insert H) theorem mem_of_mem_insert_of_ne {x a : A} {s : finset A} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := or_resolve_right (eq_or_mem_of_mem_insert xin) theorem mem_insert_eq (x a : A) (s : finset A) : x ∈ insert a s = (x = a ∨ x ∈ s) := propext (iff.intro !eq_or_mem_of_mem_insert (or.rec (λH', (eq.substr H' !mem_insert)) !mem_insert_of_mem)) theorem insert_empty_eq (a : A) : '{a} = singleton a := rfl theorem insert_eq_of_mem {a : A} {s : finset A} (H : a ∈ s) : insert a s = s := ext (λ x, eq.substr (mem_insert_eq x a s) (or_iff_right_of_imp (λH1, eq.substr H1 H))) -- useful in proofs by induction theorem forall_of_forall_insert {P : A → Prop} {a : A} {s : finset A} (H : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := λ x xs, H x (!mem_insert_of_mem xs) theorem insert.comm (x y : A) (s : finset A) : insert x (insert y s) = insert y (insert x s) := ext (take a, by rewrite [*mem_insert_eq, propext !or.left_comm]) theorem card_insert_of_mem {a : A} {s : finset A} : a ∈ s → card (insert a s) = card s := quot.induction_on s (λ (l : nodup_list A) (ainl : a ∈ ⟦l⟧), list.length_insert_of_mem ainl) theorem card_insert_of_not_mem {a : A} {s : finset A} : a ∉ s → card (insert a s) = card s + 1 := quot.induction_on s (λ (l : nodup_list A) (nainl : a ∉ ⟦l⟧), list.length_insert_of_not_mem nainl) theorem card_insert_le (a : A) (s : finset A) : card (insert a s) ≤ card s + 1 := if H : a ∈ s then by rewrite [card_insert_of_mem H]; apply le_succ else by rewrite [card_insert_of_not_mem H] protected theorem induction [recursor 6] {P : finset A → Prop} (H1 : P empty) (H2 : ∀ ⦃a : A⦄, ∀{s : finset A}, a ∉ s → P s → P (insert a s)) : ∀s, P s := take s, quot.induction_on s (take u, subtype.destruct u (take l, list.induction_on l (assume nodup_l, H1) (take a l', assume IH nodup_al', have a ∉ l', from not_mem_of_nodup_cons nodup_al', assert e : list.insert a l' = a :: l', from insert_eq_of_not_mem this, assert nodup l', from nodup_of_nodup_cons nodup_al', assert P (quot.mk (subtype.tag l' this)), from IH this, assert P (insert a (quot.mk (subtype.tag l' _))), from H2 `a ∉ l'` this, begin revert nodup_al', rewrite [-e], intros, apply this end))) protected theorem induction_on {P : finset A → Prop} (s : finset A) (H1 : P empty) (H2 : ∀ ⦃a : A⦄, ∀ {s : finset A}, a ∉ s → P s → P (insert a s)) : P s := finset.induction H1 H2 s theorem exists_of_not_empty {s : finset A} : s ≠ ∅ → ∃ a : A, a ∈ s := begin induction s with a s nin ih, {intro h, exact absurd rfl h}, {intro h, existsi a, apply mem_insert} end theorem eq_empty_of_card_eq_zero {s : finset A} (H : card s = 0) : s = ∅ := begin induction s with a s' H1 IH, { reflexivity }, { rewrite (card_insert_of_not_mem H1) at H, apply nat.no_confusion H} end end insert /- erase -/ section erase variable [h : decidable_eq A] include h definition erase (a : A) (s : finset A) : finset A := quot.lift_on s (λ l, to_finset_of_nodup (erase a (elt_of l)) (nodup_erase_of_nodup a (has_property l))) (λ (l₁ l₂ : nodup_list A) (p : l₁ ~ l₂), quot.sound (erase_perm_erase_of_perm a p)) theorem mem_erase (a : A) (s : finset A) : a ∉ erase a s := quot.induction_on s (λ l, list.mem_erase_of_nodup _ (has_property l)) theorem card_erase_of_mem {a : A} {s : finset A} : a ∈ s → card (erase a s) = pred (card s) := quot.induction_on s (λ l ainl, list.length_erase_of_mem ainl) theorem card_erase_of_not_mem {a : A} {s : finset A} : a ∉ s → card (erase a s) = card s := quot.induction_on s (λ l nainl, list.length_erase_of_not_mem nainl) theorem erase_empty (a : A) : erase a ∅ = ∅ := rfl theorem ne_of_mem_erase {a b : A} {s : finset A} : b ∈ erase a s → b ≠ a := by intro h beqa; subst b; exact absurd h !mem_erase theorem mem_of_mem_erase {a b : A} {s : finset A} : b ∈ erase a s → b ∈ s := quot.induction_on s (λ l bin, mem_of_mem_erase bin) theorem mem_erase_of_ne_of_mem {a b : A} {s : finset A} : a ≠ b → a ∈ s → a ∈ erase b s := quot.induction_on s (λ l n ain, list.mem_erase_of_ne_of_mem n ain) theorem mem_erase_iff (a b : A) (s : finset A) : a ∈ erase b s ↔ a ∈ s ∧ a ≠ b := iff.intro (assume H, and.intro (mem_of_mem_erase H) (ne_of_mem_erase H)) (assume H, mem_erase_of_ne_of_mem (and.right H) (and.left H)) theorem mem_erase_eq (a b : A) (s : finset A) : a ∈ erase b s = (a ∈ s ∧ a ≠ b) := propext !mem_erase_iff open decidable theorem erase_insert {a : A} {s : finset A} : a ∉ s → erase a (insert a s) = s := λ anins, finset.ext (λ b, by_cases (λ beqa : b = a, iff.intro (λ bin, by subst b; exact absurd bin !mem_erase) (λ bin, by subst b; contradiction)) (λ bnea : b ≠ a, iff.intro (λ bin, assert b ∈ insert a s, from mem_of_mem_erase bin, mem_of_mem_insert_of_ne this bnea) (λ bin, have b ∈ insert a s, from mem_insert_of_mem _ bin, mem_erase_of_ne_of_mem bnea this))) theorem insert_erase {a : A} {s : finset A} : a ∈ s → insert a (erase a s) = s := λ ains, finset.ext (λ b, by_cases (suppose b = a, iff.intro (λ bin, by subst b; assumption) (λ bin, by subst b; apply mem_insert)) (suppose b ≠ a, iff.intro (λ bin, mem_of_mem_erase (mem_of_mem_insert_of_ne bin `b ≠ a`)) (λ bin, mem_insert_of_mem _ (mem_erase_of_ne_of_mem `b ≠ a` bin)))) end erase /- union -/ section union variable [h : decidable_eq A] include h definition union (s₁ s₂ : finset A) : finset A := quot.lift_on₂ s₁ s₂ (λ l₁ l₂, to_finset_of_nodup (list.union (elt_of l₁) (elt_of l₂)) (nodup_union_of_nodup_of_nodup (has_property l₁) (has_property l₂))) (λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_union p₁ p₂)) infix [priority finset.prio] ∪ := union theorem mem_union_left {a : A} {s₁ : finset A} (s₂ : finset A) : a ∈ s₁ → a ∈ s₁ ∪ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁, list.mem_union_left _ ainl₁) theorem mem_union_l {a : A} {s₁ : finset A} {s₂ : finset A} : a ∈ s₁ → a ∈ s₁ ∪ s₂ := mem_union_left s₂ theorem mem_union_right {a : A} {s₂ : finset A} (s₁ : finset A) : a ∈ s₂ → a ∈ s₁ ∪ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₂, list.mem_union_right _ ainl₂) theorem mem_union_r {a : A} {s₂ : finset A} {s₁ : finset A} : a ∈ s₂ → a ∈ s₁ ∪ s₂ := mem_union_right s₁ theorem mem_or_mem_of_mem_union {a : A} {s₁ s₂ : finset A} : a ∈ s₁ ∪ s₂ → a ∈ s₁ ∨ a ∈ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁l₂, list.mem_or_mem_of_mem_union ainl₁l₂) theorem mem_union_iff (a : A) (s₁ s₂ : finset A) : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := iff.intro (λ h, mem_or_mem_of_mem_union h) (λ d, or.elim d (λ i, mem_union_left _ i) (λ i, mem_union_right _ i)) theorem mem_union_eq (a : A) (s₁ s₂ : finset A) : (a ∈ s₁ ∪ s₂) = (a ∈ s₁ ∨ a ∈ s₂) := propext !mem_union_iff theorem union.comm (s₁ s₂ : finset A) : s₁ ∪ s₂ = s₂ ∪ s₁ := ext (λ a, by rewrite [*mem_union_eq]; exact or.comm) theorem union.assoc (s₁ s₂ s₃ : finset A) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := ext (λ a, by rewrite [*mem_union_eq]; exact or.assoc) theorem union.left_comm (s₁ s₂ s₃ : finset A) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := !left_comm union.comm union.assoc s₁ s₂ s₃ theorem union.right_comm (s₁ s₂ s₃ : finset A) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := !right_comm union.comm union.assoc s₁ s₂ s₃ theorem union_self (s : finset A) : s ∪ s = s := ext (λ a, iff.intro (λ ain, or.elim (mem_or_mem_of_mem_union ain) (λ i, i) (λ i, i)) (λ i, mem_union_left _ i)) theorem union_empty (s : finset A) : s ∪ ∅ = s := ext (λ a, iff.intro (suppose a ∈ s ∪ ∅, or.elim (mem_or_mem_of_mem_union this) (λ i, i) (λ i, absurd i !not_mem_empty)) (suppose a ∈ s, mem_union_left _ this)) theorem empty_union (s : finset A) : ∅ ∪ s = s := calc ∅ ∪ s = s ∪ ∅ : union.comm ... = s : union_empty theorem insert_eq (a : A) (s : finset A) : insert a s = singleton a ∪ s := ext (take x, calc x ∈ insert a s ↔ x ∈ insert a s : iff.refl ... = (x = a ∨ x ∈ s) : mem_insert_eq ... = (x ∈ singleton a ∨ x ∈ s) : mem_singleton_eq ... = (x ∈ '{a} ∪ s) : mem_union_eq) theorem insert_union (a : A) (s t : finset A) : insert a (s ∪ t) = insert a s ∪ t := by rewrite [*insert_eq, union.assoc] end union /- inter -/ section inter variable [h : decidable_eq A] include h definition inter (s₁ s₂ : finset A) : finset A := quot.lift_on₂ s₁ s₂ (λ l₁ l₂, to_finset_of_nodup (list.inter (elt_of l₁) (elt_of l₂)) (nodup_inter_of_nodup _ (has_property l₁))) (λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_inter p₁ p₂)) infix [priority finset.prio] ∩ := inter theorem mem_of_mem_inter_left {a : A} {s₁ s₂ : finset A} : a ∈ s₁ ∩ s₂ → a ∈ s₁ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁l₂, list.mem_of_mem_inter_left ainl₁l₂) theorem mem_of_mem_inter_right {a : A} {s₁ s₂ : finset A} : a ∈ s₁ ∩ s₂ → a ∈ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁l₂, list.mem_of_mem_inter_right ainl₁l₂) theorem mem_inter {a : A} {s₁ s₂ : finset A} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁ ainl₂, list.mem_inter_of_mem_of_mem ainl₁ ainl₂) theorem mem_inter_iff (a : A) (s₁ s₂ : finset A) : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := iff.intro (λ h, and.intro (mem_of_mem_inter_left h) (mem_of_mem_inter_right h)) (λ h, mem_inter (and.elim_left h) (and.elim_right h)) theorem mem_inter_eq (a : A) (s₁ s₂ : finset A) : (a ∈ s₁ ∩ s₂) = (a ∈ s₁ ∧ a ∈ s₂) := propext !mem_inter_iff theorem inter.comm (s₁ s₂ : finset A) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext (λ a, by rewrite [*mem_inter_eq]; exact and.comm) theorem inter.assoc (s₁ s₂ s₃ : finset A) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext (λ a, by rewrite [*mem_inter_eq]; exact and.assoc) theorem inter.left_comm (s₁ s₂ s₃ : finset A) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := !left_comm inter.comm inter.assoc s₁ s₂ s₃ theorem inter.right_comm (s₁ s₂ s₃ : finset A) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := !right_comm inter.comm inter.assoc s₁ s₂ s₃ theorem inter_self (s : finset A) : s ∩ s = s := ext (λ a, iff.intro (λ h, mem_of_mem_inter_right h) (λ h, mem_inter h h)) theorem inter_empty (s : finset A) : s ∩ ∅ = ∅ := ext (λ a, iff.intro (suppose a ∈ s ∩ ∅, absurd (mem_of_mem_inter_right this) !not_mem_empty) (suppose a ∈ ∅, absurd this !not_mem_empty)) theorem empty_inter (s : finset A) : ∅ ∩ s = ∅ := calc ∅ ∩ s = s ∩ ∅ : inter.comm ... = ∅ : inter_empty theorem singleton_inter_of_mem {a : A} {s : finset A} (H : a ∈ s) : singleton a ∩ s = singleton a := ext (take x, begin rewrite [mem_inter_eq, !mem_singleton_eq], exact iff.intro (suppose x = a ∧ x ∈ s, and.left this) (suppose x = a, and.intro this (eq.subst (eq.symm this) H)) end) theorem singleton_inter_of_not_mem {a : A} {s : finset A} (H : a ∉ s) : singleton a ∩ s = ∅ := ext (take x, begin rewrite [mem_inter_eq, !mem_singleton_eq, mem_empty_eq], exact iff.intro (suppose x = a ∧ x ∈ s, H (eq.subst (and.left this) (and.right this))) (false.elim) end) end inter /- distributivity laws -/ section inter variable [h : decidable_eq A] include h theorem inter.distrib_left (s t u : finset A) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (take x, by rewrite [mem_inter_eq, *mem_union_eq, *mem_inter_eq]; apply and.left_distrib) theorem inter.distrib_right (s t u : finset A) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (take x, by rewrite [mem_inter_eq, *mem_union_eq, *mem_inter_eq]; apply and.right_distrib) theorem union.distrib_left (s t u : finset A) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (take x, by rewrite [mem_union_eq, *mem_inter_eq, *mem_union_eq]; apply or.left_distrib) theorem union.distrib_right (s t u : finset A) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (take x, by rewrite [mem_union_eq, *mem_inter_eq, *mem_union_eq]; apply or.right_distrib) end inter /- disjoint -/ -- Mainly for internal use; library will use s₁ ∩ s₂ = ∅. Note that it does not require decidable equality. definition disjoint (s₁ s₂ : finset A) : Prop := quot.lift_on₂ s₁ s₂ (λ l₁ l₂, disjoint (elt_of l₁) (elt_of l₂)) (λ v₁ v₂ w₁ w₂ p₁ p₂, propext (iff.intro (λ d₁ a (ainw₁ : a ∈ elt_of w₁), have a ∈ elt_of v₁, from mem_perm (perm.symm p₁) ainw₁, have a ∉ elt_of v₂, from disjoint_left d₁ this, not_mem_perm p₂ this) (λ d₂ a (ainv₁ : a ∈ elt_of v₁), have a ∈ elt_of w₁, from mem_perm p₁ ainv₁, have a ∉ elt_of w₂, from disjoint_left d₂ this, not_mem_perm (perm.symm p₂) this))) theorem disjoint.elim {s₁ s₂ : finset A} {x : A} : disjoint s₁ s₂ → x ∈ s₁ → x ∈ s₂ → false := quot.induction_on₂ s₁ s₂ (take u₁ u₂, assume H H1 H2, H x H1 H2) theorem disjoint.intro {s₁ s₂ : finset A} : (∀{x : A}, x ∈ s₁ → x ∈ s₂ → false) → disjoint s₁ s₂ := quot.induction_on₂ s₁ s₂ (take u₁ u₂, assume H, H) theorem inter_eq_empty_of_disjoint [h : decidable_eq A] {s₁ s₂ : finset A} (H : disjoint s₁ s₂) : s₁ ∩ s₂ = ∅ := ext (take x, iff_false_intro (assume H1, disjoint.elim H (mem_of_mem_inter_left H1) (mem_of_mem_inter_right H1))) theorem disjoint_of_inter_eq_empty [h : decidable_eq A] {s₁ s₂ : finset A} (H : s₁ ∩ s₂ = ∅) : disjoint s₁ s₂ := disjoint.intro (take x H1 H2, have x ∈ s₁ ∩ s₂, from mem_inter H1 H2, !not_mem_empty (eq.subst H this)) theorem disjoint.comm {s₁ s₂ : finset A} : disjoint s₁ s₂ → disjoint s₂ s₁ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ d, list.disjoint.comm d) theorem inter_eq_empty [h : decidable_eq A] {s₁ s₂ : finset A} (H : ∀x : A, x ∈ s₁ → x ∈ s₂ → false) : s₁ ∩ s₂ = ∅ := inter_eq_empty_of_disjoint (disjoint.intro H) /- subset -/ definition subset (s₁ s₂ : finset A) : Prop := quot.lift_on₂ s₁ s₂ (λ l₁ l₂, sublist (elt_of l₁) (elt_of l₂)) (λ v₁ v₂ w₁ w₂ p₁ p₂, propext (iff.intro (λ s₁ a i, mem_perm p₂ (s₁ a (mem_perm (perm.symm p₁) i))) (λ s₂ a i, mem_perm (perm.symm p₂) (s₂ a (mem_perm p₁ i))))) infix [priority finset.prio] ⊆ := subset theorem empty_subset (s : finset A) : ∅ ⊆ s := quot.induction_on s (λ l, list.nil_sub (elt_of l)) theorem subset_univ [h : fintype A] (s : finset A) : s ⊆ univ := quot.induction_on s (λ l a i, fintype.complete a) theorem subset.refl (s : finset A) : s ⊆ s := quot.induction_on s (λ l, list.sub.refl (elt_of l)) theorem subset.trans {s₁ s₂ s₃ : finset A} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := quot.induction_on₃ s₁ s₂ s₃ (λ l₁ l₂ l₃ h₁ h₂, list.sub.trans h₁ h₂) theorem mem_of_subset_of_mem {s₁ s₂ : finset A} {a : A} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h₁ h₂, h₁ a h₂) theorem subset.antisymm {s₁ s₂ : finset A} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext (take x, iff.intro (assume H, mem_of_subset_of_mem H₁ H) (assume H, mem_of_subset_of_mem H₂ H)) -- alternative name theorem eq_of_subset_of_subset {s₁ s₂ : finset A} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := subset.antisymm H₁ H₂ theorem subset_of_forall {s₁ s₂ : finset A} : (∀x, x ∈ s₁ → x ∈ s₂) → s₁ ⊆ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ H, H) theorem subset_insert [h : decidable_eq A] (s : finset A) (a : A) : s ⊆ insert a s := subset_of_forall (take x, suppose x ∈ s, mem_insert_of_mem _ this) theorem eq_empty_of_subset_empty {x : finset A} (H : x ⊆ ∅) : x = ∅ := subset.antisymm H (empty_subset x) theorem subset_empty_iff (x : finset A) : x ⊆ ∅ ↔ x = ∅ := iff.intro eq_empty_of_subset_empty (take xeq, by rewrite xeq; apply subset.refl ∅) section variable [decA : decidable_eq A] include decA theorem erase_subset_erase (a : A) {s t : finset A} (H : s ⊆ t) : erase a s ⊆ erase a t := begin apply subset_of_forall, intro x, rewrite *mem_erase_eq, intro H', show x ∈ t ∧ x ≠ a, from and.intro (mem_of_subset_of_mem H (and.left H')) (and.right H') end theorem erase_subset (a : A) (s : finset A) : erase a s ⊆ s := begin apply subset_of_forall, intro x, rewrite mem_erase_eq, intro H, apply and.left H end theorem erase_eq_of_not_mem {a : A} {s : finset A} (anins : a ∉ s) : erase a s = s := eq_of_subset_of_subset !erase_subset (subset_of_forall (take x, assume xs : x ∈ s, have x ≠ a, from assume H', anins (eq.subst H' xs), mem_erase_of_ne_of_mem this xs)) theorem erase_insert_subset (a : A) (s : finset A) : erase a (insert a s) ⊆ s := decidable.by_cases (assume ains : a ∈ s, by rewrite [insert_eq_of_mem ains]; apply erase_subset) (assume nains : a ∉ s, by rewrite [!erase_insert nains]; apply subset.refl) theorem erase_subset_of_subset_insert {a : A} {s t : finset A} (H : s ⊆ insert a t) : erase a s ⊆ t := subset.trans (!erase_subset_erase H) !erase_insert_subset theorem insert_erase_subset (a : A) (s : finset A) : s ⊆ insert a (erase a s) := decidable.by_cases (assume ains : a ∈ s, by rewrite [!insert_erase ains]; apply subset.refl) (assume nains : a ∉ s, by rewrite[erase_eq_of_not_mem nains]; apply subset_insert) theorem insert_subset_insert (a : A) {s t : finset A} (H : s ⊆ t) : insert a s ⊆ insert a t := begin apply subset_of_forall, intro x, rewrite *mem_insert_eq, intro H', cases H' with [xeqa, xins], exact (or.inl xeqa), exact (or.inr (mem_of_subset_of_mem H xins)) end theorem subset_insert_of_erase_subset {s t : finset A} {a : A} (H : erase a s ⊆ t) : s ⊆ insert a t := subset.trans (insert_erase_subset a s) (!insert_subset_insert H) theorem subset_insert_iff (s t : finset A) (a : A) : s ⊆ insert a t ↔ erase a s ⊆ t := iff.intro !erase_subset_of_subset_insert !subset_insert_of_erase_subset end /- upto -/ section upto definition upto (n : nat) : finset nat := to_finset_of_nodup (list.upto n) (nodup_upto n) theorem card_upto : ∀ n, card (upto n) = n := list.length_upto theorem lt_of_mem_upto {n a : nat} : a ∈ upto n → a < n := list.lt_of_mem_upto theorem mem_upto_succ_of_mem_upto {n a : nat} : a ∈ upto n → a ∈ upto (succ n) := list.mem_upto_succ_of_mem_upto theorem mem_upto_of_lt {n a : nat} : a < n → a ∈ upto n := list.mem_upto_of_lt theorem mem_upto_iff (a n : nat) : a ∈ upto n ↔ a < n := iff.intro lt_of_mem_upto mem_upto_of_lt theorem mem_upto_eq (a n : nat) : a ∈ upto n = (a < n) := propext !mem_upto_iff end upto /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff {A : Type} (P : A → Prop) : (∃ x, x ∈ ∅ ∧ P x) ↔ false := iff.intro (assume H, obtain x (H1 : x ∈ ∅ ∧ P x), from H, !not_mem_empty (and.left H1)) (assume H, false.elim H) theorem exists_mem_empty_eq {A : Type} (P : A → Prop) : (∃ x, x ∈ ∅ ∧ P x) = false := propext !exists_mem_empty_iff theorem exists_mem_insert_iff {A : Type} [d : decidable_eq A] (a : A) (s : finset A) (P : A → Prop) : (∃ x, x ∈ insert a s ∧ P x) ↔ P a ∨ (∃ x, x ∈ s ∧ P x) := iff.intro (assume H, obtain x [H1 H2], from H, or.elim (eq_or_mem_of_mem_insert H1) (suppose x = a, or.inl (eq.subst this H2)) (suppose x ∈ s, or.inr (exists.intro x (and.intro this H2)))) (assume H, or.elim H (suppose P a, exists.intro a (and.intro !mem_insert this)) (suppose ∃ x, x ∈ s ∧ P x, obtain x [H2 H3], from this, exists.intro x (and.intro (!mem_insert_of_mem H2) H3))) theorem exists_mem_insert_eq {A : Type} [d : decidable_eq A] (a : A) (s : finset A) (P : A → Prop) : (∃ x, x ∈ insert a s ∧ P x) = (P a ∨ (∃ x, x ∈ s ∧ P x)) := propext !exists_mem_insert_iff theorem forall_mem_empty_iff {A : Type} (P : A → Prop) : (∀ x, x ∈ ∅ → P x) ↔ true := iff.intro (assume H, trivial) (assume H, take x, assume H', absurd H' !not_mem_empty) theorem forall_mem_empty_eq {A : Type} (P : A → Prop) : (∀ x, x ∈ ∅ → P x) = true := propext !forall_mem_empty_iff theorem forall_mem_insert_iff {A : Type} [d : decidable_eq A] (a : A) (s : finset A) (P : A → Prop) : (∀ x, x ∈ insert a s → P x) ↔ P a ∧ (∀ x, x ∈ s → P x) := iff.intro (assume H, and.intro (H _ !mem_insert) (take x, assume H', H _ (!mem_insert_of_mem H'))) (assume H, take x, assume H' : x ∈ insert a s, or.elim (eq_or_mem_of_mem_insert H') (suppose x = a, eq.subst (eq.symm this) (and.left H)) (suppose x ∈ s, and.right H _ this)) theorem forall_mem_insert_eq {A : Type} [d : decidable_eq A] (a : A) (s : finset A) (P : A → Prop) : (∀ x, x ∈ insert a s → P x) = (P a ∧ (∀ x, x ∈ s → P x)) := propext !forall_mem_insert_iff end finset
f648cdc121deff5214cf33ee59594a7665d2a8ab
27a31d06bcfc7c5d379fd04a08a9f5ed3f5302d4
/stage0/src/Lean/Elab/PreDefinition/WF/Fix.lean
6cc1f7220cde428993645e507e9d971af0456704
[ "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
joehendrix/lean4
0d1486945f7ca9fe225070374338f4f7e74bab03
1221bdd3c7d5395baa451ce8fdd2c2f8a00cbc8f
refs/heads/master
1,640,573,727,861
1,639,662,710,000
1,639,665,515,000
198,893,504
0
0
Apache-2.0
1,564,084,645,000
1,564,084,644,000
null
UTF-8
Lean
false
false
6,903
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.Match.Match import Lean.Meta.Tactic.Simp.Main import Lean.Meta.Tactic.Cleanup import Lean.Elab.PreDefinition.Basic import Lean.Elab.PreDefinition.Structural.Basic namespace Lean.Elab.WF open Meta private def toUnfold : Std.PHashSet Name := [``measure, ``id, ``Prod.lex, ``invImage, ``InvImage, ``Nat.lt_wfRel].foldl (init := {}) fun s a => s.insert a private def applyDefaultDecrTactic (mvarId : MVarId) : TermElabM Unit := do let ctx ← Simp.Context.mkDefault let ctx := { ctx with simpLemmas.toUnfold := toUnfold } if let some mvarId ← simpTarget mvarId ctx then -- TODO: invoke tactic to close the goal Term.reportUnsolvedGoals [mvarId] throwAbortTactic private def mkDecreasingProof (decreasingProp : Expr) (decrTactic? : Option Syntax) : TermElabM Expr := do let mvar ← mkFreshExprSyntheticOpaqueMVar decreasingProp let mvarId := mvar.mvarId! let mvarId ← cleanup mvarId match decrTactic? with | none => applyDefaultDecrTactic mvarId | some decrTactic => Term.runTactic mvarId decrTactic instantiateMVars mvar private partial def replaceRecApps (recFnName : Name) (decrTactic? : Option Syntax) (F : Expr) (e : Expr) : TermElabM Expr := let rec loop (F : Expr) (e : Expr) : TermElabM Expr := do match e with | Expr.lam n d b c => withLocalDecl n c.binderInfo (← loop F d) fun x => do mkLambdaFVars #[x] (← loop F (b.instantiate1 x)) | Expr.forallE n d b c => withLocalDecl n c.binderInfo (← loop F d) fun x => do mkForallFVars #[x] (← loop F (b.instantiate1 x)) | Expr.letE n type val body _ => withLetDecl n (← loop F type) (← loop F val) fun x => do mkLetFVars #[x] (← loop F (body.instantiate1 x)) (usedLetOnly := false) | Expr.mdata d e _ => return mkMData d (← loop F e) | Expr.proj n i e _ => return mkProj n i (← loop F e) | Expr.app _ _ _ => let processApp (e : Expr) : TermElabM Expr := e.withApp fun f args => do if f.isConstOf recFnName && args.size == 1 then let r := mkApp F args[0] let decreasingProp := (← whnf (← inferType r)).bindingDomain! return mkApp r (← mkDecreasingProof decreasingProp decrTactic?) else return mkAppN (← loop F f) (← args.mapM (loop F)) let matcherApp? ← matchMatcherApp? e match matcherApp? with | some matcherApp => if !Structural.recArgHasLooseBVarsAt recFnName 0 e then processApp e else let matcherApp ← mapError (matcherApp.addArg F) (fun msg => "failed to add functional argument to 'matcher' application" ++ indentD msg) let altsNew ← (Array.zip matcherApp.alts matcherApp.altNumParams).mapM fun (alt, numParams) => lambdaTelescope alt fun xs altBody => do unless xs.size >= numParams do throwError "unexpected matcher application alternative{indentExpr alt}\nat application{indentExpr e}" let FAlt := xs[numParams - 1] mkLambdaFVars xs (← loop FAlt altBody) pure { matcherApp with alts := altsNew }.toExpr | none => processApp e | e => Structural.ensureNoRecFn recFnName e loop F e /-- Refine `F` over `Sum.casesOn` -/ private partial def processSumCasesOn (x F val : Expr) (k : (x : Expr) → (F : Expr) → (val : Expr) → TermElabM Expr) : TermElabM Expr := do if x.isFVar && val.isAppOfArity ``Sum.casesOn 6 && val.getArg! 3 == x && (val.getArg! 4).isLambda && (val.getArg! 5).isLambda then let args := val.getAppArgs let α := args[0] let β := args[1] let FDecl ← getLocalDecl F.fvarId! let (motiveNew, u) ← lambdaTelescope args[2] fun xs type => do let type ← mkArrow (FDecl.type.replaceFVar x xs[0]) type return (← mkLambdaFVars xs type, ← getLevel type) let mkMinorNew (ctorName : Name) (minor : Expr) : TermElabM Expr := lambdaTelescope minor fun xs body => do let xNew ← xs[0] let valNew ← mkLambdaFVars xs[1:] body let FTypeNew := FDecl.type.replaceFVar x (← mkAppOptM ctorName #[α, β, xNew]) withLocalDeclD FDecl.userName FTypeNew fun FNew => do mkLambdaFVars #[xNew, FNew] (← processSumCasesOn xNew FNew valNew k) let minorLeft ← mkMinorNew ``Sum.inl args[4] let minorRight ← mkMinorNew ``Sum.inr args[5] let result := mkAppN (mkConst ``Sum.casesOn [u, (← getDecLevel α), (← getDecLevel β)]) #[α, β, motiveNew, x, minorLeft, minorRight, F] return result else k x F val /-- Refine `F` over `PSigma.casesOn` -/ private partial def processPSigmaCasesOn (x F val : Expr) (k : (F : Expr) → (val : Expr) → TermElabM Expr) : TermElabM Expr := do if x.isFVar && val.isAppOfArity ``PSigma.casesOn 5 && val.getArg! 3 == x && (val.getArg! 4).isLambda && (val.getArg! 4).bindingBody!.isLambda then let args := val.getAppArgs let [_, u, v] ← val.getAppFn.constLevels! | unreachable! let α := args[0] let β := args[1] let FDecl ← getLocalDecl F.fvarId! let (motiveNew, w) ← lambdaTelescope args[2] fun xs type => do let type ← mkArrow (FDecl.type.replaceFVar x xs[0]) type return (← mkLambdaFVars xs type, ← getLevel type) let minor ← lambdaTelescope args[4] fun xs body => do let a ← xs[0] let xNew ← xs[1] let valNew ← mkLambdaFVars xs[2:] body let FTypeNew := FDecl.type.replaceFVar x (← mkAppOptM `PSigma.mk #[α, β, a, xNew]) withLocalDeclD FDecl.userName FTypeNew fun FNew => do mkLambdaFVars #[a, xNew, FNew] (← processPSigmaCasesOn xNew FNew valNew k) let result := mkAppN (mkConst ``PSigma.casesOn [w, u, v]) #[α, β, motiveNew, x, minor, F] return result else k F val def mkFix (preDef : PreDefinition) (wfRel : Expr) (decrTactic? : Option Syntax) : TermElabM PreDefinition := do let wfFix ← forallBoundedTelescope preDef.type (some 1) fun x type => do let x := x[0] let α ← inferType x let u ← getLevel α let v ← getLevel type let motive ← mkLambdaFVars #[x] type let rel := mkProj ``WellFoundedRelation 0 wfRel let wf := mkProj ``WellFoundedRelation 1 wfRel return mkApp4 (mkConst ``WellFounded.fix [u, v]) α motive rel wf forallBoundedTelescope (← whnf (← inferType wfFix)).bindingDomain! (some 2) fun xs _ => do let x := xs[0] let F := xs[1] let val := preDef.value.betaRev #[x] let val ← processSumCasesOn x F val fun x F val => processPSigmaCasesOn x F val (replaceRecApps preDef.declName decrTactic?) return { preDef with value := mkApp wfFix (← mkLambdaFVars #[x, F] val) } end Lean.Elab.WF
9271043b2005ca9451333d0a4edb8d9229c453ed
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/topology/order.lean
e1c72754af4b44aae38ce6cf47d06f475ec601b8
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
27,308
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 topology.basic /-! # Ordering on topologies and (co)induced topologies Topologies on a fixed type `α` are ordered, by reverse inclusion. That is, for topologies `t₁` and `t₂` on `α`, we write `t₁ ≤ t₂` if every set open in `t₂` is also open in `t₁`. (One also calls `t₁` finer than `t₂`, and `t₂` coarser than `t₁`.) Any function `f : α → β` induces `induced f : topological_space β → topological_space α` and `coinduced f : topological_space α → topological_space β`. Continuity, the ordering on topologies and (co)induced topologies are related as follows: * The identity map (α, t₁) → (α, t₂) is continuous iff t₁ ≤ t₂. * A map f : (α, t) → (β, u) is continuous iff t ≤ induced f u (`continuous_iff_le_induced`) iff coinduced f t ≤ u (`continuous_iff_coinduced_le`). Topologies on α form a complete lattice, with ⊥ the discrete topology and ⊤ the indiscrete topology. For a function f : α → β, (coinduced f, induced f) is a Galois connection between topologies on α and topologies on β. ## Implementation notes There is a Galois insertion between topologies on α (with the inclusion ordering) and all collections of sets in α. The complete lattice structure on topologies on α is defined as the reverse of the one obtained via this Galois insertion. ## Tags finer, coarser, induced topology, coinduced topology -/ open set filter lattice classical open_locale classical topological_space universes u v w namespace topological_space variables {α : Type u} /-- The open sets of 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) := by rw nhds_def; exact 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⟩, begin revert as, clear_, 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) lemma tendsto_nhds_generate_from {β : Type*} {m : α → β} {f : filter α} {g : set (set β)} {b : β} (h : ∀s∈g, b ∈ s → m ⁻¹' s ∈ f) : 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) /-- Construct a topology on α given the filter of neighborhoods of each point of α. -/ protected def mk_of_nhds (n : α → filter α) : topological_space α := { is_open := λs, ∀a∈s, s ∈ n a, 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 → ∃ t ∈ n a, t ⊆ s ∧ ∀a' ∈ t, s ∈ n a') : @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} ⊆ s := assume b hb, mem_pure_sets.1 $ h₀ b hb, have h₁ : {b | s ∈ n b} ∈ 𝓝 a, { refine mem_nhds_sets (assume b (hb : s ∈ n b), _) 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} /-- The inclusion ordering on topologies on α. We use it to get a complete lattice instance via the Galois insertion method, but the partial order that we will eventually impose on `topological_space α` is the reverse one. -/ def tmp_order : 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₂ } local attribute [instance] tmp_order /- We'll later restate this lemma in terms of the correct order on `topological_space α`. -/ private 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)) /-- If `s` equals the collection of open sets in the topology it generates, then `s` defines a topology. -/ 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 /-- The Galois insertion between `set (set α)` and `topological_space α` whose lower part sends a collection of subsets of α to the topology they generate, and whose upper part sends a topology to its collection of open subsets. -/ 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 /-- The complete lattice of topological spaces, but built on the inclusion ordering. -/ def tmp_complete_lattice {α : Type u} : complete_lattice (topological_space α) := (gi_generate_from α).lift_complete_lattice /-- The ordering on topologies on the type `α`. `t ≤ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/ instance : partial_order (topological_space α) := { le := λ t s, s.is_open ≤ t.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 h₂ h₁ } lemma le_generate_from_iff_subset_is_open {g : set (set α)} {t : topological_space α} : t ≤ topological_space.generate_from g ↔ g ⊆ {s | t.is_open s} := generate_from_le_iff_subset_is_open /-- Topologies on `α` form a complete lattice, with `⊥` the discrete topology and `⊤` the indiscrete topology. The infimum of a collection of topologies is the topology generated by all their open sets, while the supremem is the topology whose open sets are those sets open in every member of the collection. -/ instance : complete_lattice (topological_space α) := @order_dual.lattice.complete_lattice _ tmp_complete_lattice /-- A topological space is discrete if every set is open, that is, its topology equals the discrete topology `⊥`. -/ class discrete_topology (α : Type*) [t : topological_space α] : Prop := (eq_bot : t = ⊥) @[simp] lemma is_open_discrete [topological_space α] [discrete_topology α] (s : set α) : is_open s := (discrete_topology.eq_bot α).symm ▸ trivial lemma continuous_of_discrete_topology [topological_space α] [discrete_topology α] [topological_space β] {f : α → β} : continuous f := λs hs, is_open_discrete _ lemma nhds_bot (α : Type*) : (@nhds α ⊥) = pure := begin refine le_antisymm _ (@pure_le_nhds α ⊥), assume a s hs, exact @mem_nhds_sets α ⊥ a s trivial hs end lemma nhds_discrete (α : Type*) [topological_space α] [discrete_topology α] : (@nhds α _) = pure := (discrete_topology.eq_bot α).symm ▸ nhds_bot α 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, by { simp only [is_open_iff_nhds, le_principal_iff], exact assume hs a ha, h _ $ hs _ ha } 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_bot_of_singletons_open {t : topological_space α} (h : ∀ x, t.is_open {x}) : t = ⊥ := bot_unique $ λ s hs, bUnion_of_singleton s ▸ is_open_bUnion (λ x _, h 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.rfl 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.rfl variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α} lemma coinduced_le_iff_le_induced {f : α → β } {tα : topological_space α} {tβ : topological_space β} : tα.coinduced f ≤ tβ ↔ tα ≤ tβ.induced f := iff.intro (assume h s ⟨t, ht, hst⟩, hst ▸ h _ ht) (assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩) lemma gc_coinduced_induced (f : α → β) : galois_connection (topological_space.coinduced f) (topological_space.induced f) := assume f g, coinduced_le_iff_le_induced lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g := (gc_coinduced_induced g).monotone_u h lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f := (gc_coinduced_induced f).monotone_l h @[simp] lemma induced_top : (⊤ : topological_space α).induced g = ⊤ := (gc_coinduced_induced g).u_top @[simp] lemma induced_inf : (t₁ ⊓ t₂).induced g = t₁.induced g ⊓ t₂.induced g := (gc_coinduced_induced g).u_inf @[simp] lemma induced_infi {ι : Sort w} {t : ι → topological_space α} : (⨅i, t i).induced g = (⨅i, (t i).induced g) := (gc_coinduced_induced g).u_infi @[simp] lemma coinduced_bot : (⊥ : topological_space α).coinduced f = ⊥ := (gc_coinduced_induced f).l_bot @[simp] lemma coinduced_sup : (t₁ ⊔ t₂).coinduced f = t₁.coinduced f ⊔ t₂.coinduced f := (gc_coinduced_induced f).l_sup @[simp] lemma coinduced_supr {ι : Sort w} {t : ι → topological_space α} : (⨆i, t i).coinduced f = (⨆i, (t i).coinduced f) := (gc_coinduced_induced f).l_supr 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 γ] {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}} lemma le_generate_from {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) : t ≤ generate_from g := le_generate_from_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 (le_generate_from $ ball_image_iff.2 $ assume s hs, ⟨s, generate_open.basic _ hs, rfl⟩) (coinduced_le_iff_le_induced.1 $ le_generate_from $ assume s hs, generate_open.basic _ $ mem_image_of_mem _ hs) /-- This construction is left adjoint to the operation sending a topology on `α` to its neighborhood filter at a fixed point `a : α`. -/ protected def topological_space.nhds_adjoint (a : α) (f : filter α) : topological_space α := { is_open := λs, a ∈ s → s ∈ f, 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 (topological_space.nhds_adjoint a) (λt, @nhds α t a) := assume f t, by { rw le_nhds_iff, exact ⟨λ H s hs has, H _ has hs, λ H s has hs, H _ hs has⟩ } lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₁ a ≤ @nhds α t₂ a := (gc_nhds a).monotone_u h lemma nhds_infi {ι : Sort*} {t : ι → topological_space α} {a : α} : @nhds α (infi t) a = (⨅i, @nhds α (t i) a) := (gc_nhds a).u_infi lemma nhds_Inf {s : set (topological_space α)} {a : α} : @nhds α (Inf s) a = (⨅t∈s, @nhds α t a) := (gc_nhds a).u_Inf lemma nhds_inf {t₁ t₂ : topological_space α} {a : α} : @nhds α (t₁ ⊓ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a := (gc_nhds a).u_inf lemma nhds_top {a : α} : @nhds α ⊤ a = ⊤ := (gc_nhds a).u_top local notation `cont` := @continuous _ _ local notation `tspace` := topological_space open topological_space variables {γ : Type*} {f : α → β} {ι : Sort*} lemma continuous_iff_coinduced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ coinduced f t₁ ≤ t₂ := iff.rfl lemma continuous_iff_le_induced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ t₁ ≤ induced f t₂ := iff.trans continuous_iff_coinduced_le (gc_coinduced_induced f _ _) theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f := continuous_iff_coinduced_le.2 $ le_generate_from h lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f := assume s h, ⟨_, h, rfl⟩ lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ} (h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g := assume s ⟨t, ht, s_eq⟩, s_eq ▸ h t ht lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f := assume s h, h lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ} (h : cont t₁ t₂ (g ∘ f)) : cont (coinduced f t₁) t₂ g := assume s hs, h s hs lemma continuous_le_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : t₂ ≤ t₁) (h₂ : cont t₁ t₃ f) : cont t₂ t₃ f := assume s h, h₁ _ (h₂ s h) lemma continuous_le_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : t₂ ≤ t₃) (h₂ : cont t₁ t₂ f) : cont t₁ t₃ f := assume s h, h₂ s (h₁ s h) lemma continuous_sup_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : cont t₁ t₃ f) (h₂ : cont t₂ t₃ f) : cont (t₁ ⊔ t₂) t₃ f := assume s h, ⟨h₁ s h, h₂ s h⟩ lemma continuous_sup_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₂ f → cont t₁ (t₂ ⊔ t₃) f := continuous_le_rng le_sup_left lemma continuous_sup_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₃ f → cont t₁ (t₂ ⊔ t₃) f := continuous_le_rng le_sup_right lemma continuous_Sup_dom {t₁ : set (tspace α)} {t₂ : tspace β} (h : ∀t∈t₁, cont t t₂ f) : cont (Sup t₁) t₂ f := continuous_iff_le_induced.2 $ Sup_le $ assume t ht, continuous_iff_le_induced.1 $ h t ht lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Sup t₂) f := continuous_iff_coinduced_le.2 $ le_Sup_of_le h₁ $ continuous_iff_coinduced_le.1 hf lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β} (h : ∀i, cont (t₁ i) t₂ f) : cont (supr t₁) t₂ f := continuous_Sup_dom $ assume t ⟨i, (t_eq : t₁ i = t)⟩, t_eq ▸ h i lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι} (h : cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f := continuous_Sup_rng ⟨i, rfl⟩ h lemma continuous_inf_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊓ t₃) f := continuous_iff_coinduced_le.2 $ le_inf (continuous_iff_coinduced_le.1 h₁) (continuous_iff_coinduced_le.1 h₂) lemma continuous_inf_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₁ t₃ f → cont (t₁ ⊓ t₂) t₃ f := continuous_le_dom inf_le_left lemma continuous_inf_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₂ t₃ f → cont (t₁ ⊓ t₂) t₃ f := continuous_le_dom inf_le_right lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) : cont t t₂ f → cont (Inf t₁) t₂ f := continuous_le_dom $ Inf_le h₁ lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)} (h : ∀t∈t₂, cont t₁ t f) : cont t₁ (Inf t₂) f := continuous_iff_coinduced_le.2 $ le_Inf $ assume b hb, continuous_iff_coinduced_le.1 $ h b hb lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} : cont (t₁ i) t₂ f → cont (infi t₁) t₂ f := continuous_le_dom $ infi_le _ _ lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β} (h : ∀i, cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f := continuous_iff_coinduced_le.2 $ le_infi $ assume i, continuous_iff_coinduced_le.1 $ h i lemma continuous_bot {t : tspace β} : cont ⊥ t f := continuous_iff_le_induced.2 $ bot_le lemma continuous_top {t : tspace α} : cont t ⊤ f := continuous_iff_coinduced_le.2 $ le_top /- 𝓝 in the induced topology -/ theorem mem_nhds_induced [T : topological_space α] (f : β → α) (a : β) (s : set β) : s ∈ @nhds β (topological_space.induced f T) a ↔ ∃ u ∈ 𝓝 (f a), 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 (𝓝 (f a)) := filter_eq $ by ext s; rw mem_nhds_induced; rw mem_comap_sets lemma induced_iff_nhds_eq [tα : topological_space α] [tβ : topological_space β] (f : β → α) : tβ = tα.induced f ↔ ∀ b, 𝓝 b = comap f (𝓝 $ f b) := ⟨λ h a, h.symm ▸ nhds_induced f a, λ h, eq_of_nhds_eq_nhds $ λ x, by rw [h, nhds_induced]⟩ theorem map_nhds_induced_of_surjective [T : topological_space α] {f : β → α} (hf : function.surjective f) (a : β) : map f (@nhds β (topological_space.induced f T) a) = 𝓝 (f a) := by rw [nhds_induced, map_comap_of_surjective hf] end constructions section induced open topological_space variables {α : Type*} {β : Type*} variables [t : topological_space β] {f : α → β} theorem is_open_induced_eq {s : set α} : @_root_.is_open _ (induced f t) s ↔ s ∈ preimage f '' {s | is_open s} := iff.rfl theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) := ⟨s, h, rfl⟩ lemma map_nhds_induced_eq {a : α} (h : range f ∈ 𝓝 (f a)) : map f (@nhds α (induced f t) a) = 𝓝 (f a) := by rw [nhds_induced, filter.map_comap h] lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} (hf : ∀x y, f x = f y → x = y) : a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) := have comap f (𝓝 (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ 𝓝 (f a) ⊓ principal (f '' s) ≠ ⊥, from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ comap_bot, assume h, forall_sets_ne_empty_iff_ne_bot.mp $ assume s₁ ⟨s₂, hs₂, (hs : f ⁻¹' s₂ ⊆ s₁)⟩, have f '' s ∈ 𝓝 (f a) ⊓ principal (f '' s), from mem_inf_sets_of_right $ by simp [subset.refl], have s₂ ∩ f '' s ∈ 𝓝 (f a) ⊓ principal (f '' s), from inter_mem_sets hs₂ this, let ⟨b, hb₁, ⟨a, ha, ha₂⟩⟩ := inhabited_of_mem_sets h this in ne_empty_of_mem $ hs $ by rwa [←ha₂] at hb₁⟩, calc a ∈ @closure α (topological_space.induced f t) s ↔ (@nhds α (topological_space.induced f t) a) ⊓ principal s ≠ ⊥ : by rw [closure_eq_nhds]; refl ... ↔ comap f (𝓝 (f a)) ⊓ principal (f ⁻¹' (f '' s)) ≠ ⊥ : by rw [nhds_induced, preimage_image_eq _ hf] ... ↔ comap f (𝓝 (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [comap_inf, ←comap_principal] ... ↔ _ : by rwa [closure_eq_nhds] end induced section sierpinski variables {α : Type*} [topological_space α] @[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) := topological_space.generate_open.basic _ (by simp) lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} := ⟨assume h : continuous p, have is_open (p ⁻¹' {true}), from h _ is_open_singleton_true, by simp [preimage, eq_true] at this; assumption, assume h : is_open {x | p x}, continuous_generated_from $ assume s (hs : s ∈ {{true}}), by simp at hs; simp [hs, preimage, eq_true, h]⟩ end sierpinski section infi variables {α : Type u} {ι : Type v} {t : ι → topological_space α} lemma is_open_supr_iff {s : set α} : @is_open _ (⨆ i, t i) s ↔ ∀ i, @is_open _ (t i) s := begin -- s defines a map from α to Prop, which is continuous iff s is open. suffices : @continuous _ _ (⨆ i, t i) _ s ↔ ∀ i, @continuous _ _ (t i) _ s, { simpa only [continuous_Prop] using this }, simp only [continuous_iff_le_induced, supr_le_iff] end lemma is_closed_infi_iff {s : set α} : @is_closed _ (⨆ i, t i) s ↔ ∀ i, @is_closed _ (t i) s := is_open_supr_iff end infi
b764bb3652d557a36cb2dccc4e5b28fec3ac653b
968e2f50b755d3048175f176376eff7139e9df70
/examples/pred_logic/unnamed_278.lean
df951784c984019fd3c1bf8e90f215bafe3e3aa2
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
94
lean
variables (U : Type*) (P : U → Prop) (u : U) example (h : ∀ x, P x) : P u := by exact h u
7bb35e52b84848010a0f1bc9a784efecadbcc378
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/covering/vitali.lean
c8ca760e001c4bb8bc7a90b3527e13a25e1b2987
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
25,961
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.metric_space.basic import measure_theory.constructions.borel_space import measure_theory.covering.vitali_family /-! # Vitali covering theorems The topological Vitali covering theorem, in its most classical version, states the following. Consider a family of balls `(B (x_i, r_i))_{i ∈ I}` in a metric space, with uniformly bounded radii. Then one can extract a disjoint subfamily indexed by `J ⊆ I`, such that any `B (x_i, r_i)` is included in a ball `B (x_j, 5 r_j)`. We prove this theorem in `vitali.exists_disjoint_subfamily_covering_enlargment_closed_ball`. It is deduced from a more general version, called `vitali.exists_disjoint_subfamily_covering_enlargment`, which applies to any family of sets together with a size function `δ` (think "radius" or "diameter"). We deduce the measurable Vitali covering theorem. Assume one is given a family `t` of closed sets with nonempty interior, such that each `a ∈ t` is included in a ball `B (x, r)` and covers a definite proportion of the ball `B (x, 6 r)` for a given measure `μ` (think of the situation where `μ` is a doubling measure and `t` is a family of balls). Consider a set `s` at which the family is fine, i.e., every point of `s` belongs to arbitrarily small elements of `t`. Then one can extract from `t` a disjoint subfamily that covers almost all `s`. It is proved in `vitali.exists_disjoint_covering_ae`. A way to restate this theorem is to say that the set of closed sets `a` with nonempty interior covering a fixed proportion `1/C` of the ball `closed_ball x (3 * diam a)` forms a Vitali family. This version is given in `vitali.vitali_family`. -/ variables {α ι : Type*} open set metric measure_theory topological_space filter open_locale nnreal classical ennreal topological_space namespace vitali /-- Vitali covering theorem: given a set `t` of subsets of a type, one may extract a disjoint subfamily `u` such that the `τ`-enlargment of this family covers all elements of `t`, where `τ > 1` is any fixed number. When `t` is a family of balls, the `τ`-enlargment of `ball x r` is `ball x ((1+2τ) r)`. In general, it is expressed in terms of a function `δ` (think "radius" or "diameter"), positive and bounded on all elements of `t`. The condition is that every element `a` of `t` should intersect an element `b` of `u` of size larger than that of `a` up to `τ`, i.e., `δ b ≥ δ a / τ`. We state the lemma slightly more generally, with an indexed family of sets `B a` for `a ∈ t`, for wider applicability. -/ theorem exists_disjoint_subfamily_covering_enlargment (B : ι → set α) (t : set ι) (δ : ι → ℝ) (τ : ℝ) (hτ : 1 < τ) (δnonneg : ∀ a ∈ t, 0 ≤ δ a) (R : ℝ) (δle : ∀ a ∈ t, δ a ≤ R) (hne : ∀ a ∈ t, (B a).nonempty) : ∃ u ⊆ t, u.pairwise_disjoint B ∧ ∀ a ∈ t, ∃ b ∈ u, (B a ∩ B b).nonempty ∧ δ a ≤ τ * δ b := begin /- The proof could be formulated as a transfinite induction. First pick an element of `t` with `δ` as large as possible (up to a factor of `τ`). Then among the remaining elements not intersecting the already chosen one, pick another element with large `δ`. Go on forever (transfinitely) until there is nothing left. Instead, we give a direct Zorn-based argument. Consider a maximal family `u` of disjoint sets with the following property: if an element `a` of `t` intersects some element `b` of `u`, then it intersects some `b' ∈ u` with `δ b' ≥ δ a / τ`. Such a maximal family exists by Zorn. If this family did not intersect some element `a ∈ t`, then take an element `a' ∈ t` which does not intersect any element of `u`, with `δ a'` almost as large as possible. One checks easily that `u ∪ {a'}` still has this property, contradicting the maximality. Therefore, `u` intersects all elements of `t`, and by definition it satisfies all the desired properties. -/ let T : set (set ι) := {u | u ⊆ t ∧ u.pairwise_disjoint B ∧ ∀ a ∈ t, ∀ b ∈ u, (B a ∩ B b).nonempty → ∃ c ∈ u, (B a ∩ B c).nonempty ∧ δ a ≤ τ * δ c}, -- By Zorn, choose a maximal family in the good set `T` of disjoint families. obtain ⟨u, uT, hu⟩ : ∃ u ∈ T, ∀ v ∈ T, u ⊆ v → v = u, { refine zorn_subset _ (λ U UT hU, _), refine ⟨⋃₀ U, _, λ s hs, subset_sUnion_of_mem hs⟩, simp only [set.sUnion_subset_iff, and_imp, exists_prop, forall_exists_index, mem_sUnion, set.mem_set_of_eq], refine ⟨λ u hu, (UT hu).1, (pairwise_disjoint_sUnion hU.directed_on).2 (λ u hu, (UT hu).2.1), λ a hat b u uU hbu hab, _⟩, obtain ⟨c, cu, ac, hc⟩ : ∃ (c : ι) (H : c ∈ u), (B a ∩ B c).nonempty ∧ δ a ≤ τ * δ c := (UT uU).2.2 a hat b hbu hab, exact ⟨c, ⟨u, uU, cu⟩, ac, hc⟩ }, -- the only nontrivial bit is to check that every `a ∈ t` intersects an element `b ∈ u` with -- comparatively large `δ b`. Assume this is not the case, then we will contradict the maximality. refine ⟨u, uT.1, uT.2.1, λ a hat, _⟩, contrapose! hu, have a_disj : ∀ c ∈ u, disjoint (B a) (B c), { assume c hc, by_contra, rw not_disjoint_iff_nonempty_inter at h, obtain ⟨d, du, ad, hd⟩ : ∃ (d : ι) (H : d ∈ u), (B a ∩ B d).nonempty ∧ δ a ≤ τ * δ d := uT.2.2 a hat c hc h, exact lt_irrefl _ ((hu d du ad).trans_le hd) }, -- Let `A` be all the elements of `t` which do not intersect the family `u`. It is nonempty as it -- contains `a`. We will pick an element `a'` of `A` with `δ a'` almost as large as possible. let A := {a' | a' ∈ t ∧ ∀ c ∈ u, disjoint (B a') (B c)}, have Anonempty : A.nonempty := ⟨a, hat, a_disj⟩, let m := Sup (δ '' A), have bddA : bdd_above (δ '' A), { refine ⟨R, λ x xA, _⟩, rcases (mem_image _ _ _).1 xA with ⟨a', ha', rfl⟩, exact δle a' ha'.1 }, obtain ⟨a', a'A, ha'⟩ : ∃ a' ∈ A, m / τ ≤ δ a', { have : 0 ≤ m := (δnonneg a hat).trans (le_cSup bddA (mem_image_of_mem _ ⟨hat, a_disj⟩)), rcases eq_or_lt_of_le this with mzero|mpos, { refine ⟨a, ⟨hat, a_disj⟩, _⟩, simpa only [← mzero, zero_div] using δnonneg a hat }, { have I : m / τ < m, { rw div_lt_iff (zero_lt_one.trans hτ), conv_lhs { rw ← mul_one m }, exact (mul_lt_mul_left mpos).2 hτ }, rcases exists_lt_of_lt_cSup (nonempty_image_iff.2 Anonempty) I with ⟨x, xA, hx⟩, rcases (mem_image _ _ _).1 xA with ⟨a', ha', rfl⟩, exact ⟨a', ha', hx.le⟩, } }, clear hat hu a_disj a, have a'_ne_u : a' ∉ u := λ H, (hne _ a'A.1).ne_empty (disjoint_self.1 (a'A.2 _ H)), -- we claim that `u ∪ {a'}` still belongs to `T`, contradicting the maximality of `u`. refine ⟨insert a' u, ⟨_, _, _⟩, subset_insert _ _, (ne_insert_of_not_mem _ a'_ne_u).symm⟩, -- check that `u ∪ {a'}` is made of elements of `t`. { rw insert_subset, exact ⟨a'A.1, uT.1⟩ }, -- check that `u ∪ {a'}` is a disjoint family. This follows from the fact that `a'` does not -- intersect `u`. { exact uT.2.1.insert (λ b bu ba', a'A.2 b bu) }, -- check that every element `c` of `t` intersecting `u ∪ {a'}` intersects an element of this -- family with large `δ`. { assume c ct b ba'u hcb, -- if `c` already intersects an element of `u`, then it intersects an element of `u` with -- large `δ` by the assumption on `u`, and there is nothing left to do. by_cases H : ∃ d ∈ u, (B c ∩ B d).nonempty, { rcases H with ⟨d, du, hd⟩, rcases uT.2.2 c ct d du hd with ⟨d', d'u, hd'⟩, exact ⟨d', mem_insert_of_mem _ d'u, hd'⟩ }, -- otherwise, `c` belongs to `A`. The element of `u ∪ {a'}` that it intersects has to be `a'`. -- moreover, `δ c` is smaller than the maximum `m` of `δ` over `A`, which is `≤ δ a' / τ` -- thanks to the good choice of `a'`. This is the desired inequality. { push_neg at H, simp only [← not_disjoint_iff_nonempty_inter, not_not] at H, rcases mem_insert_iff.1 ba'u with rfl|H', { refine ⟨b, mem_insert _ _, hcb, _⟩, calc δ c ≤ m : le_cSup bddA (mem_image_of_mem _ ⟨ct, H⟩) ... = τ * (m / τ) : by { field_simp [(zero_lt_one.trans hτ).ne'], ring } ... ≤ τ * δ b : mul_le_mul_of_nonneg_left ha' (zero_le_one.trans hτ.le) }, { rw ← not_disjoint_iff_nonempty_inter at hcb, exact (hcb (H _ H')).elim } } } end /-- Vitali covering theorem, closed balls version: given a family `t` of closed balls, one can extract a disjoint subfamily `u ⊆ t` so that all balls in `t` are covered by the 5-times dilations of balls in `u`. -/ theorem exists_disjoint_subfamily_covering_enlargment_closed_ball [metric_space α] (t : set ι) (x : ι → α) (r : ι → ℝ) (R : ℝ) (hr : ∀ a ∈ t, r a ≤ R) : ∃ u ⊆ t, u.pairwise_disjoint (λ a, closed_ball (x a) (r a)) ∧ ∀ a ∈ t, ∃ b ∈ u, closed_ball (x a) (r a) ⊆ closed_ball (x b) (5 * r b) := begin rcases eq_empty_or_nonempty t with rfl|tnonempty, { exact ⟨∅, subset.refl _, pairwise_disjoint_empty, by simp⟩ }, by_cases ht : ∀ a ∈ t, r a < 0, { exact ⟨t, subset.rfl, λ a ha b hb hab, by simp only [function.on_fun, closed_ball_eq_empty.2 (ht a ha), empty_disjoint], λ a ha, ⟨a, ha, by simp only [closed_ball_eq_empty.2 (ht a ha), empty_subset]⟩⟩ }, push_neg at ht, let t' := {a ∈ t | 0 ≤ r a}, rcases exists_disjoint_subfamily_covering_enlargment (λ a, closed_ball (x a) (r a)) t' r 2 one_lt_two (λ a ha, ha.2) R (λ a ha, hr a ha.1) (λ a ha, ⟨x a, mem_closed_ball_self ha.2⟩) with ⟨u, ut', u_disj, hu⟩, have A : ∀ a ∈ t', ∃ b ∈ u, closed_ball (x a) (r a) ⊆ closed_ball (x b) (5 * r b), { assume a ha, rcases hu a ha with ⟨b, bu, hb, rb⟩, refine ⟨b, bu, _⟩, have : dist (x a) (x b) ≤ r a + r b := dist_le_add_of_nonempty_closed_ball_inter_closed_ball hb, apply closed_ball_subset_closed_ball', linarith }, refine ⟨u, ut'.trans (λ a ha, ha.1), u_disj, λ a ha, _⟩, rcases le_or_lt 0 (r a) with h'a|h'a, { exact A a ⟨ha, h'a⟩ }, { rcases ht with ⟨b, rb⟩, rcases A b ⟨rb.1, rb.2⟩ with ⟨c, cu, hc⟩, refine ⟨c, cu, by simp only [closed_ball_eq_empty.2 h'a, empty_subset]⟩ }, end /-- The measurable Vitali covering theorem. Assume one is given a family `t` of closed sets with nonempty interior, such that each `a ∈ t` is included in a ball `B (x, r)` and covers a definite proportion of the ball `B (x, 3 r)` for a given measure `μ` (think of the situation where `μ` is a doubling measure and `t` is a family of balls). Consider a (possibly non-measurable) set `s` at which the family is fine, i.e., every point of `s` belongs to arbitrarily small elements of `t`. Then one can extract from `t` a disjoint subfamily that covers almost all `s`. For more flexibility, we give a statement with a parameterized family of sets. -/ theorem exists_disjoint_covering_ae [metric_space α] [measurable_space α] [opens_measurable_space α] [second_countable_topology α] (μ : measure α) [is_locally_finite_measure μ] (s : set α) (t : set ι) (C : ℝ≥0) (r : ι → ℝ) (c : ι → α) (B : ι → set α) (hB : ∀ a ∈ t, B a ⊆ closed_ball (c a) (r a)) (μB : ∀ a ∈ t, μ (closed_ball (c a) (3 * r a)) ≤ C * μ (B a)) (ht : ∀ a ∈ t, (interior (B a)).nonempty) (h't : ∀ a ∈ t, is_closed (B a)) (hf : ∀ x ∈ s, ∀ (ε > (0 : ℝ)), ∃ a ∈ t, r a ≤ ε ∧ c a = x) : ∃ u ⊆ t, u.countable ∧ u.pairwise_disjoint B ∧ μ (s \ ⋃ a ∈ u, B a) = 0 := begin /- The idea of the proof is the following. Assume for simplicity that `μ` is finite. Applying the abstract Vitali covering theorem with `δ = r` given by `hf`, one obtains a disjoint subfamily `u`, such that any element of `t` intersects an element of `u` with comparable radius. Fix `ε > 0`. Since the elements of `u` have summable measure, one can remove finitely elements `w_1, ..., w_n`. so that the measure of the remaining elements is `< ε`. Consider now a point `z` not in the `w_i`. There is a small ball around `z` not intersecting the `w_i` (as they are closed), an element `a ∈ t` contained in this small ball (as the family `t` is fine at `z`) and an element `b ∈ u` intersecting `a`, with comparable radius (by definition of `u`). Then `z` belongs to the enlargement of `b`. This shows that `s \ (w_1 ∪ ... ∪ w_n)` is contained in `⋃ (b ∈ u \ {w_1, ... w_n}) (enlargement of b)`. The measure of the latter set is bounded by `∑ (b ∈ u \ {w_1, ... w_n}) C * μ b` (by the doubling property of the measure), which is at most `C ε`. Letting `ε` tend to `0` shows that `s` is almost everywhere covered by the family `u`. For the real argument, the measure is only locally finite. Therefore, we implement the same strategy, but locally restricted to balls on which the measure is finite. For this, we do not use the whole family `t`, but a subfamily `t'` supported on small balls (which is possible since the family is assumed to be fine at every point of `s`). -/ -- choose around each `x` a small ball on which the measure is finite have : ∀ x, ∃ R, 0 < R ∧ R ≤ 1 ∧ μ (closed_ball x (20 * R)) < ∞, { assume x, obtain ⟨R, Rpos, μR⟩ : ∃ (R : ℝ) (hR : 0 < R), μ (closed_ball x R) < ∞ := (μ.finite_at_nhds x).exists_mem_basis nhds_basis_closed_ball, refine ⟨min 1 (R/20), _, min_le_left _ _, _⟩, { simp only [true_and, lt_min_iff, zero_lt_one], linarith }, { apply lt_of_le_of_lt (measure_mono _) μR, apply closed_ball_subset_closed_ball, calc 20 * min 1 (R / 20) ≤ 20 * (R/20) : mul_le_mul_of_nonneg_left (min_le_right _ _) (by norm_num) ... = R : by ring } }, choose R hR0 hR1 hRμ, -- we restrict to a subfamily `t'` of `t`, made of elements small enough to ensure that -- they only see a finite part of the measure, and with a doubling property let t' := {a ∈ t | r a ≤ R (c a)}, -- extract a disjoint subfamily `u` of `t'` thanks to the abstract Vitali covering theorem. obtain ⟨u, ut', u_disj, hu⟩ : ∃ u ⊆ t', u.pairwise_disjoint B ∧ ∀ a ∈ t', ∃ b ∈ u, (B a ∩ B b).nonempty ∧ r a ≤ 2 * r b, { have A : ∀ a ∈ t', r a ≤ 1, { assume a ha, apply ha.2.trans (hR1 (c a)), }, have A' : ∀ a ∈ t', (B a).nonempty := λ a hat', set.nonempty.mono interior_subset (ht a hat'.1), refine exists_disjoint_subfamily_covering_enlargment B t' r 2 one_lt_two (λ a ha, _) 1 A A', exact nonempty_closed_ball.1 ((A' a ha).mono (hB a ha.1)) }, have ut : u ⊆ t := λ a hau, (ut' hau).1, -- As the space is second countable, the family is countable since all its sets have nonempty -- interior. have u_count : u.countable := u_disj.countable_of_nonempty_interior (λ a ha, ht a (ut ha)), -- the family `u` will be the desired family refine ⟨u, λ a hat', (ut' hat').1, u_count, u_disj, _⟩, -- it suffices to show that it covers almost all `s` locally around each point `x`. refine null_of_locally_null _ (λ x hx, _), -- let `v` be the subfamily of `u` made of those sets intersecting the small ball `ball x (r x)` let v := {a ∈ u | (B a ∩ ball x (R x)).nonempty }, have vu : v ⊆ u := λ a ha, ha.1, -- they are all contained in a fixed ball of finite measure, thanks to our choice of `t'` obtain ⟨K, μK, hK⟩ : ∃ K, μ (closed_ball x K) < ∞ ∧ ∀ a ∈ u, (B a ∩ ball x (R x)).nonempty → B a ⊆ closed_ball x K, { have Idist_v : ∀ a ∈ v, dist (c a) x ≤ r a + R x, { assume a hav, apply dist_le_add_of_nonempty_closed_ball_inter_closed_ball, refine hav.2.mono _, apply inter_subset_inter _ ball_subset_closed_ball, exact hB a (ut (vu hav)) }, set R0 := Sup (r '' v) with R0_def, have R0_bdd : bdd_above (r '' v), { refine ⟨1, λ r' hr', _⟩, rcases (mem_image _ _ _).1 hr' with ⟨b, hb, rfl⟩, exact le_trans (ut' (vu hb)).2 (hR1 (c b)) }, rcases le_total R0 (R x) with H|H, { refine ⟨20 * R x, hRμ x, λ a au hax, _⟩, refine (hB a (ut au)).trans _, apply closed_ball_subset_closed_ball', have : r a ≤ R0 := le_cSup R0_bdd (mem_image_of_mem _ ⟨au, hax⟩), linarith [Idist_v a ⟨au, hax⟩, hR0 x] }, { have R0pos : 0 < R0 := (hR0 x).trans_le H, have vnonempty : v.nonempty, { by_contra, rw [← ne_empty_iff_nonempty, not_not] at h, simp only [h, real.Sup_empty, image_empty] at R0_def, exact lt_irrefl _ (R0pos.trans_le (le_of_eq R0_def)) }, obtain ⟨a, hav, R0a⟩ : ∃ a ∈ v, R0/2 < r a, { obtain ⟨r', r'mem, hr'⟩ : ∃ r' ∈ r '' v, R0 / 2 < r' := exists_lt_of_lt_cSup (nonempty_image_iff.2 vnonempty) (half_lt_self R0pos), rcases (mem_image _ _ _).1 r'mem with ⟨a, hav, rfl⟩, exact ⟨a, hav, hr'⟩ }, refine ⟨8 * R0, _, _⟩, { apply lt_of_le_of_lt (measure_mono _) (hRμ (c a)), apply closed_ball_subset_closed_ball', rw dist_comm, linarith [Idist_v a hav, (ut' (vu hav)).2] }, { assume b bu hbx, refine (hB b (ut bu)).trans _, apply closed_ball_subset_closed_ball', have : r b ≤ R0 := le_cSup R0_bdd (mem_image_of_mem _ ⟨bu, hbx⟩), linarith [Idist_v b ⟨bu, hbx⟩] } } }, -- we will show that, in `ball x (R x)`, almost all `s` is covered by the family `u`. refine ⟨_ ∩ ball x (R x), inter_mem_nhds_within _ (ball_mem_nhds _ (hR0 _)), nonpos_iff_eq_zero.mp (le_of_forall_le_of_dense (λ ε εpos, _))⟩, -- the elements of `v` are disjoint and all contained in a finite volume ball, hence the sum -- of their measures is finite. have I : ∑' (a : v), μ (B a) < ∞, { calc ∑' (a : v), μ (B a) = μ (⋃ (a ∈ v), B a) : begin rw measure_bUnion (u_count.mono vu) _ (λ a ha, (h't _ (vu.trans ut ha)).measurable_set), exact u_disj.subset vu end ... ≤ μ (closed_ball x K) : measure_mono (Union₂_subset (λ a ha, hK a (vu ha) ha.2)) ... < ∞ : μK }, -- we can obtain a finite subfamily of `v`, such that the measures of the remaining elements -- add up to an arbitrarily small number, say `ε / C`. obtain ⟨w, hw⟩ : ∃ (w : finset ↥v), ∑' (a : {a // a ∉ w}), μ (B a) < ε / C, { have : 0 < ε / C, by simp only [ennreal.div_pos_iff, εpos.ne', ennreal.coe_ne_top, ne.def, not_false_iff, and_self], exact ((tendsto_order.1 (ennreal.tendsto_tsum_compl_at_top_zero I.ne)).2 _ this).exists }, -- main property: the points `z` of `s` which are not covered by `u` are contained in the -- enlargements of the elements not in `w`. have M : (s \ ⋃ a ∈ u, B a) ∩ ball x (R x) ⊆ ⋃ (a : {a // a ∉ w}), closed_ball (c a) (3 * r a), { assume z hz, set k := ⋃ (a : v) (ha : a ∈ w), B a with hk, have k_closed : is_closed k := is_closed_bUnion w.finite_to_set (λ i hi, h't _ (ut (vu i.2))), have z_notmem_k : z ∉ k, { simp only [not_exists, exists_prop, mem_Union, mem_sep_iff, forall_exists_index, set_coe.exists, not_and, exists_and_distrib_right, subtype.coe_mk], assume b hbv h'b h'z, have : z ∈ (s \ ⋃ a ∈ u, B a) ∩ (⋃ a ∈ u, B a) := mem_inter (mem_of_mem_inter_left hz) (mem_bUnion (vu hbv) h'z), simpa only [diff_inter_self] }, -- since the elements of `w` are closed and finitely many, one can find a small ball around `z` -- not intersecting them have : ball x (R x) \ k ∈ 𝓝 z, { apply is_open.mem_nhds (is_open_ball.sdiff k_closed) _, exact (mem_diff _).2 ⟨mem_of_mem_inter_right hz, z_notmem_k⟩ }, obtain ⟨d, dpos, hd⟩ : ∃ (d : ℝ) (dpos : 0 < d), closed_ball z d ⊆ ball x (R x) \ k := nhds_basis_closed_ball.mem_iff.1 this, -- choose an element `a` of the family `t` contained in this small ball obtain ⟨a, hat, ad, rfl⟩ : ∃ a ∈ t, r a ≤ min d (R z) ∧ c a = z, from hf z ((mem_diff _).1 (mem_of_mem_inter_left hz)).1 (min d (R z)) (lt_min dpos (hR0 z)), have ax : B a ⊆ ball x (R x), { refine (hB a hat).trans _, refine subset.trans _ (hd.trans (diff_subset (ball x (R x)) k)), exact closed_ball_subset_closed_ball (ad.trans (min_le_left _ _)), }, -- it intersects an element `b` of `u` with comparable diameter, by definition of `u` obtain ⟨b, bu, ab, bdiam⟩ : ∃ b ∈ u, (B a ∩ B b).nonempty ∧ r a ≤ 2 * r b, from hu a ⟨hat, ad.trans (min_le_right _ _)⟩, have bv : b ∈ v, { refine ⟨bu, ab.mono _⟩, rw inter_comm, exact inter_subset_inter_right _ ax }, let b' : v := ⟨b, bv⟩, -- `b` can not belong to `w`, as the elements of `w` do not intersect `closed_ball z d`, -- contrary to `b` have b'_notmem_w : b' ∉ w, { assume b'w, have b'k : B b' ⊆ k, from @finset.subset_set_bUnion_of_mem _ _ _ (λ (y : v), B y) _ b'w, have : ((ball x (R x) \ k) ∩ k).nonempty, { apply ab.mono (inter_subset_inter _ b'k), refine ((hB _ hat).trans _).trans hd, exact (closed_ball_subset_closed_ball (ad.trans (min_le_left _ _))) }, simpa only [diff_inter_self, not_nonempty_empty] }, let b'' : {a // a ∉ w} := ⟨b', b'_notmem_w⟩, -- since `a` and `b` have comparable diameters, it follows that `z` belongs to the -- enlargement of `b` have zb : c a ∈ closed_ball (c b) (3 * r b), { rcases ab with ⟨e, ⟨ea, eb⟩⟩, have A : dist (c a) e ≤ r a, from mem_closed_ball'.1 (hB a hat ea), have B : dist e (c b) ≤ r b, from mem_closed_ball.1 (hB b (ut bu) eb), simp only [mem_closed_ball], linarith [dist_triangle (c a) e (c b)] }, suffices H : closed_ball (c b'') (3 * r b'') ⊆ ⋃ (a : {a // a ∉ w}), closed_ball (c a) (3 * r a), from H zb, exact subset_Union (λ (a : {a // a ∉ w}), closed_ball (c a) (3 * r a)) b'' }, -- now that we have proved our main inclusion, we can use it to estimate the measure of the points -- in `ball x (r x)` not covered by `u`. haveI : encodable v := (u_count.mono vu).to_encodable, calc μ ((s \ ⋃ a ∈ u, B a) ∩ ball x (R x)) ≤ μ (⋃ (a : {a // a ∉ w}), closed_ball (c a) (3 * r a)) : measure_mono M ... ≤ ∑' (a : {a // a ∉ w}), μ (closed_ball (c a) (3 * r a)) : measure_Union_le _ ... ≤ ∑' (a : {a // a ∉ w}), C * μ (B a) : ennreal.tsum_le_tsum (λ a, μB a (ut (vu a.1.2))) ... = C * ∑' (a : {a // a ∉ w}), μ (B a) : ennreal.tsum_mul_left ... ≤ C * (ε / C) : ennreal.mul_le_mul le_rfl hw.le ... ≤ ε : ennreal.mul_div_le end /-- Assume that around every point there are arbitrarily small scales at which the measure is doubling. Then the set of closed sets `a` with nonempty interior contained in `closed_ball x r` and covering a fixed proportion `1/C` of the ball `closed_ball x (3 * r)` forms a Vitali family. This is essentially a restatement of the measurable Vitali theorem. -/ protected def vitali_family [metric_space α] [measurable_space α] [opens_measurable_space α] [second_countable_topology α] (μ : measure α) [is_locally_finite_measure μ] (C : ℝ≥0) (h : ∀ x, ∃ᶠ r in 𝓝[>] 0, μ (closed_ball x (3 * r)) ≤ C * μ (closed_ball x r)) : vitali_family μ := { sets_at := λ x, {a | is_closed a ∧ (interior a).nonempty ∧ ∃ r, (a ⊆ closed_ball x r ∧ μ (closed_ball x (3 * r)) ≤ C * μ a)}, measurable_set' := λ x a ha, ha.1.measurable_set, nonempty_interior := λ x a ha, ha.2.1, nontrivial := λ x ε εpos, begin obtain ⟨r, μr, rpos, rε⟩ : ∃ r, μ (closed_ball x (3 * r)) ≤ C * μ (closed_ball x r) ∧ r ∈ Ioc (0 : ℝ) ε := ((h x).and_eventually (Ioc_mem_nhds_within_Ioi ⟨le_rfl, εpos⟩)).exists, refine ⟨closed_ball x r, ⟨is_closed_ball, _, ⟨r, subset.rfl, μr⟩⟩, closed_ball_subset_closed_ball rε⟩, exact (nonempty_ball.2 rpos).mono (ball_subset_interior_closed_ball) end, covering := begin assume s f fsubset ffine, let t : set (ℝ × α × set α) := {p | p.2.2 ⊆ closed_ball p.2.1 p.1 ∧ μ (closed_ball p.2.1 (3 * p.1)) ≤ C * μ p.2.2 ∧ (interior p.2.2).nonempty ∧ is_closed p.2.2 ∧ p.2.2 ∈ f p.2.1 ∧ p.2.1 ∈ s}, have A : ∀ x ∈ s, ∀ (ε : ℝ), ε > 0 → (∃ (p : ℝ × α × set α) (Hp : p ∈ t), p.1 ≤ ε ∧ p.2.1 = x), { assume x xs ε εpos, rcases ffine x xs ε εpos with ⟨a, ha, h'a⟩, rcases fsubset x xs ha with ⟨a_closed, a_int, ⟨r, ar, μr⟩⟩, refine ⟨⟨min r ε, x, a⟩, ⟨_, _, a_int, a_closed, ha, xs⟩, min_le_right _ _, rfl⟩, { rcases min_cases r ε with h'|h'; rwa h'.1 }, { apply le_trans (measure_mono (closed_ball_subset_closed_ball _)) μr, exact mul_le_mul_of_nonneg_left (min_le_left _ _) zero_le_three } }, rcases exists_disjoint_covering_ae μ s t C (λ p, p.1) (λ p, p.2.1) (λ p, p.2.2) (λ p hp, hp.1) (λ p hp, hp.2.1) (λ p hp, hp.2.2.1) (λ p hp, hp.2.2.2.1) A with ⟨t', t't, t'_count, t'_disj, μt'⟩, refine ⟨(λ (p : ℝ × α × set α), p.2) '' t', _, _, _, _⟩, { rintros - ⟨q, hq, rfl⟩, exact (t't hq).2.2.2.2.2 }, { rintros p ⟨q, hq, rfl⟩ p' ⟨q', hq', rfl⟩ hqq', exact t'_disj hq hq' (ne_of_apply_ne _ hqq') }, { rintros - ⟨q, hq, rfl⟩, exact (t't hq).2.2.2.2.1 }, { convert μt' using 3, rw bUnion_image } end } end vitali
0d77e635e4fa7dc8cfb1c2ae63c3d057cebb9c75
bdb33f8b7ea65f7705fc342a178508e2722eb851
/algebra/module.lean
422eb4296cef51bcde601bdddf2e8592b03fa1a9
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
10,321
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl Modules over a ring. -/ import algebra.ring algebra.big_operators data.set.lattice open function universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} lemma set.sInter_eq_Inter {s : set (set α)} : (⋂₀ s) = (⋂i ∈ s, i) := set.ext $ by simp /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/ class has_scalar (α : out_param $ Type u) (γ : Type v) := (smul : α → γ → γ) infixr ` • `:73 := has_scalar.smul /-- A module is a generalization of vector spaces to a scalar ring. It consists of a scalar ring `α` and an additive group of "vectors" `β`, connected by a "scalar multiplication" operation `r • x : β` (where `r : α` and `x : β`) with some natural associativity and distributivity axioms similar to those on a ring. -/ class module (α : out_param $ Type u) (β : Type v) [out_param $ ring α] extends has_scalar α β, add_comm_group β := (smul_add : ∀r (x y : β), r • (x + y) = r • x + r • y) (add_smul : ∀r s (x : β), (r + s) • x = r • x + s • x) (mul_smul : ∀r s (x : β), (r * s) • x = r • s • x) (one_smul : ∀x : β, (1 : α) • x = x) section module variables [ring α] [module α β] {r s : α} {x y : β} theorem smul_add : r • (x + y) = r • x + r • y := module.smul_add r x y theorem add_smul : (r + s) • x = r • x + s • x := module.add_smul r s x theorem mul_smul : (r * s) • x = r • s • x := module.mul_smul r s x @[simp] theorem one_smul : (1 : α) • x = x := module.one_smul x @[simp] theorem zero_smul : (0 : α) • x = 0 := have (0 : α) • x + 0 • x = 0 • x + 0, by rw ← add_smul; simp, add_left_cancel this @[simp] theorem smul_zero : r • (0 : β) = 0 := have r • (0:β) + r • 0 = r • 0 + 0, by rw ← smul_add; simp, add_left_cancel this @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) theorem neg_one_smul (x : β) : (-1 : α) • x = -x := by simp @[simp] theorem smul_neg : r • (-x) = -(r • x) := by rw [← neg_one_smul x, ← mul_smul, mul_neg_one, neg_smul] theorem smul_sub (r : α) (x y : β) : r • (x - y) = r • x - r • y := by simp [smul_add] theorem sub_smul (r s : α) (y : β) : (r - s) • y = r • y - s • y := by simp [add_smul] lemma smul_smul : r • s • x = (r * s) • x := mul_smul.symm end module instance ring.to_module [r : ring α] : module α α := { smul := (*), smul_add := mul_add, add_smul := add_mul, mul_smul := mul_assoc, one_smul := one_mul, ..r } @[simp] lemma smul_eq_mul [ring α] {a a' : α} : a • a' = a * a' := rfl structure is_linear_map {α : Type u} {β : Type v} {γ : Type w} [ring α] [module α β] [module α γ] (f : β → γ) : Prop := (add : ∀x y, f (x + y) = f x + f y) (smul : ∀c x, f (c • x) = c • f x) namespace is_linear_map variables [ring α] [module α β] [module α γ] [module α δ] variables {f g h : β → γ} {r : α} {x y : β} include α section variable (hf : is_linear_map f) include hf @[simp] lemma zero : f 0 = 0 := calc f 0 = f (0 • 0 : β) : by rw [zero_smul] ... = 0 : by rw [hf.smul]; simp @[simp] lemma neg (x : β) : f (- x) = - f x := eq_neg_of_add_eq_zero $ by rw [←hf.add]; simp [hf.zero] @[simp] lemma sub (x y : β) : f (x - y) = f x - f y := by simp [hf.neg, hf.add] @[simp] lemma sum {ι : Type x} {t : finset ι} {g : ι → β} : f (t.sum g) = t.sum (λi, f (g i)) := (finset.sum_hom f hf.zero hf.add).symm end lemma comp {g : δ → β} (hf : is_linear_map f) (hg : is_linear_map g) : is_linear_map (f ∘ g) := by refine {..}; simp [(∘), hg.add, hf.add, hg.smul, hf.smul] lemma id : is_linear_map (id : β → β) := by refine {..}; simp lemma inverse {f : γ → β} {g : β → γ} (hf : is_linear_map f) (h₁ : left_inverse g f) (h₂ : right_inverse g f): is_linear_map g := ⟨assume x y, have g (f (g (x + y))) = g (f (g x + g y)), by rw [h₂ (x + y), hf.add, h₂ x, h₂ y], by rwa [h₁ (g (x + y)), h₁ (g x + g y)] at this, assume a b, have g (f (g (a • b))) = g (f (a • g b)), by rw [h₂ (a • b), hf.smul, h₂ b], by rwa [h₁ (g (a • b)), h₁ (a • g b)] at this ⟩ lemma map_zero : is_linear_map (λb, 0 : β → γ) := by refine {..}; simp lemma map_neg (hf : is_linear_map f) : is_linear_map (λb, - f b) := by refine {..}; simp [hf.add, hf.smul] lemma map_add (hf : is_linear_map f) (hg : is_linear_map g) : is_linear_map (λb, f b + g b) := by refine {..}; simp [hg.add, hf.add, hg.smul, hf.smul, smul_add] lemma map_sum [decidable_eq δ] {t : finset δ} {f : δ → β → γ} : (∀d∈t, is_linear_map (f d)) → is_linear_map (λb, t.sum $ λd, f d b) := finset.induction_on t (by simp [map_zero]) (by simp [map_add] {contextual := tt}) lemma map_sub (hf : is_linear_map f) (hg : is_linear_map g) : is_linear_map (λb, f b - g b) := by refine {..}; simp [hg.add, hf.add, hg.smul, hf.smul, smul_add] lemma map_smul_right {α : Type u} {β : Type v} {γ : Type w} [comm_ring α] [module α β] [module α γ] {f : β → γ} {r : α} (hf : is_linear_map f) : is_linear_map (λb, r • f b) := by refine {..}; simp [hf.add, hf.smul, smul_add, smul_smul, mul_comm] lemma map_smul_left {f : γ → α} (hf : is_linear_map f) : is_linear_map (λb, f b • x) := by refine {..}; simp [hf.add, hf.smul, add_smul, smul_smul] end is_linear_map /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ class is_submodule {α : Type u} {β : Type v} [ring α] [module α β] (p : set β) : Prop := (zero_ : (0:β) ∈ p) (add_ : ∀ {x y}, x ∈ p → y ∈ p → x + y ∈ p) (smul : ∀ c {x}, x ∈ p → c • x ∈ p) namespace is_submodule variables [ring α] [module α β] [module α γ] variables {p p' : set β} [is_submodule p] [is_submodule p'] variables {r : α} include α section variables {x y : β} lemma zero : (0 : β) ∈ p := is_submodule.zero_ α p lemma add : x ∈ p → y ∈ p → x + y ∈ p := is_submodule.add_ α lemma neg (hx : x ∈ p) : -x ∈ p := by rw ← neg_one_smul x; exact smul _ hx lemma sub (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := add hx (neg hy) lemma sum {ι : Type w} [decidable_eq ι] {t : finset ι} {f : ι → β} : (∀c∈t, f c ∈ p) → t.sum f ∈ p := finset.induction_on t (by simp [zero]) (by simp [add] {contextual := tt}) lemma smul_ne_0 {a : α} {b : β} (h : a ≠ 0 → b ∈ p) : a • b ∈ p := classical.by_cases (assume : a = 0, by simp [this, zero]) (assume : a ≠ 0, by simp [h this, smul]) instance single_zero : is_submodule ({0} : set β) := by refine {..}; by simp {contextual := tt} instance univ : is_submodule (set.univ : set β) := by refine {..}; by simp {contextual := tt} instance image {f : β → γ} (hf : is_linear_map f) : is_submodule (f '' p) := { is_submodule . zero_ := ⟨0, zero, hf.zero⟩, add_ := assume c₁ c₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ + b₂, add hb₁ hb₂, by simp [eq₁, eq₂, hf.add]⟩, smul := assume a c ⟨b, hb, eq⟩, ⟨a • b, smul a hb, by simp [hf.smul, eq]⟩ } instance range {f : β → γ} (hf : is_linear_map f) : is_submodule (set.range f) := by rw [← set.image_univ]; exact is_submodule.image hf instance preimage {f : γ → β} (hf : is_linear_map f) : is_submodule (f ⁻¹' p) := by refine {..}; simp [hf.zero, hf.add, hf.smul, zero, add, smul] {contextual:=tt} instance add_submodule : is_submodule {z | ∃x∈p, ∃y∈p', z = x + y} := { is_submodule . zero_ := ⟨0, zero, 0, zero, by simp⟩, add_ := assume b₁ b₂ ⟨x₁, hx₁, y₁, hy₁, eq₁⟩ ⟨x₂, hx₂, y₂, hy₂, eq₂⟩, ⟨x₁ + x₂, add hx₁ hx₂, y₁ + y₂, add hy₁ hy₂, by simp [eq₁, eq₂]⟩, smul := assume a b ⟨x, hx, y, hy, eq⟩, ⟨a • x, smul _ hx, a • y, smul _ hy, by simp [eq, smul_add]⟩ } lemma Inter_submodule {ι : Sort w} {s : ι → set β} (h : ∀i, is_submodule (s i)) : is_submodule (⋂i, s i) := by refine {..}; simp [zero, add, smul] {contextual := tt} instance Inter_submodule' {ι : Sort w} {s : ι → set β} [h : ∀i, is_submodule (s i)] : is_submodule (⋂i, s i) := Inter_submodule h instance sInter_submodule {s : set (set β)} [hs : ∀t∈s, is_submodule t] : is_submodule (⋂₀ s) := by rw [set.sInter_eq_Inter]; exact Inter_submodule (assume t, Inter_submodule $ hs t) instance inter_submodule : is_submodule (p ∩ p') := suffices is_submodule (⋂₀ {p, p'} : set β), by simpa [set.inter_comm], @is_submodule.sInter_submodule α β _ _ {p, p'} $ by simp [or_imp_distrib, ‹is_submodule p›, ‹is_submodule p'›] {contextual := tt} end end is_submodule section comm_ring theorem is_submodule.eq_univ_of_contains_unit {α : Type u} [comm_ring α] (S : set α) [is_submodule S] (x y : α) (hx : x ∈ S) (h : y * x = 1) : S = set.univ := set.ext $ λ z, ⟨λ hz, trivial, λ hz, calc z = z * (y * x) : by simp [h] ... = (z * y) * x : eq.symm $ mul_assoc z y x ... ∈ S : is_submodule.smul (z * y) hx⟩ theorem is_submodule.univ_of_one_mem {α : Type u} [comm_ring α] (S : set α) [is_submodule S] : (1:α) ∈ S → S = set.univ := λ h, set.ext $ λ z, ⟨λ hz, trivial, λ hz, by simpa using (is_submodule.smul z h : z * 1 ∈ S)⟩ end comm_ring /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ class vector_space (α : out_param $ Type u) (β : Type v) [field α] extends module α β /-- Subspace of a vector space. Defined to equal `is_submodule`. -/ @[reducible] def subspace {α : Type u} {β : Type v} [field α] [vector_space α β] (p : set β) : Prop := is_submodule p
c415fd6470d81644123f3faedd1f7fe1ace68405
f1a12d4db0f46eee317d703e3336d33950a2fe7e
/common/to_fm.lean
2a17359a3210b68616615d71f9aa2c53b572c664
[ "Apache-2.0" ]
permissive
avigad/qelim
bce89b79c717b7649860d41a41a37e37c982624f
b7d22864f1f0a2d21adad0f4fb3fc7ba665f8e60
refs/heads/master
1,584,548,938,232
1,526,773,708,000
1,526,773,708,000
134,967,693
2
0
null
null
null
null
UTF-8
Lean
false
false
1,803
lean
import .logic open tactic -- def varstr (n : nat) := "x" ++ to_string n 11 variables {α : Type} #check expr meta def to_fm (α β : Type) (to_atom : nat → expr → tactic (α × list β)) : nat → expr → tactic (fm α × list β) := let to_fm_bin (n) (c : fm α → fm α → fm α) (e1) (e2) := do (p,bs1) ← to_fm n e1, (q,bs2) ← to_fm (n + list.length bs1) e2, return ((c p q), bs1++bs2) in λ n e, match e with | `(true) := return (⊤',[]) | `(false) := return (⊥',[]) | `(¬ %%pe) := do (p,bs) ← to_fm n pe, return (¬' p, bs) | `(%%pe ∧ %%qe) := to_fm_bin n fm.and pe qe | `(%%pe ∨ %%qe) := to_fm_bin n fm.or pe qe | `(Exists %%e) := match e with | (expr.lam _ _ d pe) := do (p,bs) ← to_fm (n+1) pe, return (∃' p, bs) | _ := failed end | e := do (a,bs) ← to_atom n e, return (A' a, bs) end #exit meta def to_fm_rec : expr → tactic (fm α) := let to_fm_rec_bin (c : fm α → fm α → fm α) (e1) (e2) := do φ ← to_fm_rec e1, ψ ← to_fm_rec e2, return $ c φ ψ in λ e, match e with | `(%%e1 ∧ %%e2) := to_fm_rec_bin fm.and e1 e2 | `(%%e1 ∨ %%e2) := to_fm_rec_bin fm.or e1 e2 -- | `(%%e1 ↔ %%e2) := to_fm_rec_bin fm.iff n e1 e2 | `(Exists %%e2) := let xs := varstr n in do e' ← to_fm_rec (n+1) e2, return $ fm.ex xs $ inst_dbv_frm xs e' -- | (expr.pi _ _ e1 e2) := -- monad.cond (is_prop e1) -- (to_fm_rec_bin formula.Imp n e1 e2) -- (let xs := varstr n in -- do e' ← to_fm_rec (n+1) e2, -- return $ formula.Forall xs $ inst_dbv_frm xs e') | _ := failed end meta def to_fm (e) := to_fm_rec 0 e -- example : forall (x : nat), exists (y : nat), x = 2 * y ∨ x = 2 * y + 1 := sorry
8ac453980884dad882d76937df8f50f78e9cbc37
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed/group/quotient.lean
2d01162f39f48a3d33b7ffbc179d189b5a62b4d2
[ "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
27,398
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Riccardo Brasca -/ import analysis.normed.group.hom /-! # Quotients of seminormed groups For any `seminormed_add_comm_group M` and any `S : add_subgroup M`, we provide a `seminormed_add_comm_group`, the group quotient `M ⧸ S`. If `S` is closed, we provide `normed_add_comm_group (M ⧸ S)` (regardless of whether `M` itself is separated). The two main properties of these structures are the underlying topology is the quotient topology and the projection is a normed group homomorphism which is norm non-increasing (better, it has operator norm exactly one unless `S` is dense in `M`). The corresponding universal property is that every normed group hom defined on `M` which vanishes on `S` descends to a normed group hom defined on `M ⧸ S`. This file also introduces a predicate `is_quotient` characterizing normed group homs that are isomorphic to the canonical projection onto a normed group quotient. In addition, this file also provides normed structures for quotients of modules by submodules, and of (commutative) rings by ideals. The `seminormed_add_comm_group` and `normed_add_comm_group` instances described above are transferred directly, but we also define instances of `normed_space`, `semi_normed_comm_ring`, `normed_comm_ring` and `normed_algebra` under appropriate type class assumptions on the original space. Moreover, while `quotient_add_group.complete_space` works out-of-the-box for quotients of `normed_add_comm_group`s by `add_subgroup`s, we need to transfer this instance in `submodule.quotient.complete_space` so that it applies to these other quotients. ## Main definitions We use `M` and `N` to denote seminormed groups and `S : add_subgroup M`. All the following definitions are in the `add_subgroup` namespace. Hence we can access `add_subgroup.normed_mk S` as `S.normed_mk`. * `seminormed_add_comm_group_quotient` : The seminormed group structure on the quotient by an additive subgroup. This is an instance so there is no need to explictly use it. * `normed_add_comm_group_quotient` : The normed group structure on the quotient by a closed additive subgroup. This is an instance so there is no need to explictly use it. * `normed_mk S` : the normed group hom from `M` to `M ⧸ S`. * `lift S f hf`: implements the universal property of `M ⧸ S`. Here `(f : normed_add_group_hom M N)`, `(hf : ∀ s ∈ S, f s = 0)` and `lift S f hf : normed_add_group_hom (M ⧸ S) N`. * `is_quotient`: given `f : normed_add_group_hom M N`, `is_quotient f` means `N` is isomorphic to a quotient of `M` by a subgroup, with projection `f`. Technically it asserts `f` is surjective and the norm of `f x` is the infimum of the norms of `x + m` for `m` in `f.ker`. ## Main results * `norm_normed_mk` : the operator norm of the projection is `1` if the subspace is not dense. * `is_quotient.norm_lift`: Provided `f : normed_hom M N` satisfies `is_quotient f`, for every `n : N` and positive `ε`, there exists `m` such that `f m = n ∧ ∥m∥ < ∥n∥ + ε`. ## Implementation details For any `seminormed_add_comm_group M` and any `S : add_subgroup M` we define a norm on `M ⧸ S` by `∥x∥ = Inf (norm '' {m | mk' S m = x})`. This formula is really an implementation detail, it shouldn't be needed outside of this file setting up the theory. Since `M ⧸ S` is automatically a topological space (as any quotient of a topological space), one needs to be careful while defining the `seminormed_add_comm_group` instance to avoid having two different topologies on this quotient. This is not purely a technological issue. Mathematically there is something to prove. The main point is proved in the auxiliary lemma `quotient_nhd_basis` that has no use beyond this verification and states that zero in the quotient admits as basis of neighborhoods in the quotient topology the sets `{x | ∥x∥ < ε}` for positive `ε`. Once this mathematical point it settled, we have two topologies that are propositionaly equal. This is not good enough for the type class system. As usual we ensure *definitional* equality using forgetful inheritance, see Note [forgetful inheritance]. A (semi)-normed group structure includes a uniform space structure which includes a topological space structure, together with propositional fields asserting compatibility conditions. The usual way to define a `seminormed_add_comm_group` is to let Lean build a uniform space structure using the provided norm, and then trivially build a proof that the norm and uniform structure are compatible. Here the uniform structure is provided using `topological_add_group.to_uniform_space` which uses the topological structure and the group structure to build the uniform structure. This uniform structure induces the correct topological structure by construction, but the fact that it is compatible with the norm is not obvious; this is where the mathematical content explained in the previous paragraph kicks in. -/ noncomputable theory open quotient_add_group metric set open_locale topological_space nnreal variables {M N : Type*} [seminormed_add_comm_group M] [seminormed_add_comm_group N] /-- The definition of the norm on the quotient by an additive subgroup. -/ noncomputable instance norm_on_quotient (S : add_subgroup M) : has_norm (M ⧸ S) := { norm := λ x, Inf (norm '' {m | mk' S m = x}) } lemma image_norm_nonempty {S : add_subgroup M} : ∀ x : M ⧸ S, (norm '' {m | mk' S m = x}).nonempty := begin rintro ⟨m⟩, rw set.nonempty_image_iff, use m, change mk' S m = _, refl end lemma bdd_below_image_norm (s : set M) : bdd_below (norm '' s) := begin use 0, rintro _ ⟨x, hx, rfl⟩, apply norm_nonneg end /-- The norm on the quotient satisfies `∥-x∥ = ∥x∥`. -/ lemma quotient_norm_neg {S : add_subgroup M} (x : M ⧸ S) : ∥-x∥ = ∥x∥ := begin suffices : norm '' {m | mk' S m = x} = norm '' {m | mk' S m = -x}, by simp only [this, norm], ext r, split, { rintros ⟨m, rfl : mk' S m = x, rfl⟩, rw ← norm_neg, exact ⟨-m, by simp only [(mk' S).map_neg, set.mem_set_of_eq], rfl⟩ }, { rintros ⟨m, hm : mk' S m = -x, rfl⟩, exact ⟨-m, by simpa [eq_comm] using eq_neg_iff_eq_neg.mp ((mk'_apply _ _).symm.trans hm)⟩ } end lemma quotient_norm_sub_rev {S : add_subgroup M} (x y : M ⧸ S) : ∥x - y∥ = ∥y - x∥ := by rw [show x - y = -(y - x), by abel, quotient_norm_neg] /-- The norm of the projection is smaller or equal to the norm of the original element. -/ lemma quotient_norm_mk_le (S : add_subgroup M) (m : M) : ∥mk' S m∥ ≤ ∥m∥ := begin apply cInf_le, use 0, { rintros _ ⟨n, h, rfl⟩, apply norm_nonneg }, { apply set.mem_image_of_mem, rw set.mem_set_of_eq } end /-- The norm of the projection is smaller or equal to the norm of the original element. -/ lemma quotient_norm_mk_le' (S : add_subgroup M) (m : M) : ∥(m : M ⧸ S)∥ ≤ ∥m∥ := quotient_norm_mk_le S m /-- The norm of the image under the natural morphism to the quotient. -/ lemma quotient_norm_mk_eq (S : add_subgroup M) (m : M) : ∥mk' S m∥ = Inf ((λ x, ∥m + x∥) '' S) := begin change Inf _ = _, congr' 1, ext r, simp_rw [coe_mk', eq_iff_sub_mem], split, { rintros ⟨y, h, rfl⟩, use [y - m, h], simp }, { rintros ⟨y, h, rfl⟩, use m + y, simpa using h }, end /-- The quotient norm is nonnegative. -/ lemma quotient_norm_nonneg (S : add_subgroup M) : ∀ x : M ⧸ S, 0 ≤ ∥x∥ := begin rintros ⟨m⟩, change 0 ≤ ∥mk' S m∥, apply le_cInf (image_norm_nonempty _), rintros _ ⟨n, h, rfl⟩, apply norm_nonneg end /-- The quotient norm is nonnegative. -/ lemma norm_mk_nonneg (S : add_subgroup M) (m : M) : 0 ≤ ∥mk' S m∥ := quotient_norm_nonneg S _ /-- The norm of the image of `m : M` in the quotient by `S` is zero if and only if `m` belongs to the closure of `S`. -/ lemma quotient_norm_eq_zero_iff (S : add_subgroup M) (m : M) : ∥mk' S m∥ = 0 ↔ m ∈ closure (S : set M) := begin have : 0 ≤ ∥mk' S m∥ := norm_mk_nonneg S m, rw [← this.le_iff_eq, quotient_norm_mk_eq, real.Inf_le_iff], simp_rw [zero_add], { calc (∀ ε > (0 : ℝ), ∃ r ∈ (λ x, ∥m + x∥) '' (S : set M), r < ε) ↔ (∀ ε > 0, (∃ x ∈ S, ∥m + x∥ < ε)) : by simp [set.bex_image_iff] ... ↔ ∀ ε > 0, (∃ x ∈ S, ∥m + -x∥ < ε) : _ ... ↔ ∀ ε > 0, (∃ x ∈ S, x ∈ metric.ball m ε) : by simp [dist_eq_norm, ← sub_eq_add_neg, norm_sub_rev] ... ↔ m ∈ closure ↑S : by simp [metric.mem_closure_iff, dist_comm], refine forall₂_congr (λ ε ε_pos, _), rw [← S.exists_neg_mem_iff_exists_mem], simp }, { use 0, rintro _ ⟨x, x_in, rfl⟩, apply norm_nonneg }, rw set.nonempty_image_iff, use [0, S.zero_mem] end /-- For any `x : M ⧸ S` and any `0 < ε`, there is `m : M` such that `mk' S m = x` and `∥m∥ < ∥x∥ + ε`. -/ lemma norm_mk_lt {S : add_subgroup M} (x : M ⧸ S) {ε : ℝ} (hε : 0 < ε) : ∃ (m : M), mk' S m = x ∧ ∥m∥ < ∥x∥ + ε := begin obtain ⟨_, ⟨m : M, H : mk' S m = x, rfl⟩, hnorm : ∥m∥ < ∥x∥ + ε⟩ := real.lt_Inf_add_pos (image_norm_nonempty x) hε, subst H, exact ⟨m, rfl, hnorm⟩, end /-- For any `m : M` and any `0 < ε`, there is `s ∈ S` such that `∥m + s∥ < ∥mk' S m∥ + ε`. -/ lemma norm_mk_lt' (S : add_subgroup M) (m : M) {ε : ℝ} (hε : 0 < ε) : ∃ s ∈ S, ∥m + s∥ < ∥mk' S m∥ + ε := begin obtain ⟨n : M, hn : mk' S n = mk' S m, hn' : ∥n∥ < ∥mk' S m∥ + ε⟩ := norm_mk_lt (quotient_add_group.mk' S m) hε, erw [eq_comm, quotient_add_group.eq] at hn, use [- m + n, hn], rwa [add_neg_cancel_left] end /-- The quotient norm satisfies the triangle inequality. -/ lemma quotient_norm_add_le (S : add_subgroup M) (x y : M ⧸ S) : ∥x + y∥ ≤ ∥x∥ + ∥y∥ := begin refine le_of_forall_pos_le_add (λ ε hε, _), replace hε := half_pos hε, obtain ⟨m, rfl, hm : ∥m∥ < ∥mk' S m∥ + ε / 2⟩ := norm_mk_lt x hε, obtain ⟨n, rfl, hn : ∥n∥ < ∥mk' S n∥ + ε / 2⟩ := norm_mk_lt y hε, calc ∥mk' S m + mk' S n∥ = ∥mk' S (m + n)∥ : by rw (mk' S).map_add ... ≤ ∥m + n∥ : quotient_norm_mk_le S (m + n) ... ≤ ∥m∥ + ∥n∥ : norm_add_le _ _ ... ≤ ∥mk' S m∥ + ∥mk' S n∥ + ε : by linarith end /-- The quotient norm of `0` is `0`. -/ lemma norm_mk_zero (S : add_subgroup M) : ∥(0 : M ⧸ S)∥ = 0 := begin erw quotient_norm_eq_zero_iff, exact subset_closure S.zero_mem end /-- If `(m : M)` has norm equal to `0` in `M ⧸ S` for a closed subgroup `S` of `M`, then `m ∈ S`. -/ lemma norm_zero_eq_zero (S : add_subgroup M) (hS : is_closed (S : set M)) (m : M) (h : ∥mk' S m∥ = 0) : m ∈ S := by rwa [quotient_norm_eq_zero_iff, hS.closure_eq] at h lemma quotient_nhd_basis (S : add_subgroup M) : (𝓝 (0 : M ⧸ S)).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {x | ∥x∥ < ε}) := ⟨begin intros U, split, { intros U_in, rw ← (mk' S).map_zero at U_in, have := preimage_nhds_coinduced U_in, rcases metric.mem_nhds_iff.mp this with ⟨ε, ε_pos, H⟩, use [ε/2, half_pos ε_pos], intros x x_in, dsimp at x_in, rcases norm_mk_lt x (half_pos ε_pos) with ⟨y, rfl, ry⟩, apply H, rw ball_zero_eq, dsimp, linarith }, { rintros ⟨ε, ε_pos, h⟩, have : (mk' S) '' (ball (0 : M) ε) ⊆ {x | ∥x∥ < ε}, { rintros _ ⟨x, x_in, rfl⟩, rw mem_ball_zero_iff at x_in, exact lt_of_le_of_lt (quotient_norm_mk_le S x) x_in }, apply filter.mem_of_superset _ (set.subset.trans this h), clear h U this, apply is_open.mem_nhds, { change is_open ((mk' S) ⁻¹' _), erw quotient_add_group.preimage_image_coe, apply is_open_Union, rintros ⟨s, s_in⟩, exact (continuous_add_right s).is_open_preimage _ is_open_ball }, { exact ⟨(0 : M), mem_ball_self ε_pos, (mk' S).map_zero⟩ } }, end⟩ /-- The seminormed group structure on the quotient by an additive subgroup. -/ noncomputable instance add_subgroup.seminormed_add_comm_group_quotient (S : add_subgroup M) : seminormed_add_comm_group (M ⧸ S) := { dist := λ x y, ∥x - y∥, dist_self := λ x, by simp only [norm_mk_zero, sub_self], dist_comm := quotient_norm_sub_rev, dist_triangle := λ x y z, begin unfold dist, have : x - z = (x - y) + (y - z) := by abel, rw this, exact quotient_norm_add_le S (x - y) (y - z) end, dist_eq := λ x y, rfl, to_uniform_space := topological_add_group.to_uniform_space (M ⧸ S), uniformity_dist := begin rw uniformity_eq_comap_nhds_zero', have := (quotient_nhd_basis S).comap (λ (p : (M ⧸ S) × M ⧸ S), p.2 - p.1), apply this.eq_of_same_basis, have : ∀ ε : ℝ, (λ (p : (M ⧸ S) × M ⧸ S), p.snd - p.fst) ⁻¹' {x | ∥x∥ < ε} = {p : (M ⧸ S) × M ⧸ S | ∥p.fst - p.snd∥ < ε}, { intro ε, ext x, dsimp, rw quotient_norm_sub_rev }, rw funext this, refine filter.has_basis_binfi_principal _ set.nonempty_Ioi, rintros ε (ε_pos : 0 < ε) η (η_pos : 0 < η), refine ⟨min ε η, lt_min ε_pos η_pos, _, _⟩, { suffices : ∀ (a b : M ⧸ S), ∥a - b∥ < ε → ∥a - b∥ < η → ∥a - b∥ < ε, by simpa, exact λ a b h h', h }, { simp } end } -- This is a sanity check left here on purpose to ensure that potential refactors won't destroy -- this important property. example (S : add_subgroup M) : (quotient.topological_space : topological_space $ M ⧸ S) = S.seminormed_add_comm_group_quotient.to_uniform_space.to_topological_space := rfl /-- The quotient in the category of normed groups. -/ noncomputable instance add_subgroup.normed_add_comm_group_quotient (S : add_subgroup M) [is_closed (S : set M)] : normed_add_comm_group (M ⧸ S) := { eq_of_dist_eq_zero := begin rintros ⟨m⟩ ⟨m'⟩ (h : ∥mk' S m - mk' S m'∥ = 0), erw [← (mk' S).map_sub, quotient_norm_eq_zero_iff, ‹is_closed _›.closure_eq, ← quotient_add_group.eq_iff_sub_mem] at h, exact h end, .. add_subgroup.seminormed_add_comm_group_quotient S } -- This is a sanity check left here on purpose to ensure that potential refactors won't destroy -- this important property. example (S : add_subgroup M) [is_closed (S : set M)] : S.seminormed_add_comm_group_quotient = normed_add_comm_group.to_seminormed_add_comm_group := rfl namespace add_subgroup open normed_add_group_hom /-- The morphism from a seminormed group to the quotient by a subgroup. -/ noncomputable def normed_mk (S : add_subgroup M) : normed_add_group_hom M (M ⧸ S) := { bound' := ⟨1, λ m, by simpa [one_mul] using quotient_norm_mk_le _ m⟩, .. quotient_add_group.mk' S } /-- `S.normed_mk` agrees with `quotient_add_group.mk' S`. -/ @[simp] lemma normed_mk.apply (S : add_subgroup M) (m : M) : normed_mk S m = quotient_add_group.mk' S m := rfl /-- `S.normed_mk` is surjective. -/ lemma surjective_normed_mk (S : add_subgroup M) : function.surjective (normed_mk S) := surjective_quot_mk _ /-- The kernel of `S.normed_mk` is `S`. -/ lemma ker_normed_mk (S : add_subgroup M) : S.normed_mk.ker = S := quotient_add_group.ker_mk _ /-- The operator norm of the projection is at most `1`. -/ lemma norm_normed_mk_le (S : add_subgroup M) : ∥S.normed_mk∥ ≤ 1 := normed_add_group_hom.op_norm_le_bound _ zero_le_one (λ m, by simp [quotient_norm_mk_le']) /-- The operator norm of the projection is `1` if the subspace is not dense. -/ lemma norm_normed_mk (S : add_subgroup M) (h : (S.topological_closure : set M) ≠ univ) : ∥S.normed_mk∥ = 1 := begin obtain ⟨x, hx⟩ := set.nonempty_compl.2 h, let y := S.normed_mk x, have hy : ∥y∥ ≠ 0, { intro h0, exact set.not_mem_of_mem_compl hx ((quotient_norm_eq_zero_iff S x).1 h0) }, refine le_antisymm (norm_normed_mk_le S) (le_of_forall_pos_le_add (λ ε hε, _)), suffices : 1 ≤ ∥S.normed_mk∥ + min ε ((1 : ℝ)/2), { exact le_add_of_le_add_left this (min_le_left ε ((1 : ℝ)/2)) }, have hδ := sub_pos.mpr (lt_of_le_of_lt (min_le_right ε ((1 : ℝ)/2)) one_half_lt_one), have hδpos : 0 < min ε ((1 : ℝ)/2) := lt_min hε one_half_pos, have hδnorm := mul_pos (div_pos hδpos hδ) (lt_of_le_of_ne (norm_nonneg y) hy.symm), obtain ⟨m, hm, hlt⟩ := norm_mk_lt y hδnorm, have hrw : ∥y∥ + min ε (1 / 2) / (1 - min ε (1 / 2)) * ∥y∥ = ∥y∥ * (1 + min ε (1 / 2) / (1 - min ε (1 / 2))) := by ring, rw [hrw] at hlt, have hm0 : ∥m∥ ≠ 0, { intro h0, have hnorm := quotient_norm_mk_le S m, rw [h0, hm] at hnorm, replace hnorm := le_antisymm hnorm (norm_nonneg _), simpa [hnorm] using hy }, replace hlt := (div_lt_div_right (lt_of_le_of_ne (norm_nonneg m) hm0.symm)).2 hlt, simp only [hm0, div_self, ne.def, not_false_iff] at hlt, have hrw₁ : ∥y∥ * (1 + min ε (1 / 2) / (1 - min ε (1 / 2))) / ∥m∥ = (∥y∥ / ∥m∥) * (1 + min ε (1 / 2) / (1 - min ε (1 / 2))) := by ring, rw [hrw₁] at hlt, replace hlt := (inv_pos_lt_iff_one_lt_mul (lt_trans (div_pos hδpos hδ) (lt_one_add _))).2 hlt, suffices : ∥S.normed_mk∥ ≥ 1 - min ε (1 / 2), { exact sub_le_iff_le_add.mp this }, calc ∥S.normed_mk∥ ≥ ∥(S.normed_mk) m∥ / ∥m∥ : ratio_le_op_norm S.normed_mk m ... = ∥y∥ / ∥m∥ : by rw [normed_mk.apply, hm] ... ≥ (1 + min ε (1 / 2) / (1 - min ε (1 / 2)))⁻¹ : le_of_lt hlt ... = 1 - min ε (1 / 2) : by field_simp [(ne_of_lt hδ).symm] end /-- The operator norm of the projection is `0` if the subspace is dense. -/ lemma norm_trivial_quotient_mk (S : add_subgroup M) (h : (S.topological_closure : set M) = set.univ) : ∥S.normed_mk∥ = 0 := begin refine le_antisymm (op_norm_le_bound _ le_rfl (λ x, _)) (norm_nonneg _), have hker : x ∈ (S.normed_mk).ker.topological_closure, { rw [S.ker_normed_mk], exact set.mem_of_eq_of_mem h trivial }, rw [ker_normed_mk] at hker, simp only [(quotient_norm_eq_zero_iff S x).mpr hker, normed_mk.apply, zero_mul], end end add_subgroup namespace normed_add_group_hom /-- `is_quotient f`, for `f : M ⟶ N` means that `N` is isomorphic to the quotient of `M` by the kernel of `f`. -/ structure is_quotient (f : normed_add_group_hom M N) : Prop := (surjective : function.surjective f) (norm : ∀ x, ∥f x∥ = Inf ((λ m, ∥x + m∥) '' f.ker)) /-- Given `f : normed_add_group_hom M N` such that `f s = 0` for all `s ∈ S`, where, `S : add_subgroup M` is closed, the induced morphism `normed_add_group_hom (M ⧸ S) N`. -/ noncomputable def lift {N : Type*} [seminormed_add_comm_group N] (S : add_subgroup M) (f : normed_add_group_hom M N) (hf : ∀ s ∈ S, f s = 0) : normed_add_group_hom (M ⧸ S) N := { bound' := begin obtain ⟨c : ℝ, hcpos : (0 : ℝ) < c, hc : ∀ x, ∥f x∥ ≤ c * ∥x∥⟩ := f.bound, refine ⟨c, λ mbar, le_of_forall_pos_le_add (λ ε hε, _)⟩, obtain ⟨m : M, rfl : mk' S m = mbar, hmnorm : ∥m∥ < ∥mk' S m∥ + ε/c⟩ := norm_mk_lt mbar (div_pos hε hcpos), calc ∥f m∥ ≤ c * ∥m∥ : hc m ... ≤ c*(∥mk' S m∥ + ε/c) : ((mul_lt_mul_left hcpos).mpr hmnorm).le ... = c * ∥mk' S m∥ + ε : by rw [mul_add, mul_div_cancel' _ hcpos.ne.symm] end, .. quotient_add_group.lift S f.to_add_monoid_hom hf } lemma lift_mk {N : Type*} [seminormed_add_comm_group N] (S : add_subgroup M) (f : normed_add_group_hom M N) (hf : ∀ s ∈ S, f s = 0) (m : M) : lift S f hf (S.normed_mk m) = f m := rfl lemma lift_unique {N : Type*} [seminormed_add_comm_group N] (S : add_subgroup M) (f : normed_add_group_hom M N) (hf : ∀ s ∈ S, f s = 0) (g : normed_add_group_hom (M ⧸ S) N) : g.comp (S.normed_mk) = f → g = lift S f hf := begin intro h, ext, rcases add_subgroup.surjective_normed_mk _ x with ⟨x,rfl⟩, change (g.comp (S.normed_mk) x) = _, simpa only [h] end /-- `S.normed_mk` satisfies `is_quotient`. -/ lemma is_quotient_quotient (S : add_subgroup M) : is_quotient (S.normed_mk) := ⟨S.surjective_normed_mk, λ m, by simpa [S.ker_normed_mk] using quotient_norm_mk_eq _ m⟩ lemma is_quotient.norm_lift {f : normed_add_group_hom M N} (hquot : is_quotient f) {ε : ℝ} (hε : 0 < ε) (n : N) : ∃ (m : M), f m = n ∧ ∥m∥ < ∥n∥ + ε := begin obtain ⟨m, rfl⟩ := hquot.surjective n, have nonemp : ((λ m', ∥m + m'∥) '' f.ker).nonempty, { rw set.nonempty_image_iff, exact ⟨0, f.ker.zero_mem⟩ }, rcases real.lt_Inf_add_pos nonemp hε with ⟨_, ⟨⟨x, hx, rfl⟩, H : ∥m + x∥ < Inf ((λ (m' : M), ∥m + m'∥) '' f.ker) + ε⟩⟩, exact ⟨m+x, by rw [map_add,(normed_add_group_hom.mem_ker f x).mp hx, add_zero], by rwa hquot.norm⟩, end lemma is_quotient.norm_le {f : normed_add_group_hom M N} (hquot : is_quotient f) (m : M) : ∥f m∥ ≤ ∥m∥ := begin rw hquot.norm, apply cInf_le, { use 0, rintros _ ⟨m', hm', rfl⟩, apply norm_nonneg }, { exact ⟨0, f.ker.zero_mem, by simp⟩ } end lemma lift_norm_le {N : Type*} [seminormed_add_comm_group N] (S : add_subgroup M) (f : normed_add_group_hom M N) (hf : ∀ s ∈ S, f s = 0) {c : ℝ≥0} (fb : ∥f∥ ≤ c) : ∥lift S f hf∥ ≤ c := begin apply op_norm_le_bound _ c.coe_nonneg, intros x, by_cases hc : c = 0, { simp only [hc, nnreal.coe_zero, zero_mul] at fb ⊢, obtain ⟨x, rfl⟩ := surjective_quot_mk _ x, show ∥f x∥ ≤ 0, calc ∥f x∥ ≤ 0 * ∥x∥ : f.le_of_op_norm_le fb x ... = 0 : zero_mul _ }, { replace hc : 0 < c := pos_iff_ne_zero.mpr hc, apply le_of_forall_pos_le_add, intros ε hε, have aux : 0 < (ε / c) := div_pos hε hc, obtain ⟨x, rfl, Hx⟩ : ∃ x', S.normed_mk x' = x ∧ ∥x'∥ < ∥x∥ + (ε / c) := (is_quotient_quotient _).norm_lift aux _, rw lift_mk, calc ∥f x∥ ≤ c * ∥x∥ : f.le_of_op_norm_le fb x ... ≤ c * (∥S.normed_mk x∥ + ε / c) : (mul_le_mul_left _).mpr Hx.le ... = c * _ + ε : _, { exact_mod_cast hc }, { rw [mul_add, mul_div_cancel'], exact_mod_cast hc.ne' } }, end lemma lift_norm_noninc {N : Type*} [seminormed_add_comm_group N] (S : add_subgroup M) (f : normed_add_group_hom M N) (hf : ∀ s ∈ S, f s = 0) (fb : f.norm_noninc) : (lift S f hf).norm_noninc := λ x, begin have fb' : ∥f∥ ≤ (1 : ℝ≥0) := norm_noninc.norm_noninc_iff_norm_le_one.mp fb, simpa using le_of_op_norm_le _ (f.lift_norm_le _ _ fb') _, end end normed_add_group_hom /-! ### Submodules and ideals In what follows, the norm structures created above for quotients of (semi)`normed_add_comm_group`s by `add_subgroup`s are transferred via definitional equality to quotients of modules by submodules, and of rings by ideals, thereby preserving the definitional equality for the topological group and uniform structures worked for above. Completeness is also transferred via this definitional equality. In addition, instances are constructed for `normed_space`, `semi_normed_comm_ring`, `normed_comm_ring` and `normed_algebra` under the appropriate hypotheses. Currently, we do not have quotients of rings by two-sided ideals, hence the commutativity hypotheses are required. -/ section submodule variables {R : Type*} [ring R] [module R M] (S : submodule R M) instance submodule.quotient.seminormed_add_comm_group : seminormed_add_comm_group (M ⧸ S) := add_subgroup.seminormed_add_comm_group_quotient S.to_add_subgroup instance submodule.quotient.normed_add_comm_group [hS : is_closed (S : set M)] : normed_add_comm_group (M ⧸ S) := @add_subgroup.normed_add_comm_group_quotient _ _ S.to_add_subgroup hS instance submodule.quotient.complete_space [complete_space M] : complete_space (M ⧸ S) := quotient_add_group.complete_space M S.to_add_subgroup /-- For any `x : M ⧸ S` and any `0 < ε`, there is `m : M` such that `submodule.quotient.mk m = x` and `∥m∥ < ∥x∥ + ε`. -/ lemma submodule.quotient.norm_mk_lt {S : submodule R M} (x : M ⧸ S) {ε : ℝ} (hε : 0 < ε) : ∃ m : M, submodule.quotient.mk m = x ∧ ∥m∥ < ∥x∥ + ε := norm_mk_lt x hε lemma submodule.quotient.norm_mk_le (m : M) : ∥(submodule.quotient.mk m : M ⧸ S)∥ ≤ ∥m∥ := quotient_norm_mk_le S.to_add_subgroup m instance submodule.quotient.normed_space (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 M] [has_smul 𝕜 R] [is_scalar_tower 𝕜 R M] : normed_space 𝕜 (M ⧸ S) := { norm_smul_le := λ k x, le_of_forall_pos_le_add $ λ ε hε, begin have := (nhds_basis_ball.tendsto_iff nhds_basis_ball).mp ((@real.uniform_continuous_const_mul (∥k∥)).continuous.tendsto (∥x∥)) ε hε, simp only [mem_ball, exists_prop, dist, abs_sub_lt_iff] at this, rcases this with ⟨δ, hδ, h⟩, obtain ⟨a, rfl, ha⟩ := submodule.quotient.norm_mk_lt x hδ, specialize h (∥a∥) (⟨by linarith, by linarith [submodule.quotient.norm_mk_le S a]⟩), calc _ ≤ ∥k∥ * ∥a∥ : (quotient_norm_mk_le S.to_add_subgroup (k • a)).trans_eq (norm_smul k a) ... ≤ _ : (sub_lt_iff_lt_add'.mp h.1).le end, .. submodule.quotient.module' S, } end submodule section ideal variables {R : Type*} [semi_normed_comm_ring R] (I : ideal R) lemma ideal.quotient.norm_mk_lt {I : ideal R} (x : R ⧸ I) {ε : ℝ} (hε : 0 < ε) : ∃ r : R, ideal.quotient.mk I r = x ∧ ∥r∥ < ∥x∥ + ε := norm_mk_lt x hε lemma ideal.quotient.norm_mk_le (r : R) : ∥ideal.quotient.mk I r∥ ≤ ∥r∥ := quotient_norm_mk_le I.to_add_subgroup r instance ideal.quotient.semi_normed_comm_ring : semi_normed_comm_ring (R ⧸ I) := { mul_comm := mul_comm, norm_mul := λ x y, le_of_forall_pos_le_add $ λ ε hε, begin have := ((nhds_basis_ball.prod_nhds nhds_basis_ball).tendsto_iff nhds_basis_ball).mp (real.continuous_mul.tendsto (∥x∥, ∥y∥)) ε hε, simp only [set.mem_prod, mem_ball, and_imp, prod.forall, exists_prop, prod.exists] at this, rcases this with ⟨ε₁, ε₂, ⟨h₁, h₂⟩, h⟩, obtain ⟨⟨a, rfl, ha⟩, ⟨b, rfl, hb⟩⟩ := ⟨ideal.quotient.norm_mk_lt x h₁, ideal.quotient.norm_mk_lt y h₂⟩, simp only [dist, abs_sub_lt_iff] at h, specialize h (∥a∥) (∥b∥) (⟨by linarith, by linarith [ideal.quotient.norm_mk_le I a]⟩) (⟨by linarith, by linarith [ideal.quotient.norm_mk_le I b]⟩), calc _ ≤ ∥a∥ * ∥b∥ : (ideal.quotient.norm_mk_le I (a * b)).trans (norm_mul_le a b) ... ≤ _ : (sub_lt_iff_lt_add'.mp h.1).le end, .. submodule.quotient.seminormed_add_comm_group I } instance ideal.quotient.normed_comm_ring [is_closed (I : set R)] : normed_comm_ring (R ⧸ I) := { .. ideal.quotient.semi_normed_comm_ring I, .. submodule.quotient.normed_add_comm_group I } variables (𝕜 : Type*) [normed_field 𝕜] instance ideal.quotient.normed_algebra [normed_algebra 𝕜 R] : normed_algebra 𝕜 (R ⧸ I) := { .. submodule.quotient.normed_space I 𝕜, .. ideal.quotient.algebra 𝕜 } end ideal
f42f2203d7c3b6042e211d320721f1cb2759e1f4
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Meta/Tactic/Generalize.lean
1cd8889c69c74054eb5418b0dc25f9cc869ff0b8
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,079
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.KAbstract import Lean.Meta.Tactic.Util namespace Lean.Meta def generalize (mvarId : MVarId) (e : Expr) (x : Name) (failIfNotInTarget : Bool := true) : MetaM MVarId := withMVarContext mvarId do checkNotAssigned mvarId `generalize let tag ← getMVarTag mvarId let target ← getMVarType mvarId let target ← instantiateMVars target let targetAbst ← kabstract target e if failIfNotInTarget && !targetAbst.hasLooseBVars then throwTacticEx `generalize mvarId "failed to find expression in the target" let eType ← inferType e let targetNew := Lean.mkForall x BinderInfo.default eType targetAbst unless (← isTypeCorrect targetNew) do throwTacticEx `generalize mvarId "result is not type correct" let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag assignExprMVar mvarId (mkApp mvarNew e) pure mvarNew.mvarId! end Lean.Meta
bc377bfb0cd97bb2e5d5f628c3f54c994d76dd8f
7cef822f3b952965621309e88eadf618da0c8ae9
/src/category_theory/pempty.lean
1623a42f6b0ae53272414f7efc0cff204983370e
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
1,072
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.discrete_category import category_theory.equivalence /-! # The empty category Defines a category structure on `pempty`, and the unique functor `pempty ⥤ C` for any category `C`. -/ universes v u w -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory /-- The empty category -/ instance pempty_category : small_category.{w} pempty.{w+1} := { hom := λ X Y, pempty, id := by obviously, comp := by obviously } namespace functor variables (C : Type u) [𝒞 : category.{v} C] include 𝒞 /-- The unique functor from the empty category to any target category. -/ def empty : pempty.{v+1} ⥤ C := by tidy end functor /-- The category `pempty` is equivalent to the category `discrete pempty`. -/ instance pempty_equiv_discrete_pempty : is_equivalence (functor.empty.{v} (discrete pempty.{v+1})) := by obviously end category_theory
bc964daa35fb9e4a28fafbc7e16c88205a69cd98
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/geometry/manifold/instances/sphere.lean
bda59338083a2c3f386217a0860f9c7d215a0b1d
[ "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
20,194
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import geometry.manifold.instances.real import analysis.complex.circle import analysis.inner_product_space.calculus /-! # Manifold structure on the sphere This file defines stereographic projection from the sphere in an inner product space `E`, and uses it to put a smooth manifold structure on the sphere. ## Main results For a unit vector `v` in `E`, the definition `stereographic` gives the stereographic projection centred at `v`, a local homeomorphism from the sphere to `(ℝ ∙ v)ᗮ` (the orthogonal complement of `v`). For finite-dimensional `E`, we then construct a smooth manifold instance on the sphere; the charts here are obtained by composing the local homeomorphisms `stereographic` with arbitrary isometries from `(ℝ ∙ v)ᗮ` to Euclidean space. We prove two lemmas about smooth maps: * `times_cont_mdiff_coe_sphere` states that the coercion map from the sphere into `E` is smooth; this is a useful tool for constructing smooth maps *from* the sphere. * `times_cont_mdiff.cod_restrict_sphere` states that a map from a manifold into the sphere is smooth if its lift to a map to `E` is smooth; this is a useful tool for constructing smooth maps *to* the sphere. As an application we prove `times_cont_mdiff_neg_sphere`, that the antipodal map is smooth. Finally, we equip the `circle` (defined in `analysis.complex.circle` to be the sphere in `ℂ` centred at `0` of radius `1`) with the following structure: * a charted space with model space `euclidean_space ℝ (fin 1)` (inherited from `metric.sphere`) * a Lie group with model with corners `𝓡 1` We furthermore show that `exp_map_circle` (defined in `analysis.complex.circle` to be the natural map `λ t, exp (t * I)` from `ℝ` to `circle`) is smooth. ## Implementation notes The model space for the charted space instance is `euclidean_space ℝ (fin n)`, where `n` is a natural number satisfying the typeclass assumption `[fact (finrank ℝ E = n + 1)]`. This may seem a little awkward, but it is designed to circumvent the problem that the literal expression for the dimension of the model space (up to definitional equality) determines the type. If one used the naive expression `euclidean_space ℝ (fin (finrank ℝ E - 1))` for the model space, then the sphere in `ℂ` would be a manifold with model space `euclidean_space ℝ (fin (2 - 1))` but not with model space `euclidean_space ℝ (fin 1)`. -/ variables {E : Type*} [inner_product_space ℝ E] noncomputable theory open metric finite_dimensional open_locale manifold local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ section stereographic_projection variables (v : E) /-! ### Construction of the stereographic projection -/ /-- Stereographic projection, forward direction. This is a map from an inner product space `E` to the orthogonal complement of an element `v` of `E`. It is smooth away from the affine hyperplane through `v` parallel to the orthogonal complement. It restricts on the sphere to the stereographic projection. -/ def stereo_to_fun [complete_space E] (x : E) : (ℝ ∙ v)ᗮ := (2 / ((1:ℝ) - inner_right v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x variables {v} @[simp] lemma stereo_to_fun_apply [complete_space E] (x : E) : stereo_to_fun v x = (2 / ((1:ℝ) - inner_right v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x := rfl lemma times_cont_diff_on_stereo_to_fun [complete_space E] : times_cont_diff_on ℝ ⊤ (stereo_to_fun v) {x : E | inner_right v x ≠ (1:ℝ)} := begin refine times_cont_diff_on.smul _ (orthogonal_projection ((ℝ ∙ v)ᗮ)).times_cont_diff.times_cont_diff_on, refine times_cont_diff_const.times_cont_diff_on.div _ _, { exact (times_cont_diff_const.sub (inner_right v).times_cont_diff).times_cont_diff_on }, { intros x h h', exact h (sub_eq_zero.mp h').symm } end lemma continuous_on_stereo_to_fun [complete_space E] : continuous_on (stereo_to_fun v) {x : E | inner_right v x ≠ (1:ℝ)} := times_cont_diff_on_stereo_to_fun.continuous_on variables (v) /-- Auxiliary function for the construction of the reverse direction of the stereographic projection. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to `E`; we will later prove that it takes values in the unit sphere. For most purposes, use `stereo_inv_fun`, not `stereo_inv_fun_aux`. -/ def stereo_inv_fun_aux (w : E) : E := (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) variables {v} @[simp] lemma stereo_inv_fun_aux_apply (w : E) : stereo_inv_fun_aux v w = (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_aux_mem (hv : ∥v∥ = 1) {w : E} (hw : w ∈ (ℝ ∙ v)ᗮ) : stereo_inv_fun_aux v w ∈ (sphere (0:E) 1) := begin have h₁ : 0 ≤ ∥w∥ ^ 2 + 4 := by nlinarith, suffices : ∥(4:ℝ) • w + (∥w∥ ^ 2 - 4) • v∥ = ∥w∥ ^ 2 + 4, { have h₂ : ∥w∥ ^ 2 + 4 ≠ 0 := by nlinarith, simp only [mem_sphere_zero_iff_norm, norm_smul, real.norm_eq_abs, abs_inv, this, abs_of_nonneg h₁, stereo_inv_fun_aux_apply], field_simp }, suffices : ∥(4:ℝ) • w + (∥w∥ ^ 2 - 4) • v∥ ^ 2 = (∥w∥ ^ 2 + 4) ^ 2, { have h₃ : 0 ≤ ∥stereo_inv_fun_aux v w∥ := norm_nonneg _, simpa [h₁, h₃, -one_pow] using this }, simp [norm_add_sq_real, norm_smul, inner_smul_left, inner_smul_right, inner_left_of_mem_orthogonal_singleton _ hw, mul_pow, real.norm_eq_abs, hv], ring end lemma times_cont_diff_stereo_inv_fun_aux : times_cont_diff ℝ ⊤ (stereo_inv_fun_aux v) := begin have h₀ : times_cont_diff ℝ ⊤ (λ w : E, ∥w∥ ^ 2) := times_cont_diff_norm_sq, have h₁ : times_cont_diff ℝ ⊤ (λ w : E, (∥w∥ ^ 2 + 4)⁻¹), { refine (h₀.add times_cont_diff_const).inv _, intros x, nlinarith }, have h₂ : times_cont_diff ℝ ⊤ (λ w, (4:ℝ) • w + (∥w∥ ^ 2 - 4) • v), { refine (times_cont_diff_const.smul times_cont_diff_id).add _, refine (h₀.sub times_cont_diff_const).smul times_cont_diff_const }, exact h₁.smul h₂ end /-- Stereographic projection, reverse direction. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to the unit sphere in `E`. -/ def stereo_inv_fun (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : sphere (0:E) 1 := ⟨stereo_inv_fun_aux v (w:E), stereo_inv_fun_aux_mem hv w.2⟩ @[simp] lemma stereo_inv_fun_apply (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : (stereo_inv_fun hv w : E) = (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_ne_north_pole (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_inv_fun hv w ≠ (⟨v, by simp [hv]⟩ : sphere (0:E) 1) := begin refine subtype.ne_of_val_ne _, rw ← inner_lt_one_iff_real_of_norm_one _ hv, { have hw : ⟪v, w⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v w.2, have hw' : (∥(w:E)∥ ^ 2 + 4)⁻¹ * (∥(w:E)∥ ^ 2 - 4) < 1, { refine (inv_mul_lt_iff' _).mpr _, { nlinarith }, linarith }, simpa [real_inner_comm, inner_add_right, inner_smul_right, real_inner_self_eq_norm_mul_norm, hw, hv] using hw' }, { simpa using stereo_inv_fun_aux_mem hv w.2 } end lemma continuous_stereo_inv_fun (hv : ∥v∥ = 1) : continuous (stereo_inv_fun hv) := continuous_induced_rng (times_cont_diff_stereo_inv_fun_aux.continuous.comp continuous_subtype_coe) variables [complete_space E] lemma stereo_left_inv (hv : ∥v∥ = 1) {x : sphere (0:E) 1} (hx : (x:E) ≠ v) : stereo_inv_fun hv (stereo_to_fun v x) = x := begin ext, simp only [stereo_to_fun_apply, stereo_inv_fun_apply, smul_add], -- name two frequently-occuring quantities and write down their basic properties set a : ℝ := inner_right v x, set y := orthogonal_projection (ℝ ∙ v)ᗮ x, have split : ↑x = a • v + ↑y, { convert eq_sum_orthogonal_projection_self_orthogonal_complement (ℝ ∙ v) x, exact (orthogonal_projection_unit_singleton ℝ hv x).symm }, have hvy : ⟪v, y⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v y.2, have pythag : 1 = a ^ 2 + ∥y∥ ^ 2, { have hvy' : ⟪a • v, y⟫_ℝ = 0 := by simp [inner_smul_left, hvy], convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero _ _ hvy' using 2, { simp [← split] }, { simp [norm_smul, hv, real.norm_eq_abs, ← sq, sq_abs] }, { exact sq _ } }, -- two facts which will be helpful for clearing denominators in the main calculation have ha : 1 - a ≠ 0, { have : a < 1 := (inner_lt_one_iff_real_of_norm_one hv (by simp)).mpr hx.symm, linarith }, have : 2 ^ 2 * ∥y∥ ^ 2 + 4 * (1 - a) ^ 2 ≠ 0, { refine ne_of_gt _, have := norm_nonneg (y:E), have : 0 < (1 - a) ^ 2 := sq_pos_of_ne_zero (1 - a) ha, nlinarith }, -- the core of the problem is these two algebraic identities: have h₁ : (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 + 4)⁻¹ * 4 * (2 / (1 - a)) = 1, { field_simp, nlinarith }, have h₂ : (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 + 4)⁻¹ * (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 - 4) = a, { field_simp, transitivity (1 - a) ^ 2 * (a * (2 ^ 2 * ∥y∥ ^ 2 + 4 * (1 - a) ^ 2)), { congr, nlinarith }, ring_nf, ring }, -- deduce the result convert congr_arg2 has_add.add (congr_arg (λ t, t • (y:E)) h₁) (congr_arg (λ t, t • v) h₂) using 1, { simp [inner_add_right, inner_smul_right, hvy, real_inner_self_eq_norm_mul_norm, hv, mul_smul, mul_pow, real.norm_eq_abs, sq_abs, norm_smul] }, { simp [split, add_comm] } end lemma stereo_right_inv (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_to_fun v (stereo_inv_fun hv w) = w := begin have : 2 / (1 - (∥(w:E)∥ ^ 2 + 4)⁻¹ * (∥(w:E)∥ ^ 2 - 4)) * (∥(w:E)∥ ^ 2 + 4)⁻¹ * 4 = 1, { have : ∥(w:E)∥ ^ 2 + 4 ≠ 0 := by nlinarith, have : (4:ℝ) + 4 ≠ 0 := by nlinarith, field_simp, ring }, convert congr_arg (λ c, c • w) this, { have h₁ : orthogonal_projection (ℝ ∙ v)ᗮ v = 0 := orthogonal_projection_orthogonal_complement_singleton_eq_zero v, have h₂ : orthogonal_projection (ℝ ∙ v)ᗮ w = w := orthogonal_projection_mem_subspace_eq_self w, have h₃ : inner_right v w = (0:ℝ) := inner_right_of_mem_orthogonal_singleton v w.2, have h₄ : inner_right v v = (1:ℝ) := by simp [real_inner_self_eq_norm_mul_norm, hv], simp [h₁, h₂, h₃, h₄, continuous_linear_map.map_add, continuous_linear_map.map_smul, mul_smul] }, { simp } end /-- Stereographic projection from the unit sphere in `E`, centred at a unit vector `v` in `E`; this is the version as a local homeomorphism. -/ def stereographic (hv : ∥v∥ = 1) : local_homeomorph (sphere (0:E) 1) (ℝ ∙ v)ᗮ := { to_fun := (stereo_to_fun v) ∘ coe, inv_fun := stereo_inv_fun hv, source := {⟨v, by simp [hv]⟩}ᶜ, target := set.univ, map_source' := by simp, map_target' := λ w _, stereo_inv_fun_ne_north_pole hv w, left_inv' := λ _ hx, stereo_left_inv hv (λ h, hx (subtype.ext h)), right_inv' := λ w _, stereo_right_inv hv w, open_source := is_open_compl_singleton, open_target := is_open_univ, continuous_to_fun := continuous_on_stereo_to_fun.comp continuous_subtype_coe.continuous_on (λ w h, h ∘ subtype.ext ∘ eq.symm ∘ (inner_eq_norm_mul_iff_of_norm_one hv (by simp)).mp), continuous_inv_fun := (continuous_stereo_inv_fun hv).continuous_on } @[simp] lemma stereographic_source (hv : ∥v∥ = 1) : (stereographic hv).source = {⟨v, by simp [hv]⟩}ᶜ := rfl @[simp] lemma stereographic_target (hv : ∥v∥ = 1) : (stereographic hv).target = set.univ := rfl end stereographic_projection section charted_space /-! ### Charted space structure on the sphere In this section we construct a charted space structure on the unit sphere in a finite-dimensional real inner product space `E`; that is, we show that it is locally homeomorphic to the Euclidean space of dimension one less than `E`. The restriction to finite dimension is for convenience. The most natural `charted_space` structure for the sphere uses the stereographic projection from the antipodes of a point as the canonical chart at this point. However, the codomain of the stereographic projection constructed in the previous section is `(ℝ ∙ v)ᗮ`, the orthogonal complement of the vector `v` in `E` which is the "north pole" of the projection, so a priori these charts all have different codomains. So it is necessary to prove that these codomains are all continuously linearly equivalent to a fixed normed space. This could be proved in general by a simple case of Gram-Schmidt orthogonalization, but in the finite-dimensional case it follows more easily by dimension-counting. -/ /-- Variant of the stereographic projection, for the sphere in an `n + 1`-dimensional inner product space `E`. This version has codomain the Euclidean space of dimension `n`, and is obtained by composing the original sterographic projection (`stereographic`) with an arbitrary linear isometry from `(ℝ ∙ v)ᗮ` to the Euclidean space. -/ def stereographic' (n : ℕ) [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : local_homeomorph (sphere (0:E) 1) (euclidean_space ℝ (fin n)) := (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ (linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v)).to_homeomorph.to_local_homeomorph @[simp] lemma stereographic'_source {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).source = {v}ᶜ := by simp [stereographic'] @[simp] lemma stereographic'_target {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).target = set.univ := by simp [stereographic'] /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a charted space modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : charted_space (euclidean_space ℝ (fin n)) (sphere (0:E) 1) := { atlas := {f | ∃ v : (sphere (0:E) 1), f = stereographic' n v}, chart_at := λ v, stereographic' n (-v), mem_chart_source := λ v, by simpa using ne_neg_of_mem_unit_sphere ℝ v, chart_mem_atlas := λ v, ⟨-v, rfl⟩ } end charted_space section smooth_manifold /-! ### Smooth manifold structure on the sphere -/ /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a smooth manifold, modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : smooth_manifold_with_corners (𝓡 n) (sphere (0:E) 1) := smooth_manifold_with_corners_of_times_cont_diff_on (𝓡 n) (sphere (0:E) 1) begin rintros _ _ ⟨v, rfl⟩ ⟨v', rfl⟩, let U : (ℝ ∙ (v:E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v), let U' : (ℝ ∙ (v':E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v'), have hUv : stereographic' n v = (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ U.to_homeomorph.to_local_homeomorph := rfl, have hU'v' : stereographic' n v' = (stereographic (norm_eq_of_mem_sphere v')).trans U'.to_homeomorph.to_local_homeomorph := rfl, have H₁ := U'.times_cont_diff.comp_times_cont_diff_on times_cont_diff_on_stereo_to_fun, have H₂ := (times_cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ (v:E))ᗮ.subtypeL.times_cont_diff).comp U.symm.times_cont_diff, convert H₁.comp' (H₂.times_cont_diff_on : times_cont_diff_on ℝ ⊤ _ set.univ) using 1, have h_set : ∀ p : sphere (0:E) 1, p = v' ↔ ⟪(p:E), v'⟫_ℝ = 1, { simp [subtype.ext_iff, inner_eq_norm_mul_iff_of_norm_one] }, ext, simp [h_set, hUv, hU'v', stereographic, real_inner_comm] end /-- The inclusion map (i.e., `coe`) from the sphere in `E` to `E` is smooth. -/ lemma times_cont_mdiff_coe_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : times_cont_mdiff (𝓡 n) 𝓘(ℝ, E) ∞ (coe : (sphere (0:E) 1) → E) := begin rw times_cont_mdiff_iff, split, { exact continuous_subtype_coe }, { intros v _, let U : (ℝ ∙ ((-v):E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere (-v)), exact ((times_cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ ((-v):E))ᗮ.subtypeL.times_cont_diff).comp U.symm.times_cont_diff).times_cont_diff_on } end variables {F : Type*} [normed_group F] [normed_space ℝ F] variables {H : Type*} [topological_space H] {I : model_with_corners ℝ F H} variables {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] /-- If a `times_cont_mdiff` function `f : M → E`, where `M` is some manifold, takes values in the sphere, then it restricts to a `times_cont_mdiff` function from `M` to the sphere. -/ lemma times_cont_mdiff.cod_restrict_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] {m : with_top ℕ} {f : M → E} (hf : times_cont_mdiff I 𝓘(ℝ, E) m f) (hf' : ∀ x, f x ∈ sphere (0:E) 1) : times_cont_mdiff I (𝓡 n) m (set.cod_restrict _ _ hf' : M → (sphere (0:E) 1)) := begin rw times_cont_mdiff_iff_target, refine ⟨continuous_induced_rng hf.continuous, _⟩, intros v, let U : (ℝ ∙ ((-v):E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := (linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere (-v))), have h : times_cont_diff_on ℝ ⊤ U set.univ := U.times_cont_diff.times_cont_diff_on, have H₁ := (h.comp' times_cont_diff_on_stereo_to_fun).times_cont_mdiff_on, have H₂ : times_cont_mdiff_on _ _ _ _ set.univ := hf.times_cont_mdiff_on, convert (H₁.of_le le_top).comp' H₂ using 1, ext x, have hfxv : f x = -↑v ↔ ⟪f x, -↑v⟫_ℝ = 1, { have hfx : ∥f x∥ = 1 := by simpa using hf' x, rw inner_eq_norm_mul_iff_of_norm_one hfx, exact norm_eq_of_mem_sphere (-v) }, dsimp [chart_at], simp [not_iff_not, subtype.ext_iff, hfxv, real_inner_comm] end /-- The antipodal map is smooth. -/ lemma times_cont_mdiff_neg_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : times_cont_mdiff (𝓡 n) (𝓡 n) ∞ (λ x : sphere (0:E) 1, -x) := (times_cont_diff_neg.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _ end smooth_manifold section circle open complex local attribute [instance] finrank_real_complex_fact /-- The unit circle in `ℂ` is a charted space modelled on `euclidean_space ℝ (fin 1)`. This follows by definition from the corresponding result for `metric.sphere`. -/ instance : charted_space (euclidean_space ℝ (fin 1)) circle := metric.sphere.charted_space instance : smooth_manifold_with_corners (𝓡 1) circle := metric.sphere.smooth_manifold_with_corners /-- The unit circle in `ℂ` is a Lie group. -/ instance : lie_group (𝓡 1) circle := { smooth_mul := begin let c : circle → ℂ := coe, have h₁ : times_cont_mdiff _ _ _ (prod.map c c) := times_cont_mdiff_coe_sphere.prod_map times_cont_mdiff_coe_sphere, have h₂ : times_cont_mdiff (𝓘(ℝ, ℂ).prod 𝓘(ℝ, ℂ)) 𝓘(ℝ, ℂ) ∞ (λ (z : ℂ × ℂ), z.fst * z.snd), { rw times_cont_mdiff_iff, exact ⟨continuous_mul, λ x y, (times_cont_diff_mul.restrict_scalars ℝ).times_cont_diff_on⟩ }, exact (h₂.comp h₁).cod_restrict_sphere _, end, smooth_inv := (complex.conj_cle.times_cont_diff.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _, .. metric.sphere.smooth_manifold_with_corners } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ` is smooth. -/ lemma times_cont_mdiff_exp_map_circle : times_cont_mdiff 𝓘(ℝ, ℝ) (𝓡 1) ∞ exp_map_circle := (((times_cont_diff_exp.restrict_scalars ℝ).comp (times_cont_diff_id.smul times_cont_diff_const)).times_cont_mdiff).cod_restrict_sphere _ end circle
967869fcc42f44921824ab8d7290190de1789b32
df7bb3acd9623e489e95e85d0bc55590ab0bc393
/lean/love09_hoare_logic_exercise_solution.lean
d10a11150988d86a14b12f987ff898d79f866628
[]
no_license
MaschavanderMarel/logical_verification_2020
a41c210b9237c56cb35f6cd399e3ac2fe42e775d
7d562ef174cc6578ca6013f74db336480470b708
refs/heads/master
1,692,144,223,196
1,634,661,675,000
1,634,661,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,712
lean
import .love09_hoare_logic_demo /- # LoVe Exercise 9: Hoare Logic -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Question 1: Program Verification The following WHILE program is intended to compute the Gaussian sum up to `n`, leaving the result in `r`. -/ def GAUSS : stmt := stmt.assign "r" (λs, 0) ;; stmt.while (λs, s "n" ≠ 0) (stmt.assign "r" (λs, s "r" + s "n") ;; stmt.assign "n" (λs, s "n" - 1)) /- The summation function: -/ def sum_upto : ℕ → ℕ | 0 := 0 | (n + 1) := n + 1 + sum_upto n /- 1.1. Prove the correctness of `GAUSS` using `vcg`. The main challenge is to figure out which invariant to use for the while loop. The invariant should capture both the work that has been done already (the intermediate result) and the work that remains to be done. -/ lemma GAUSS_correct (n₀ : ℕ) : {* λs, s "n" = n₀ *} GAUSS {* λs, s "r" = sum_upto n₀ *} := show {* λs, s "n" = n₀ *} stmt.assign "r" (λs, 0) ;; stmt.while_inv (λs, s "r" + sum_upto (s "n") = sum_upto n₀) (λs, s "n" ≠ 0) (stmt.assign "r" (λs, s "r" + s "n") ;; stmt.assign "n" (λs, s "n" - 1)) {* λs, s "r" = sum_upto n₀ *}, from begin vcg; simp [sum_upto] { contextual := tt }, intro s, cases' s "n", { simp }, { simp [nat.succ_eq_add_one, sum_upto], cc } end /- 1.2. The following WHILE program is intended to compute the product of `n` and `m`, leaving the result in `r`. Prove its correctness using `vcg`. Hint: If a variable `x` does not change in a program, it might be useful to record this in the invariant, by adding a conjunct `s "x" = x₀`. -/ def MUL : stmt := stmt.assign "r" (λs, 0) ;; stmt.while (λs, s "n" ≠ 0) (stmt.assign "r" (λs, s "r" + s "m") ;; stmt.assign "n" (λs, s "n" - 1)) lemma MUL_correct (n₀ m₀ : ℕ) : {* λs, s "n" = n₀ ∧ s "m" = m₀ *} MUL {* λs, s "r" = n₀ * m₀ *} := show {* λs, s "n" = n₀ ∧ s "m" = m₀ *} stmt.assign "r" (λs, 0) ;; stmt.while_inv (λs, s "m" = m₀ ∧ s "r" + s "n" * s "m" = n₀ * s "m") (λs, s "n" ≠ 0) (stmt.assign "r" (λs, s "r" + s "m") ;; stmt.assign "n" (λs, s "n" - 1)) {* λs, s "r" = n₀ * m₀ *}, from begin vcg; simp { contextual := tt }, intro s, cases' s "n", { simp }, { simp [nat.succ_eq_add_one, mul_add, add_mul], cc } end /- ## Question 2: Hoare Triples for Total Correctness -/ def total_hoare (P : state → Prop) (S : stmt) (Q : state → Prop) : Prop := ∀s, P s → ∃t, (S, s) ⟹ t ∧ Q t notation `[* ` P : 1 ` *] ` S : 1 ` [* ` Q : 1 ` *]` := total_hoare P S Q namespace total_hoare /- 2.1. Prove the consequence rule. -/ lemma consequence {P P' Q Q' : state → Prop} {S} (hS : [* P *] S [* Q *]) (hP : ∀s, P' s → P s) (hQ : ∀s, Q s → Q' s) : [* P' *] S [* Q' *] := begin intros s hs, cases' hS s (hP s hs) with t ht, apply exists.intro t, apply and.intro, { exact and.elim_left ht }, { exact hQ t (and.elim_right ht) } end /- 2.2. Prove the rule for `skip`. -/ lemma skip_intro {P} : [* P *] stmt.skip [* P *] := begin intros s hs, apply exists.intro s, exact and.intro big_step.skip hs end /- 2.3. Prove the rule for `assign`. -/ lemma assign_intro {P : state → Prop} {x} {a : state → ℕ} : [* λs, P (s{x ↦ a s}) *] stmt.assign x a [* P *] := begin intros s hs, apply exists.intro (s{x ↦ a s}), exact and.intro big_step.assign hs end /- 2.4. Prove the rule for `seq`. -/ lemma seq_intro {P Q R S T} (hS : [* P *] S [* Q *]) (hT : [* Q *] T [* R *]) : [* P *] S ;; T [* R *] := begin intros s hs, cases' hS s hs with t hS', cases' hT t (and.elim_right hS') with u hT', apply exists.intro u, apply and.intro, { exact big_step.seq (and.elim_left hS') (and.elim_left hT') }, { exact and.elim_right hT' } end /- 2.5. Complete the proof of the rule for `ite`. Hint: This requires a case distinction on the truth value of `b s`. -/ lemma ite_intro {b P Q : state → Prop} {S T} (hS : [* λs, P s ∧ b s *] S [* Q *]) (hT : [* λs, P s ∧ ¬ b s *] T [* Q *]) : [* P *] stmt.ite b S T [* Q *] := begin intros s hs, cases' classical.em (b s), { cases' hS s (and.intro hs h) with t ht, apply exists.intro t, apply and.intro, { exact big_step.ite_true h (and.elim_left ht) }, { exact and.elim_right ht } }, { cases' hT s (and.intro hs h) with t ht, apply exists.intro t, apply and.intro, { exact big_step.ite_false h (and.elim_left ht) }, { exact and.elim_right ht } } end /- 2.6 (**optional**). Try to prove the rule for `while`. The rule is parameterized by a loop invariant `I` and by a variant `V` that decreases with each iteration of the loop body. Before we prove the desired lemma, we introduce an auxiliary lemma. Its proof requires well-founded induction. When using `while_var_intro_aux` as induction hypothesis we recommend to do it directly after proving that the argument is less than `v₀`: have ih : ∃u, (stmt.while b S, t) ⟹ u ∧ I u ∧ ¬ b u := have V t < v₀ := …, while_var_intro_aux (V t) …, Similarly to `ite`, the proof requires a case distinction on `b s ∨ ¬ b s`. -/ lemma while_var_intro_aux {b : state → Prop} (I : state → Prop) (V : state → ℕ) {S} (h_inv : ∀v₀, [* λs, I s ∧ b s ∧ V s = v₀ *] S [* λs, I s ∧ V s < v₀ *]) : ∀v₀ s, V s = v₀ → I s → ∃t, (stmt.while b S, s) ⟹ t ∧ I t ∧ ¬ b t | v₀ s V_eq hs := begin cases' classical.em (b s) with hcs hncs, { have h_inv : ∃t, (S, s) ⟹ t ∧ I t ∧ V t < v₀ := h_inv v₀ s (and.intro hs (and.intro hcs V_eq)), cases' h_inv with t ht, have ih : ∃u, (stmt.while b S, t) ⟹ u ∧ I u ∧ ¬ b u := have V t < v₀ := and.elim_right (and.elim_right ht), while_var_intro_aux (V t) t (by refl) (and.elim_left (and.elim_right ht)), cases' ih with u hu, apply exists.intro u, apply and.intro, { exact big_step.while_true hcs (and.elim_left ht) (and.elim_left hu) }, { exact and.elim_right hu } }, { apply exists.intro s, apply and.intro, { exact big_step.while_false hncs }, { exact and.intro hs hncs } } end lemma while_var_intro {b : state → Prop} (I : state → Prop) (V : state → ℕ) {S} (hinv : ∀v₀, [* λs, I s ∧ b s ∧ V s = v₀ *] S [* λs, I s ∧ V s < v₀ *]) : [* I *] stmt.while b S [* λs, I s ∧ ¬ b s *] := begin intros s hs, exact while_var_intro_aux I V hinv (V s) s (by refl) hs end end total_hoare end LoVe
222a9020edace4e0c5c490c96d54bb9129036f0b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/witt_vector/isocrystal.lean
02374a808e1f445484cc3edc121b06deb9cdac62
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
8,760
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 ring_theory.witt_vector.frobenius_fraction_field /-! ## F-isocrystals over a perfect field > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. When `k` is an integral domain, so is `𝕎 k`, and we can consider its field of fractions `K(p, k)`. The endomorphism `witt_vector.frobenius` lifts to `φ : K(p, k) → K(p, k)`; if `k` is perfect, `φ` is an automorphism. Let `k` be a perfect integral domain. Let `V` be a vector space over `K(p,k)`. An *isocrystal* is a bijective map `V → V` that is `φ`-semilinear. A theorem of Dieudonné and Manin classifies the finite-dimensional isocrystals over algebraically closed fields. In the one-dimensional case, this classification states that the isocrystal structures are parametrized by their "slope" `m : ℤ`. Any one-dimensional isocrystal is isomorphic to `φ(p^m • x) : K(p,k) → K(p,k)` for some `m`. This file proves this one-dimensional case of the classification theorem. The construction is described in Dupuis, Lewis, and Macbeth, [Formalized functional analysis via semilinear maps][dupuis-lewis-macbeth2022]. ## Main declarations * `witt_vector.isocrystal`: a vector space over the field `K(p, k)` additionally equipped with a Frobenius-linear automorphism. * `witt_vector.isocrystal_classification`: a one-dimensional isocrystal admits an isomorphism to one of the standard one-dimensional isocrystals. ## Notation This file introduces notation in the locale `isocrystal`. * `K(p, k)`: `fraction_ring (witt_vector p k)` * `φ(p, k)`: `witt_vector.fraction_ring.frobenius_ring_hom p k` * `M →ᶠˡ[p, k] M₂`: `linear_map (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂` * `M ≃ᶠˡ[p, k] M₂`: `linear_equiv (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂` * `Φ(p, k)`: `witt_vector.isocrystal.frobenius p k` * `M →ᶠⁱ[p, k] M₂`: `witt_vector.isocrystal_hom p k M M₂` * `M ≃ᶠⁱ[p, k] M₂`: `witt_vector.isocrystal_equiv p k M M₂` ## References * [Formalized functional analysis via semilinear maps][dupuis-lewis-macbeth2022] * [Theory of commutative formal groups over fields of finite characteristic][manin1963] * <https://www.math.ias.edu/~lurie/205notes/Lecture26-Isocrystals.pdf> -/ noncomputable theory open finite_dimensional namespace witt_vector variables (p : ℕ) [fact p.prime] variables (k : Type*) [comm_ring k] localized "notation (name := witt_vector.fraction_ring) `K(`p`, `k`)` := fraction_ring (witt_vector p k)" in isocrystal section perfect_ring variables [is_domain k] [char_p k p] [perfect_ring k p] /-! ### Frobenius-linear maps -/ /-- The Frobenius automorphism of `k` induces an automorphism of `K`. -/ def fraction_ring.frobenius : K(p, k) ≃+* K(p, k) := is_fraction_ring.field_equiv_of_ring_equiv (frobenius_equiv p k) /-- The Frobenius automorphism of `k` induces an endomorphism of `K`. For notation purposes. -/ def fraction_ring.frobenius_ring_hom : K(p, k) →+* K(p, k) := fraction_ring.frobenius p k localized "notation (name := witt_vector.frobenius_ring_hom) `φ(`p`, `k`)` := witt_vector.fraction_ring.frobenius_ring_hom p k" in isocrystal instance inv_pair₁ : ring_hom_inv_pair (φ(p, k)) _ := ring_hom_inv_pair.of_ring_equiv (fraction_ring.frobenius p k) instance inv_pair₂ : ring_hom_inv_pair ((fraction_ring.frobenius p k).symm : K(p, k) →+* K(p, k)) _ := ring_hom_inv_pair.of_ring_equiv (fraction_ring.frobenius p k).symm localized "notation (name := frobenius_ring_hom.linear_map) M ` →ᶠˡ[`:50 p `, ` k `] ` M₂ := linear_map (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂" in isocrystal localized "notation (name := frobenius_ring_hom.linear_equiv) M ` ≃ᶠˡ[`:50 p `, ` k `] ` M₂ := linear_equiv (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂" in isocrystal /-! ### Isocrystals -/ /-- An isocrystal is a vector space over the field `K(p, k)` additionally equipped with a Frobenius-linear automorphism. -/ class isocrystal (V : Type*) [add_comm_group V] extends module K(p, k) V := ( frob : V ≃ᶠˡ[p, k] V ) variables (V : Type*) [add_comm_group V] [isocrystal p k V] variables (V₂ : Type*) [add_comm_group V₂] [isocrystal p k V₂] variables {V} /-- Project the Frobenius automorphism from an isocrystal. Denoted by `Φ(p, k)` when V can be inferred. -/ def isocrystal.frobenius : V ≃ᶠˡ[p, k] V := @isocrystal.frob p _ k _ _ _ _ _ _ _ variables (V) localized "notation `Φ(`p`, `k`)` := witt_vector.isocrystal.frobenius p k" in isocrystal /-- A homomorphism between isocrystals respects the Frobenius map. -/ @[nolint has_nonempty_instance] structure isocrystal_hom extends V →ₗ[K(p, k)] V₂ := ( frob_equivariant : ∀ x : V, Φ(p, k) (to_linear_map x) = to_linear_map (Φ(p, k) x) ) /-- An isomorphism between isocrystals respects the Frobenius map. -/ @[nolint has_nonempty_instance] structure isocrystal_equiv extends V ≃ₗ[K(p, k)] V₂ := ( frob_equivariant : ∀ x : V, Φ(p, k) (to_linear_equiv x) = to_linear_equiv (Φ(p, k) x) ) localized "notation (name := isocrystal_hom) M ` →ᶠⁱ[`:50 p `, ` k `] ` M₂ := witt_vector.isocrystal_hom p k M M₂" in isocrystal localized "notation (name := isocrystal_equiv) M ` ≃ᶠⁱ[`:50 p `, ` k `] ` M₂ := witt_vector.isocrystal_equiv p k M M₂" in isocrystal end perfect_ring open_locale isocrystal /-! ### Classification of isocrystals in dimension 1 -/ /-- A helper instance for type class inference. -/ local attribute [instance] def fraction_ring.module : module K(p, k) K(p, k) := semiring.to_module /-- Type synonym for `K(p, k)` to carry the standard 1-dimensional isocrystal structure of slope `m : ℤ`. -/ @[nolint unused_arguments has_nonempty_instance, derive [add_comm_group, module K(p, k)]] def standard_one_dim_isocrystal (m : ℤ) : Type* := K(p, k) section perfect_ring variables [is_domain k] [char_p k p] [perfect_ring k p] /-- The standard one-dimensional isocrystal of slope `m : ℤ` is an isocrystal. -/ instance (m : ℤ) : isocrystal p k (standard_one_dim_isocrystal p k m) := { frob := (fraction_ring.frobenius p k).to_semilinear_equiv.trans (linear_equiv.smul_of_ne_zero _ _ _ (zpow_ne_zero m (witt_vector.fraction_ring.p_nonzero p k))) } @[simp] lemma standard_one_dim_isocrystal.frobenius_apply (m : ℤ) (x : standard_one_dim_isocrystal p k m) : Φ(p, k) x = (p:K(p, k)) ^ m • φ(p, k) x := rfl end perfect_ring /-- A one-dimensional isocrystal over an algebraically closed field admits an isomorphism to one of the standard (indexed by `m : ℤ`) one-dimensional isocrystals. -/ theorem isocrystal_classification (k : Type*) [field k] [is_alg_closed k] [char_p k p] (V : Type*) [add_comm_group V] [isocrystal p k V] (h_dim : finrank K(p, k) V = 1) : ∃ (m : ℤ), nonempty (standard_one_dim_isocrystal p k m ≃ᶠⁱ[p, k] V) := begin haveI : nontrivial V := finite_dimensional.nontrivial_of_finrank_eq_succ h_dim, obtain ⟨x, hx⟩ : ∃ x : V, x ≠ 0 := exists_ne 0, have : Φ(p, k) x ≠ 0 := by simpa only [map_zero] using Φ(p,k).injective.ne hx, obtain ⟨a, ha, hax⟩ : ∃ a : K(p, k), a ≠ 0 ∧ Φ(p, k) x = a • x, { rw finrank_eq_one_iff_of_nonzero' x hx at h_dim, obtain ⟨a, ha⟩ := h_dim (Φ(p, k) x), refine ⟨a, _, ha.symm⟩, intros ha', apply this, simp only [←ha, ha', zero_smul] }, obtain ⟨b, hb, m, hmb⟩ := witt_vector.exists_frobenius_solution_fraction_ring p ha, replace hmb : φ(p, k) b * a = p ^ m * b := by convert hmb, use m, let F₀ : standard_one_dim_isocrystal p k m →ₗ[K(p,k)] V := linear_map.to_span_singleton K(p, k) V x, let F : standard_one_dim_isocrystal p k m ≃ₗ[K(p,k)] V, { refine linear_equiv.of_bijective F₀ ⟨_, _⟩, { rw ← linear_map.ker_eq_bot, exact linear_map.ker_to_span_singleton K(p, k) V hx }, { rw ← linear_map.range_eq_top, rw ← (finrank_eq_one_iff_of_nonzero x hx).mp h_dim, rw linear_map.span_singleton_eq_range } }, refine ⟨⟨(linear_equiv.smul_of_ne_zero K(p, k) _ _ hb).trans F, _⟩⟩, intros c, rw [linear_equiv.trans_apply, linear_equiv.trans_apply, linear_equiv.smul_of_ne_zero_apply, linear_equiv.smul_of_ne_zero_apply, linear_equiv.map_smul, linear_equiv.map_smul], simp only [hax, linear_equiv.of_bijective_apply, linear_map.to_span_singleton_apply, linear_equiv.map_smulₛₗ, standard_one_dim_isocrystal.frobenius_apply, algebra.id.smul_eq_mul], simp only [←mul_smul], congr' 1, linear_combination φ(p,k) c * hmb, end end witt_vector
b7e6e23402858578d2fe2c182d3ddeebe73af68b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/derive_fintype_auto.lean
75b2bbe635fd2d8eb039d652ea82b65b3dab0306
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
9,311
lean
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.logic.basic import Mathlib.data.fintype.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Derive handler for `fintype` instances This file introduces a derive handler to automatically generate `fintype` instances for structures and inductives. ## Implementation notes To construct a fintype instance, we need 3 things: 1. A list `l` of elements 2. A proof that `l` has no duplicates 3. A proof that every element in the type is in `l` Now fintype is defined as a finset which enumerates all elements, so steps (1) and (2) are bundled together. It is possible to use finset operations that remove duplicates to avoid the need to prove (2), but this adds unnecessary functions to the constructed term, which makes it more expensive to compute the list, and it also adds a dependence on decidable equality for the type, which we want to avoid. Because we will rely on fintype instances for constructor arguments, we can't actually build a list directly, so (1) and (2) are necessarily somewhat intertwined. The inductive types we will be proving instances for look something like this: ``` @[derive fintype] inductive foo | zero : foo | one : bool → foo | two : ∀ x : fin 3, bar x → foo ``` The list of elements that we generate is ``` {foo.zero} ∪ (finset.univ : bool).map (λ b, finset.one b) ∪ (finset.univ : Σ' x : fin 3, bar x).map (λ ⟨x, y⟩, finset.two x y) ``` except that instead of `∪`, that is `finset.union`, we use `finset.disj_union` which doesn't require any deduplication, but does require a proof that the two parts of the union are disjoint. We use `finset.cons` to append singletons like `foo.zero`. The proofs of disjointness would be somewhat expensive since there are quadratically many of them, so instead we use a "discriminant" function. Essentially, we define ``` def foo.enum : foo → ℕ | foo.zero := 0 | (foo.one _) := 1 | (foo.two _ _) := 2 ``` and now the existence of this function implies that foo.zero is not foo.two and so on because they map to different natural numbers. We can prove that sets of natural numbers are mutually disjoint more easily because they have a linear order: `0 < 1 < 2` so `0 ≠ 2`. To package this argument up, we define `finset_above foo foo.enum n` to be a finset `s` together with a proof that all elements `a ∈ s` have `n ≤ enum a`. Now we only have to prove that `enum foo.zero = 0`, `enum (foo.one _) = 1`, etc. (linearly many proofs, all `rfl`) in order to prove that all variants are mutually distinct. We mirror the `finset.cons` and `finset.disj_union` functions into `finset_above.cons` and `finset_above.union`, and this forms the main part of the finset construction. This only handles distinguishing variants of a finset. Now we must enumerate the elements of a variant, for example `{foo.one ff, foo.one tt}`, while at the same time proving that all these elements have discriminant `1` in this case. To do that, we use the `finset_in` type, which is a finset satisfying a property `P`, here `λ a, foo.enum a = 1`. We could use `finset.bind` many times to construct the finset but it turns out to be somewhat complicated to get good side goals for a naturally nodup version of `finset.bind` in the same way as we did with `finset.cons` and `finset.union`. Instead, we tuple up all arguments into one type, leveraging the `fintype` instance on `psigma`, and then define a map from this type to the inductive type that untuples them and applies the constructor. The injectivity property of the constructor ensures that this function is injective, so we can use `finset.map` to apply it. This is the content of the constructor `finset_in.mk`. That completes the proofs of (1) and (2). To prove (3), we perform one case analysis over the inductive type, proving theorems like ``` foo.one a ∈ {foo.zero} ∪ (finset.univ : bool).map (λ b, finset.one b) ∪ (finset.univ : Σ' x : fin 3, bar x).map (λ ⟨x, y⟩, finset.two x y) ``` by seeking to the relevant disjunct and then supplying the constructor arguments. This part of the proof is quadratic, but quite simple. (We could do it in `O(n log n)` if we used a balanced tree for the unions.) The tactics perform the following parts of this proof scheme: * `mk_sigma` constructs the type `Γ` in `finset_in.mk` * `mk_sigma_elim` constructs the function `f` in `finset_in.mk` * `mk_sigma_elim_inj` proves that `f` is injective * `mk_sigma_elim_eq` proves that `∀ a, enum (f a) = k` * `mk_finset` constructs the finset `S = {foo.zero} ∪ ...` by recursion on the variants * `mk_finset_total` constructs the proof `|- foo.zero ∈ S; |- foo.one a ∈ S; |- foo.two a b ∈ S` by recursion on the subgoals coming out of the initial `cases` * `mk_fintype_instance` puts it all together to produce a proof of `fintype foo`. The construction of `foo.enum` is also done in this function. -/ namespace derive_fintype /-- A step in the construction of `finset.univ` for a finite inductive type. We will set `enum` to the discriminant of the inductive type, so a `finset_above` represents a finset that enumerates all elements in a tail of the constructor list. -/ def finset_above (α : Type u_1) (enum : α → ℕ) (n : ℕ) := Subtype fun (s : finset α) => ∀ (x : α), x ∈ s → n ≤ enum x /-- Construct a fintype instance from a completed `finset_above`. -/ def mk_fintype {α : Type u_1} (enum : α → ℕ) (s : finset_above α enum 0) (H : ∀ (x : α), x ∈ subtype.val s) : fintype α := fintype.mk (subtype.val s) H /-- This is the case for a simple variant (no arguments) in an inductive type. -/ def finset_above.cons {α : Type u_1} {enum : α → ℕ} (n : ℕ) (a : α) (h : enum a = n) (s : finset_above α enum (n + 1)) : finset_above α enum n := { val := finset.cons a (subtype.val s) sorry, property := sorry } theorem finset_above.mem_cons_self {α : Type u_1} {enum : α → ℕ} {n : ℕ} {a : α} {h : enum a = n} {s : finset_above α enum (n + 1)} : a ∈ subtype.val (finset_above.cons n a h s) := multiset.mem_cons_self a (finset.val (subtype.val s)) theorem finset_above.mem_cons_of_mem {α : Type u_1} {enum : α → ℕ} {n : ℕ} {a : α} {h : enum a = n} {s : finset_above α enum (n + 1)} {b : α} : b ∈ subtype.val s → b ∈ subtype.val (finset_above.cons n a h s) := multiset.mem_cons_of_mem /-- The base case is when we run out of variants; we just put an empty finset at the end. -/ def finset_above.nil {α : Type u_1} {enum : α → ℕ} (n : ℕ) : finset_above α enum n := { val := ∅, property := sorry } protected instance finset_above.inhabited (α : Type u_1) (enum : α → ℕ) (n : ℕ) : Inhabited (finset_above α enum n) := { default := finset_above.nil n } /-- This is a finset covering a nontrivial variant (with one or more constructor arguments). The property `P` here is `λ a, enum a = n` where `n` is the discriminant for the current variant. -/ def finset_in {α : Type u_1} (P : α → Prop) := Subtype fun (s : finset α) => ∀ (x : α), x ∈ s → P x /-- To construct the finset, we use an injective map from the type `Γ`, which will be the sigma over all constructor arguments. We use sigma instances and existing fintype instances to prove that `Γ` is a fintype, and construct the function `f` that maps `⟨a, b, c, ...⟩` to `C_n a b c ...` where `C_n` is the nth constructor, and `mem` asserts `enum (C_n a b c ...) = n`. -/ def finset_in.mk {α : Type u_1} {P : α → Prop} (Γ : Type u_2) [fintype Γ] (f : Γ → α) (inj : function.injective f) (mem : ∀ (x : Γ), P (f x)) : finset_in P := { val := finset.map (function.embedding.mk f inj) finset.univ, property := sorry } theorem finset_in.mem_mk {α : Type u_1} {P : α → Prop} {Γ : Type u_2} {s : fintype Γ} {f : Γ → α} {inj : function.injective f} {mem : ∀ (x : Γ), P (f x)} {a : α} (b : Γ) (H : f b = a) : a ∈ subtype.val (finset_in.mk Γ f inj mem) := iff.mpr finset.mem_map (Exists.intro b (Exists.intro (finset.mem_univ b) H)) /-- For nontrivial variants, we split the constructor list into a `finset_in` component for the current constructor and a `finset_above` for the rest. -/ def finset_above.union {α : Type u_1} {enum : α → ℕ} (n : ℕ) (s : finset_in fun (a : α) => enum a = n) (t : finset_above α enum (n + 1)) : finset_above α enum n := { val := finset.disj_union (subtype.val s) (subtype.val t) sorry, property := sorry } theorem finset_above.mem_union_left {α : Type u_1} {enum : α → ℕ} {n : ℕ} {s : finset_in fun (a : α) => enum a = n} {t : finset_above α enum (n + 1)} {a : α} (H : a ∈ subtype.val s) : a ∈ subtype.val (finset_above.union n s t) := iff.mpr multiset.mem_add (Or.inl H) theorem finset_above.mem_union_right {α : Type u_1} {enum : α → ℕ} {n : ℕ} {s : finset_in fun (a : α) => enum a = n} {t : finset_above α enum (n + 1)} {a : α} (H : a ∈ subtype.val t) : a ∈ subtype.val (finset_above.union n s t) := iff.mpr multiset.mem_add (Or.inr H) end Mathlib
c2d55f2e6657a59139e7f38e0f1e9c72f0f7bbfe
022215fec0be87ac6243b0f4fa3cc2939361d7d0
/src/category_theory/instances/Top/opens.lean
e9af3c1348d0261a71d6644a77b79a599e930be8
[ "Apache-2.0" ]
permissive
PaulGustafson/mathlib
4aa7bc81ca971fdd7b6e50bf3a245fade2978391
c49ac06ff9fa1371e9b6050a121df618cfd3fb80
refs/heads/master
1,590,798,947,521
1,559,220,227,000
1,559,220,227,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,023
lean
-- Copyright (c) 2019 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.instances.Top.basic import category_theory.natural_isomorphism import category_theory.opposites import category_theory.eq_to_hom import topology.opens open category_theory open category_theory.instances open topological_space universe u open category_theory.instances opposite namespace topological_space.opens variables {X Y Z : Top.{u}} instance opens_category : category.{u+1} (opens X) := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans f.down.down g.down.down ⟩ ⟩ } def to_Top (X : Top.{u}) : opens X ⥤ Top := { obj := λ U, ⟨U.val, infer_instance⟩, map := λ U V i, ⟨λ x, ⟨x.1, i.down.down x.2⟩, (embedding.continuous_iff embedding_subtype_val).2 continuous_induced_dom⟩ } /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟶ Y) : opens Y ⥤ opens X := { obj := λ U, ⟨ f.val ⁻¹' U.val, f.property _ U.property ⟩, map := λ U V i, ⟨ ⟨ λ a b, i.down.down b ⟩ ⟩ }. @[simp] lemma map_obj (f : X ⟶ Y) (U) (p) : (map f).obj ⟨U, p⟩ = ⟨ f.val ⁻¹' U, f.property _ p ⟩ := rfl @[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U := by simp section variable (X) def map_id : map (𝟙 X) ≅ functor.id (opens X) := { hom := { app := λ U, eq_to_hom (map_id_obj U) }, inv := { app := λ U, eq_to_hom (map_id_obj U).symm } } @[simp] lemma map_id_hom_app (U) : (map_id X).hom.app U = eq_to_hom (map_id_obj U) := rfl @[simp] lemma map_id_inv_app (U) : (map_id X).inv.app U = eq_to_hom (map_id_obj U).symm := rfl end @[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := by simp @[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := by simp def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := { hom := { app := λ U, eq_to_hom (map_comp_obj f g U) }, inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } } @[simp] lemma map_comp_hom_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).hom.app U = eq_to_hom (map_comp_obj f g U) := rfl @[simp] lemma map_comp_inv_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).inv.app U = eq_to_hom (map_comp_obj f g U).symm := rfl -- We could make f g implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g := nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) @[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl end topological_space.opens
2532835bffb64190c875042abd4a42b551c2dad3
495c02489c2d6a1db94dfdba71dd800d3cc67df2
/group_theory/cyclic.lean
2c1623f5260026fa01f6bed456aa684c2e9c87e3
[ "Apache-2.0" ]
permissive
leodemoura/leanproved
e0fcbe4f4d72bf0dad9a962ed111b5975cf90712
de56e0af159dd0c0421733289c76aa79c78a0191
refs/heads/master
1,606,822,676,898
1,435,711,541,000
1,435,711,541,000
36,675,856
0
0
null
1,433,178,724,000
1,433,178,724,000
null
UTF-8
Lean
false
false
19,206
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import data algebra.group algebra.group_power .finsubg .hom .finfun .perm open function algebra finset open eq.ops namespace group section cyclic open nat fin local attribute madd [reducible] definition mk_mod [reducible] (n i : nat) : fin (succ n) := mk (i mod (succ n)) (mod_lt _ !zero_lt_succ) lemma mk_mod_of_lt {n i : nat} (Plt : i < succ n) : mk_mod n i = mk i Plt := begin esimp [mk_mod], congruence, exact mod_eq_of_lt Plt end lemma mk_succ_ne_zero {n i : nat} : ∀ {P}, mk (succ i) P ≠ zero n := assume P Pe, absurd (veq_of_eq Pe) !succ_ne_zero lemma madd_mk_mod {n i j : nat} : madd (mk_mod n i) (mk_mod n j) = mk_mod n (i+j) := eq_of_veq begin esimp [madd, mk_mod], rewrite [ mod_add_mod, add_mod_mod ] end definition diff [reducible] (i j : nat) := if (i < j) then (j - i) else (i - j) lemma diff_eq_dist {i j : nat} : diff i j = dist i j := #nat decidable.by_cases (λ Plt : i < j, by rewrite [if_pos Plt, ↑dist, sub_eq_zero_of_le (le_of_lt Plt), zero_add]) (λ Pnlt : ¬ i < j, by rewrite [if_neg Pnlt, ↑dist, sub_eq_zero_of_le (le_of_not_gt Pnlt)]) lemma diff_eq_max_sub_min {i j : nat} : diff i j = (max i j) - min i j := decidable.by_cases (λ Plt : i < j, begin rewrite [↑max, ↑min, *(if_pos Plt)] end) (λ Pnlt : ¬ i < j, begin rewrite [↑max, ↑min, *(if_neg Pnlt)] end) lemma diff_succ {i j : nat} : diff (succ i) (succ j) = diff i j := by rewrite [*diff_eq_dist, ↑dist, *succ_sub_succ] lemma diff_add {i j k : nat} : diff (i + k) (j + k) = diff i j := by rewrite [*diff_eq_dist, dist_add_add_right] lemma diff_le_max {i j : nat} : diff i j ≤ max i j := begin rewrite diff_eq_max_sub_min, apply sub_le end lemma diff_gt_zero_of_ne {i j : nat} : i ≠ j → diff i j > 0 := assume Pne, decidable.by_cases (λ Plt : i < j, begin rewrite [if_pos Plt], apply sub_pos_of_lt Plt end) (λ Pnlt : ¬ i < j, begin rewrite [if_neg Pnlt], apply sub_pos_of_lt, apply lt_of_le_and_ne (nat.le_of_not_gt Pnlt) (ne.symm Pne) end) lemma max_lt_of_lt_of_lt {i j n : nat} : i < n → j < n → max i j < n := assume Pilt Pjlt, decidable.by_cases (λ Plt : i < j, by rewrite [↑max, if_pos Plt]; exact Pjlt) (λ Pnlt : ¬ i < j, by rewrite [↑max, if_neg Pnlt]; exact Pilt) lemma max_lt {n : nat} (i j : fin n) : max i j < n := max_lt_of_lt_of_lt (is_lt i) (is_lt j) variable {A : Type} open list lemma zero_lt_length_of_mem {a : A} : ∀ {l : list A}, a ∈ l → 0 < length l | [] := assume Pinnil, by contradiction | (b::l) := assume Pin, !zero_lt_succ variable [ambG : group A] include ambG lemma pow_mod {a : A} {n m : nat} : a ^ m = 1 → a ^ n = a ^ (n mod m) := assume Pid, have Pm : a ^ (n div m * m) = 1, from calc a ^ (n div m * m) = a ^ (m * (n div m)) : {mul.comm (n div m) m} ... = (a ^ m) ^ (n div m) : !pow_mul ... = 1 ^ (n div m) : {Pid} ... = 1 : one_pow (n div m), calc a ^ n = a ^ (n div m * m + n mod m) : {eq_div_mul_add_mod n m} ... = a ^ (n div m * m) * a ^ (n mod m) : !pow_add ... = 1 * a ^ (n mod m) : {Pm} ... = a ^ (n mod m) : !one_mul lemma pow_sub_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j → a^(i - j) = 1 := assume Pe, or.elim (lt_or_ge i j) (assume Piltj, begin rewrite [sub_eq_zero_of_le (nat.le_of_lt Piltj)] end) (assume Pigej, begin rewrite [pow_sub a Pigej, Pe, mul.right_inv] end) lemma pow_diff_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j → a^(diff i j) = 1 := assume Pe, decidable.by_cases (λ Plt : i < j, by rewrite [if_pos Plt]; exact pow_sub_eq_one_of_pow_eq (eq.symm Pe)) (λ Pnlt : ¬ i < j, by rewrite [if_neg Pnlt]; exact pow_sub_eq_one_of_pow_eq Pe) lemma pow_madd {a : A} {n : nat} {i j : fin (succ n)} : a^(succ n) = 1 → a^(val (i + j)) = a^i * a^j := assume Pe, calc a^(val (i + j)) = a^((i + j) mod (succ n)) : rfl ... = a^(i + j) : pow_mod Pe ... = a^i * a^j : !pow_add lemma mk_pow_mod {a : A} {n m : nat} : a ^ (succ m) = 1 → a ^ n = a ^ (mk_mod m n) := assume Pe, pow_mod Pe variable [finA : fintype A] include finA open fintype lemma card_pos : 0 < card A := zero_lt_length_of_mem (mem_univ 1) variable [deceqA : decidable_eq A] include deceqA lemma exists_pow_eq_one (a : A) : ∃ n, n < card A ∧ a ^ (succ n) = 1 := let f := (λ i : fin (succ (card A)), a ^ i) in assert Pninj : ¬(injective f), from assume Pinj, absurd (card_le_of_inj _ _ (exists.intro f Pinj)) (begin rewrite [card_fin], apply not_succ_le_self end), obtain i₁ P₁, from exists_not_of_not_forall Pninj, obtain i₂ P₂, from exists_not_of_not_forall P₁, obtain Pfe Pne, from iff.elim_left not_implies_iff_and_not P₂, assert Pvne : val i₁ ≠ val i₂, from assume Pveq, absurd (eq_of_veq Pveq) Pne, exists.intro (pred (diff i₁ i₂)) (begin rewrite [succ_pred_of_pos (diff_gt_zero_of_ne Pvne)], apply and.intro, apply lt_of_succ_lt_succ, rewrite [succ_pred_of_pos (diff_gt_zero_of_ne Pvne)], apply nat.lt_of_le_of_lt diff_le_max (max_lt i₁ i₂), apply pow_diff_eq_one_of_pow_eq Pfe end) -- Another possibility is to generate a list of powers and use find to get the first -- unity. -- The bound on bex is arbitrary as long as it is large enough (at least card A). Making -- it larger simplifies some proofs, such as a ∈ cyc a. definition cyc (a : A) : finset A := {x ∈ univ | bex (succ (card A)) (λ n, a ^ n = x)} definition order (a : A) := card (cyc a) definition pow_fin (a : A) (n : nat) (i : fin (order a)) := pow a (i + n) definition cyc_pow_fin (a : A) (n : nat) : finset A := image (pow_fin a n) univ lemma order_le_group_order {a : A} : order a ≤ card A := card_le_card_of_subset !subset_univ lemma cyc_has_one (a : A) : 1 ∈ cyc a := begin apply mem_filter_of_mem !mem_univ, existsi 0, apply and.intro, apply zero_lt_succ, apply pow_zero end lemma order_pos (a : A) : 0 < order a := zero_lt_length_of_mem (cyc_has_one a) lemma cyc_mul_closed (a : A) : finset_mul_closed_on (cyc a) := take g h, assume Pgin Phin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_filter Pgin, obtain j Pjlt Pjh, from of_mem_filter Phin, begin rewrite [-Pig, -Pjh, -pow_add, pow_mod Pe], apply mem_filter_of_mem !mem_univ, existsi ((i + j) mod (succ n)), apply and.intro, apply nat.lt.trans (mod_lt (i+j) !zero_lt_succ) (succ_lt_succ Plt), apply rfl end lemma cyc_has_inv (a : A) : finset_has_inv (cyc a) := take g, assume Pgin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_filter Pgin, let ni := -(mk_mod n i) in assert Pinv : g*a^ni = 1, by rewrite [-Pig, mk_pow_mod Pe, -(pow_madd Pe), add.right_inv], begin rewrite [inv_eq_of_mul_eq_one Pinv], apply mem_filter_of_mem !mem_univ, existsi ni, apply and.intro, apply nat.lt.trans (is_lt ni) (succ_lt_succ Plt), apply rfl end lemma self_mem_cyc (a : A) : a ∈ cyc a := mem_filter_of_mem !mem_univ (exists.intro (1 : nat) (and.intro (succ_lt_succ card_pos) !pow_one)) lemma mem_cyc (a : A) : ∀ {n : nat}, a^n ∈ cyc a | 0 := cyc_has_one a | (succ n) := begin rewrite pow_succ, apply cyc_mul_closed a, exact mem_cyc, apply self_mem_cyc end lemma order_le {a : A} {n : nat} : a^(succ n) = 1 → order a ≤ succ n := assume Pe, let s := image (pow a) (upto (succ n)) in assert Psub: cyc a ⊆ s, from subset_of_forall (take g, assume Pgin, obtain i Pilt Pig, from of_mem_filter Pgin, begin rewrite [-Pig, pow_mod Pe], apply mem_image_of_mem_of_eq, apply mem_upto_of_lt (mod_lt i !zero_lt_succ), exact rfl end), #nat calc order a ≤ card s : card_le_card_of_subset Psub ... ≤ card (upto (succ n)) : !card_image_le ... = succ n : card_upto (succ n) lemma pow_ne_of_lt_order {a : A} {n : nat} : succ n < order a → a^(succ n) ≠ 1 := assume Plt, not_imp_not_of_imp order_le (nat.not_le_of_gt Plt) lemma eq_zero_of_pow_eq_one {a : A} : ∀ {n : nat}, a^n = 1 → n < order a → n = 0 | 0 := assume Pe Plt, rfl | (succ n) := assume Pe Plt, absurd Pe (pow_ne_of_lt_order Plt) lemma pow_fin_inj (a : A) (n : nat) : injective (pow_fin a n) := take i j, assume Peq : a^(i + n) = a^(j + n), have Pde : a^(diff i j) = 1, from diff_add ▸ pow_diff_eq_one_of_pow_eq Peq, have Pdz : diff i j = 0, from eq_zero_of_pow_eq_one Pde (nat.lt_of_le_of_lt diff_le_max (max_lt i j)), eq_of_veq (eq_of_dist_eq_zero (diff_eq_dist ▸ Pdz)) lemma cyc_eq_cyc (a : A) (n : nat) : cyc_pow_fin a n = cyc a := assert Psub : cyc_pow_fin a n ⊆ cyc a, from subset_of_forall (take g, assume Pgin, obtain i Pin Pig, from exists_of_mem_image Pgin, by rewrite [-Pig]; apply mem_cyc), eq_of_card_eq_of_subset (begin apply eq.trans, apply card_image_eq_of_inj_on, rewrite [to_set_univ, -set.injective_iff_inj_on_univ], exact pow_fin_inj a n, rewrite [card_fin] end) Psub lemma pow_order (a : A) : a^(order a) = 1 := obtain i Pin Pone, from exists_of_mem_image (eq.symm (cyc_eq_cyc a 1) ▸ cyc_has_one a), or.elim (eq_or_lt_of_le (succ_le_of_lt (is_lt i))) (assume P, P ▸ Pone) (assume P, absurd Pone (pow_ne_of_lt_order P)) lemma order_of_min_pow {a : A} {n : nat} (Pone : a^(succ n) = 1) (Pmin : ∀ i, i < n → a^(succ i) ≠ 1) : order a = succ n := or.elim (eq_or_lt_of_le (order_le Pone)) (λ P, P) (λ P : order a < succ n, begin assert Pn : a^(order a) ≠ 1, rewrite [-(succ_pred_of_pos (order_pos a))], apply Pmin, apply nat.lt_of_succ_lt_succ, rewrite [succ_pred_of_pos !order_pos], assumption, exact absurd (pow_order a) Pn end) definition cyc_is_finsubg [instance] (a : A) : is_finsubg (cyc a) := is_finsubg.mk (cyc_has_one a) (cyc_mul_closed a) (cyc_has_inv a) lemma order_dvd_group_order (a : A) : order a ∣ card A := dvd.intro (eq.symm (!mul.comm ▸ lagrange_theorem (subset_univ (cyc a)))) definition pow_fin' (a : A) (i : fin (succ (pred (order a)))) := pow a i local attribute group_of_add_group [instance] lemma pow_fin_hom (a : A) : homomorphic (pow_fin' a) := take i j, begin rewrite [↑pow_fin'], apply pow_madd, rewrite [succ_pred_of_pos !order_pos], exact pow_order a end definition pow_fin_is_iso (a : A) : is_iso_class (pow_fin' a) := is_iso_class.mk (pow_fin_hom a) (begin rewrite [↑pow_fin', succ_pred_of_pos !order_pos], exact pow_fin_inj a 0 end) end cyclic section rot open nat list lemma lt_succ_of_lt {i j : nat} : i < j → i < succ j := assume Plt, lt.trans Plt (self_lt_succ j) lemma map_append {A B : Type} {f : A → B} : ∀ {l₁ l₂ : list A}, map f (l₁++l₂) = (map f l₁)++(map f l₂) | nil := take l, rfl | (a::l) := take l', begin rewrite [append_cons, *map_cons, append_cons, map_append] end lemma map_singleton {A B : Type} {f : A → B} (a : A) : map f [a] = [f a] := rfl lemma list.upto_step : ∀ {n : nat}, upto (succ n) = (map succ (upto n))++[0] | 0 := rfl | (succ n) := begin rewrite [upto_succ n, map_cons, append_cons, -list.upto_step] end open fin fintype list lemma dmap_map_lift {n : nat} : ∀ l : list nat, (∀ i, i ∈ l → i < n) → dmap (λ i, i < succ n) mk l = map lift_succ (dmap (λ i, i < n) mk l) | [] := assume Plt, rfl | (i::l) := assume Plt, begin rewrite [@dmap_cons_of_pos _ _ (λ i, i < succ n) _ _ _ (lt_succ_of_lt (Plt i !mem_cons)), @dmap_cons_of_pos _ _ (λ i, i < n) _ _ _ (Plt i !mem_cons), map_cons], congruence, apply dmap_map_lift, intro j Pjinl, apply Plt, apply mem_cons_of_mem, assumption end lemma upto_succ (n : nat) : upto (succ n) = maxi :: map lift_succ (upto n) := begin rewrite [↑fin.upto, list.upto_succ, @dmap_cons_of_pos _ _ (λ i, i < succ n) _ _ _ (nat.self_lt_succ n)], congruence, apply dmap_map_lift, apply @list.lt_of_mem_upto end lemma succ_max {n : nat} : fin.succ maxi = (@maxi (succ n)) := rfl lemma lift_zero {n : nat} : lift_succ (zero n) = zero (succ n) := rfl lemma lift_succ.comm {n : nat} : lift_succ ∘ (@succ n) = succ ∘ lift_succ := funext take i, eq_of_veq (begin rewrite [↑lift_succ, -val_lift, *val_succ, -val_lift] end) definition upto_step : ∀ {n : nat}, fin.upto (succ n) = (map succ (upto n))++[zero n] | 0 := rfl | (succ n) := begin rewrite [upto_succ n, map_cons, append_cons, succ_max, upto_succ, -lift_zero], congruence, rewrite [map_map, -lift_succ.comm, -map_map, -(map_singleton (zero n)), -map_append, -upto_step] end section local attribute group_of_add_group [instance] local infix ^ := algebra.pow lemma pow_eq_mul {n : nat} {i : fin (succ n)} : ∀ {k : nat}, i^k = mk_mod n (i*k) | 0 := by rewrite [pow_zero] | (succ k) := begin assert Psucc : i^(succ k) = madd (i^k) i, apply pow_succ, rewrite [Psucc, pow_eq_mul], apply eq_of_veq, rewrite [mul_succ, val_madd, ↑mk_mod, mod_add_mod] end end definition rotl : ∀ {n : nat} m : nat, fin n → fin n | 0 := take m i, elim0 i | (succ n) := take m, madd (mk_mod n (n*m)) definition rotr : ∀ {n : nat} m : nat, fin n → fin n | (0:nat) := take m i, elim0 i | (nat.succ n) := take m, madd (-(mk_mod n (n*m))) lemma rotl_succ' {n m : nat} : rotl m = madd (mk_mod n (n*m)) := rfl lemma rotl_zero : ∀ {n : nat}, @rotl n 0 = id | 0 := funext take i, elim0 i | (succ n) := funext take i, zero_add i lemma rotl_id : ∀ {n : nat}, @rotl n n = id | 0 := funext take i, elim0 i | (succ n) := assert P : mk_mod n (n * succ n) = mk_mod n 0, from eq_of_veq !mul_mod_left, begin rewrite [rotl_succ', P], apply rotl_zero end lemma rotl_to_zero {n i : nat} : rotl i (mk_mod n i) = zero n := eq_of_veq begin rewrite [↑rotl, val_madd], esimp [mk_mod], rewrite [ mod_add_mod, add_mod_mod, -succ_mul, mul_mod_right] end lemma rotl_compose : ∀ {n : nat} {j k : nat}, (@rotl n j) ∘ (rotl k) = rotl (j + k) | 0 := take j k, funext take i, elim0 i | (succ n) := take j k, funext take i, eq.symm begin rewrite [*rotl_succ', mul.left_distrib, -(@madd_mk_mod n (n*j)), madd_assoc], end lemma rotr_rotl : ∀ {n : nat} (m : nat) {i : fin n}, rotr m (rotl m i) = i | 0 := take m i, elim0 i | (nat.succ n) := take m i, calc (-(mk_mod n (n*m))) + ((mk_mod n (n*m)) + i) = i : by rewrite neg_add_cancel_left lemma rotl_rotr : ∀ {n : nat} (m : nat), (@rotl n m) ∘ (rotr m) = id | 0 := take m, funext take i, elim0 i | (nat.succ n) := take m, funext take i, calc (mk_mod n (n*m)) + (-(mk_mod n (n*m)) + i) = i : add_neg_cancel_left lemma rotl_succ {n : nat} : (rotl 1) ∘ (@succ n) = lift_succ := funext (take i, eq_of_veq (begin rewrite [↑compose, ↑rotl, ↑madd, mul_one n, ↑mk_mod, mod_add_mod, ↑lift_succ, val_succ, -succ_add_eq_succ_add, add_mod_self_left, mod_eq_of_lt (lt.trans (is_lt i) !lt_succ_self), -val_lift] end)) definition list.rotl {A : Type} : ∀ l : list A, list A | [] := [] | (a::l) := l++[a] lemma rotl_cons {A : Type} {a : A} {l} : list.rotl (a::l) = l++[a] := rfl lemma rotl_map {A B : Type} {f : A → B} : ∀ {l : list A}, list.rotl (map f l) = map f (list.rotl l) | [] := rfl | (a::l) := begin rewrite [map_cons, *rotl_cons, map_append] end lemma rotl_eq_rotl : ∀ {n : nat}, map (rotl 1) (upto n) = list.rotl (upto n) | 0 := rfl | (succ n) := begin rewrite [upto_step at {1}, upto_succ, rotl_cons, map_append], congruence, rewrite [map_map], congruence, exact rotl_succ, rewrite [map_singleton], congruence, rewrite [↑rotl, mul_one n, ↑mk_mod, ↑zero, ↑maxi, ↑madd], congruence, rewrite [ mod_add_mod, nat.add_zero, mod_eq_of_lt !lt_succ_self ] end definition seq [reducible] (A : Type) (n : nat) := fin n → A variable {A : Type} definition rotl_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotl m) definition rotr_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotr m) lemma rotl_seq_zero {n : nat} : rotl_fun 0 = @id (seq A n) := funext take f, begin rewrite [↑rotl_fun, rotl_zero] end lemma rotl_seq_ne_id : ∀ {n : nat}, (∃ a b : A, a ≠ b) → ∀ i, i < n → rotl_fun (succ i) ≠ (@id (seq A (succ n))) | 0 := assume Pex, take i, assume Piltn, absurd Piltn !not_lt_zero | (succ n) := assume Pex, obtain a b Pne, from Pex, take i, assume Pilt, let f := (λ j : fin (succ (succ n)), if j = zero (succ n) then a else b), fi := mk_mod (succ n) (succ i) in have Pfne : rotl_fun (succ i) f fi ≠ f fi, from begin rewrite [↑rotl_fun, rotl_to_zero, mk_mod_of_lt (succ_lt_succ Pilt), if_pos rfl, if_neg mk_succ_ne_zero], assumption end, have P : rotl_fun (succ i) f ≠ f, from assume Peq, absurd (congr_fun Peq fi) Pfne, assume Peq, absurd (congr_fun Peq f) P lemma rotr_rotl_fun {n : nat} (m : nat) (f : seq A n) : rotr_fun m (rotl_fun m f) = f := calc f ∘ (rotl m) ∘ (rotr m) = f ∘ ((rotl m) ∘ (rotr m)) : compose.assoc ... = f ∘ id : {rotl_rotr m} lemma rotl_fun_inj {n : nat} {m : nat} : @injective (seq A n) (seq A n) (rotl_fun m) := injective_of_has_left_inverse (exists.intro (rotr_fun m) (rotr_rotl_fun m)) lemma seq_rotl_eq_list_rotl {n : nat} (f : seq A n) : fun_to_list (rotl_fun 1 f) = list.rotl (fun_to_list f) := begin rewrite [↑fun_to_list, ↑rotl_fun, -map_map, rotl_map], congruence, exact rotl_eq_rotl end end rot section rotg open nat fin fintype definition rotl_perm [reducible] (A : Type) [finA : fintype A] [deceqA : decidable_eq A] (n : nat) (m : nat) : perm (seq A n) := perm.mk (rotl_fun m) rotl_fun_inj variable {A : Type} variable [finA : fintype A] include finA variable [deceqA : decidable_eq A] include deceqA variable {n : nat} lemma rotl_perm_mul {i j : nat} : (rotl_perm A n i) * (rotl_perm A n j) = rotl_perm A n (j+i) := eq_of_feq (funext take f, calc f ∘ (rotl j) ∘ (rotl i) = f ∘ ((rotl j) ∘ (rotl i)) : compose.assoc ... = f ∘ (rotl (j+i)) : rotl_compose) lemma rotl_perm_pow_eq : ∀ {i : nat}, (rotl_perm A n 1) ^ i = rotl_perm A n i | 0 := begin rewrite [pow_zero, ↑rotl_perm, perm_one, -eq_iff_feq], esimp, rewrite rotl_seq_zero end | (succ i) := begin rewrite [pow_succ, rotl_perm_pow_eq, rotl_perm_mul, one_add] end lemma rotl_perm_pow_eq_one : (rotl_perm A n 1) ^ n = 1 := eq.trans rotl_perm_pow_eq (eq_of_feq begin esimp [rotl_perm], rewrite [↑rotl_fun, rotl_id] end) lemma rotl_perm_mod {i : nat} : rotl_perm A n i = rotl_perm A n (i mod n) := calc rotl_perm A n i = (rotl_perm A n 1) ^ i : rotl_perm_pow_eq ... = (rotl_perm A n 1) ^ (i mod n) : pow_mod rotl_perm_pow_eq_one ... = rotl_perm A n (i mod n) : rotl_perm_pow_eq -- needs A to have at least two elements! lemma rotl_perm_pow_ne_one (Pex : ∃ a b : A, a ≠ b) : ∀ i, i < n → (rotl_perm A (succ n) 1)^(succ i) ≠ 1 := take i, assume Piltn, begin intro P, revert P, rewrite [rotl_perm_pow_eq, -eq_iff_feq, perm_one, *perm.f_mk], intro P, exact absurd P (rotl_seq_ne_id Pex i Piltn) end lemma rotl_perm_order (Pex : ∃ a b : A, a ≠ b) : order (rotl_perm A (succ n) 1) = (succ n) := order_of_min_pow rotl_perm_pow_eq_one (rotl_perm_pow_ne_one Pex) end rotg end group
aa810a58d76076b8b7d2787928f39cdd9cc7c07f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/probability/kernel/integral_comp_prod.lean
53b9df2f7c6e8104890199ea862b4de378e32f48
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
14,440
lean
/- Copyright (c) 2023 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import probability.kernel.composition import measure_theory.integral.set_integral /-! # Bochner integral of a function against the composition-product of two kernels > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We prove properties of the composition-product of two kernels. If `κ` is an s-finite kernel from `α` to `β` and `η` is an s-finite kernel from `α × β` to `γ`, we can form their composition-product `κ ⊗ₖ η : kernel α (β × γ)`. We proved in `probability.kernel.lintegral_comp_prod` that it verifies `∫⁻ bc, f bc ∂((κ ⊗ₖ η) a) = ∫⁻ b, ∫⁻ c, f (b, c) ∂(η (a, b)) ∂(κ a)`. In this file, we prove the same equality for the Bochner integral. ## Main statements * `probability_theory.integral_comp_prod`: the integral against the composition-product is `∫ z, f z ∂((κ ⊗ₖ η) a) = ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a)` ## Implementation details This file is to a large extent a copy of part of `measure_theory.constructions.prod`. The product of two measures is a particular case of composition-product of kernels and it turns out that once the measurablity of the Lebesgue integral of a kernel is proved, almost all proofs about integrals against products of measures extend with minimal modifications to the composition-product of two kernels. -/ noncomputable theory open_locale topology ennreal measure_theory probability_theory open set function real ennreal measure_theory filter probability_theory probability_theory.kernel variables {α β γ E : Type*} {mα : measurable_space α} {mβ : measurable_space β} {mγ : measurable_space γ} [normed_add_comm_group E] {κ : kernel α β} [is_s_finite_kernel κ] {η : kernel (α × β) γ} [is_s_finite_kernel η] {a : α} namespace probability_theory lemma has_finite_integral_prod_mk_left (a : α) {s : set (β × γ)} (h2s : (κ ⊗ₖ η) a s ≠ ∞) : has_finite_integral (λ b, (η (a, b) (prod.mk b ⁻¹' s)).to_real) (κ a) := begin let t := to_measurable ((κ ⊗ₖ η) a) s, simp_rw [has_finite_integral, ennnorm_eq_of_real to_real_nonneg], calc ∫⁻ b, ennreal.of_real (η (a, b) (prod.mk b ⁻¹' s)).to_real ∂(κ a) ≤ ∫⁻ b, η (a, b) (prod.mk b ⁻¹' t) ∂(κ a) : begin refine lintegral_mono_ae _, filter_upwards [ae_kernel_lt_top a h2s] with b hb, rw of_real_to_real hb.ne, exact measure_mono (preimage_mono (subset_to_measurable _ _)), end ... ≤ (κ ⊗ₖ η) a t : le_comp_prod_apply _ _ _ _ ... = (κ ⊗ₖ η) a s : measure_to_measurable s ... < ⊤ : h2s.lt_top, end lemma integrable_kernel_prod_mk_left (a : α) {s : set (β × γ)} (hs : measurable_set s) (h2s : (κ ⊗ₖ η) a s ≠ ∞) : integrable (λ b, (η (a, b) (prod.mk b ⁻¹' s)).to_real) (κ a) := begin split, { exact (measurable_kernel_prod_mk_left' hs a).ennreal_to_real.ae_strongly_measurable }, { exact has_finite_integral_prod_mk_left a h2s, }, end lemma _root_.measure_theory.ae_strongly_measurable.integral_kernel_comp_prod [normed_space ℝ E] [complete_space E] ⦃f : β × γ → E⦄ (hf : ae_strongly_measurable f ((κ ⊗ₖ η) a)) : ae_strongly_measurable (λ x, ∫ y, f (x, y) ∂(η (a, x))) (κ a) := ⟨λ x, ∫ y, hf.mk f (x, y) ∂(η (a, x)), hf.strongly_measurable_mk.integral_kernel_prod_right'', by { filter_upwards [ae_ae_of_ae_comp_prod hf.ae_eq_mk] with _ hx using integral_congr_ae hx }⟩ lemma _root_.measure_theory.ae_strongly_measurable.comp_prod_mk_left {δ : Type*} [topological_space δ] {f : β × γ → δ} (hf : ae_strongly_measurable f ((κ ⊗ₖ η) a)) : ∀ᵐ x ∂(κ a), ae_strongly_measurable (λ y, f (x, y)) (η (a, x)) := by filter_upwards [ae_ae_of_ae_comp_prod hf.ae_eq_mk] with x hx using ⟨λ y, hf.mk f (x, y), hf.strongly_measurable_mk.comp_measurable measurable_prod_mk_left, hx⟩ /-! ### Integrability -/ lemma has_finite_integral_comp_prod_iff ⦃f : β × γ → E⦄ (h1f : strongly_measurable f) : has_finite_integral f ((κ ⊗ₖ η) a) ↔ (∀ᵐ x ∂(κ a), has_finite_integral (λ y, f (x, y)) (η (a, x))) ∧ has_finite_integral (λ x, ∫ y, ‖f (x, y)‖ ∂(η (a, x))) (κ a) := begin simp only [has_finite_integral], rw kernel.lintegral_comp_prod _ _ _ h1f.ennnorm, have : ∀ x, ∀ᵐ y ∂(η (a, x)), 0 ≤ ‖f (x, y)‖ := λ x, eventually_of_forall (λ y, norm_nonneg _), simp_rw [integral_eq_lintegral_of_nonneg_ae (this _) (h1f.norm.comp_measurable measurable_prod_mk_left).ae_strongly_measurable, ennnorm_eq_of_real to_real_nonneg, of_real_norm_eq_coe_nnnorm], have : ∀ {p q r : Prop} (h1 : r → p), (r ↔ p ∧ q) ↔ (p → (r ↔ q)) := λ p q r h1, by rw [← and.congr_right_iff, and_iff_right_of_imp h1], rw [this], { intro h2f, rw lintegral_congr_ae, refine h2f.mp _, apply eventually_of_forall, intros x hx, dsimp only, rw [of_real_to_real], rw [← lt_top_iff_ne_top], exact hx }, { intro h2f, refine ae_lt_top _ h2f.ne, exact h1f.ennnorm.lintegral_kernel_prod_right'' }, end lemma has_finite_integral_comp_prod_iff' ⦃f : β × γ → E⦄ (h1f : ae_strongly_measurable f ((κ ⊗ₖ η) a)) : has_finite_integral f ((κ ⊗ₖ η) a) ↔ (∀ᵐ x ∂(κ a), has_finite_integral (λ y, f (x, y)) (η (a, x))) ∧ has_finite_integral (λ x, ∫ y, ‖f (x, y)‖ ∂(η (a, x))) (κ a) := begin rw [has_finite_integral_congr h1f.ae_eq_mk, has_finite_integral_comp_prod_iff h1f.strongly_measurable_mk], apply and_congr, { apply eventually_congr, filter_upwards [ae_ae_of_ae_comp_prod h1f.ae_eq_mk.symm], assume x hx, exact has_finite_integral_congr hx }, { apply has_finite_integral_congr, filter_upwards [ae_ae_of_ae_comp_prod h1f.ae_eq_mk.symm] with _ hx using integral_congr_ae (eventually_eq.fun_comp hx _), }, end lemma integrable_comp_prod_iff ⦃f : β × γ → E⦄ (hf : ae_strongly_measurable f ((κ ⊗ₖ η) a)) : integrable f ((κ ⊗ₖ η) a) ↔ (∀ᵐ x ∂(κ a), integrable (λ y, f (x, y)) (η (a, x))) ∧ integrable (λ x, ∫ y, ‖f (x, y)‖ ∂(η (a, x))) (κ a) := by simp only [integrable, has_finite_integral_comp_prod_iff' hf, hf.norm.integral_kernel_comp_prod, hf, hf.comp_prod_mk_left, eventually_and, true_and] lemma _root_.measure_theory.integrable.comp_prod_mk_left_ae ⦃f : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) : ∀ᵐ x ∂(κ a), integrable (λ y, f (x, y)) (η (a, x)) := ((integrable_comp_prod_iff hf.ae_strongly_measurable).mp hf).1 lemma _root_.measure_theory.integrable.integral_norm_comp_prod ⦃f : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) : integrable (λ x, ∫ y, ‖f (x, y)‖ ∂(η (a, x))) (κ a) := ((integrable_comp_prod_iff hf.ae_strongly_measurable).mp hf).2 lemma _root_.measure_theory.integrable.integral_comp_prod [normed_space ℝ E] [complete_space E] ⦃f : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) : integrable (λ x, ∫ y, f (x, y) ∂(η (a, x))) (κ a) := integrable.mono hf.integral_norm_comp_prod hf.ae_strongly_measurable.integral_kernel_comp_prod $ eventually_of_forall $ λ x, (norm_integral_le_integral_norm _).trans_eq $ (norm_of_nonneg $ integral_nonneg_of_ae $ eventually_of_forall $ λ y, (norm_nonneg (f (x, y)) : _)).symm /-! ### Bochner integral -/ variables [normed_space ℝ E] [complete_space E] {E' : Type*} [normed_add_comm_group E'] [complete_space E'] [normed_space ℝ E'] lemma kernel.integral_fn_integral_add ⦃f g : β × γ → E⦄ (F : E → E') (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫ x, F (∫ y, f (x, y) + g (x, y) ∂(η (a, x))) ∂(κ a) = ∫ x, F (∫ y, f (x, y) ∂(η (a, x)) + ∫ y, g (x, y) ∂(η (a, x))) ∂(κ a) := begin refine integral_congr_ae _, filter_upwards [hf.comp_prod_mk_left_ae, hg.comp_prod_mk_left_ae] with _ h2f h2g, simp [integral_add h2f h2g], end lemma kernel.integral_fn_integral_sub ⦃f g : β × γ → E⦄ (F : E → E') (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫ x, F (∫ y, f (x, y) - g (x, y) ∂(η (a, x))) ∂(κ a) = ∫ x, F (∫ y, f (x, y) ∂(η (a, x)) - ∫ y, g (x, y) ∂(η (a, x))) ∂(κ a) := begin refine integral_congr_ae _, filter_upwards [hf.comp_prod_mk_left_ae, hg.comp_prod_mk_left_ae] with _ h2f h2g, simp [integral_sub h2f h2g], end lemma kernel.lintegral_fn_integral_sub ⦃f g : β × γ → E⦄ (F : E → ℝ≥0∞) (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫⁻ x, F (∫ y, f (x, y) - g (x, y) ∂(η (a, x))) ∂(κ a) = ∫⁻ x, F (∫ y, f (x, y) ∂(η (a, x)) - ∫ y, g (x, y) ∂(η (a, x))) ∂(κ a) := begin refine lintegral_congr_ae _, filter_upwards [hf.comp_prod_mk_left_ae, hg.comp_prod_mk_left_ae] with _ h2f h2g, simp [integral_sub h2f h2g], end lemma kernel.integral_integral_add ⦃f g : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫ x, ∫ y, f (x, y) + g (x, y) ∂(η (a, x)) ∂(κ a) = ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a) + ∫ x, ∫ y, g (x, y) ∂(η (a, x)) ∂(κ a) := (kernel.integral_fn_integral_add id hf hg).trans $ integral_add hf.integral_comp_prod hg.integral_comp_prod lemma kernel.integral_integral_add' ⦃f g : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫ x, ∫ y, (f + g) (x, y) ∂(η (a, x)) ∂(κ a) = ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a) + ∫ x, ∫ y, g (x, y) ∂(η (a, x)) ∂(κ a) := kernel.integral_integral_add hf hg lemma kernel.integral_integral_sub ⦃f g : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫ x, ∫ y, f (x, y) - g (x, y) ∂(η (a, x)) ∂(κ a) = ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a) - ∫ x, ∫ y, g (x, y) ∂(η (a, x)) ∂(κ a) := (kernel.integral_fn_integral_sub id hf hg).trans $ integral_sub hf.integral_comp_prod hg.integral_comp_prod lemma kernel.integral_integral_sub' ⦃f g : β × γ → E⦄ (hf : integrable f ((κ ⊗ₖ η) a)) (hg : integrable g ((κ ⊗ₖ η) a)) : ∫ x, ∫ y, (f - g) (x, y) ∂(η (a, x)) ∂(κ a) = ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a) - ∫ x, ∫ y, g (x, y) ∂(η (a, x)) ∂(κ a) := kernel.integral_integral_sub hf hg lemma kernel.continuous_integral_integral : continuous (λ (f : α × β →₁[(κ ⊗ₖ η) a] E), ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a)) := begin rw [continuous_iff_continuous_at], intro g, refine tendsto_integral_of_L1 _ (L1.integrable_coe_fn g).integral_comp_prod (eventually_of_forall $ λ h, (L1.integrable_coe_fn h).integral_comp_prod) _, simp_rw [← kernel.lintegral_fn_integral_sub (λ x, (‖x‖₊ : ℝ≥0∞)) (L1.integrable_coe_fn _) (L1.integrable_coe_fn g)], refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds _ (λ i, zero_le _) _, { exact λ i, ∫⁻ x, ∫⁻ y, ‖i (x, y) - g (x, y)‖₊ ∂(η (a, x)) ∂(κ a) }, swap, { exact λ i, lintegral_mono (λ x, ennnorm_integral_le_lintegral_ennnorm _) }, show tendsto (λ (i : β × γ →₁[(κ ⊗ₖ η) a] E), ∫⁻ x, ∫⁻ (y : γ), ‖i (x, y) - g (x, y)‖₊ ∂(η (a, x)) ∂(κ a)) (𝓝 g) (𝓝 0), have : ∀ (i : α × β →₁[(κ ⊗ₖ η) a] E), measurable (λ z, (‖i z - g z‖₊ : ℝ≥0∞)) := λ i, ((Lp.strongly_measurable i).sub (Lp.strongly_measurable g)).ennnorm, simp_rw [← kernel.lintegral_comp_prod _ _ _ (this _), ← L1.of_real_norm_sub_eq_lintegral, ← of_real_zero], refine (continuous_of_real.tendsto 0).comp _, rw [← tendsto_iff_norm_tendsto_zero], exact tendsto_id end lemma integral_comp_prod : ∀ {f : β × γ → E} (hf : integrable f ((κ ⊗ₖ η) a)), ∫ z, f z ∂((κ ⊗ₖ η) a) = ∫ x, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a) := begin apply integrable.induction, { intros c s hs h2s, simp_rw [integral_indicator hs, ← indicator_comp_right, function.comp, integral_indicator (measurable_prod_mk_left hs), measure_theory.set_integral_const, integral_smul_const], congr' 1, rw integral_to_real, rotate, { exact (kernel.measurable_kernel_prod_mk_left' hs _).ae_measurable, }, { exact (ae_kernel_lt_top a h2s.ne), }, rw kernel.comp_prod_apply _ _ _ hs, refl, }, { intros f g hfg i_f i_g hf hg, simp_rw [integral_add' i_f i_g, kernel.integral_integral_add' i_f i_g, hf, hg] }, { exact is_closed_eq continuous_integral kernel.continuous_integral_integral }, { intros f g hfg i_f hf, convert hf using 1, { exact integral_congr_ae hfg.symm }, { refine integral_congr_ae _, refine (ae_ae_of_ae_comp_prod hfg).mp (eventually_of_forall _), exact λ x hfgx, integral_congr_ae (ae_eq_symm hfgx) } } end lemma set_integral_comp_prod {f : β × γ → E} {s : set β} {t : set γ} (hs : measurable_set s) (ht : measurable_set t) (hf : integrable_on f (s ×ˢ t) ((κ ⊗ₖ η) a)) : ∫ z in s ×ˢ t, f z ∂((κ ⊗ₖ η) a) = ∫ x in s, ∫ y in t, f (x, y) ∂(η (a, x)) ∂(κ a) := begin rw [← kernel.restrict_apply (κ ⊗ₖ η) (hs.prod ht), ← comp_prod_restrict, integral_comp_prod], { simp_rw kernel.restrict_apply, }, { rw [comp_prod_restrict, kernel.restrict_apply], exact hf, }, end lemma set_integral_comp_prod_univ_right (f : β × γ → E) {s : set β} (hs : measurable_set s) (hf : integrable_on f (s ×ˢ univ) ((κ ⊗ₖ η) a)) : ∫ z in s ×ˢ univ, f z ∂((κ ⊗ₖ η) a) = ∫ x in s, ∫ y, f (x, y) ∂(η (a, x)) ∂(κ a) := by simp_rw [set_integral_comp_prod hs measurable_set.univ hf, measure.restrict_univ] lemma set_integral_comp_prod_univ_left (f : β × γ → E) {t : set γ} (ht : measurable_set t) (hf : integrable_on f (univ ×ˢ t) ((κ ⊗ₖ η) a)) : ∫ z in univ ×ˢ t, f z ∂((κ ⊗ₖ η) a) = ∫ x, ∫ y in t, f (x, y) ∂(η (a, x)) ∂(κ a) := by simp_rw [set_integral_comp_prod measurable_set.univ ht hf, measure.restrict_univ] end probability_theory
9d99d9433c7f968f81c2d5053f5261feb32cda1c
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/nat/bitwise.lean
2299e4ee94d4ba335e1f2c912b47d0136c58bca1
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
7,864
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import tactic.linarith /-! # Bitwise operations on natural numbers In the first half of this file, we provide theorems for reasoning about natural numbers from their bitwise properties. In the second half of this file, we show properties of the bitwise operations `lor`, `land` and `lxor`, which are defined in core. ## Main results * `eq_of_test_bit_eq`: two natural numbers are equal if they have equal bits at every position. * `exists_most_significant_bit`: if `n ≠ 0`, then there is some position `i` that contains the most significant `1`-bit of `n`. * `lt_of_test_bit`: if `n` and `m` are numbers and `i` is a position such that the `i`-th bit of of `n` is zero, the `i`-th bit of `m` is one, and all more significant bits are equal, then `n < m`. ## Future work There is another way to express bitwise properties of natural number: `digits 2`. The two ways should be connected. ## Keywords bitwise, and, or, xor -/ namespace nat @[simp] lemma bit_ff : bit ff = bit0 := rfl @[simp] lemma bit_tt : bit tt = bit1 := rfl @[simp] lemma bit_eq_zero {n : ℕ} {b : bool} : n.bit b = 0 ↔ n = 0 ∧ b = ff := by { cases b; norm_num [bit0_eq_zero, nat.bit1_ne_zero] } lemma zero_of_test_bit_eq_ff {n : ℕ} (h : ∀ i, test_bit n i = ff) : n = 0 := begin induction n using nat.binary_rec with b n hn, { refl }, { have : b = ff := by simpa using h 0, rw [this, bit_ff, bit0_val, hn (λ i, by rw [←h (i + 1), test_bit_succ]), mul_zero] } end @[simp] lemma zero_test_bit (i : ℕ) : test_bit 0 i = ff := by simp [test_bit] /-- Bitwise extensionality: Two numbers agree if they agree at every bit position. -/ lemma eq_of_test_bit_eq {n m : ℕ} (h : ∀ i, test_bit n i = test_bit m i) : n = m := begin induction n using nat.binary_rec with b n hn generalizing m, { simp only [zero_test_bit] at h, exact (zero_of_test_bit_eq_ff (λ i, (h i).symm)).symm }, induction m using nat.binary_rec with b' m hm, { simp only [zero_test_bit] at h, exact zero_of_test_bit_eq_ff h }, suffices h' : n = m, { rw [h', show b = b', by simpa using h 0] }, exact hn (λ i, by convert h (i + 1) using 1; rw test_bit_succ) end lemma exists_most_significant_bit {n : ℕ} (h : n ≠ 0) : ∃ i, test_bit n i = tt ∧ ∀ j, i < j → test_bit n j = ff := begin induction n using nat.binary_rec with b n hn, { exact false.elim (h rfl) }, by_cases h' : n = 0, { subst h', rw (show b = tt, by { revert h, cases b; simp }), refine ⟨0, ⟨by rw [test_bit_zero], λ j hj, _⟩⟩, obtain ⟨j', rfl⟩ := exists_eq_succ_of_ne_zero (ne_of_gt hj), rw [test_bit_succ, zero_test_bit] }, { obtain ⟨k, ⟨hk, hk'⟩⟩ := hn h', refine ⟨k + 1, ⟨by rw [test_bit_succ, hk], λ j hj, _⟩⟩, obtain ⟨j', rfl⟩ := exists_eq_succ_of_ne_zero (show j ≠ 0, by linarith), exact (test_bit_succ _ _ _).trans (hk' _ (lt_of_succ_lt_succ hj)) } end lemma lt_of_test_bit {n m : ℕ} (i : ℕ) (hn : test_bit n i = ff) (hm : test_bit m i = tt) (hnm : ∀ j, i < j → test_bit n j = test_bit m j) : n < m := begin induction n using nat.binary_rec with b n hn' generalizing i m, { contrapose! hm, rw le_zero_iff at hm, simp [hm] }, induction m using nat.binary_rec with b' m hm' generalizing i, { exact false.elim (bool.ff_ne_tt ((zero_test_bit i).symm.trans hm)) }, by_cases hi : i = 0, { subst hi, simp only [test_bit_zero] at hn hm, have : n = m, { exact eq_of_test_bit_eq (λ i, by convert hnm (i + 1) dec_trivial using 1; rw test_bit_succ) }, rw [hn, hm, this, bit_ff, bit_tt, bit0_val, bit1_val], exact lt_add_one _ }, { obtain ⟨i', rfl⟩ := exists_eq_succ_of_ne_zero hi, simp only [test_bit_succ] at hn hm, have := hn' _ hn hm (λ j hj, by convert hnm j.succ (succ_lt_succ hj) using 1; rw test_bit_succ), cases b; cases b'; simp only [bit_ff, bit_tt, bit0_val n, bit1_val n, bit0_val m, bit1_val m]; linarith } end /-- If `f` is a commutative operation on bools such that `f ff ff = ff`, then `bitwise f` is also commutative. -/ lemma bitwise_comm {f : bool → bool → bool} (hf : ∀ b b', f b b' = f b' b) (hf' : f ff ff = ff) (n m : ℕ) : bitwise f n m = bitwise f m n := suffices bitwise f = function.swap (bitwise f), by conv_lhs { rw this }, calc bitwise f = bitwise (function.swap f) : congr_arg _ $ funext $ λ _, funext $ hf _ ... = function.swap (bitwise f) : bitwise_swap hf' lemma lor_comm (n m : ℕ) : lor n m = lor m n := bitwise_comm bool.bor_comm rfl n m lemma land_comm (n m : ℕ) : land n m = land m n := bitwise_comm bool.band_comm rfl n m lemma lxor_comm (n m : ℕ) : lxor n m = lxor m n := bitwise_comm bool.bxor_comm rfl n m @[simp] lemma zero_lxor (n : ℕ) : lxor 0 n = n := rfl @[simp] lemma lxor_zero (n : ℕ) : lxor n 0 = n := lxor_comm 0 n ▸ rfl @[simp] lemma zero_land (n : ℕ) : land 0 n = 0 := rfl @[simp] lemma land_zero (n : ℕ) : land n 0 = 0 := land_comm 0 n ▸ rfl @[simp] lemma zero_lor (n : ℕ) : lor 0 n = n := rfl @[simp] lemma lor_zero (n : ℕ) : lor n 0 = n := lor_comm 0 n ▸ rfl /-- Proving associativity of bitwise operations in general essentially boils down to a huge case distinction, so it is shorter to use this tactic instead of proving it in the general case. -/ meta def bitwise_assoc_tac : tactic unit := `[induction n using nat.binary_rec with b n hn generalizing m k, { simp }, induction m using nat.binary_rec with b' m hm, { simp }, induction k using nat.binary_rec with b'' k hk; simp [hn]] lemma lxor_assoc (n m k : ℕ) : lxor (lxor n m) k = lxor n (lxor m k) := by bitwise_assoc_tac lemma land_assoc (n m k : ℕ) : land (land n m) k = land n (land m k) := by bitwise_assoc_tac lemma lor_assoc (n m k : ℕ) : lor (lor n m) k = lor n (lor m k) := by bitwise_assoc_tac @[simp] lemma lxor_self (n : ℕ) : lxor n n = 0 := zero_of_test_bit_eq_ff $ λ i, by simp lemma lxor_right_inj {n m m' : ℕ} (h : lxor n m = lxor n m') : m = m' := calc m = lxor n (lxor n m') : by simp [←lxor_assoc, ←h] ... = m' : by simp [←lxor_assoc] lemma lxor_left_inj {n n' m : ℕ} (h : lxor n m = lxor n' m) : n = n' := by { rw [lxor_comm n m, lxor_comm n' m] at h, exact lxor_right_inj h } lemma lxor_eq_zero {n m : ℕ} : lxor n m = 0 ↔ n = m := ⟨by { rw ←lxor_self m, exact lxor_left_inj }, by { rintro rfl, exact lxor_self _ }⟩ lemma lxor_trichotomy {a b c : ℕ} (h : lxor a (lxor b c) ≠ 0) : lxor b c < a ∨ lxor a c < b ∨ lxor a b < c := begin set v := lxor a (lxor b c) with hv, -- The xor of any two of `a`, `b`, `c` is the xor of `v` and the third. have hab : lxor a b = lxor c v, { rw hv, conv_rhs { rw lxor_comm, simp [lxor_assoc] } }, have hac : lxor a c = lxor b v, { rw hv, conv_rhs { congr, skip, rw lxor_comm }, rw [←lxor_assoc, ←lxor_assoc, lxor_self, zero_lxor, lxor_comm] }, have hbc : lxor b c = lxor a v, { simp [hv, ←lxor_assoc] }, -- If `i` is the position of the most significant bit of `v`, then at least one of `a`, `b`, `c` -- has a one bit at position `i`. obtain ⟨i, ⟨hi, hi'⟩⟩ := exists_most_significant_bit h, have : test_bit a i = tt ∨ test_bit b i = tt ∨ test_bit c i = tt, { contrapose! hi, simp only [eq_ff_eq_not_eq_tt, ne, test_bit_lxor] at ⊢ hi, rw [hi.1, hi.2.1, hi.2.2, bxor_ff, bxor_ff] }, -- If, say, `a` has a one bit at position `i`, then `a xor v` has a zero bit at position `i`, but -- the same bits as `a` in positions greater than `j`, so `a xor v < a`. rcases this with h|h|h; [{ left, rw hbc }, { right, left, rw hac }, { right, right, rw hab }]; exact lt_of_test_bit i (by simp [h, hi]) h (λ j hj, by simp [hi' _ hj]) end end nat
8c0927623b7fe0487ea9706428a95e95b0ed4ded
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/group_theory/perm/cycle_type.lean
47f1932f8ec3911706695e1006138432e711fb4a
[ "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
26,291
lean
/- Copyright (c) 2020 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import algebra.gcd_monoid.multiset import combinatorics.partition import group_theory.perm.cycles import ring_theory.int.basic import tactic.linarith /-! # Cycle Types In this file we define the cycle type of a partition. ## Main definitions - `σ.cycle_type` where `σ` is a permutation of a `fintype` - `σ.partition` where `σ` is a permutation of a `fintype` ## Main results - `sum_cycle_type` : The sum of `σ.cycle_type` equals `σ.support.card` - `lcm_cycle_type` : The lcm of `σ.cycle_type` equals `order_of σ` - `is_conj_iff_cycle_type_eq` : Two permutations are conjugate if and only if they have the same cycle type. * `exists_prime_order_of_dvd_card`: For every prime `p` dividing the order of a finite group `G` there exists an element of order `p` in `G`. This is known as Cauchy`s theorem. -/ namespace equiv.perm open equiv list multiset variables {α : Type*} [fintype α] section cycle_type variables [decidable_eq α] /-- The cycle type of a permutation -/ def cycle_type (σ : perm α) : multiset ℕ := σ.cycle_factors_finset.1.map (finset.card ∘ support) lemma cycle_type_def (σ : perm α) : σ.cycle_type = σ.cycle_factors_finset.1.map (finset.card ∘ support) := rfl lemma cycle_type_eq' {σ : perm α} (s : finset (perm α)) (h1 : ∀ f : perm α, f ∈ s → f.is_cycle) (h2 : ∀ (a ∈ s) (b ∈ s), a ≠ b → disjoint a b) (h0 : s.noncomm_prod id (λ a ha b hb, (em (a = b)).by_cases (λ h, h ▸ commute.refl a) (set.pairwise_on.mono' (λ _ _, disjoint.commute) h2 a ha b hb)) = σ) : σ.cycle_type = s.1.map (finset.card ∘ support) := begin rw cycle_type_def, congr, rw cycle_factors_finset_eq_finset, exact ⟨h1, h2, h0⟩ end lemma cycle_type_eq {σ : perm α} (l : list (perm α)) (h0 : l.prod = σ) (h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle) (h2 : l.pairwise disjoint) : σ.cycle_type = l.map (finset.card ∘ support) := begin have hl : l.nodup := nodup_of_pairwise_disjoint_cycles h1 h2, rw cycle_type_eq' l.to_finset, { simp [list.erase_dup_eq_self.mpr hl] }, { simpa using h1 }, { simpa [hl] using h0 }, { simpa [list.erase_dup_eq_self.mpr hl] using list.forall_of_pairwise disjoint.symmetric h2 } end lemma cycle_type_one : (1 : perm α).cycle_type = 0 := cycle_type_eq [] rfl (λ _, false.elim) pairwise.nil lemma cycle_type_eq_zero {σ : perm α} : σ.cycle_type = 0 ↔ σ = 1 := by simp [cycle_type_def, cycle_factors_finset_eq_empty_iff] lemma card_cycle_type_eq_zero {σ : perm α} : σ.cycle_type.card = 0 ↔ σ = 1 := by rw [card_eq_zero, cycle_type_eq_zero] lemma two_le_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : 2 ≤ n := begin simp only [cycle_type_def, ←finset.mem_def, function.comp_app, multiset.mem_map, mem_cycle_factors_finset_iff] at h, obtain ⟨_, ⟨hc, -⟩, rfl⟩ := h, exact hc.two_le_card_support end lemma one_lt_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : 1 < n := two_le_of_mem_cycle_type h lemma is_cycle.cycle_type {σ : perm α} (hσ : is_cycle σ) : σ.cycle_type = [σ.support.card] := cycle_type_eq [σ] (mul_one σ) (λ τ hτ, (congr_arg is_cycle (list.mem_singleton.mp hτ)).mpr hσ) (pairwise_singleton disjoint σ) lemma card_cycle_type_eq_one {σ : perm α} : σ.cycle_type.card = 1 ↔ σ.is_cycle := begin rw card_eq_one, simp_rw [cycle_type_def, multiset.map_eq_singleton, ←finset.singleton_val, finset.val_inj, cycle_factors_finset_eq_singleton_iff], split, { rintro ⟨_, _, ⟨h, -⟩, -⟩, exact h }, { intro h, use [σ.support.card, σ], simp [h] } end lemma disjoint.cycle_type {σ τ : perm α} (h : disjoint σ τ) : (σ * τ).cycle_type = σ.cycle_type + τ.cycle_type := begin rw [cycle_type_def, cycle_type_def, cycle_type_def, h.cycle_factors_finset_mul_eq_union, ←map_add, finset.union_val, multiset.add_eq_union_iff_disjoint.mpr _], rw [←finset.disjoint_val], exact h.disjoint_cycle_factors_finset end lemma cycle_type_inv (σ : perm α) : σ⁻¹.cycle_type = σ.cycle_type := cycle_induction_on (λ τ : perm α, τ⁻¹.cycle_type = τ.cycle_type) σ rfl (λ σ hσ, by rw [hσ.cycle_type, hσ.inv.cycle_type, support_inv]) (λ σ τ hστ hc hσ hτ, by rw [mul_inv_rev, hστ.cycle_type, ←hσ, ←hτ, add_comm, disjoint.cycle_type (λ x, or.imp (λ h : τ x = x, inv_eq_iff_eq.mpr h.symm) (λ h : σ x = x, inv_eq_iff_eq.mpr h.symm) (hστ x).symm)]) lemma cycle_type_conj {σ τ : perm α} : (τ * σ * τ⁻¹).cycle_type = σ.cycle_type := begin revert τ, apply cycle_induction_on _ σ, { intro, simp }, { intros σ hσ τ, rw [hσ.cycle_type, hσ.is_cycle_conj.cycle_type, card_support_conj] }, { intros σ τ hd hc hσ hτ π, rw [← conj_mul, hd.cycle_type, disjoint.cycle_type, hσ, hτ], intro a, apply (hd (π⁻¹ a)).imp _ _; { intro h, rw [perm.mul_apply, perm.mul_apply, h, apply_inv_self] } } end lemma sum_cycle_type (σ : perm α) : σ.cycle_type.sum = σ.support.card := cycle_induction_on (λ τ : perm α, τ.cycle_type.sum = τ.support.card) σ (by rw [cycle_type_one, sum_zero, support_one, finset.card_empty]) (λ σ hσ, by rw [hσ.cycle_type, coe_sum, list.sum_singleton]) (λ σ τ hστ hc hσ hτ, by rw [hστ.cycle_type, sum_add, hσ, hτ, hστ.card_support_mul]) lemma sign_of_cycle_type (σ : perm α) : sign σ = (σ.cycle_type.map (λ n, -(-1 : units ℤ) ^ n)).prod := cycle_induction_on (λ τ : perm α, sign τ = (τ.cycle_type.map (λ n, -(-1 : units ℤ) ^ n)).prod) σ (by rw [sign_one, cycle_type_one, map_zero, prod_zero]) (λ σ hσ, by rw [hσ.sign, hσ.cycle_type, coe_map, coe_prod, list.map_singleton, list.prod_singleton]) (λ σ τ hστ hc hσ hτ, by rw [sign_mul, hσ, hτ, hστ.cycle_type, map_add, prod_add]) lemma lcm_cycle_type (σ : perm α) : σ.cycle_type.lcm = order_of σ := cycle_induction_on (λ τ : perm α, τ.cycle_type.lcm = order_of τ) σ (by rw [cycle_type_one, lcm_zero, order_of_one]) (λ σ hσ, by rw [hσ.cycle_type, ←singleton_coe, ←singleton_eq_cons, lcm_singleton, order_of_is_cycle hσ, normalize_eq]) (λ σ τ hστ hc hσ hτ, by rw [hστ.cycle_type, lcm_add, lcm_eq_nat_lcm, hστ.order_of, hσ, hτ]) lemma dvd_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : n ∣ order_of σ := begin rw ← lcm_cycle_type, exact dvd_lcm h, end lemma order_of_cycle_of_dvd_order_of (f : perm α) (x : α) : order_of (cycle_of f x) ∣ order_of f := begin by_cases hx : f x = x, { rw ←cycle_of_eq_one_iff at hx, simp [hx] }, { refine dvd_of_mem_cycle_type _, rw [cycle_type, multiset.mem_map], refine ⟨f.cycle_of x, _, _⟩, { rwa [←finset.mem_def, cycle_of_mem_cycle_factors_finset_iff, mem_support] }, { simp [order_of_is_cycle (is_cycle_cycle_of _ hx)] } } end lemma two_dvd_card_support {σ : perm α} (hσ : σ ^ 2 = 1) : 2 ∣ σ.support.card := (congr_arg (has_dvd.dvd 2) σ.sum_cycle_type).mp (multiset.dvd_sum (λ n hn, by rw le_antisymm (nat.le_of_dvd zero_lt_two $ (dvd_of_mem_cycle_type hn).trans $ order_of_dvd_of_pow_eq_one hσ) (two_le_of_mem_cycle_type hn))) lemma cycle_type_prime_order {σ : perm α} (hσ : (order_of σ).prime) : ∃ n : ℕ, σ.cycle_type = repeat (order_of σ) (n + 1) := begin rw eq_repeat_of_mem (λ n hn, or_iff_not_imp_left.mp (hσ.2 n (dvd_of_mem_cycle_type hn)) (ne_of_gt (one_lt_of_mem_cycle_type hn))), use σ.cycle_type.card - 1, rw nat.sub_add_cancel, rw [nat.succ_le_iff, pos_iff_ne_zero, ne, card_cycle_type_eq_zero], rintro rfl, rw order_of_one at hσ, exact hσ.ne_one rfl, end lemma is_cycle_of_prime_order {σ : perm α} (h1 : (order_of σ).prime) (h2 : σ.support.card < 2 * (order_of σ)) : σ.is_cycle := begin obtain ⟨n, hn⟩ := cycle_type_prime_order h1, rw [←σ.sum_cycle_type, hn, multiset.sum_repeat, nsmul_eq_mul, nat.cast_id, mul_lt_mul_right (order_of_pos σ), nat.succ_lt_succ_iff, nat.lt_succ_iff, nat.le_zero_iff] at h2, rw [←card_cycle_type_eq_one, hn, card_repeat, h2], end lemma cycle_type_le_of_mem_cycle_factors_finset {f g : perm α} (hf : f ∈ g.cycle_factors_finset) : f.cycle_type ≤ g.cycle_type := begin rw mem_cycle_factors_finset_iff at hf, rw [cycle_type_def, cycle_type_def, hf.left.cycle_factors_finset_eq_singleton], refine map_le_map _, simpa [←finset.mem_def, mem_cycle_factors_finset_iff] using hf end lemma cycle_type_mul_mem_cycle_factors_finset_eq_sub {f g : perm α} (hf : f ∈ g.cycle_factors_finset) : (g * f⁻¹).cycle_type = g.cycle_type - f.cycle_type := begin suffices : (g * f⁻¹).cycle_type + f.cycle_type = g.cycle_type - f.cycle_type + f.cycle_type, { rw multiset.sub_add_cancel (cycle_type_le_of_mem_cycle_factors_finset hf) at this, simp [←this] }, simp [←(disjoint_mul_inv_of_mem_cycle_factors_finset hf).cycle_type, multiset.sub_add_cancel (cycle_type_le_of_mem_cycle_factors_finset hf)] end theorem is_conj_of_cycle_type_eq {σ τ : perm α} (h : cycle_type σ = cycle_type τ) : is_conj σ τ := begin revert τ, apply cycle_induction_on _ σ, { intros τ h, rw [cycle_type_one, eq_comm, cycle_type_eq_zero] at h, rw h }, { intros σ hσ τ hστ, have hτ := card_cycle_type_eq_one.2 hσ, rw [hστ, card_cycle_type_eq_one] at hτ, apply hσ.is_conj hτ, rw [hσ.cycle_type, hτ.cycle_type, coe_eq_coe, singleton_perm] at hστ, simp only [and_true, eq_self_iff_true] at hστ, exact hστ }, { intros σ τ hστ hσ h1 h2 π hπ, rw [hστ.cycle_type] at hπ, { have h : σ.support.card ∈ map (finset.card ∘ perm.support) π.cycle_factors_finset.val, { simp [←cycle_type_def, ←hπ, hσ.cycle_type] }, obtain ⟨σ', hσ'l, hσ'⟩ := multiset.mem_map.mp h, have key : is_conj (σ' * (π * σ'⁻¹)) π, { rw is_conj_iff, use σ'⁻¹, simp [mul_assoc] }, refine is_conj_trans _ key, have hs : σ.cycle_type = σ'.cycle_type, { rw [←finset.mem_def, mem_cycle_factors_finset_iff] at hσ'l, rw [hσ.cycle_type, ←hσ', hσ'l.left.cycle_type] }, refine hστ.is_conj_mul (h1 hs) (h2 _) _, { rw [cycle_type_mul_mem_cycle_factors_finset_eq_sub, ←hπ, add_comm, hs, multiset.add_sub_cancel], rwa finset.mem_def }, { exact (disjoint_mul_inv_of_mem_cycle_factors_finset hσ'l).symm } } } end theorem is_conj_iff_cycle_type_eq {σ τ : perm α} : is_conj σ τ ↔ σ.cycle_type = τ.cycle_type := ⟨λ h, begin obtain ⟨π, rfl⟩ := is_conj_iff.1 h, rw cycle_type_conj, end, is_conj_of_cycle_type_eq⟩ @[simp] lemma cycle_type_extend_domain {β : Type*} [fintype β] [decidable_eq β] {p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) {g : perm α} : cycle_type (g.extend_domain f) = cycle_type g := begin apply cycle_induction_on _ g, { rw [extend_domain_one, cycle_type_one, cycle_type_one] }, { intros σ hσ, rw [(hσ.extend_domain f).cycle_type, hσ.cycle_type, card_support_extend_domain] }, { intros σ τ hd hc hσ hτ, rw [hd.cycle_type, ← extend_domain_mul, (hd.extend_domain f).cycle_type, hσ, hτ] } end lemma mem_cycle_type_iff {n : ℕ} {σ : perm α} : n ∈ cycle_type σ ↔ ∃ c τ : perm α, σ = c * τ ∧ disjoint c τ ∧ is_cycle c ∧ c.support.card = n := begin split, { intro h, obtain ⟨l, rfl, hlc, hld⟩ := trunc_cycle_factors σ, rw cycle_type_eq _ rfl hlc hld at h, obtain ⟨c, cl, rfl⟩ := list.exists_of_mem_map h, rw (list.perm_cons_erase cl).pairwise_iff (λ _ _ hd, _) at hld, swap, { exact hd.symm }, refine ⟨c, (l.erase c).prod, _, _, hlc _ cl, rfl⟩, { rw [← list.prod_cons, (list.perm_cons_erase cl).symm.prod_eq' (hld.imp (λ _ _, disjoint.commute))] }, { exact disjoint_prod_right _ (λ g, list.rel_of_pairwise_cons hld) } }, { rintros ⟨c, t, rfl, hd, hc, rfl⟩, simp [hd.cycle_type, hc.cycle_type] } end lemma le_card_support_of_mem_cycle_type {n : ℕ} {σ : perm α} (h : n ∈ cycle_type σ) : n ≤ σ.support.card := (le_sum_of_mem h).trans (le_of_eq σ.sum_cycle_type) lemma cycle_type_of_card_le_mem_cycle_type_add_two {n : ℕ} {g : perm α} (hn2 : fintype.card α < n + 2) (hng : n ∈ g.cycle_type) : g.cycle_type = {n} := begin obtain ⟨c, g', rfl, hd, hc, rfl⟩ := mem_cycle_type_iff.1 hng, by_cases g'1 : g' = 1, { rw [hd.cycle_type, hc.cycle_type, multiset.singleton_eq_cons, multiset.singleton_coe, g'1, cycle_type_one, add_zero] }, contrapose! hn2, apply le_trans _ (c * g').support.card_le_univ, rw [hd.card_support_mul], exact add_le_add_left (two_le_card_support_of_ne_one g'1) _, end end cycle_type lemma card_compl_support_modeq [decidable_eq α] {p n : ℕ} [hp : fact p.prime] {σ : perm α} (hσ : σ ^ p ^ n = 1) : σ.supportᶜ.card ≡ fintype.card α [MOD p] := begin rw [nat.modeq_iff_dvd' σ.supportᶜ.card_le_univ, ←finset.card_compl, compl_compl], refine (congr_arg _ σ.sum_cycle_type).mp (multiset.dvd_sum (λ k hk, _)), obtain ⟨m, -, hm⟩ := (nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hσ), obtain ⟨l, -, rfl⟩ := (nat.dvd_prime_pow hp.out).mp ((congr_arg _ hm).mp (dvd_of_mem_cycle_type hk)), exact dvd_pow_self _ (λ h, (one_lt_of_mem_cycle_type hk).ne $ by rw [h, pow_zero]), end lemma exists_fixed_point_of_prime {p n : ℕ} [hp : fact p.prime] (hα : ¬ p ∣ fintype.card α) {σ : perm α} (hσ : σ ^ p ^ n = 1) : ∃ a : α, σ a = a := begin classical, contrapose! hα, simp_rw ← mem_support at hα, exact nat.modeq_zero_iff_dvd.mp ((congr_arg _ (finset.card_eq_zero.mpr (compl_eq_bot.mpr (finset.eq_univ_iff_forall.mpr hα)))).mp (card_compl_support_modeq hσ).symm), end lemma exists_fixed_point_of_prime' {p n : ℕ} [hp : fact p.prime] (hα : p ∣ fintype.card α) {σ : perm α} (hσ : σ ^ p ^ n = 1) {a : α} (ha : σ a = a) : ∃ b : α, σ b = b ∧ b ≠ a := begin classical, have h : ∀ b : α, b ∈ σ.supportᶜ ↔ σ b = b := λ b, by rw [finset.mem_compl, mem_support, not_not], obtain ⟨b, hb1, hb2⟩ := finset.exists_ne_of_one_lt_card (lt_of_lt_of_le hp.out.one_lt (nat.le_of_dvd (finset.card_pos.mpr ⟨a, (h a).mpr ha⟩) (nat.modeq_zero_iff_dvd.mp ((card_compl_support_modeq hσ).trans (nat.modeq_zero_iff_dvd.mpr hα))))) a, exact ⟨b, (h b).mp hb1, hb2⟩, end lemma is_cycle_of_prime_order' {σ : perm α} (h1 : (order_of σ).prime) (h2 : fintype.card α < 2 * (order_of σ)) : σ.is_cycle := begin classical, exact is_cycle_of_prime_order h1 (lt_of_le_of_lt σ.support.card_le_univ h2), end lemma is_cycle_of_prime_order'' {σ : perm α} (h1 : (fintype.card α).prime) (h2 : order_of σ = fintype.card α) : σ.is_cycle := is_cycle_of_prime_order' ((congr_arg nat.prime h2).mpr h1) begin classical, rw [←one_mul (fintype.card α), ←h2, mul_lt_mul_right (order_of_pos σ)], exact one_lt_two, end section cauchy variables (G : Type*) [group G] (n : ℕ) /-- The type of vectors with terms from `G`, length `n`, and product equal to `1:G`. -/ def vectors_prod_eq_one : set (vector G n) := {v | v.to_list.prod = 1} namespace vectors_prod_eq_one lemma mem_iff {n : ℕ} (v : vector G n) : v ∈ vectors_prod_eq_one G n ↔ v.to_list.prod = 1 := iff.rfl lemma zero_eq : vectors_prod_eq_one G 0 = {vector.nil} := set.eq_singleton_iff_unique_mem.mpr ⟨eq.refl (1 : G), λ v hv, v.eq_nil⟩ lemma one_eq : vectors_prod_eq_one G 1 = {vector.nil.cons 1} := begin simp_rw [set.eq_singleton_iff_unique_mem, mem_iff, vector.to_list_singleton, list.prod_singleton, vector.head_cons], exact ⟨rfl, λ v hv, v.cons_head_tail.symm.trans (congr_arg2 vector.cons hv v.tail.eq_nil)⟩, end instance zero_unique : unique (vectors_prod_eq_one G 0) := by { rw zero_eq, exact set.unique_singleton vector.nil } instance one_unique : unique (vectors_prod_eq_one G 1) := by { rw one_eq, exact set.unique_singleton (vector.nil.cons 1) } /-- Given a vector `v` of length `n`, make a vector of length `n + 1` whose product is `1`, by appending the inverse of the product of `v`. -/ @[simps] def vector_equiv : vector G n ≃ vectors_prod_eq_one G (n + 1) := { to_fun := λ v, ⟨v.to_list.prod⁻¹ ::ᵥ v, by rw [mem_iff, vector.to_list_cons, list.prod_cons, inv_mul_self]⟩, inv_fun := λ v, v.1.tail, left_inv := λ v, v.tail_cons v.to_list.prod⁻¹, right_inv := λ v, subtype.ext ((congr_arg2 vector.cons (eq_inv_of_mul_eq_one (by { rw [←list.prod_cons, ←vector.to_list_cons, v.1.cons_head_tail], exact v.2 })).symm rfl).trans v.1.cons_head_tail) } /-- Given a vector `v` of length `n` whose product is 1, make a vector of length `n - 1`, by deleting the last entry of `v`. -/ def equiv_vector : vectors_prod_eq_one G n ≃ vector G (n - 1) := ((vector_equiv G (n - 1)).trans (if hn : n = 0 then (show vectors_prod_eq_one G (n - 1 + 1) ≃ vectors_prod_eq_one G n, by { rw hn, exact equiv_of_unique_of_unique }) else by rw nat.sub_add_cancel (nat.pos_of_ne_zero hn))).symm instance [fintype G] : fintype (vectors_prod_eq_one G n) := fintype.of_equiv (vector G (n - 1)) (equiv_vector G n).symm lemma card [fintype G] : fintype.card (vectors_prod_eq_one G n) = fintype.card G ^ (n - 1) := (fintype.card_congr (equiv_vector G n)).trans (card_vector (n - 1)) variables {G n} {g : G} (v : vectors_prod_eq_one G n) (j k : ℕ) /-- Rotate a vector whose product is 1. -/ def rotate : vectors_prod_eq_one G n := ⟨⟨_, (v.1.1.length_rotate k).trans v.1.2⟩, list.prod_rotate_eq_one_of_prod_eq_one v.2 k⟩ lemma rotate_zero : rotate v 0 = v := subtype.ext (subtype.ext v.1.1.rotate_zero) lemma rotate_rotate : rotate (rotate v j) k = rotate v (j + k) := subtype.ext (subtype.ext (v.1.1.rotate_rotate j k)) lemma rotate_length : rotate v n = v := subtype.ext (subtype.ext ((congr_arg _ v.1.2.symm).trans v.1.1.rotate_length)) end vectors_prod_eq_one lemma exists_prime_order_of_dvd_card {G : Type*} [group G] [fintype G] (p : ℕ) [hp : fact p.prime] (hdvd : p ∣ fintype.card G) : ∃ x : G, order_of x = p := begin have hp' : p - 1 ≠ 0 := mt nat.sub_eq_zero_iff_le.mp (not_le_of_lt hp.out.one_lt), have Scard := calc p ∣ fintype.card G ^ (p - 1) : hdvd.trans (dvd_pow (dvd_refl _) hp') ... = fintype.card (vectors_prod_eq_one G p) : (vectors_prod_eq_one.card G p).symm, let f : ℕ → vectors_prod_eq_one G p → vectors_prod_eq_one G p := λ k v, vectors_prod_eq_one.rotate v k, have hf1 : ∀ v, f 0 v = v := vectors_prod_eq_one.rotate_zero, have hf2 : ∀ j k v, f k (f j v) = f (j + k) v := λ j k v, vectors_prod_eq_one.rotate_rotate v j k, have hf3 : ∀ v, f p v = v := vectors_prod_eq_one.rotate_length, let σ := equiv.mk (f 1) (f (p - 1)) (λ s, by rw [hf2, nat.add_sub_cancel' hp.out.pos, hf3]) (λ s, by rw [hf2, nat.sub_add_cancel hp.out.pos, hf3]), have hσ : ∀ k v, (σ ^ k) v = f k v := λ k v, nat.rec (hf1 v).symm (λ k hk, eq.trans (by exact congr_arg σ hk) (hf2 k 1 v)) k, replace hσ : σ ^ (p ^ 1) = 1 := perm.ext (λ v, by rw [pow_one, hσ, hf3, one_apply]), let v₀ : vectors_prod_eq_one G p := ⟨vector.repeat 1 p, (list.prod_repeat 1 p).trans (one_pow p)⟩, have hv₀ : σ v₀ = v₀ := subtype.ext (subtype.ext (list.rotate_repeat (1 : G) p 1)), obtain ⟨v, hv1, hv2⟩ := exists_fixed_point_of_prime' Scard hσ hv₀, refine exists_imp_exists (λ g hg, order_of_eq_prime _ (λ hg', hv2 _)) (list.rotate_one_eq_self_iff_eq_repeat.mp (subtype.ext_iff.mp (subtype.ext_iff.mp hv1))), { rw [←list.prod_repeat, ←v.1.2, ←hg, (show v.val.val.prod = 1, from v.2)] }, { rw [subtype.ext_iff_val, subtype.ext_iff_val, hg, hg', v.1.2], refl }, end end cauchy lemma subgroup_eq_top_of_swap_mem [decidable_eq α] {H : subgroup (perm α)} [d : decidable_pred (∈ H)] {τ : perm α} (h0 : (fintype.card α).prime) (h1 : fintype.card α ∣ fintype.card H) (h2 : τ ∈ H) (h3 : is_swap τ) : H = ⊤ := begin haveI : fact (fintype.card α).prime := ⟨h0⟩, obtain ⟨σ, hσ⟩ := exists_prime_order_of_dvd_card (fintype.card α) h1, have hσ1 : order_of (σ : perm α) = fintype.card α := (order_of_subgroup σ).trans hσ, have hσ2 : is_cycle ↑σ := is_cycle_of_prime_order'' h0 hσ1, have hσ3 : (σ : perm α).support = ⊤ := finset.eq_univ_of_card (σ : perm α).support ((order_of_is_cycle hσ2).symm.trans hσ1), have hσ4 : subgroup.closure {↑σ, τ} = ⊤ := closure_prime_cycle_swap h0 hσ2 hσ3 h3, rw [eq_top_iff, ←hσ4, subgroup.closure_le, set.insert_subset, set.singleton_subset_iff], exact ⟨subtype.mem σ, h2⟩, end section partition variables [decidable_eq α] /-- The partition corresponding to a permutation -/ def partition (σ : perm α) : partition (fintype.card α) := { parts := σ.cycle_type + repeat 1 (fintype.card α - σ.support.card), parts_pos := λ n hn, begin cases mem_add.mp hn with hn hn, { exact zero_lt_one.trans (one_lt_of_mem_cycle_type hn) }, { exact lt_of_lt_of_le zero_lt_one (ge_of_eq (multiset.eq_of_mem_repeat hn)) }, end, parts_sum := by rw [sum_add, sum_cycle_type, multiset.sum_repeat, nsmul_eq_mul, nat.cast_id, mul_one, nat.add_sub_cancel' σ.support.card_le_univ] } lemma parts_partition {σ : perm α} : σ.partition.parts = σ.cycle_type + repeat 1 (fintype.card α - σ.support.card) := rfl lemma filter_parts_partition_eq_cycle_type {σ : perm α} : (partition σ).parts.filter (λ n, 2 ≤ n) = σ.cycle_type := begin rw [parts_partition, filter_add, multiset.filter_eq_self.2 (λ _, two_le_of_mem_cycle_type), multiset.filter_eq_nil.2 (λ a h, _), add_zero], rw multiset.eq_of_mem_repeat h, dec_trivial end lemma partition_eq_of_is_conj {σ τ : perm α} : is_conj σ τ ↔ σ.partition = τ.partition := begin rw [is_conj_iff_cycle_type_eq], refine ⟨λ h, _, λ h, _⟩, { rw [partition.ext_iff, parts_partition, parts_partition, ← sum_cycle_type, ← sum_cycle_type, h] }, { rw [← filter_parts_partition_eq_cycle_type, ← filter_parts_partition_eq_cycle_type, h] } end end partition /-! ### 3-cycles -/ /-- A three-cycle is a cycle of length 3. -/ def is_three_cycle [decidable_eq α] (σ : perm α) : Prop := σ.cycle_type = {3} namespace is_three_cycle variables [decidable_eq α] {σ : perm α} lemma cycle_type (h : is_three_cycle σ) : σ.cycle_type = {3} := h lemma card_support (h : is_three_cycle σ) : σ.support.card = 3 := by rw [←sum_cycle_type, h.cycle_type, multiset.sum_singleton] lemma _root_.card_support_eq_three_iff : σ.support.card = 3 ↔ σ.is_three_cycle := begin refine ⟨λ h, _, is_three_cycle.card_support⟩, by_cases h0 : σ.cycle_type = 0, { rw [←sum_cycle_type, h0, sum_zero] at h, exact (ne_of_lt zero_lt_three h).elim }, obtain ⟨n, hn⟩ := exists_mem_of_ne_zero h0, by_cases h1 : σ.cycle_type.erase n = 0, { rw [←sum_cycle_type, ←cons_erase hn, h1, ←singleton_eq_cons, multiset.sum_singleton] at h, rw [is_three_cycle, ←cons_erase hn, h1, h, singleton_eq_cons] }, obtain ⟨m, hm⟩ := exists_mem_of_ne_zero h1, rw [←sum_cycle_type, ←cons_erase hn, ←cons_erase hm, multiset.sum_cons, multiset.sum_cons] at h, linarith [two_le_of_mem_cycle_type hn, two_le_of_mem_cycle_type (mem_of_mem_erase hm)], end lemma is_cycle (h : is_three_cycle σ) : is_cycle σ := by rw [←card_cycle_type_eq_one, h.cycle_type, card_singleton] lemma sign (h : is_three_cycle σ) : sign σ = 1 := begin rw [sign_of_cycle_type, h.cycle_type], refl, end lemma inv {f : perm α} (h : is_three_cycle f) : is_three_cycle (f⁻¹) := by rwa [is_three_cycle, cycle_type_inv] @[simp] lemma inv_iff {f : perm α} : is_three_cycle (f⁻¹) ↔ is_three_cycle f := ⟨by { rw ← inv_inv f, apply inv }, inv⟩ lemma order_of {g : perm α} (ht : is_three_cycle g) : order_of g = 3 := by rw [←lcm_cycle_type, ht.cycle_type, multiset.lcm_singleton, normalize_eq] lemma is_three_cycle_sq {g : perm α} (ht : is_three_cycle g) : is_three_cycle (g * g) := begin rw [←pow_two, ←card_support_eq_three_iff, support_pow_coprime, ht.card_support], rw [ht.order_of, nat.coprime_iff_gcd_eq_one], norm_num, end end is_three_cycle section variable [decidable_eq α] lemma is_three_cycle_swap_mul_swap_same {a b c : α} (ab : a ≠ b) (ac : a ≠ c) (bc : b ≠ c) : is_three_cycle (swap a b * swap a c) := begin suffices h : support (swap a b * swap a c) = {a, b, c}, { rw [←card_support_eq_three_iff, h], simp [ab, ac, bc] }, apply le_antisymm ((support_mul_le _ _).trans (λ x, _)) (λ x hx, _), { simp [ab, ac, bc] }, { simp only [finset.mem_insert, finset.mem_singleton] at hx, rw mem_support, simp only [perm.coe_mul, function.comp_app, ne.def], obtain rfl | rfl | rfl := hx, { rw [swap_apply_left, swap_apply_of_ne_of_ne ac.symm bc.symm], exact ac.symm }, { rw [swap_apply_of_ne_of_ne ab.symm bc, swap_apply_right], exact ab }, { rw [swap_apply_right, swap_apply_left], exact bc } } end open subgroup lemma swap_mul_swap_same_mem_closure_three_cycles {a b c : α} (ab : a ≠ b) (ac : a ≠ c) : (swap a b * swap a c) ∈ closure {σ : perm α | is_three_cycle σ } := begin by_cases bc : b = c, { subst bc, simp [one_mem] }, exact subset_closure (is_three_cycle_swap_mul_swap_same ab ac bc) end lemma is_swap.mul_mem_closure_three_cycles {σ τ : perm α} (hσ : is_swap σ) (hτ : is_swap τ) : σ * τ ∈ closure {σ : perm α | is_three_cycle σ } := begin obtain ⟨a, b, ab, rfl⟩ := hσ, obtain ⟨c, d, cd, rfl⟩ := hτ, by_cases ac : a = c, { subst ac, exact swap_mul_swap_same_mem_closure_three_cycles ab cd }, have h' : swap a b * swap c d = swap a b * swap a c * (swap c a * swap c d), { simp [swap_comm c a, mul_assoc] }, rw h', exact mul_mem _ (swap_mul_swap_same_mem_closure_three_cycles ab ac) (swap_mul_swap_same_mem_closure_three_cycles (ne.symm ac) cd), end end end equiv.perm
681ef781fc895d9b3bcf59184721cc03782e280d
bb31430994044506fa42fd667e2d556327e18dfe
/src/combinatorics/set_family/intersecting.lean
ce2503b935cd15889b985ebc625167be2bd1dabd
[ "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
7,613
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.fintype.card import order.upper_lower /-! # Intersecting families This file defines intersecting families and proves their basic properties. ## Main declarations * `set.intersecting`: Predicate for a set of elements in a generalized boolean algebra to be an intersecting family. * `set.intersecting.card_le`: An intersecting family can only take up to half the elements, because `a` and `aᶜ` cannot simultaneously be in it. * `set.intersecting.is_max_iff_card_eq`: Any maximal intersecting family takes up half the elements. ## References * [D. J. Kleitman, *Families of non-disjoint subsets*][kleitman1966] -/ open finset variables {α : Type*} namespace set section semilattice_inf variables [semilattice_inf α] [order_bot α] {s t : set α} {a b c : α} /-- A set family is intersecting if every pair of elements is non-disjoint. -/ def intersecting (s : set α) : Prop := ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → ¬ disjoint a b @[mono] lemma intersecting.mono (h : t ⊆ s) (hs : s.intersecting) : t.intersecting := λ a ha b hb, hs (h ha) (h hb) lemma intersecting.not_bot_mem (hs : s.intersecting) : ⊥ ∉ s := λ h, hs h h disjoint_bot_left lemma intersecting.ne_bot (hs : s.intersecting) (ha : a ∈ s) : a ≠ ⊥ := ne_of_mem_of_not_mem ha hs.not_bot_mem lemma intersecting_empty : (∅ : set α).intersecting := λ _, false.elim @[simp] lemma intersecting_singleton : ({a} : set α).intersecting ↔ a ≠ ⊥ := by simp [intersecting] lemma intersecting.insert (hs : s.intersecting) (ha : a ≠ ⊥) (h : ∀ b ∈ s, ¬ disjoint a b) : (insert a s).intersecting := begin rintro b (rfl | hb) c (rfl | hc), { rwa disjoint_self }, { exact h _ hc }, { exact λ H, h _ hb H.symm }, { exact hs hb hc } end lemma intersecting_insert : (insert a s).intersecting ↔ s.intersecting ∧ a ≠ ⊥ ∧ ∀ b ∈ s, ¬ disjoint a b := ⟨λ h, ⟨h.mono $ subset_insert _ _, h.ne_bot $ mem_insert _ _, λ b hb, h (mem_insert _ _) $ mem_insert_of_mem _ hb⟩, λ h, h.1.insert h.2.1 h.2.2⟩ lemma intersecting_iff_pairwise_not_disjoint : s.intersecting ↔ s.pairwise (λ a b, ¬ disjoint a b) ∧ s ≠ {⊥} := begin refine ⟨λ h, ⟨λ a ha b hb _, h ha hb, _⟩, λ h a ha b hb hab, _⟩, { rintro rfl, exact intersecting_singleton.1 h rfl }, { have := h.1.eq ha hb (not_not.2 hab), rw [this, disjoint_self] at hab, rw hab at hb, exact h.2 (eq_singleton_iff_unique_mem.2 ⟨hb, λ c hc, not_ne_iff.1 $ λ H, h.1 hb hc H.symm disjoint_bot_left⟩) } end protected lemma subsingleton.intersecting (hs : s.subsingleton) : s.intersecting ↔ s ≠ {⊥} := intersecting_iff_pairwise_not_disjoint.trans $ and_iff_right $ hs.pairwise _ lemma intersecting_iff_eq_empty_of_subsingleton [subsingleton α] (s : set α) : s.intersecting ↔ s = ∅ := begin refine subsingleton_of_subsingleton.intersecting.trans ⟨not_imp_comm.2 $ λ h, subsingleton_of_subsingleton.eq_singleton_of_mem _, _⟩, { obtain ⟨a, ha⟩ := nonempty_iff_ne_empty.2 h, rwa subsingleton.elim ⊥ a }, { rintro rfl, exact (set.singleton_nonempty _).ne_empty.symm } end /-- Maximal intersecting families are upper sets. -/ protected lemma intersecting.is_upper_set (hs : s.intersecting) (h : ∀ t : set α, t.intersecting → s ⊆ t → s = t) : is_upper_set s := begin classical, rintro a b hab ha, rw h (insert b s) _ (subset_insert _ _), { exact mem_insert _ _ }, exact hs.insert (mt (eq_bot_mono hab) $ hs.ne_bot ha) (λ c hc hbc, hs ha hc $ hbc.mono_left hab), end /-- Maximal intersecting families are upper sets. Finset version. -/ lemma intersecting.is_upper_set' {s : finset α} (hs : (s : set α).intersecting) (h : ∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t) : is_upper_set (s : set α) := begin classical, rintro a b hab ha, rw h (insert b s) _ (finset.subset_insert _ _), { exact mem_insert_self _ _ }, rw coe_insert, exact hs.insert (mt (eq_bot_mono hab) $ hs.ne_bot ha) (λ c hc hbc, hs ha hc $ hbc.mono_left hab), end end semilattice_inf lemma intersecting.exists_mem_set {𝒜 : set (set α)} (h𝒜 : 𝒜.intersecting) {s t : set α} (hs : s ∈ 𝒜) (ht : t ∈ 𝒜) : ∃ a, a ∈ s ∧ a ∈ t := not_disjoint_iff.1 $ h𝒜 hs ht lemma intersecting.exists_mem_finset [decidable_eq α] {𝒜 : set (finset α)} (h𝒜 : 𝒜.intersecting) {s t : finset α} (hs : s ∈ 𝒜) (ht : t ∈ 𝒜) : ∃ a, a ∈ s ∧ a ∈ t := not_disjoint_iff.1 $ disjoint_coe.not.2 $ h𝒜 hs ht variables [boolean_algebra α] lemma intersecting.not_compl_mem {s : set α} (hs : s.intersecting) {a : α} (ha : a ∈ s) : aᶜ ∉ s := λ h, hs ha h disjoint_compl_right lemma intersecting.not_mem {s : set α} (hs : s.intersecting) {a : α} (ha : aᶜ ∈ s) : a ∉ s := λ h, hs ha h disjoint_compl_left lemma intersecting.disjoint_map_compl {s : finset α} (hs : (s : set α).intersecting) : disjoint s (s.map ⟨compl, compl_injective⟩) := begin rw finset.disjoint_left, rintro x hx hxc, obtain ⟨x, hx', rfl⟩ := mem_map.mp hxc, exact hs.not_compl_mem hx' hx, end lemma intersecting.card_le [fintype α] {s : finset α} (hs : (s : set α).intersecting) : 2 * s.card ≤ fintype.card α := begin classical, refine (s.disj_union _ hs.disjoint_map_compl).card_le_univ.trans_eq' _, rw [two_mul, card_disj_union, card_map], end variables [nontrivial α] [fintype α] {s : finset α} -- Note, this lemma is false when `α` has exactly one element and boring when `α` is empty. lemma intersecting.is_max_iff_card_eq (hs : (s : set α).intersecting) : (∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t) ↔ 2 * s.card = fintype.card α := begin classical, refine ⟨λ h, _, λ h t ht hst, finset.eq_of_subset_of_card_le hst $ le_of_mul_le_mul_left (ht.card_le.trans_eq h.symm) two_pos⟩, suffices : s.disj_union (s.map ⟨compl, compl_injective⟩) (hs.disjoint_map_compl) = finset.univ, { rw [fintype.card, ←this, two_mul, card_disj_union, card_map] }, rw [←coe_eq_univ, disj_union_eq_union, coe_union, coe_map, function.embedding.coe_fn_mk, image_eq_preimage_of_inverse compl_compl compl_compl], refine eq_univ_of_forall (λ a, _), simp_rw [mem_union, mem_preimage], by_contra' ha, refine s.ne_insert_of_not_mem _ ha.1 (h _ _ $ s.subset_insert _), rw coe_insert, refine hs.insert _ (λ b hb hab, ha.2 $ (hs.is_upper_set' h) hab.le_compl_left hb), rintro rfl, have := h {⊤} (by { rw coe_singleton, exact intersecting_singleton.2 top_ne_bot }), rw compl_bot at ha, rw coe_eq_empty.1 ((hs.is_upper_set' h).not_top_mem.1 ha.2) at this, exact finset.singleton_ne_empty _ (this $ empty_subset _).symm, end lemma intersecting.exists_card_eq (hs : (s : set α).intersecting) : ∃ t, s ⊆ t ∧ 2 * t.card = fintype.card α ∧ (t : set α).intersecting := begin have := hs.card_le, rw [mul_comm, ←nat.le_div_iff_mul_le' two_pos] at this, revert hs, refine s.strong_downward_induction_on _ this, rintro s ih hcard hs, by_cases ∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t, { exact ⟨s, subset.rfl, hs.is_max_iff_card_eq.1 h, hs⟩ }, push_neg at h, obtain ⟨t, ht, hst⟩ := h, refine (ih _ (_root_.ssubset_iff_subset_ne.2 hst) ht).imp (λ u, and.imp_left hst.1.trans), rw [nat.le_div_iff_mul_le' two_pos, mul_comm], exact ht.card_le, end end set
979f5716eba7a5647d5924a453b71a30f9facaf3
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/instructor/lectures/lecture_21.lean
ecc39d0b2b5974c836cbb6d5a3362f3c71a11894
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
8,416
lean
import data.set namespace hidden /- PROPERTIES OF RELATIONS -/ section relation /- For any types, α and β we will refine a relation, r, to be a predicate on values of these types. It will implicitly define the set of all such pairs, also called a relation, that satisfy the predicate (by yielding a proposition for which there is a proof). -/ variables {α β : Type} (r : β → β → Prop) /- This variables declaration implicitly adds the following parameters to the front of each definition below, as needed based on the variables used in the rest of a given definition. We'll see an example shortly. -/ /- We will introduce an infix notation, ≺, for the relation/predicate, r, so that instead of writing (r a b) to denote the proposition that a is related to b by r, we can write (a ≺ b) read as "a is related to b." -/ local infix `≺`:50 := r -- infix notation /- With these concepts and notations, we can now define many essential properties of relations in an entirely general way. -/ /- REFLEXIVITY AS A PROPERTY OF RELATIONS -/ /- Let's see an example in detail. Using the preceding definitions, implict arguments, and notations, the definition of reflexive is exceedingly clear. This is it. In English you can say "a relation, ≺, is reflexive if it relates every value, x, to itself."" -/ def reflexive := ∀ x, x ≺ x /- Filling in the implicit arguments and unfolding the notation gives us the full picture. See the next definition. -/ def reflexive' {β : Type} {r : β → β → Prop} := ∀ x, r x x /- These definitions are parameterized by a type, β, and a (binary) relation, r, on β (r ⊆ β × β), and the yield *propositions*, that every value of a type β is related to itself by r. One will often then want a proof, but now that is just a matter of ordinarly logical reasoning. We have thus now "reduced" one crucial property of mathematial relations to our underlying predicate logic. As an example, let's state and prove the proposition that the equality relation for natural numbers is is reflexive. -/ theorem eq_is_refl : reflexive (@eq nat) := -- proof below /- Let's unpack the notation, @eq α. When we write, 0 = 0, that's infix notation for the term, eq 0 0 (the application of the two-place predicate, eq, to 0 and 0. It is the meaning of the notation 0 = 0.). But what (eq 0 0) means is (@eq nat 0 0). The first argument of eq is implicit, so is usually omitted from code and inferred from the following values. But here we don't give such values. The @tells Lean to let us write the implicit argument(s) explicitly. -/ begin end /- Here's an English version. Theorem: Equality is reflexive. Proof. Unfolding the definition of reflexive, what we are to show is ∀ x, x = x. To prove it, assume x is an arbitrary value and show x = x. That's true by (application of) the introduction rule for equality (to x). QED. You'd usually leave out the parenthesized parts, as people with mathematical training, like you now have, will understand what is meant implicitly. Indeed, most people would just say, "and that's trivially true. QED" Of course an even simpler proof is "by the reflexivity introduction rule for equality!" We're just re-proving something we already knew, be here we're forming the proposition that equality is reflexivity using a general definition of what that means in the form of a *predicate on binary relations on any type.* -/ /- Note well: reflexive as we're defined it here is a *one-place* predicate on binary relations, which we represent as two-place predicates. So we expect the type of the reflexive predicate / property to be (β → β → Prop) → Prop. Lean is of course plenty smart to already know that! -/ #check @reflexive /- Π {β : Type}, (β → β → Prop) → Prop Again, think of Π as ∀. What defined reflexive to be, given any type β, is a predicate on binary relations on β. Some binary relations are reflexive, some aren't: i.e., some have the property of being reflexive (of reflexivity), some don't. This predicate picks out (logically) those that do. Building on our discussion of sets, it appears to be the case, and it is, that it defines the set of binary relations on β that are reflexive. We can define this set explicitly by using reflexive as a predicate on binary relations in a set builder expression. -/ def reflexive_relations := { r : β → β → Prop | reflexive r } -- That's pretty cool. Says a lot, very precisely. /- Now we can even state and prove theorems about relations being reflexive, using the language of *set theory*. Here we say that "the equality relation on natural numbers is an element in the set of all reflexive binary relations on the natural numbers." Here it is formally. The use of @ here again turns off implicit argument inference. I do expect you to understand what it means when you see it. I will not give you any problems, except perhaps extra credit, where I expect you to know when exactly to use it. -/ example : @eq nat ∈ @reflexive_relations nat := begin show reflexive_relations (@eq nat), unfold reflexive_relations, exact eq_is_refl, -- Already proved, use theorem! end -- You can just feel your brains getting bigger here! #check (λ x, x + 1) /- IMPORTANT ASIDE ON FIRST ORDER PREDICATE LOGIC As an aside that we will return to later, you cannot express properties of relations, with= this degree of clarity, in what we call first- order predicate logic (FOPL). You are expected to understand FOPL when leaving this course. The good news is that it is simply a restricted form of higher-order predicate logic in which you are not enabled to quantify over relations. The syntax of FOPL is simply not capable of expressing the kind of idea that we developed righ there: that we can specify properties of relations by using our logic. From now on, therefore, if you are asked to write or prove propositions in first-order predicate logic, you can use everything you have learned, here except that you must not quantify over functions or relations. In the case at hand, you would not be allowed to say "for any binary relation, r, on β" or "there exists such a relation," because either of those statements involves quantification over the set of all binary relations on a type, β. The broad experience of the computer science community over the last few decades has shown that there are many practical benefits to the use of higher-order logic. In particular, it is the logical foundation for automated proof assistants such as Lean, which are now rapidly coming into their own, especially for safety- / mission- critical sofware, provable system security, and in the design, analysis, and implementation of programming languages, as well as in attempts to formalize mathematics, per se. As you're instructor, I believe, based on a decade of work in this area, that the beauty with which one can express *properties of relations* (and the vital importance of this idea), the availability of automated checking of logical syntax and proof correctness (when doing formal proofs), and that fact that first-order logic is simply a restricted form of higher-order logic, make it *far* preferable to FOPL as a logic to learn in a first course on logic and proof for computer scientists. And now we return to our regularly scheduled programming! -/ /- SYMMETRY AS A PROPERTY OF RELATIONS -/ def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x /- Exercise: prove that = is symmetric. And answer the question, is ≤ symmetric, and give a brief defense of your answer. -/ theorem eq_is_symm : symmetric (@eq α) := begin end /- Exercise prove that = is transitive as per the following formal and general definition of exactly what that means. Give both formal and especially informal (mathematical English) proofs. -/ def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z example : transitive (@eq α) := begin end /- Here's a new concept: a relation is said to be an equivalence relation if it is reflexive, symmetric, and transitive. -/ def equivalence := reflexive r ∧ symmetric r ∧ transitive r lemma mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) : equivalence r := ⟨rfl, symm, trans⟩ -- Exercise theorem eq_is_equivalence : equivalence (@eq β) := begin end end relation end hidden
a3575b10c4d19ed76f3814bb24cb6fa22a936aa1
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/nat/default.lean
89b96899f1cbd024f435378b89533df3fb9cdb77
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
194
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Jeremy Avigad import .basic .order .sub .div
f4949fbaf31ee014d91ded39fef6e8e1ec2bda30
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/functorial.lean
4b85a1c6254338b70a1f5a6c416f208ce1dc6eaf
[ "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
2,219
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.functor /-! # Unbundled functors, as a typeclass decorating the object-level function. -/ namespace category_theory universes v v₁ v₂ v₃ u u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- A unbundled functor. -/ -- Perhaps in the future we could redefine `functor` in terms of this, but that isn't the -- immediate plan. class functorial (F : C → D) : Type (max v₁ v₂ u₁ u₂) := (map : Π {X Y : C}, (X ⟶ Y) → ((F X) ⟶ (F Y))) (map_id' : ∀ (X : C), map (𝟙 X) = 𝟙 (F X) . obviously) (map_comp' : ∀ {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z), map (f ≫ g) = (map f) ≫ (map g) . obviously) restate_axiom functorial.map_id' attribute [simp] functorial.map_id restate_axiom functorial.map_comp' attribute [simp] functorial.map_comp /-- If `F : C → D` (just a function) has `[functorial F]`, we can write `map F f : F X ⟶ F Y` for the action of `F` on a morphism `f : X ⟶ Y`. -/ def map (F : C → D) [functorial.{v₁ v₂} F] {X Y : C} (f : X ⟶ Y) : F X ⟶ F Y := functorial.map.{v₁ v₂} f namespace functor /-- Bundle a functorial function as a functor. -/ def of (F : C → D) [I : functorial.{v₁ v₂} F] : C ⥤ D := { obj := F, ..I } end functor instance (F : C ⥤ D) : functorial.{v₁ v₂} (F.obj) := { .. F } @[simp] lemma map_functorial_obj (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) : map F.obj f = F.map f := rfl instance functorial_id : functorial.{v₁ v₁} (id : C → C) := { map := λ X Y f, f } section variables {E : Type u₃} [category.{v₃} E] /-- `G ∘ F` is a functorial if both `F` and `G` are. -/ -- This is no longer viable as an instance in Lean 3.7, -- #lint reports an instance loop -- Will this be a problem? def functorial_comp (F : C → D) [functorial.{v₁ v₂} F] (G : D → E) [functorial.{v₂ v₃} G] : functorial.{v₁ v₃} (G ∘ F) := { ..(functor.of F ⋙ functor.of G) } end end category_theory
ebf3daec1ebfa0d4cbe940287a626ca4fee25c62
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/float_from_bignum.lean
d3587bad154a2402fe23f0b2c447b6109e256945
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
339
lean
def check (b : Bool) : IO Unit := «unless» b $ throw $ IO.userError "check failed" def tst1 : IO Unit := do check (Nat.toFloat (10^40) > Nat.toFloat (10^30)); check (Nat.toFloat (10^40) >= Nat.toFloat (10^30)); check (Nat.toFloat (10^40) == Nat.toFloat (10^40)); check (Nat.toFloat (10^80) > Nat.toFloat (10^40)); pure () #eval tst1
c8b0cf3153e6e2a842f5aa0ec72ce1ae8da35c4e
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/limits/seq_proveLimit.lean
79db20d45a4793baa5a79fe19a82b919005d4767
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,584
lean
import game.limits.L01defs import game.limits.seq_limitLinear notation `|` x `|` := abs x -- hide namespace xena -- hide /- A simple limit proof. -/ -- begin hide -- The proof below is from M1P1-lean. -- end hide /- Lemma Prove that the limit of $a_n = 1/n$ is zero. -/ theorem limit_check : is_limit (λ n, 1 / n) 0 := begin -- say ε is a positive real intros ε Hε, -- we need to find N such that n ≥ N → |1 / n| < ε. -- It's a standard fact there exists some integer M ≥ 0 -- such that 1 / (M + 1) < ε... cases (exists_nat_one_div_lt Hε) with M HM, -- ...so let's set N = M + 1. let N := M + 1, use N, -- Now say n ≥ N. intros n Hn, change N ≤ n at Hn, -- We need to show |1/n| < ε (because 1/n = 1/n - 0) rw sub_zero, show abs ((1 : ℝ) / n) < ε, -- Because n ≥ N = M + 1 ≥ 1 > 0, we have n > 0 have HM' : 0 < N := lt_of_lt_of_le zero_lt_one (by simp), have HM'' : (0 : ℝ) < N := by rwa [←nat.cast_zero,nat.cast_lt], -- note to self -- Lean rewriting M + 1 as 1 + M have HNn : (N : ℝ) ≤ n := by rwa nat.cast_le, have Hn'' : 0 < (n : ℝ) := by linarith, -- so 1 / n > 0, have H2 : (1 : ℝ) / n > 0, exact one_div_pos_of_pos Hn'', -- and hence |1 / n| = 1 / n. rw abs_of_pos H2, -- We want to prove 1 / n < ε, but we know 1 / N < ε, -- so it suffices to prove 1 / n ≤ 1 / N suffices : (1 : ℝ) / n ≤ 1 / N, exact lt_of_le_of_lt this HM, -- This follows easily from the fact that N ≤ n refine div_le_div_of_le_left zero_le_one HM'' HNn, done end end xena -- hide
1c58f249ed00deab1b6762ff699b29441c15bab7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/e12.lean
13da1cca149866219c7fc736228e03e8d3a51816
[ "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
970
lean
prelude precedence `+`:65 namespace nat constant nat : Type.{1} constant add : nat → nat → nat infixl + := add end nat namespace int open nat (nat) constant int : Type.{1} constant add : int → int → int infixl + := add constant of_nat : nat → int attribute of_nat [coercion] end int constants n m : nat.nat constants i j : int.int section open [notation] nat open [notation] int open [decl] nat open [decl] int check n+m check i+j -- check i+n -- Error end namespace int open [decl] nat (nat) -- Here is a possible trick for this kind of configuration definition add_ni (a : nat) (b : int) := (of_nat a) + b definition add_in (a : int) (b : nat) := a + (of_nat b) infixl + := add_ni infixl + := add_in end int section open [notation] nat open [notation] int open [declaration] nat open [declaration] int check n+m check i+n check n+i end section open nat open int check n+m check i+n end
9c10988f633f4d5646a56a77363a60022abc36c1
4fa161becb8ce7378a709f5992a594764699e268
/src/geometry/manifold/manifold.lean
9db0c99852b73a687b0c82abbcddd7c078b4faa8
[ "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
27,160
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.local_homeomorph /-! # Manifolds A manifold is a topological space M locally modelled on a model space H, i.e., the manifold is covered by open subsets on which there are local homeomorphisms (the charts) going to H. If the changes of charts satisfy some additional property (for instance if they are smooth), then M inherits additional structure (it makes sense to talk about smooth manifolds). There are therefore two different ingredients in a manifold: * the set of charts, which is data * the fact that changes of charts belong to some group (in fact groupoid), which is additional Prop. We separate these two parts in the definition: the manifold structure is just the set of charts, and then the different smoothness requirements (smooth manifold, orientable manifold, contact manifold, and so on) are additional properties of these charts. These properties are formalized through the notion of structure groupoid, i.e., a set of local homeomorphisms stable under composition and inverse, to which the change of coordinates should belong. ## Main definitions * `structure_groupoid H` : a subset of local homeomorphisms of `H` stable under composition, inverse and restriction (ex: local diffeos) * `pregroupoid H` : a subset of local homeomorphisms of `H` stable under composition and restriction, but not inverse (ex: smooth maps) * `groupoid_of_pregroupoid`: construct a groupoid from a pregroupoid, by requiring that a map and its inverse both belong to the pregroupoid (ex: construct diffeos from smooth maps) * `continuous_groupoid H` : the groupoid of all local homeomorphisms of `H` * `manifold H M` : manifold structure on `M` modelled on `H`, given by an atlas of local homeomorphisms from `M` to `H` whose sources cover `M`. This is a type class. * `has_groupoid M G` : when `G` is a structure groupoid on `H` and `M` is a manifold modelled on `H`, require that all coordinate changes belong to `G`. This is a type class * `atlas H M` : when `M` is a manifold modelled on `H`, the atlas of this manifold structure, i.e., the set of charts * `structomorph G M M'` : the set of diffeomorphisms between the manifolds `M` and `M'` for the groupoid `G`. We avoid the word diffeomorphisms, keeping it for the smooth category. As a basic example, we give the instance `instance manifold_model_space (H : Type*) [topological_space H] : manifold H H` saying that a topological space is a manifold over itself, with the identity as unique chart. This manifold structure is compatible with any groupoid. ## Implementation notes The atlas in a manifold is *not* a maximal atlas in general: the notion of maximality depends on the groupoid one considers, and changing groupoids changes the maximal atlas. With the current formalization, it makes sense first to choose the atlas, and then to ask whether this precise atlas defines a smooth manifold, an orientable manifold, and so on. A consequence is that structomorphisms between M and M' do *not* induce a bijection between the atlases of M and M': the definition is only that, read in charts, the structomorphism locally belongs to the groupoid under consideration. (This is equivalent to inducing a bijection between elements of the maximal atlas). A consequence is that the invariance under structomorphisms of properties defined in terms of the atlas is not obvious in general, and could require some work in theory (amounting to the fact that these properties only depend on the maximal atlas, for instance). In practice, this does not create any real difficulty. We use the letter `H` for the model space thinking of the case of manifolds with boundary, where the model space is a half space. Manifolds are sometimes defined as topological spaces with an atlas of local diffeomorphisms, and sometimes as spaces with an atlas from which a topology is deduced. We use the former approach: otherwise, there would be an instance from manifolds to topological spaces, which means that any instance search for topological spaces would try to find manifold structures involving a yet unknown model space, leading to problems. However, we also introduce the latter approach, through a structure `manifold_core` making it possible to construct a topology out of a set of local equivs with compatibility conditions (but we do not register it as an instance). In the definition of a manifold, the model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over ℂ^n) will also be seen sometimes as a real manifold modelled over ℝ^(2n). -/ noncomputable theory local attribute [instance, priority 0] classical.decidable_inhabited classical.prop_decidable universes u variables {H : Type u} {M : Type*} {M' : Type*} {M'' : Type*} /- Notational shortcut for the composition of local homeomorphisms, i.e., `local_homeomorph.trans`. Note that, as is usual for equivs, the composition is from left to right, hence the direction of the arrow. -/ local infixr ` ≫ₕ `:100 := local_homeomorph.trans open set local_homeomorph section groupoid /- One could add to the definition of a structure groupoid the fact that the restriction of an element of the groupoid to any open set still belongs to the groupoid. (This is in Kobayashi-Nomizu.) I am not sure I want this, for instance on H × E where E is a vector space, and the groupoid is made of functions respecting the fibers and linear in the fibers (so that a manifold over this groupoid is naturally a vector bundle) I prefer that the members of the groupoid are always defined on sets of the form s × E The only nontrivial requirement is locality: if a local homeomorphism belongs to the groupoid around each point in its domain of definition, then it belongs to the groupoid. Without this requirement, the composition of diffeomorphisms does not have to be a diffeomorphism. Note that this implies that a local homeomorphism with empty source belongs to any structure groupoid, as it trivially satisfies this condition. There is also a technical point, related to the fact that a local homeomorphism is by definition a global map which is a homeomorphism when restricted to its source subset (and its values outside of the source are not relevant). Therefore, we also require that being a member of the groupoid only depends on the values on the source. -/ /-- A structure groupoid is a set of local homeomorphisms of a topological space stable under composition and inverse. They appear in the definition of the smoothness class of a manifold. -/ structure structure_groupoid (H : Type u) [topological_space H] := (members : set (local_homeomorph H H)) (comp : ∀e e' : local_homeomorph H H, e ∈ members → e' ∈ members → e ≫ₕ e' ∈ members) (inv : ∀e : local_homeomorph H H, e ∈ members → e.symm ∈ members) (id_mem : local_homeomorph.refl H ∈ members) (locality : ∀e : local_homeomorph H H, (∀x ∈ e.source, ∃s, is_open s ∧ x ∈ s ∧ e.restr s ∈ members) → e ∈ members) (eq_on_source : ∀ e e' : local_homeomorph H H, e ∈ members → e' ≈ e → e' ∈ members) variable [topological_space H] @[reducible] instance : has_mem (local_homeomorph H H) (structure_groupoid H) := ⟨λ(e : local_homeomorph H H) (G : structure_groupoid H), e ∈ G.members⟩ /-- Partial order on the set of groupoids, given by inclusion of the members of the groupoid -/ instance structure_groupoid.partial_order : partial_order (structure_groupoid H) := partial_order.lift structure_groupoid.members (λa b h, by { cases a, cases b, dsimp at h, induction h, refl }) /-- The trivial groupoid, containing only the identity (and maps with empty source, as this is necessary from the definition) -/ def id_groupoid (H : Type u) [topological_space H] : structure_groupoid H := { members := {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅}, comp := λe e' he he', begin cases he; simp at he he', { simpa [he] }, { have : (e ≫ₕ e').source ⊆ e.source := sep_subset _ _, rw he at this, have : (e ≫ₕ e') ∈ {e : local_homeomorph H H | e.source = ∅} := disjoint_iff.1 this, exact (mem_union _ _ _).2 (or.inr this) }, end, inv := λe he, begin cases (mem_union _ _ _).1 he with E E, { finish }, { right, simpa [e.to_local_equiv.image_source_eq_target.symm] using E }, end, id_mem := mem_union_left _ rfl, locality := λe he, begin cases e.source.eq_empty_or_nonempty with h h, { right, exact h }, { left, rcases h with ⟨x, hx⟩, rcases he x hx with ⟨s, open_s, xs, hs⟩, have x's : x ∈ (e.restr s).source, { rw [restr_source, interior_eq_of_open open_s], exact ⟨hx, xs⟩ }, cases hs, { replace hs : local_homeomorph.restr e s = local_homeomorph.refl H, by simpa using hs, have : (e.restr s).source = univ, by { rw hs, simp }, change (e.to_local_equiv).source ∩ interior s = univ at this, have : univ ⊆ interior s, by { rw ← this, exact inter_subset_right _ _ }, have : s = univ, by rwa [interior_eq_of_open open_s, univ_subset_iff] at this, simpa [this, restr_univ] using hs }, { exfalso, rw mem_set_of_eq at hs, rwa hs at x's } }, end, eq_on_source := λe e' he he'e, begin cases he, { left, have : e = e', { refine eq_of_eq_on_source_univ (setoid.symm he'e) _ _; rw set.mem_singleton_iff.1 he ; refl }, rwa ← this }, { right, change (e.to_local_equiv).source = ∅ at he, rwa [set.mem_set_of_eq, he'e.source_eq] } end } /-- Every structure groupoid contains the identity groupoid -/ instance : order_bot (structure_groupoid H) := { bot := id_groupoid H, bot_le := begin assume u f hf, change f ∈ {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅} at hf, simp only [singleton_union, mem_set_of_eq, mem_insert_iff] at hf, cases hf, { rw hf, apply u.id_mem }, { apply u.locality, assume x hx, rw [hf, mem_empty_eq] at hx, exact hx.elim } end, ..structure_groupoid.partial_order } instance (H : Type u) [topological_space H] : inhabited (structure_groupoid H) := ⟨id_groupoid H⟩ /-- To construct a groupoid, one may consider classes of local homeos such that both the function and its inverse have some property. If this property is stable under composition, one gets a groupoid. `pregroupoid` bundles the properties needed for this construction, with the groupoid of smooth functions with smooth inverses as an application. -/ structure pregroupoid (H : Type*) [topological_space H] := (property : (H → H) → (set H) → Prop) (comp : ∀{f g u v}, property f u → property g v → is_open u → is_open v → is_open (u ∩ f ⁻¹' v) → property (g ∘ f) (u ∩ f ⁻¹' v)) (id_mem : property id univ) (locality : ∀{f u}, is_open u → (∀x∈u, ∃v, is_open v ∧ x ∈ v ∧ property f (u ∩ v)) → property f u) (congr : ∀{f g : H → H} {u}, is_open u → (∀x∈u, g x = f x) → property f u → property g u) /-- Construct a groupoid of local homeos for which the map and its inverse have some property, from a pregroupoid asserting that this property is stable under composition. -/ def pregroupoid.groupoid (PG : pregroupoid H) : structure_groupoid H := { members := {e : local_homeomorph H H | PG.property e e.source ∧ PG.property e.symm e.target}, comp := λe e' he he', begin split, { apply PG.comp he.1 he'.1 e.open_source e'.open_source, apply e.continuous_to_fun.preimage_open_of_open e.open_source e'.open_source }, { apply PG.comp he'.2 he.2 e'.open_target e.open_target, apply e'.continuous_inv_fun.preimage_open_of_open e'.open_target e.open_target } end, inv := λe he, ⟨he.2, he.1⟩, id_mem := ⟨PG.id_mem, PG.id_mem⟩, locality := λe he, begin split, { apply PG.locality e.open_source (λx xu, _), rcases he x xu with ⟨s, s_open, xs, hs⟩, refine ⟨s, s_open, xs, _⟩, convert hs.1, exact (interior_eq_of_open s_open).symm }, { apply PG.locality e.open_target (λx xu, _), rcases he (e.symm x) (e.map_target xu) with ⟨s, s_open, xs, hs⟩, refine ⟨e.target ∩ e.symm ⁻¹' s, _, ⟨xu, xs⟩, _⟩, { exact continuous_on.preimage_open_of_open e.continuous_inv_fun e.open_target s_open }, { rw [← inter_assoc, inter_self], convert hs.2, exact (interior_eq_of_open s_open).symm } }, end, eq_on_source := λe e' he ee', begin split, { apply PG.congr e'.open_source ee'.2, simp only [ee'.1, he.1] }, { have A := ee'.symm', apply PG.congr e'.symm.open_source A.2, convert he.2, rw A.1, refl } end } lemma mem_groupoid_of_pregroupoid (PG : pregroupoid H) (e : local_homeomorph H H) : e ∈ PG.groupoid ↔ PG.property e e.source ∧ PG.property e.symm e.target := iff.rfl lemma groupoid_of_pregroupoid_le (PG₁ PG₂ : pregroupoid H) (h : ∀f s, PG₁.property f s → PG₂.property f s) : PG₁.groupoid ≤ PG₂.groupoid := begin assume e he, rw mem_groupoid_of_pregroupoid at he ⊢, exact ⟨h _ _ he.1, h _ _ he.2⟩ end lemma mem_pregroupoid_of_eq_on_source (PG : pregroupoid H) {e e' : local_homeomorph H H} (he' : e ≈ e') (he : PG.property e e.source) : PG.property e' e'.source := begin rw ← he'.1, exact PG.congr e.open_source he'.eq_on.symm he, end /-- The pregroupoid of all local maps on a topological space H -/ @[reducible] def continuous_pregroupoid (H : Type*) [topological_space H] : pregroupoid H := { property := λf s, true, comp := λf g u v hf hg hu hv huv, trivial, id_mem := trivial, locality := λf u u_open h, trivial, congr := λf g u u_open hcongr hf, trivial } instance (H : Type*) [topological_space H] : inhabited (pregroupoid H) := ⟨continuous_pregroupoid H⟩ /-- The groupoid of all local homeomorphisms on a topological space H -/ def continuous_groupoid (H : Type*) [topological_space H] : structure_groupoid H := pregroupoid.groupoid (continuous_pregroupoid H) /-- Every structure groupoid is contained in the groupoid of all local homeomorphisms -/ instance : order_top (structure_groupoid H) := { top := continuous_groupoid H, le_top := λ u f hf, by { split; exact dec_trivial }, ..structure_groupoid.partial_order } end groupoid /-- A manifold is a topological space endowed with an atlas, i.e., a set of local homeomorphisms taking value in a model space `H`, called charts, such that the domains of the charts cover the whole space. We express the covering property by chosing for each x a member `chart_at x` of the atlas containing `x` in its source: in the smooth case, this is convenient to construct the tangent bundle in an efficient way. The model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over `ℂ^n`) will also be seen sometimes as a real manifold over `ℝ^(2n)`. -/ class manifold (H : Type*) [topological_space H] (M : Type*) [topological_space M] := (atlas [] : set (local_homeomorph M H)) (chart_at [] : M → local_homeomorph M H) (mem_chart_source [] : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas [] : ∀x, chart_at x ∈ atlas) export manifold attribute [simp] mem_chart_source chart_mem_atlas section manifold /-- Any space is a manifold modelled over itself, by just using the identity chart -/ instance manifold_model_space (H : Type*) [topological_space H] : manifold H H := { atlas := {local_homeomorph.refl H}, chart_at := λx, local_homeomorph.refl H, mem_chart_source := λx, mem_univ x, chart_mem_atlas := λx, mem_singleton _ } /-- In the trivial manifold structure of a space modelled over itself through the identity, the atlas members are just the identity -/ @[simp] lemma model_space_atlas {H : Type*} [topological_space H] {e : local_homeomorph H H} : e ∈ atlas H H ↔ e = local_homeomorph.refl H := by simp [atlas, manifold.atlas] /-- In the model space, chart_at is always the identity -/ @[simp] lemma chart_at_model_space_eq {H : Type*} [topological_space H] {x : H} : chart_at H x = local_homeomorph.refl H := by simpa using chart_mem_atlas H x end manifold /-- Sometimes, one may want to construct a manifold structure on a space which does not yet have a topological structure, where the topology would come from the charts. For this, one needs charts that are only local equivs, and continuity properties for their composition. This is formalised in `manifold_core`. -/ @[nolint has_inhabited_instance] structure manifold_core (H : Type*) [topological_space H] (M : Type*) := (atlas : set (local_equiv M H)) (chart_at : M → local_equiv M H) (mem_chart_source : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas : ∀x, chart_at x ∈ atlas) (open_source : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → is_open (e.symm.trans e').source) (continuous_to_fun : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → continuous_on (e.symm.trans e') (e.symm.trans e').source) namespace manifold_core variables [topological_space H] (c : manifold_core H M) {e : local_equiv M H} /-- Topology generated by a set of charts on a Type. -/ protected def to_topological_space : topological_space M := topological_space.generate_from $ ⋃ (e : local_equiv M H) (he : e ∈ c.atlas) (s : set H) (s_open : is_open s), {e ⁻¹' s ∩ e.source} lemma open_source' (he : e ∈ c.atlas) : @is_open M c.to_topological_space e.source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨e, he, univ, is_open_univ, _⟩, simp only [set.univ_inter, set.preimage_univ] end lemma open_target (he : e ∈ c.atlas) : is_open e.target := begin have E : e.target ∩ e.symm ⁻¹' e.source = e.target := subset.antisymm (inter_subset_left _ _) (λx hx, ⟨hx, local_equiv.target_subset_preimage_source _ hx⟩), simpa [local_equiv.trans_source, E] using c.open_source e e he he end /-- An element of the atlas in a manifold without topology becomes a local homeomorphism for the topology constructed from this atlas. The `local_homeomorph` version is given in this definition. -/ def local_homeomorph (e : local_equiv M H) (he : e ∈ c.atlas) : @local_homeomorph M H c.to_topological_space _ := { open_source := by convert c.open_source' he, open_target := by convert c.open_target he, continuous_to_fun := begin letI : topological_space M := c.to_topological_space, rw continuous_on_open_iff (c.open_source' he), assume s s_open, rw inter_comm, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨e, he, ⟨s, s_open, rfl⟩⟩ end, continuous_inv_fun := begin letI : topological_space M := c.to_topological_space, apply continuous_on_open_of_generate_from (c.open_target he), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, rcases ht with ⟨e', e'_atlas, s, s_open, ts⟩, rw ts, let f := e.symm.trans e', have : is_open (f ⁻¹' s ∩ f.source), by simpa [inter_comm] using (continuous_on_open_iff (c.open_source e e' he e'_atlas)).1 (c.continuous_to_fun e e' he e'_atlas) s s_open, have A : e' ∘ e.symm ⁻¹' s ∩ (e.target ∩ e.symm ⁻¹' e'.source) = e.target ∩ (e' ∘ e.symm ⁻¹' s ∩ e.symm ⁻¹' e'.source), by { rw [← inter_assoc, ← inter_assoc], congr' 1, exact inter_comm _ _ }, simpa [local_equiv.trans_source, preimage_inter, preimage_comp.symm, A] using this end, ..e } /-- Given a manifold without topology, endow it with a genuine manifold structure with respect to the topology constructed from the atlas. -/ def to_manifold : @manifold H _ M c.to_topological_space := { atlas := ⋃ (e : local_equiv M H) (he : e ∈ c.atlas), {c.local_homeomorph e he}, chart_at := λx, c.local_homeomorph (c.chart_at x) (c.chart_mem_atlas x), mem_chart_source := λx, c.mem_chart_source x, chart_mem_atlas := λx, begin simp only [mem_Union, mem_singleton_iff], exact ⟨c.chart_at x, c.chart_mem_atlas x, rfl⟩, end } end manifold_core section has_groupoid variables [topological_space H] [topological_space M] [manifold H M] /-- A manifold has an atlas in a groupoid G if the change of coordinates belong to the groupoid -/ class has_groupoid {H : Type*} [topological_space H] (M : Type*) [topological_space M] [manifold H M] (G : structure_groupoid H) : Prop := (compatible [] : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → e.symm ≫ₕ e' ∈ G) lemma has_groupoid_of_le {G₁ G₂ : structure_groupoid H} (h : has_groupoid M G₁) (hle : G₁ ≤ G₂) : has_groupoid M G₂ := ⟨ λ e e' he he', hle ((h.compatible : _) he he') ⟩ lemma has_groupoid_of_pregroupoid (PG : pregroupoid H) (h : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → PG.property (e.symm ≫ₕ e') (e.symm ≫ₕ e').source) : has_groupoid M (PG.groupoid) := ⟨assume e e' he he', (mem_groupoid_of_pregroupoid PG _).mpr ⟨h he he', h he' he⟩⟩ /-- The trivial manifold structure on the model space is compatible with any groupoid -/ instance has_groupoid_model_space (H : Type*) [topological_space H] (G : structure_groupoid H) : has_groupoid H G := { compatible := λe e' he he', begin replace he : e ∈ atlas H H := he, replace he' : e' ∈ atlas H H := he', rw model_space_atlas at he he', simp [he, he', structure_groupoid.id_mem] end } /-- Any manifold structure is compatible with the groupoid of all local homeomorphisms -/ instance has_groupoid_continuous_groupoid : has_groupoid M (continuous_groupoid H) := ⟨begin assume e e' he he', rw [continuous_groupoid, mem_groupoid_of_pregroupoid], simp only [and_self] end⟩ /-- A G-diffeomorphism between two manifolds is a homeomorphism which, when read in the charts, belongs to G. We avoid the word diffeomorph as it is too related to the smooth category, and use structomorph instead. -/ @[nolint has_inhabited_instance] structure structomorph (G : structure_groupoid H) (M : Type*) (M' : Type*) [topological_space M] [topological_space M'] [manifold H M] [manifold H M'] extends homeomorph M M' := (mem_groupoid : ∀c : local_homeomorph M H, ∀c' : local_homeomorph M' H, c ∈ atlas H M → c' ∈ atlas H M' → c.symm ≫ₕ to_homeomorph.to_local_homeomorph ≫ₕ c' ∈ G) variables [topological_space M'] [topological_space M''] {G : structure_groupoid H} [manifold H M'] [manifold H M''] /-- The identity is a diffeomorphism of any manifold, for any groupoid. -/ def structomorph.refl (M : Type*) [topological_space M] [manifold H M] [has_groupoid M G] : structomorph G M M := { mem_groupoid := λc c' hc hc', begin change (local_homeomorph.symm c) ≫ₕ (local_homeomorph.refl M) ≫ₕ c' ∈ G, rw local_homeomorph.refl_trans, exact has_groupoid.compatible G hc hc' end, ..homeomorph.refl M } /-- The inverse of a structomorphism is a structomorphism -/ def structomorph.symm (e : structomorph G M M') : structomorph G M' M := { mem_groupoid := begin assume c c' hc hc', have : (c'.symm ≫ₕ e.to_homeomorph.to_local_homeomorph ≫ₕ c).symm ∈ G := G.inv _ (e.mem_groupoid c' c hc' hc), simp at this, rwa [trans_symm_eq_symm_trans_symm, trans_symm_eq_symm_trans_symm, symm_symm, trans_assoc] at this, end, ..e.to_homeomorph.symm} /-- The composition of structomorphisms is a structomorphism -/ def structomorph.trans (e : structomorph G M M') (e' : structomorph G M' M'') : structomorph G M M'' := { mem_groupoid := begin /- Let c and c' be two charts in M and M''. We want to show that e' ∘ e is smooth in these charts, around any point x. For this, let y = e (c⁻¹ x), and consider a chart g around y. Then g ∘ e ∘ c⁻¹ and c' ∘ e' ∘ g⁻¹ are both smooth as e and e' are structomorphisms, so their composition is smooth, and it coincides with c' ∘ e' ∘ e ∘ c⁻¹ around x. -/ assume c c' hc hc', refine G.locality _ (λx hx, _), let f₁ := e.to_homeomorph.to_local_homeomorph, let f₂ := e'.to_homeomorph.to_local_homeomorph, let f := (e.to_homeomorph.trans e'.to_homeomorph).to_local_homeomorph, have feq : f = f₁ ≫ₕ f₂ := homeomorph.trans_to_local_homeomorph _ _, -- define the atlas g around y let y := (c.symm ≫ₕ f₁) x, let g := chart_at H y, have hg₁ := chart_mem_atlas H y, have hg₂ := mem_chart_source H y, let s := (c.symm ≫ₕ f₁).source ∩ (c.symm ≫ₕ f₁) ⁻¹' g.source, have open_s : is_open s, by apply (c.symm ≫ₕ f₁).continuous_to_fun.preimage_open_of_open; apply open_source, have : x ∈ s, { split, { simp only [trans_source, preimage_univ, inter_univ, homeomorph.to_local_homeomorph_source], rw trans_source at hx, exact hx.1 }, { exact hg₂ } }, refine ⟨s, open_s, ⟨this, _⟩⟩, let F₁ := (c.symm ≫ₕ f₁ ≫ₕ g) ≫ₕ (g.symm ≫ₕ f₂ ≫ₕ c'), have A : F₁ ∈ G := G.comp _ _ (e.mem_groupoid c g hc hg₁) (e'.mem_groupoid g c' hg₁ hc'), let F₂ := (c.symm ≫ₕ f ≫ₕ c').restr s, have : F₁ ≈ F₂ := calc F₁ ≈ c.symm ≫ₕ f₁ ≫ₕ (g ≫ₕ g.symm) ≫ₕ f₂ ≫ₕ c' : by simp [F₁, trans_assoc] ... ≈ c.symm ≫ₕ f₁ ≫ₕ (of_set g.source g.open_source) ≫ₕ f₂ ≫ₕ c' : by simp [eq_on_source.trans', trans_self_symm g] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (of_set g.source g.open_source)) ≫ₕ (f₂ ≫ₕ c') : by simp [trans_assoc] ... ≈ ((c.symm ≫ₕ f₁).restr s) ≫ₕ (f₂ ≫ₕ c') : by simp [s, trans_of_set'] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (f₂ ≫ₕ c')).restr s : by simp [restr_trans] ... ≈ (c.symm ≫ₕ (f₁ ≫ₕ f₂) ≫ₕ c').restr s : by simp [eq_on_source.restr, trans_assoc] ... ≈ F₂ : by simp [F₂, feq], have : F₂ ∈ G := G.eq_on_source F₁ F₂ A (setoid.symm this), exact this end, ..homeomorph.trans e.to_homeomorph e'.to_homeomorph } end has_groupoid
e8ec986bc9659009219b4f1eaa81dc0087b42caa
b815abf92ce063fe0d1fabf5b42da483552aa3e8
/library/data/list.lean
c5769bcecdd185886e4cc05f58a3347e258bb180
[ "Apache-2.0" ]
permissive
yodalee/lean
a368d842df12c63e9f79414ed7bbee805b9001ef
317989bf9ef6ae1dec7488c2363dbfcdc16e0756
refs/heads/master
1,610,551,176,860
1,481,430,138,000
1,481,646,441,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,851
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad This is a minimal port of functions from the lean2 list library. -/ import init.data.list.basic import algebra.order import data.nat universe variables u v w namespace list open nat variables {α : Type u} {β : Type v} {φ : Type w} /- length theorems -/ theorem length_append : ∀ (x y : list α), length (x ++ y) = length x + length y | [] l := eq.symm (nat.zero_add (length l)) | (a::s) l := calc nat.succ (length (s ++ l)) = nat.succ (length s + length l) : congr_arg nat.succ (length_append s l) ... = nat.succ (length s) + length l : eq.symm (nat.succ_add (length s) (length l)) theorem length_repeat (a : α) : ∀ (n : ℕ), length (repeat a n) = n | 0 := eq.refl 0 | (succ i) := congr_arg succ (length_repeat i) theorem length_map (f : α → β) : ∀ (a : list α), length (map f a) = length a | [] := rfl | (a :: l) := congr_arg succ (length_map l) theorem length_dropn : ∀ (i : ℕ) (l : list α), length (dropn i l) = length l - i | 0 l := rfl | (succ i) [] := eq.symm (nat.zero_sub_eq_zero (succ i)) | (succ i) (x::l) := calc length (dropn (succ i) (x::l)) = length l - i : length_dropn i l ... = succ (length l) - succ i : nat.sub_eq_succ_sub_succ (length l) i /- firstn -/ def firstn : ℕ → list α → list α | 0 l := [] | (succ n) [] := [] | (succ n) (a::l) := a :: firstn n l theorem length_firstn : ∀ (i : ℕ) (l : list α), length (firstn i l) = min i (length l) | 0 l := eq.symm (nat.min_zero_left (length l)) | (succ n) [] := eq.symm (nat.min_zero_right (succ n)) | (succ n) (a::l) := calc succ (length (firstn n l)) = succ (min n (length l)) : congr_arg succ (length_firstn n l) ... = min (succ n) (succ (length l)) : eq.symm (nat.min_succ_succ n (length l)) /- map₂ -/ definition map₂ {α : Type u} {β : Type v} {φ : Type w} (f : α → β → φ) : list α → list β → list φ | [] l := [] | l [] := [] | (a::s) (b::t) := f a b :: map₂ s t theorem map₂_nil_1 {α : Type u} {β : Type v} {φ : Type w} (f : α → β → φ) : Π y, map₂ f nil y = nil | [] := eq.refl nil | (b::t) := eq.refl nil theorem map₂_nil_2 {α β φ : Type} (f : α → β → φ) : Π (x : list α), map₂ f x nil = nil | [] := eq.refl nil | (b::t) := eq.refl nil theorem length_map₂ {α β φ : Type} (f : α → β → φ) : Π x y, length (map₂ f x y) = min (length x) (length y) | [] y := calc length (map₂ f nil y) = 0 : congr_arg length (map₂_nil_1 f y) ... = min 0 (length y) : eq.symm (nat.min_zero_left (length y)) | x [] := calc length (map₂ f x nil) = 0 : congr_arg length (map₂_nil_2 f x) ... = min (length x) 0 : eq.symm (nat.min_zero_right (length x)) | (a::x) (b::y) := calc succ (length (map₂ f x y)) = succ (min (length x) (length y)) : congr_arg succ (length_map₂ x y) ... = min (succ (length x)) (succ (length y)) : eq.symm (min_succ_succ (length x) (length y)) section map_accumr variable {σ : Type} -- This runs a function over a list returning the intermediate results and a -- a final result. definition map_accumr (f : α → σ → σ × β) : list α → σ → (σ × list β) | [] c := (c, []) | (y::yr) c := let r := map_accumr yr c in let z := f y (prod.fst r) in (prod.fst z, prod.snd z :: prod.snd r) theorem length_map_accumr : ∀ (f : α → σ → σ × β) (x : list α) (s : σ), length (prod.snd (map_accumr f x s)) = length x | f (a::x) s := congr_arg succ (length_map_accumr f x s) | f [] s := rfl end map_accumr section map_accumr₂ -- This runs a function over two lists returning the intermediate results and a -- a final result. definition map_accumr₂ {α β σ φ : Type} (f : α → β → σ → σ × φ) : list α → list β → σ → σ × list φ | [] _ c := (c,[]) | _ [] c := (c,[]) | (x::xr) (y::yr) c := let r := map_accumr₂ xr yr c in let q := f x y (prod.fst r) in (prod.fst q, prod.snd q :: (prod.snd r)) theorem length_map_accumr₂ {α β σ φ : Type} : ∀ (f : α → β → σ → σ × φ) x y c, length (prod.snd (map_accumr₂ f x y c)) = min (length x) (length y) | f (a::x) (b::y) c := calc succ (length (prod.snd (map_accumr₂ f x y c))) = succ (min (length x) (length y)) : congr_arg succ (length_map_accumr₂ f x y c) ... = min (succ (length x)) (succ (length y)) : eq.symm (min_succ_succ (length x) (length y)) | f (a::x) [] c := rfl | f [] (b::y) c := rfl | f [] [] c := rfl end map_accumr₂ end list
4c5f7bb6c0fbe752b62fda9a52692ebb81a2baf6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/padics/padic_integers.lean
d0c66151065c377cece84b61f4798238a2255b2c
[ "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
21,509
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, Mario Carneiro, Johan Commelin -/ import number_theory.padics.padic_numbers import ring_theory.discrete_valuation_ring.basic /-! # p-adic integers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the `p`-adic integers `ℤ_[p]` as the subtype of `ℚ_[p]` with norm `≤ 1`. We show that `ℤ_[p]` * is complete, * is nonarchimedean, * is a normed ring, * is a local ring, and * is a discrete valuation ring. The relation between `ℤ_[p]` and `zmod p` is established in another file. ## Important definitions * `padic_int` : the type of `p`-adic integers ## Notation We introduce the notation `ℤ_[p]` for the `p`-adic integers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact p.prime]` as a type class argument. Coercions into `ℤ_[p]` are set up to work with the `norm_cast` tactic. ## References * [F. Q. Gouvêa, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, p-adic integer -/ open padic metric local_ring noncomputable theory open_locale classical /-- The `p`-adic integers `ℤ_[p]` are the `p`-adic numbers with norm `≤ 1`. -/ def padic_int (p : ℕ) [fact p.prime] := {x : ℚ_[p] // ‖x‖ ≤ 1} notation `ℤ_[`p`]` := padic_int p namespace padic_int /-! ### Ring structure and coercion to `ℚ_[p]` -/ variables {p : ℕ} [fact p.prime] instance : has_coe ℤ_[p] ℚ_[p] := ⟨subtype.val⟩ lemma ext {x y : ℤ_[p]} : (x : ℚ_[p]) = y → x = y := subtype.ext variables (p) /-- The `p`-adic integers as a subring of `ℚ_[p]`. -/ def subring : subring (ℚ_[p]) := { carrier := {x : ℚ_[p] | ‖x‖ ≤ 1}, zero_mem' := by norm_num, one_mem' := by norm_num, add_mem' := λ x y hx hy, (padic_norm_e.nonarchimedean _ _).trans $ max_le_iff.2 ⟨hx, hy⟩, mul_mem' := λ x y hx hy, (padic_norm_e.mul _ _).trans_le $ mul_le_one hx (norm_nonneg _) hy, neg_mem' := λ x hx, (norm_neg _).trans_le hx } @[simp] lemma mem_subring_iff {x : ℚ_[p]} : x ∈ subring p ↔ ‖x‖ ≤ 1 := iff.rfl variables {p} /-- Addition on `ℤ_[p]` is inherited from `ℚ_[p]`. -/ instance : has_add ℤ_[p] := (by apply_instance : has_add (subring p)) /-- Multiplication on `ℤ_[p]` is inherited from `ℚ_[p]`. -/ instance : has_mul ℤ_[p] := (by apply_instance : has_mul (subring p)) /-- Negation on `ℤ_[p]` is inherited from `ℚ_[p]`. -/ instance : has_neg ℤ_[p] := (by apply_instance : has_neg (subring p)) /-- Subtraction on `ℤ_[p]` is inherited from `ℚ_[p]`. -/ instance : has_sub ℤ_[p] := (by apply_instance : has_sub (subring p)) /-- Zero on `ℤ_[p]` is inherited from `ℚ_[p]`. -/ instance : has_zero ℤ_[p] := (by apply_instance : has_zero (subring p)) instance : inhabited ℤ_[p] := ⟨0⟩ /-- One on `ℤ_[p]` is inherited from `ℚ_[p]`. -/ instance : has_one ℤ_[p] := ⟨⟨1, by norm_num⟩⟩ @[simp] lemma mk_zero {h} : (⟨0, h⟩ : ℤ_[p]) = (0 : ℤ_[p]) := rfl @[simp] lemma val_eq_coe (z : ℤ_[p]) : z.val = z := rfl @[simp, norm_cast] lemma coe_add (z1 z2 : ℤ_[p]) : ((z1 + z2 : ℤ_[p]) : ℚ_[p]) = z1 + z2 := rfl @[simp, norm_cast] lemma coe_mul (z1 z2 : ℤ_[p]) : ((z1 * z2 : ℤ_[p]) : ℚ_[p]) = z1 * z2 := rfl @[simp, norm_cast] lemma coe_neg (z1 : ℤ_[p]) : ((-z1 : ℤ_[p]) : ℚ_[p]) = -z1 := rfl @[simp, norm_cast] lemma coe_sub (z1 z2 : ℤ_[p]) : ((z1 - z2 : ℤ_[p]) : ℚ_[p]) = z1 - z2 := rfl @[simp, norm_cast] lemma coe_one : ((1 : ℤ_[p]) : ℚ_[p]) = 1 := rfl @[simp, norm_cast] lemma coe_zero : ((0 : ℤ_[p]) : ℚ_[p]) = 0 := rfl lemma coe_eq_zero (z : ℤ_[p]) : (z : ℚ_[p]) = 0 ↔ z = 0 := by rw [← coe_zero, subtype.coe_inj] lemma coe_ne_zero (z : ℤ_[p]) : (z : ℚ_[p]) ≠ 0 ↔ z ≠ 0 := z.coe_eq_zero.not instance : add_comm_group ℤ_[p] := (by apply_instance : add_comm_group (subring p)) instance : comm_ring ℤ_[p] := (by apply_instance : comm_ring (subring p)) @[simp, norm_cast] lemma coe_nat_cast (n : ℕ) : ((n : ℤ_[p]) : ℚ_[p]) = n := rfl @[simp, norm_cast] lemma coe_int_cast (z : ℤ) : ((z : ℤ_[p]) : ℚ_[p]) = z := rfl /-- The coercion from `ℤ_[p]` to `ℚ_[p]` as a ring homomorphism. -/ def coe.ring_hom : ℤ_[p] →+* ℚ_[p] := (subring p).subtype @[simp, norm_cast] lemma coe_pow (x : ℤ_[p]) (n : ℕ) : (↑(x^n) : ℚ_[p]) = (↑x : ℚ_[p])^n := rfl @[simp] lemma mk_coe (k : ℤ_[p]) : (⟨k, k.2⟩ : ℤ_[p]) = k := subtype.coe_eta _ _ /-- The inverse of a `p`-adic integer with norm equal to `1` is also a `p`-adic integer. Otherwise, the inverse is defined to be `0`. -/ def inv : ℤ_[p] → ℤ_[p] | ⟨k, _⟩ := if h : ‖k‖ = 1 then ⟨k⁻¹, by simp [h]⟩ else 0 instance : char_zero ℤ_[p] := { cast_injective := λ m n h, nat.cast_injective $ show (m:ℚ_[p]) = n, by { rw subtype.ext_iff at h, norm_cast at h, exact h } } @[simp, norm_cast] lemma coe_int_eq (z1 z2 : ℤ) : (z1 : ℤ_[p]) = z2 ↔ z1 = z2 := suffices (z1 : ℚ_[p]) = z2 ↔ z1 = z2, from iff.trans (by norm_cast) this, by norm_cast /-- A sequence of integers that is Cauchy with respect to the `p`-adic norm converges to a `p`-adic integer. -/ def of_int_seq (seq : ℕ → ℤ) (h : is_cau_seq (padic_norm p) (λ n, seq n)) : ℤ_[p] := ⟨⟦⟨_, h⟩⟧, show ↑(padic_seq.norm _) ≤ (1 : ℝ), begin rw padic_seq.norm, split_ifs with hne; norm_cast, { exact zero_le_one }, { apply padic_norm.of_int } end ⟩ end padic_int namespace padic_int /-! ### Instances We now show that `ℤ_[p]` is a * complete metric space * normed ring * integral domain -/ variables (p : ℕ) [fact p.prime] instance : metric_space ℤ_[p] := subtype.metric_space instance complete_space : complete_space ℤ_[p] := have is_closed {x : ℚ_[p] | ‖x‖ ≤ 1}, from is_closed_le continuous_norm continuous_const, this.complete_space_coe instance : has_norm ℤ_[p] := ⟨λ z, ‖(z : ℚ_[p])‖⟩ variables {p} lemma norm_def {z : ℤ_[p]} : ‖z‖ = ‖(z : ℚ_[p])‖ := rfl variables (p) instance : normed_comm_ring ℤ_[p] := { dist_eq := λ ⟨_, _⟩ ⟨_, _⟩, rfl, norm_mul := by simp [norm_def], norm := norm, .. padic_int.comm_ring, .. padic_int.metric_space p } instance : norm_one_class ℤ_[p] := ⟨norm_def.trans norm_one⟩ instance is_absolute_value : is_absolute_value (λ z : ℤ_[p], ‖z‖) := { abv_nonneg := norm_nonneg, abv_eq_zero := λ ⟨_, _⟩, by simp [norm_eq_zero], abv_add := λ ⟨_,_⟩ ⟨_, _⟩, norm_add_le _ _, abv_mul := λ _ _, by simp only [norm_def, padic_norm_e.mul, padic_int.coe_mul] } variables {p} instance : is_domain ℤ_[p] := function.injective.is_domain (subring p).subtype subtype.coe_injective end padic_int namespace padic_int /-! ### Norm -/ variables {p : ℕ} [fact p.prime] lemma norm_le_one (z : ℤ_[p]) : ‖z‖ ≤ 1 := z.2 @[simp] lemma norm_mul (z1 z2 : ℤ_[p]) : ‖z1 * z2‖ = ‖z1‖ * ‖z2‖ := by simp [norm_def] @[simp] lemma norm_pow (z : ℤ_[p]) : ∀ n : ℕ, ‖z ^ n‖ = ‖z‖ ^ n | 0 := by simp | (k + 1) := by { rw [pow_succ, pow_succ, norm_mul], congr, apply norm_pow } theorem nonarchimedean (q r : ℤ_[p]) : ‖q + r‖ ≤ max (‖q‖) (‖r‖) := padic_norm_e.nonarchimedean _ _ theorem norm_add_eq_max_of_ne {q r : ℤ_[p]} : ‖q‖ ≠ ‖r‖ → ‖q+r‖ = max (‖q‖) (‖r‖) := padic_norm_e.add_eq_max_of_ne lemma norm_eq_of_norm_add_lt_right {z1 z2 : ℤ_[p]} (h : ‖z1 + z2‖ < ‖z2‖) : ‖z1‖ = ‖z2‖ := by_contradiction $ λ hne, not_lt_of_ge (by rw norm_add_eq_max_of_ne hne; apply le_max_right) h lemma norm_eq_of_norm_add_lt_left {z1 z2 : ℤ_[p]} (h : ‖z1 + z2‖ < ‖z1‖) : ‖z1‖ = ‖z2‖ := by_contradiction $ λ hne, not_lt_of_ge (by rw norm_add_eq_max_of_ne hne; apply le_max_left) h @[simp] lemma padic_norm_e_of_padic_int (z : ℤ_[p]) : ‖(z : ℚ_[p])‖ = ‖z‖ := by simp [norm_def] lemma norm_int_cast_eq_padic_norm (z : ℤ) : ‖(z : ℤ_[p])‖ = ‖(z : ℚ_[p])‖ := by simp [norm_def] @[simp] lemma norm_eq_padic_norm {q : ℚ_[p]} (hq : ‖q‖ ≤ 1) : @norm ℤ_[p] _ ⟨q, hq⟩ = ‖q‖ := rfl @[simp] lemma norm_p : ‖(p : ℤ_[p])‖ = p⁻¹ := padic_norm_e.norm_p @[simp] lemma norm_p_pow (n : ℕ) : ‖(p : ℤ_[p])^n‖ = p^(-n:ℤ) := padic_norm_e.norm_p_pow n private def cau_seq_to_rat_cau_seq (f : cau_seq ℤ_[p] norm) : cau_seq ℚ_[p] (λ a, ‖a‖) := ⟨ λ n, f n, λ _ hε, by simpa [norm, norm_def] using f.cauchy hε ⟩ variables (p) instance complete : cau_seq.is_complete ℤ_[p] norm := ⟨ λ f, have hqn : ‖cau_seq.lim (cau_seq_to_rat_cau_seq f)‖ ≤ 1, from padic_norm_e_lim_le zero_lt_one (λ _, norm_le_one _), ⟨⟨_, hqn⟩, λ ε, by simpa [norm, norm_def] using cau_seq.equiv_lim (cau_seq_to_rat_cau_seq f) ε⟩⟩ end padic_int namespace padic_int variables (p : ℕ) [hp : fact p.prime] include hp lemma exists_pow_neg_lt {ε : ℝ} (hε : 0 < ε) : ∃ k : ℕ, ↑p ^ -(k : ℤ) < ε := begin obtain ⟨k, hk⟩ := exists_nat_gt ε⁻¹, use k, rw ← inv_lt_inv hε (_root_.zpow_pos_of_pos _ _), { rw [zpow_neg, inv_inv, zpow_coe_nat], apply lt_of_lt_of_le hk, norm_cast, apply le_of_lt, convert nat.lt_pow_self _ _ using 1, exact hp.1.one_lt }, { exact_mod_cast hp.1.pos } end lemma exists_pow_neg_lt_rat {ε : ℚ} (hε : 0 < ε) : ∃ k : ℕ, ↑p ^ -(k : ℤ) < ε := begin obtain ⟨k, hk⟩ := @exists_pow_neg_lt p _ ε (by exact_mod_cast hε), use k, rw (show (p : ℝ) = (p : ℚ), by simp) at hk, exact_mod_cast hk end variable {p} lemma norm_int_lt_one_iff_dvd (k : ℤ) : ‖(k : ℤ_[p])‖ < 1 ↔ (p : ℤ) ∣ k := suffices ‖(k : ℚ_[p])‖ < 1 ↔ ↑p ∣ k, by rwa norm_int_cast_eq_padic_norm, padic_norm_e.norm_int_lt_one_iff_dvd k lemma norm_int_le_pow_iff_dvd {k : ℤ} {n : ℕ} : ‖(k : ℤ_[p])‖ ≤ p ^ (-n : ℤ) ↔ (p ^ n : ℤ) ∣ k := suffices ‖(k : ℚ_[p])‖ ≤ p ^ (-n : ℤ) ↔ ↑(p ^ n) ∣ k, by simpa [norm_int_cast_eq_padic_norm], padic_norm_e.norm_int_le_pow_iff_dvd _ _ /-! ### Valuation on `ℤ_[p]` -/ /-- `padic_int.valuation` lifts the `p`-adic valuation on `ℚ` to `ℤ_[p]`. -/ def valuation (x : ℤ_[p]) := padic.valuation (x : ℚ_[p]) lemma norm_eq_pow_val {x : ℤ_[p]} (hx : x ≠ 0) : ‖x‖ = (p : ℝ) ^ -x.valuation := begin convert padic.norm_eq_pow_val _, contrapose! hx, exact subtype.val_injective hx end @[simp] lemma valuation_zero : valuation (0 : ℤ_[p]) = 0 := padic.valuation_zero @[simp] lemma valuation_one : valuation (1 : ℤ_[p]) = 0 := padic.valuation_one @[simp] lemma valuation_p : valuation (p : ℤ_[p]) = 1 := by simp [valuation] lemma valuation_nonneg (x : ℤ_[p]) : 0 ≤ x.valuation := begin by_cases hx : x = 0, { simp [hx] }, have h : (1 : ℝ) < p := by exact_mod_cast hp.1.one_lt, rw [← neg_nonpos, ← (zpow_strict_mono h).le_iff_le], show (p : ℝ) ^ -valuation x ≤ p ^ (0 : ℤ), rw [← norm_eq_pow_val hx], simpa using x.property end @[simp] lemma valuation_p_pow_mul (n : ℕ) (c : ℤ_[p]) (hc : c ≠ 0) : (↑p ^ n * c).valuation = n + c.valuation := begin have : ‖(↑p ^ n * c)‖ = ‖(p ^ n : ℤ_[p])‖ * ‖c‖, { exact norm_mul _ _ }, have aux : (↑p ^ n * c) ≠ 0, { contrapose! hc, rw mul_eq_zero at hc, cases hc, { refine (hp.1.ne_zero _).elim, exact_mod_cast (pow_eq_zero hc) }, { exact hc } }, rwa [norm_eq_pow_val aux, norm_p_pow, norm_eq_pow_val hc, ← zpow_add₀, ← neg_add, zpow_inj, neg_inj] at this, { exact_mod_cast hp.1.pos }, { exact_mod_cast hp.1.ne_one }, { exact_mod_cast hp.1.ne_zero } end section units /-! ### Units of `ℤ_[p]` -/ local attribute [reducible] padic_int lemma mul_inv : ∀ {z : ℤ_[p]}, ‖z‖ = 1 → z * z.inv = 1 | ⟨k, _⟩ h := begin have hk : k ≠ 0, from λ h', zero_ne_one' ℚ_[p] (by simpa [h'] using h), unfold padic_int.inv, rw [norm_eq_padic_norm] at h, rw dif_pos h, apply subtype.ext_iff_val.2, simp [mul_inv_cancel hk] end lemma inv_mul {z : ℤ_[p]} (hz : ‖z‖ = 1) : z.inv * z = 1 := by rw [mul_comm, mul_inv hz] lemma is_unit_iff {z : ℤ_[p]} : is_unit z ↔ ‖z‖ = 1 := ⟨λ h, begin rcases is_unit_iff_dvd_one.1 h with ⟨w, eq⟩, refine le_antisymm (norm_le_one _) _, have := mul_le_mul_of_nonneg_left (norm_le_one w) (norm_nonneg z), rwa [mul_one, ← norm_mul, ← eq, norm_one] at this end, λ h, ⟨⟨z, z.inv, mul_inv h, inv_mul h⟩, rfl⟩⟩ lemma norm_lt_one_add {z1 z2 : ℤ_[p]} (hz1 : ‖z1‖ < 1) (hz2 : ‖z2‖ < 1) : ‖z1 + z2‖ < 1 := lt_of_le_of_lt (nonarchimedean _ _) (max_lt hz1 hz2) lemma norm_lt_one_mul {z1 z2 : ℤ_[p]} (hz2 : ‖z2‖ < 1) : ‖z1 * z2‖ < 1 := calc ‖z1 * z2‖ = ‖z1‖ * ‖z2‖ : by simp ... < 1 : mul_lt_one_of_nonneg_of_lt_one_right (norm_le_one _) (norm_nonneg _) hz2 @[simp] lemma mem_nonunits {z : ℤ_[p]} : z ∈ nonunits ℤ_[p] ↔ ‖z‖ < 1 := by rw lt_iff_le_and_ne; simp [norm_le_one z, nonunits, is_unit_iff] /-- A `p`-adic number `u` with `‖u‖ = 1` is a unit of `ℤ_[p]`. -/ def mk_units {u : ℚ_[p]} (h : ‖u‖ = 1) : ℤ_[p]ˣ := let z : ℤ_[p] := ⟨u, le_of_eq h⟩ in ⟨z, z.inv, mul_inv h, inv_mul h⟩ @[simp] lemma mk_units_eq {u : ℚ_[p]} (h : ‖u‖ = 1) : ((mk_units h : ℤ_[p]) : ℚ_[p]) = u := rfl @[simp] lemma norm_units (u : ℤ_[p]ˣ) : ‖(u : ℤ_[p])‖ = 1 := is_unit_iff.mp $ by simp /-- `unit_coeff hx` is the unit `u` in the unique representation `x = u * p ^ n`. See `unit_coeff_spec`. -/ def unit_coeff {x : ℤ_[p]} (hx : x ≠ 0) : ℤ_[p]ˣ := let u : ℚ_[p] := x * p ^ -x.valuation in have hu : ‖u‖ = 1, by simp [hx, nat.zpow_ne_zero_of_pos (by exact_mod_cast hp.1.pos) x.valuation, norm_eq_pow_val, zpow_neg, inv_mul_cancel], mk_units hu @[simp] lemma unit_coeff_coe {x : ℤ_[p]} (hx : x ≠ 0) : (unit_coeff hx : ℚ_[p]) = x * p ^ -x.valuation := rfl lemma unit_coeff_spec {x : ℤ_[p]} (hx : x ≠ 0) : x = (unit_coeff hx : ℤ_[p]) * p ^ int.nat_abs (valuation x) := begin apply subtype.coe_injective, push_cast, have repr : (x : ℚ_[p]) = (unit_coeff hx) * p ^ x.valuation, { rw [unit_coeff_coe, mul_assoc, ← zpow_add₀], { simp }, { exact_mod_cast hp.1.ne_zero } }, convert repr using 2, rw [← zpow_coe_nat, int.nat_abs_of_nonneg (valuation_nonneg x)] end end units section norm_le_iff /-! ### Various characterizations of open unit balls -/ lemma norm_le_pow_iff_le_valuation (x : ℤ_[p]) (hx : x ≠ 0) (n : ℕ) : ‖x‖ ≤ p ^ (-n : ℤ) ↔ ↑n ≤ x.valuation := begin rw norm_eq_pow_val hx, lift x.valuation to ℕ using x.valuation_nonneg with k hk, simp only [int.coe_nat_le, zpow_neg, zpow_coe_nat], have aux : ∀ n : ℕ, 0 < (p ^ n : ℝ), { apply pow_pos, exact_mod_cast hp.1.pos }, rw [inv_le_inv (aux _) (aux _)], have : p ^ n ≤ p ^ k ↔ n ≤ k := (pow_strict_mono_right hp.1.one_lt).le_iff_le, rw [← this], norm_cast end lemma mem_span_pow_iff_le_valuation (x : ℤ_[p]) (hx : x ≠ 0) (n : ℕ) : x ∈ (ideal.span {p ^ n} : ideal ℤ_[p]) ↔ ↑n ≤ x.valuation := begin rw [ideal.mem_span_singleton], split, { rintro ⟨c, rfl⟩, suffices : c ≠ 0, { rw [valuation_p_pow_mul _ _ this, le_add_iff_nonneg_right], apply valuation_nonneg }, contrapose! hx, rw [hx, mul_zero] }, { rw [unit_coeff_spec hx] { occs := occurrences.pos [2] }, lift x.valuation to ℕ using x.valuation_nonneg with k hk, simp only [int.nat_abs_of_nat, units.is_unit, is_unit.dvd_mul_left, int.coe_nat_le], intro H, obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le H, simp only [pow_add, dvd_mul_right] } end lemma norm_le_pow_iff_mem_span_pow (x : ℤ_[p]) (n : ℕ) : ‖x‖ ≤ p ^ (-n : ℤ) ↔ x ∈ (ideal.span {p ^ n} : ideal ℤ_[p]) := begin by_cases hx : x = 0, { subst hx, simp only [norm_zero, zpow_neg, zpow_coe_nat, inv_nonneg, iff_true, submodule.zero_mem], exact_mod_cast nat.zero_le _ }, rw [norm_le_pow_iff_le_valuation x hx, mem_span_pow_iff_le_valuation x hx] end lemma norm_le_pow_iff_norm_lt_pow_add_one (x : ℤ_[p]) (n : ℤ) : ‖x‖ ≤ p ^ n ↔ ‖x‖ < p ^ (n + 1) := begin rw norm_def, exact padic.norm_le_pow_iff_norm_lt_pow_add_one _ _, end lemma norm_lt_pow_iff_norm_le_pow_sub_one (x : ℤ_[p]) (n : ℤ) : ‖x‖ < p ^ n ↔ ‖x‖ ≤ p ^ (n - 1) := by rw [norm_le_pow_iff_norm_lt_pow_add_one, sub_add_cancel] lemma norm_lt_one_iff_dvd (x : ℤ_[p]) : ‖x‖ < 1 ↔ ↑p ∣ x := begin have := norm_le_pow_iff_mem_span_pow x 1, rw [ideal.mem_span_singleton, pow_one] at this, rw [← this, norm_le_pow_iff_norm_lt_pow_add_one], simp only [zpow_zero, int.coe_nat_zero, int.coe_nat_succ, add_left_neg, zero_add] end @[simp] lemma pow_p_dvd_int_iff (n : ℕ) (a : ℤ) : (p ^ n : ℤ_[p]) ∣ a ↔ ↑p ^ n ∣ a := by rw [← norm_int_le_pow_iff_dvd, norm_le_pow_iff_mem_span_pow, ideal.mem_span_singleton] end norm_le_iff section dvr /-! ### Discrete valuation ring -/ instance : local_ring ℤ_[p] := local_ring.of_nonunits_add $ by simp only [mem_nonunits]; exact λ x y, norm_lt_one_add lemma p_nonnunit : (p : ℤ_[p]) ∈ nonunits ℤ_[p] := have (p : ℝ)⁻¹ < 1, from inv_lt_one $ by exact_mod_cast hp.1.one_lt, by simp [this] lemma maximal_ideal_eq_span_p : maximal_ideal ℤ_[p] = ideal.span {p} := begin apply le_antisymm, { intros x hx, simp only [local_ring.mem_maximal_ideal, mem_nonunits] at hx, rwa [ideal.mem_span_singleton, ← norm_lt_one_iff_dvd] }, { rw [ideal.span_le, set.singleton_subset_iff], exact p_nonnunit } end lemma prime_p : prime (p : ℤ_[p]) := begin rw [← ideal.span_singleton_prime, ← maximal_ideal_eq_span_p], { apply_instance }, { exact_mod_cast hp.1.ne_zero } end lemma irreducible_p : irreducible (p : ℤ_[p]) := prime.irreducible prime_p instance : discrete_valuation_ring ℤ_[p] := discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization ⟨p, irreducible_p, λ x hx, ⟨x.valuation.nat_abs, unit_coeff hx, by rw [mul_comm, ← unit_coeff_spec hx]⟩⟩ lemma ideal_eq_span_pow_p {s : ideal ℤ_[p]} (hs : s ≠ ⊥) : ∃ n : ℕ, s = ideal.span {p ^ n} := discrete_valuation_ring.ideal_eq_span_pow_irreducible hs irreducible_p open cau_seq instance : is_adic_complete (maximal_ideal ℤ_[p]) ℤ_[p] := { prec' := λ x hx, begin simp only [← ideal.one_eq_top, smul_eq_mul, mul_one, smodeq.sub_mem, maximal_ideal_eq_span_p, ideal.span_singleton_pow, ← norm_le_pow_iff_mem_span_pow] at hx ⊢, let x' : cau_seq ℤ_[p] norm := ⟨x, _⟩, swap, { intros ε hε, obtain ⟨m, hm⟩ := exists_pow_neg_lt p hε, refine ⟨m, λ n hn, lt_of_le_of_lt _ hm⟩, rw [← neg_sub, norm_neg], exact hx hn }, { refine ⟨x'.lim, λ n, _⟩, have : (0:ℝ) < p ^ (-n : ℤ), { apply zpow_pos_of_pos, exact_mod_cast hp.1.pos }, obtain ⟨i, hi⟩ := equiv_def₃ (equiv_lim x') this, by_cases hin : i ≤ n, { exact (hi i le_rfl n hin).le }, { push_neg at hin, specialize hi i le_rfl i le_rfl, specialize hx hin.le, have := nonarchimedean (x n - x i) (x i - x'.lim), rw [sub_add_sub_cancel] at this, refine this.trans (max_le_iff.mpr ⟨hx, hi.le⟩) } } end } end dvr section fraction_ring instance algebra : algebra ℤ_[p] ℚ_[p] := algebra.of_subring (subring p) @[simp] lemma algebra_map_apply (x : ℤ_[p]) : algebra_map ℤ_[p] ℚ_[p] x = x := rfl instance is_fraction_ring : is_fraction_ring ℤ_[p] ℚ_[p] := { map_units := λ ⟨x, hx⟩, by rwa [set_like.coe_mk, algebra_map_apply, is_unit_iff_ne_zero, padic_int.coe_ne_zero, ←mem_non_zero_divisors_iff_ne_zero], surj := λ x, begin by_cases hx : ‖ x ‖ ≤ 1, { use (⟨x, hx⟩, 1), rw [submonoid.coe_one, map_one, mul_one, padic_int.algebra_map_apply, subtype.coe_mk] }, { set n := int.to_nat(- x.valuation) with hn, have hn_coe : (n : ℤ) = -x.valuation, { rw [hn, int.to_nat_of_nonneg], rw right.nonneg_neg_iff, rw [padic.norm_le_one_iff_val_nonneg, not_le] at hx, exact hx.le }, set a := x * p^n with ha, have ha_norm : ‖ a ‖ = 1, { have hx : x ≠ 0, { intro h0, rw [h0, norm_zero] at hx, exact hx (zero_le_one) }, rw [ha, padic_norm_e.mul, padic_norm_e.norm_p_pow, padic.norm_eq_pow_val hx, ← zpow_add', hn_coe, neg_neg, add_left_neg, zpow_zero], exact or.inl (nat.cast_ne_zero.mpr (ne_zero.ne p)), }, use (⟨a, le_of_eq ha_norm⟩, ⟨(p^n : ℤ_[p]), mem_non_zero_divisors_iff_ne_zero.mpr (ne_zero.ne _)⟩), simp only [set_like.coe_mk, map_pow, map_nat_cast, algebra_map_apply, padic_int.coe_pow, padic_int.coe_nat_cast, subtype.coe_mk] } end, eq_iff_exists := λ x y, begin rw [algebra_map_apply, algebra_map_apply, subtype.coe_inj], refine ⟨λ h, ⟨1, by rw h⟩, _⟩, rintro ⟨⟨c, hc⟩, h⟩, exact (mul_eq_mul_left_iff.mp h).resolve_right (mem_non_zero_divisors_iff_ne_zero.mp hc) end } end fraction_ring end padic_int
02f4a8c5bcc33166158589d60968e22d540a559e
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/integral/integral_eq_improper.lean
a130b77a0e62e9c1da2d413bb5afa29f1817a254
[ "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
21,918
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import measure_theory.integral.interval_integral import order.filter.at_top_bot /-! # Links between an integral and its "improper" version In its current state, mathlib only knows how to talk about definite ("proper") integrals, in the sense that it treats integrals over `[x, +∞)` the same as it treats integrals over `[y, z]`. For example, the integral over `[1, +∞)` is **not** defined to be the limit of the integral over `[1, x]` as `x` tends to `+∞`, which is known as an **improper integral**. Indeed, the "proper" definition is stronger than the "improper" one. The usual counterexample is `x ↦ sin(x)/x`, which has an improper integral over `[1, +∞)` but no definite integral. Although definite integrals have better properties, they are hardly usable when it comes to computing integrals on unbounded sets, which is much easier using limits. Thus, in this file, we prove various ways of studying the proper integral by studying the improper one. ## Definitions The main definition of this file is `measure_theory.ae_cover`. It is a rather technical definition whose sole purpose is generalizing and factoring proofs. Given an index type `ι`, a countably generated filter `l` over `ι`, and an `ι`-indexed family `φ` of subsets of a measurable space `α` equipped with a measure `μ`, one should think of a hypothesis `hφ : ae_cover μ l φ` as a sufficient condition for being able to interpret `∫ x, f x ∂μ` (if it exists) as the limit of `∫ x in φ i, f x ∂μ` as `i` tends to `l`. When using this definition with a measure restricted to a set `s`, which happens fairly often, one should not try too hard to use a `ae_cover` of subsets of `s`, as it often makes proofs more complicated than necessary. See for example the proof of `measure_theory.integrable_on_Iic_of_interval_integral_norm_tendsto` where we use `(λ x, Ioi x)` as an `ae_cover` w.r.t. `μ.restrict (Iic b)`, instead of using `(λ x, Ioc x b)`. ## Main statements - `measure_theory.ae_cover.lintegral_tendsto_of_countably_generated` : if `φ` is a `ae_cover μ l`, where `l` is a countably generated filter, and if `f` is a measurable `ennreal`-valued function, then `∫⁻ x in φ n, f x ∂μ` tends to `∫⁻ x, f x ∂μ` as `n` tends to `l` - `measure_theory.ae_cover.integrable_of_integral_norm_tendsto` : if `φ` is a `ae_cover μ l`, where `l` is a countably generated filter, if `f` is measurable and integrable on each `φ n`, and if `∫ x in φ n, ∥f x∥ ∂μ` tends to some `I : ℝ` as n tends to `l`, then `f` is integrable - `measure_theory.ae_cover.integral_tendsto_of_countably_generated` : if `φ` is a `ae_cover μ l`, where `l` is a countably generated filter, and if `f` is measurable and integrable (globally), then `∫ x in φ n, f x ∂μ` tends to `∫ x, f x ∂μ` as `n` tends to `+∞`. We then specialize these lemmas to various use cases involving intervals, which are frequent in analysis. -/ open measure_theory filter set topological_space open_locale ennreal nnreal topological_space namespace measure_theory section ae_cover variables {α ι : Type*} [measurable_space α] (μ : measure α) (l : filter ι) /-- A sequence `φ` of subsets of `α` is a `ae_cover` w.r.t. a measure `μ` and a filter `l` if almost every point (w.r.t. `μ`) of `α` eventually belongs to `φ n` (w.r.t. `l`), and if each `φ n` is measurable. This definition is a technical way to avoid duplicating a lot of proofs. It should be thought of as a sufficient condition for being able to interpret `∫ x, f x ∂μ` (if it exists) as the limit of `∫ x in φ n, f x ∂μ` as `n` tends to `l`. See for example `measure_theory.ae_cover.lintegral_tendsto_of_countably_generated`, `measure_theory.ae_cover.integrable_of_integral_norm_tendsto` and `measure_theory.ae_cover.integral_tendsto_of_countably_generated`. -/ structure ae_cover (φ : ι → set α) : Prop := (ae_eventually_mem : ∀ᵐ x ∂μ, ∀ᶠ i in l, x ∈ φ i) (measurable : ∀ i, measurable_set $ φ i) variables {μ} {l} section preorder_α variables [preorder α] [topological_space α] [order_closed_topology α] [opens_measurable_space α] {a b : ι → α} (ha : tendsto a l at_bot) (hb : tendsto b l at_top) lemma ae_cover_Icc : ae_cover μ l (λ i, Icc (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_le_at_bot x).mp $ (hb.eventually $ eventually_ge_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Icc } lemma ae_cover_Ici : ae_cover μ l (λ i, Ici $ a i) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_le_at_bot x).mono $ λ i hai, hai ), measurable := λ i, measurable_set_Ici } lemma ae_cover_Iic : ae_cover μ l (λ i, Iic $ b i) := { ae_eventually_mem := ae_of_all μ (λ x, (hb.eventually $ eventually_ge_at_top x).mono $ λ i hbi, hbi ), measurable := λ i, measurable_set_Iic } end preorder_α section linear_order_α variables [linear_order α] [topological_space α] [order_closed_topology α] [opens_measurable_space α] {a b : ι → α} (ha : tendsto a l at_bot) (hb : tendsto b l at_top) lemma ae_cover_Ioo [no_bot_order α] [no_top_order α] : ae_cover μ l (λ i, Ioo (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_lt_at_bot x).mp $ (hb.eventually $ eventually_gt_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Ioo } lemma ae_cover_Ioc [no_bot_order α] : ae_cover μ l (λ i, Ioc (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_lt_at_bot x).mp $ (hb.eventually $ eventually_ge_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Ioc } lemma ae_cover_Ico [no_top_order α] : ae_cover μ l (λ i, Ico (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_le_at_bot x).mp $ (hb.eventually $ eventually_gt_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Ico } lemma ae_cover_Ioi [no_bot_order α] : ae_cover μ l (λ i, Ioi $ a i) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_lt_at_bot x).mono $ λ i hai, hai ), measurable := λ i, measurable_set_Ioi } lemma ae_cover_Iio [no_top_order α] : ae_cover μ l (λ i, Iio $ b i) := { ae_eventually_mem := ae_of_all μ (λ x, (hb.eventually $ eventually_gt_at_top x).mono $ λ i hbi, hbi ), measurable := λ i, measurable_set_Iio } end linear_order_α lemma ae_cover.restrict {φ : ι → set α} (hφ : ae_cover μ l φ) {s : set α} : ae_cover (μ.restrict s) l φ := { ae_eventually_mem := ae_restrict_of_ae hφ.ae_eventually_mem, measurable := hφ.measurable } lemma ae_cover_restrict_of_ae_imp {s : set α} {φ : ι → set α} (hs : measurable_set s) (ae_eventually_mem : ∀ᵐ x ∂μ, x ∈ s → ∀ᶠ n in l, x ∈ φ n) (measurable : ∀ n, measurable_set $ φ n) : ae_cover (μ.restrict s) l φ := { ae_eventually_mem := by rwa ae_restrict_iff' hs, measurable := measurable } lemma ae_cover.inter_restrict {φ : ι → set α} (hφ : ae_cover μ l φ) {s : set α} (hs : measurable_set s) : ae_cover (μ.restrict s) l (λ i, φ i ∩ s) := ae_cover_restrict_of_ae_imp hs (hφ.ae_eventually_mem.mono (λ x hx hxs, hx.mono $ λ i hi, ⟨hi, hxs⟩)) (λ i, (hφ.measurable i).inter hs) lemma ae_cover.ae_tendsto_indicator {β : Type*} [has_zero β] [topological_space β] {f : α → β} {φ : ι → set α} (hφ : ae_cover μ l φ) : ∀ᵐ x ∂μ, tendsto (λ i, (φ i).indicator f x) l (𝓝 $ f x) := hφ.ae_eventually_mem.mono (λ x hx, tendsto_const_nhds.congr' $ hx.mono $ λ n hn, (indicator_of_mem hn _).symm) end ae_cover lemma ae_cover.comp_tendsto {α ι ι' : Type*} [measurable_space α] {μ : measure α} {l : filter ι} {l' : filter ι'} {φ : ι → set α} (hφ : ae_cover μ l φ) {u : ι' → ι} (hu : tendsto u l' l) : ae_cover μ l' (φ ∘ u) := { ae_eventually_mem := hφ.ae_eventually_mem.mono (λ x hx, hu.eventually hx), measurable := λ i, hφ.measurable (u i) } section ae_cover_Union_Inter_encodable variables {α ι : Type*} [encodable ι] [measurable_space α] {μ : measure α} lemma ae_cover.bUnion_Iic_ae_cover [preorder ι] {φ : ι → set α} (hφ : ae_cover μ at_top φ) : ae_cover μ at_top (λ (n : ι), ⋃ k (h : k ∈ Iic n), φ k) := { ae_eventually_mem := hφ.ae_eventually_mem.mono (λ x h, h.mono (λ i hi, mem_bUnion right_mem_Iic hi)), measurable := λ i, measurable_set.bUnion (countable_encodable _) (λ n _, hφ.measurable n) } lemma ae_cover.bInter_Ici_ae_cover [semilattice_sup ι] [nonempty ι] {φ : ι → set α} (hφ : ae_cover μ at_top φ) : ae_cover μ at_top (λ (n : ι), ⋂ k (h : k ∈ Ici n), φ k) := { ae_eventually_mem := hφ.ae_eventually_mem.mono begin intros x h, rw eventually_at_top at *, rcases h with ⟨i, hi⟩, use i, intros j hj, exact mem_bInter (λ k hk, hi k (le_trans hj hk)), end, measurable := λ i, measurable_set.bInter (countable_encodable _) (λ n _, hφ.measurable n) } end ae_cover_Union_Inter_encodable section lintegral variables {α ι : Type*} [measurable_space α] {μ : measure α} {l : filter ι} private lemma lintegral_tendsto_of_monotone_of_nat {φ : ℕ → set α} (hφ : ae_cover μ at_top φ) (hmono : monotone φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) at_top (𝓝 $ ∫⁻ x, f x ∂μ) := let F := λ n, (φ n).indicator f in have key₁ : ∀ n, ae_measurable (F n) μ, from λ n, hfm.indicator (hφ.measurable n), have key₂ : ∀ᵐ (x : α) ∂μ, monotone (λ n, F n x), from ae_of_all _ (λ x i j hij, indicator_le_indicator_of_subset (hmono hij) (λ x, zero_le $ f x) x), have key₃ : ∀ᵐ (x : α) ∂μ, tendsto (λ n, F n x) at_top (𝓝 (f x)), from hφ.ae_tendsto_indicator, (lintegral_tendsto_of_tendsto_of_monotone key₁ key₂ key₃).congr (λ n, lintegral_indicator f (hφ.measurable n)) lemma ae_cover.lintegral_tendsto_of_nat {φ : ℕ → set α} (hφ : ae_cover μ at_top φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) at_top (𝓝 $ ∫⁻ x, f x ∂μ) := begin have lim₁ := lintegral_tendsto_of_monotone_of_nat (hφ.bInter_Ici_ae_cover) (λ i j hij, bInter_subset_bInter_left (Ici_subset_Ici.mpr hij)) hfm, have lim₂ := lintegral_tendsto_of_monotone_of_nat (hφ.bUnion_Iic_ae_cover) (λ i j hij, bUnion_subset_bUnion_left (Iic_subset_Iic.mpr hij)) hfm, have le₁ := λ n, lintegral_mono_set (bInter_subset_of_mem left_mem_Ici), have le₂ := λ n, lintegral_mono_set (subset_bUnion_of_mem right_mem_Iic), exact tendsto_of_tendsto_of_tendsto_of_le_of_le lim₁ lim₂ le₁ le₂ end lemma ae_cover.lintegral_tendsto_of_countably_generated [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) l (𝓝 $ ∫⁻ x, f x ∂μ) := tendsto_of_seq_tendsto (λ u hu, (hφ.comp_tendsto hu).lintegral_tendsto_of_nat hfm) lemma ae_cover.lintegral_eq_of_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ≥0∞} (I : ℝ≥0∞) (hfm : ae_measurable f μ) (htendsto : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) l (𝓝 I)) : ∫⁻ x, f x ∂μ = I := tendsto_nhds_unique (hφ.lintegral_tendsto_of_countably_generated hfm) htendsto lemma ae_cover.supr_lintegral_eq_of_countably_generated [nonempty ι] [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : (⨆ (i : ι), ∫⁻ x in φ i, f x ∂μ) = ∫⁻ x, f x ∂μ := begin have := hφ.lintegral_tendsto_of_countably_generated hfm, refine csupr_eq_of_forall_le_of_forall_lt_exists_gt (λ i, lintegral_mono' measure.restrict_le_self (le_refl _)) (λ w hw, _), rcases exists_between hw with ⟨m, hm₁, hm₂⟩, rcases (eventually_ge_of_tendsto_gt hm₂ this).exists with ⟨i, hi⟩, exact ⟨i, lt_of_lt_of_le hm₁ hi⟩, end end lintegral section integrable variables {α ι E : Type*} [measurable_space α] {μ : measure α} {l : filter ι} [normed_group E] [measurable_space E] [opens_measurable_space E] lemma ae_cover.integrable_of_lintegral_nnnorm_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ) (hfm : ae_measurable f μ) (htendsto : tendsto (λ i, ∫⁻ x in φ i, nnnorm (f x) ∂μ) l (𝓝 $ ennreal.of_real I)) : integrable f μ := begin refine ⟨hfm, _⟩, unfold has_finite_integral, rw hφ.lintegral_eq_of_tendsto _ (measurable_nnnorm.comp_ae_measurable hfm).coe_nnreal_ennreal htendsto, exact ennreal.of_real_lt_top end lemma ae_cover.integrable_of_lintegral_nnnorm_tendsto' [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ≥0) (hfm : ae_measurable f μ) (htendsto : tendsto (λ i, ∫⁻ x in φ i, nnnorm (f x) ∂μ) l (𝓝 $ ennreal.of_real I)) : integrable f μ := hφ.integrable_of_lintegral_nnnorm_tendsto I hfm htendsto lemma ae_cover.integrable_of_integral_norm_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ) (hfm : ae_measurable f μ) (hfi : ∀ i, integrable_on f (φ i) μ) (htendsto : tendsto (λ i, ∫ x in φ i, ∥f x∥ ∂μ) l (𝓝 I)) : integrable f μ := begin refine hφ.integrable_of_lintegral_nnnorm_tendsto I hfm _, conv at htendsto in (integral _ _) { rw integral_eq_lintegral_of_nonneg_ae (ae_of_all _ (λ x, @norm_nonneg E _ (f x))) hfm.norm.restrict }, conv at htendsto in (ennreal.of_real _) { dsimp, rw ← coe_nnnorm, rw ennreal.of_real_coe_nnreal }, convert ennreal.tendsto_of_real htendsto, ext i : 1, rw ennreal.of_real_to_real _, exact ne_top_of_lt (hfi i).2 end lemma ae_cover.integrable_of_integral_tendsto_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ} (I : ℝ) (hfm : ae_measurable f μ) (hfi : ∀ i, integrable_on f (φ i) μ) (hnng : ∀ᵐ x ∂μ, 0 ≤ f x) (htendsto : tendsto (λ i, ∫ x in φ i, f x ∂μ) l (𝓝 I)) : integrable f μ := hφ.integrable_of_integral_norm_tendsto I hfm hfi (htendsto.congr $ λ i, integral_congr_ae $ ae_restrict_of_ae $ hnng.mono $ λ x hx, (real.norm_of_nonneg hx).symm) end integrable section integral variables {α ι E : Type*} [measurable_space α] {μ : measure α} {l : filter ι} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [complete_space E] [second_countable_topology E] lemma ae_cover.integral_tendsto_of_countably_generated [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (hfi : integrable f μ) : tendsto (λ i, ∫ x in φ i, f x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := suffices h : tendsto (λ i, ∫ (x : α), (φ i).indicator f x ∂μ) l (𝓝 (∫ (x : α), f x ∂μ)), by { convert h, ext n, rw integral_indicator (hφ.measurable n) }, tendsto_integral_filter_of_dominated_convergence (λ x, ∥f x∥) (eventually_of_forall $ λ i, hfi.ae_measurable.indicator $ hφ.measurable i) (eventually_of_forall $ λ i, ae_of_all _ $ λ x, norm_indicator_le_norm_self _ _) hfi.norm hφ.ae_tendsto_indicator /-- Slight reformulation of `measure_theory.ae_cover.integral_tendsto_of_countably_generated`. -/ lemma ae_cover.integral_eq_of_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : E) (hfi : integrable f μ) (h : tendsto (λ n, ∫ x in φ n, f x ∂μ) l (𝓝 I)) : ∫ x, f x ∂μ = I := tendsto_nhds_unique (hφ.integral_tendsto_of_countably_generated hfi) h lemma ae_cover.integral_eq_of_tendsto_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ} (I : ℝ) (hnng : 0 ≤ᵐ[μ] f) (hfm : ae_measurable f μ) (hfi : ∀ n, integrable_on f (φ n) μ) (htendsto : tendsto (λ n, ∫ x in φ n, f x ∂μ) l (𝓝 I)) : ∫ x, f x ∂μ = I := have hfi' : integrable f μ, from hφ.integrable_of_integral_tendsto_of_nonneg_ae I hfm hfi hnng htendsto, hφ.integral_eq_of_tendsto I hfi' htendsto end integral section integrable_of_interval_integral variables {α ι E : Type*} [topological_space α] [linear_order α] [order_closed_topology α] [measurable_space α] [opens_measurable_space α] {μ : measure α} {l : filter ι} [filter.ne_bot l] [is_countably_generated l] [measurable_space E] [normed_group E] [borel_space E] {a b : ι → α} {f : α → E} (hfm : ae_measurable f μ) include hfm lemma integrable_of_interval_integral_norm_tendsto [no_bot_order α] [nonempty α] (I : ℝ) (hfi : ∀ i, integrable_on f (Ioc (a i) (b i)) μ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) (h : tendsto (λ i, ∫ x in a i .. b i, ∥f x∥ ∂μ) l (𝓝 $ I)) : integrable f μ := begin let φ := λ n, Ioc (a n) (b n), let c : α := classical.choice ‹_›, have hφ : ae_cover μ l φ := ae_cover_Ioc ha hb, refine hφ.integrable_of_integral_norm_tendsto _ hfm hfi (h.congr' _), filter_upwards [ha.eventually (eventually_le_at_bot c), hb.eventually (eventually_ge_at_top c)], intros i hai hbi, exact interval_integral.integral_of_le (hai.trans hbi) end lemma integrable_on_Iic_of_interval_integral_norm_tendsto [no_bot_order α] (I : ℝ) (b : α) (hfi : ∀ i, integrable_on f (Ioc (a i) b) μ) (ha : tendsto a l at_bot) (h : tendsto (λ i, ∫ x in a i .. b, ∥f x∥ ∂μ) l (𝓝 $ I)) : integrable_on f (Iic b) μ := begin let φ := λ i, Ioi (a i), have hφ : ae_cover (μ.restrict $ Iic b) l φ := ae_cover_Ioi ha, have hfi : ∀ i, integrable_on f (φ i) (μ.restrict $ Iic b), { intro i, rw [integrable_on, measure.restrict_restrict (hφ.measurable i)], exact hfi i }, refine hφ.integrable_of_integral_norm_tendsto _ hfm.restrict hfi (h.congr' _), filter_upwards [ha.eventually (eventually_le_at_bot b)], intros i hai, rw [interval_integral.integral_of_le hai, measure.restrict_restrict (hφ.measurable i)], refl end lemma integrable_on_Ioi_of_interval_integral_norm_tendsto (I : ℝ) (a : α) (hfi : ∀ i, integrable_on f (Ioc a (b i)) μ) (hb : tendsto b l at_top) (h : tendsto (λ i, ∫ x in a .. b i, ∥f x∥ ∂μ) l (𝓝 $ I)) : integrable_on f (Ioi a) μ := begin let φ := λ i, Iic (b i), have hφ : ae_cover (μ.restrict $ Ioi a) l φ := ae_cover_Iic hb, have hfi : ∀ i, integrable_on f (φ i) (μ.restrict $ Ioi a), { intro i, rw [integrable_on, measure.restrict_restrict (hφ.measurable i), inter_comm], exact hfi i }, refine hφ.integrable_of_integral_norm_tendsto _ hfm.restrict hfi (h.congr' _), filter_upwards [hb.eventually (eventually_ge_at_top $ a)], intros i hbi, rw [interval_integral.integral_of_le hbi, measure.restrict_restrict (hφ.measurable i), inter_comm], refl end end integrable_of_interval_integral section integral_of_interval_integral variables {α ι E : Type*} [topological_space α] [linear_order α] [order_closed_topology α] [measurable_space α] [opens_measurable_space α] {μ : measure α} {l : filter ι} [is_countably_generated l] [measurable_space E] [normed_group E] [normed_space ℝ E] [borel_space E] [complete_space E] [second_countable_topology E] {a b : ι → α} {f : α → E} lemma interval_integral_tendsto_integral [no_bot_order α] [nonempty α] (hfi : integrable f μ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) : tendsto (λ i, ∫ x in a i .. b i, f x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := begin let φ := λ i, Ioc (a i) (b i), let c : α := classical.choice ‹_›, have hφ : ae_cover μ l φ := ae_cover_Ioc ha hb, refine (hφ.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [ha.eventually (eventually_le_at_bot c), hb.eventually (eventually_ge_at_top c)], intros i hai hbi, exact (interval_integral.integral_of_le (hai.trans hbi)).symm end lemma interval_integral_tendsto_integral_Iic [no_bot_order α] (b : α) (hfi : integrable_on f (Iic b) μ) (ha : tendsto a l at_bot) : tendsto (λ i, ∫ x in a i .. b, f x ∂μ) l (𝓝 $ ∫ x in Iic b, f x ∂μ) := begin let φ := λ i, Ioi (a i), have hφ : ae_cover (μ.restrict $ Iic b) l φ := ae_cover_Ioi ha, refine (hφ.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [ha.eventually (eventually_le_at_bot $ b)], intros i hai, rw [interval_integral.integral_of_le hai, measure.restrict_restrict (hφ.measurable i)], refl end lemma interval_integral_tendsto_integral_Ioi (a : α) (hfi : integrable_on f (Ioi a) μ) (hb : tendsto b l at_top) : tendsto (λ i, ∫ x in a .. b i, f x ∂μ) l (𝓝 $ ∫ x in Ioi a, f x ∂μ) := begin let φ := λ i, Iic (b i), have hφ : ae_cover (μ.restrict $ Ioi a) l φ := ae_cover_Iic hb, refine (hφ.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [hb.eventually (eventually_ge_at_top $ a)], intros i hbi, rw [interval_integral.integral_of_le hbi, measure.restrict_restrict (hφ.measurable i), inter_comm], refl end end integral_of_interval_integral end measure_theory
434075e2c8177edce5eb458798811abe9508dfac
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/measure_theory/bochner_integration.lean
096e07d962f38edd9ea52b178cf62673c5d28b24
[ "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
68,546
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel -/ import measure_theory.simple_func_dense import analysis.normed_space.bounded_linear_maps import measure_theory.l1_space import topology.sequences /-! # Bochner integral The Bochner integral extends the definition of the Lebesgue integral to functions that map from a measure space into a Banach space (complete normed vector space). It is constructed here by extending the integral on simple functions. ## Main definitions The Bochner integral is defined following these steps: 1. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`) where `E` is a real normed space. (See `simple_func.bintegral` and section `bintegral` for details. Also see `simple_func.integral` for the integral on simple functions of the type `simple_func α ℝ≥0∞`.) 2. Use `α →ₛ E` to cut out the simple functions from L1 functions, and define integral on these. The type of simple functions in L1 space is written as `α →₁ₛ[μ] E`. 3. Show that the embedding of `α →₁ₛ[μ] E` into L1 is a dense and uniform one. 4. Show that the integral defined on `α →₁ₛ[μ] E` is a continuous linear map. 5. Define the Bochner integral on L1 functions by extending the integral on integrable simple functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend`. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1 space. ## Main statements 1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure space and `E` is a real normed space. * `integral_zero` : `∫ 0 ∂μ = 0` * `integral_add` : `∫ x, f x + g x ∂μ = ∫ x, f ∂μ + ∫ x, g x ∂μ` * `integral_neg` : `∫ x, - f x ∂μ = - ∫ x, f x ∂μ` * `integral_sub` : `∫ x, f x - g x ∂μ = ∫ x, f x ∂μ - ∫ x, g x ∂μ` * `integral_smul` : `∫ x, r • f x ∂μ = r • ∫ x, f x ∂μ` * `integral_congr_ae` : `f =ᵐ[μ] g → ∫ x, f x ∂μ = ∫ x, g x ∂μ` * `norm_integral_le_integral_norm` : `∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ` 2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure space. * `integral_nonneg_of_ae` : `0 ≤ᵐ[μ] f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos_of_ae` : `f ≤ᵐ[μ] 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono_ae` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` * `integral_nonneg` : `0 ≤ f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos` : `f ≤ 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` 3. Propositions connecting the Bochner integral with the integral on `ℝ≥0∞`-valued functions, which is called `lintegral` and has the notation `∫⁻`. * `integral_eq_lintegral_max_sub_lintegral_min` : `∫ x, f x ∂μ = ∫⁻ x, f⁺ x ∂μ - ∫⁻ x, f⁻ x ∂μ`, where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`. * `integral_eq_lintegral_of_nonneg_ae` : `0 ≤ᵐ[μ] f → ∫ x, f x ∂μ = ∫⁻ x, f x ∂μ` 4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem ## Notes Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that you need to unfold the definition of the Bochner integral and go back to simple functions. One method is to use the theorem `integrable.induction` in the file `set_integral`, which allows you to prove something for an arbitrary measurable + integrable function. Another method is using the following steps. See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued function `f : α → ℝ`, and second and third integral sign being the integral on `ℝ≥0∞`-valued functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is scattered in sections with the name `pos_part`. Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all functions : 1. First go to the `L¹` space. For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of `f` in `L¹` space. Rewrite using `L1.norm_of_fun_eq_lintegral_norm`. 2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`. 3. Show that the property holds for all simple functions `s` in `L¹` space. Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas like `L1.integral_coe_eq_integral`. 4. Since simple functions are dense in `L¹`, ``` univ = closure {s simple} = closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions ⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} = {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself ``` Use `is_closed_property` or `dense_range.induction_on` for this argument. ## Notations * `α →ₛ E` : simple functions (defined in `measure_theory/integration`) * `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in `measure_theory/lp_space`) * `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple functions * `∫ a, f a ∂μ` : integral of `f` with respect to a measure `μ` * `∫ a, f a` : integral of `f` with respect to `volume`, the default measure on the ambient type We also define notations for integral on a set, which are described in the file `measure_theory/set_integral`. Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale classical topological_space big_operators nnreal ennreal namespace measure_theory variables {α E : Type*} [measurable_space α] [linear_order E] [has_zero E] local infixr ` →ₛ `:25 := simple_func namespace simple_func section pos_part /-- Positive part of a simple function. -/ def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λb, max b 0) /-- Negative part of a simple function. -/ def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f) lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f := begin ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], rw [pos_part, map_apply], exact le_max_right _ _ end lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f := by { rw neg_part, exact pos_part_map_norm _ } lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f := begin simp only [pos_part, neg_part], ext a, rw coe_sub, exact max_zero_sub_eq_self (f a) end end pos_part end simple_func end measure_theory namespace measure_theory open set filter topological_space ennreal emetric variables {α E F 𝕜 : Type*} [measurable_space α] local infixr ` →ₛ `:25 := simple_func namespace simple_func section integral /-! ### The Bochner integral of simple functions Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group, and prove basic property of this integral. -/ open finset variables [normed_group E] [measurable_space E] [normed_group F] variables {μ : measure α} /-- For simple functions with a `normed_group` as codomain, being integrable is the same as having finite volume support. -/ lemma integrable_iff_fin_meas_supp {f : α →ₛ E} {μ : measure α} : integrable f μ ↔ f.fin_meas_supp μ := calc integrable f μ ↔ ∫⁻ x, f.map (coe ∘ nnnorm : E → ℝ≥0∞) x ∂μ < ∞ : and_iff_right f.ae_measurable ... ↔ (f.map (coe ∘ nnnorm : E → ℝ≥0∞)).lintegral μ < ∞ : by rw lintegral_eq_lintegral ... ↔ (f.map (coe ∘ nnnorm : E → ℝ≥0∞)).fin_meas_supp μ : iff.symm $ fin_meas_supp.iff_lintegral_lt_top $ eventually_of_forall $ λ x, coe_lt_top ... ↔ _ : fin_meas_supp.map_iff $ λ b, coe_eq_zero.trans nnnorm_eq_zero lemma fin_meas_supp.integrable {f : α →ₛ E} (h : f.fin_meas_supp μ) : integrable f μ := integrable_iff_fin_meas_supp.2 h lemma integrable_pair [measurable_space F] {f : α →ₛ E} {g : α →ₛ F} : integrable f μ → integrable g μ → integrable (pair f g) μ := by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair variables [normed_space ℝ F] /-- Bochner integral of simple functions whose codomain is a real `normed_space`. -/ def integral (μ : measure α) (f : α →ₛ F) : F := ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x lemma integral_eq_sum_filter (f : α →ₛ F) (μ) : f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (ennreal.to_real (μ (f ⁻¹' {x}))) • x := eq.symm $ sum_filter_of_ne $ λ x _, mt $ λ h0, h0.symm ▸ smul_zero _ /-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/ lemma integral_eq_sum_of_subset {f : α →ₛ F} {μ : measure α} {s : finset F} (hs : f.range.filter (λ x, x ≠ 0) ⊆ s) : f.integral μ = ∑ x in s, (μ (f ⁻¹' {x})).to_real • x := begin rw [simple_func.integral_eq_sum_filter, finset.sum_subset hs], rintro x - hx, rw [finset.mem_filter, not_and_distrib, ne.def, not_not] at hx, rcases hx with hx|rfl; [skip, simp], rw [simple_func.mem_range] at hx, rw [preimage_eq_empty]; simp [disjoint_singleton_left, hx] end /-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E` and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/ lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) : (f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) := begin -- We start as in the proof of `map_lintegral` simp only [integral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← sum_measure_preimage_singleton _ (λ _ _, f.measurable_set_preimage _)], -- Now we use `hf : integrable f μ` to show that `ennreal.to_real` is additive. by_cases ha : g (f a) = 0, { simp only [ha, smul_zero], refine (sum_eq_zero $ λ x hx, _).symm, simp only [mem_filter] at hx, simp [hx.2] }, { rw [to_real_sum, sum_smul], { refine sum_congr rfl (λ x hx, _), simp only [mem_filter] at hx, rw [hx.2] }, { intros x hx, simp only [mem_filter] at hx, refine (integrable_iff_fin_meas_supp.1 hf).meas_preimage_singleton_ne_zero _, exact λ h0, ha (hx.2 ▸ h0.symm ▸ hg) } }, end /-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type `α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. See `integral_eq_lintegral` for a simpler version. -/ lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ℝ≥0∞} (hf : integrable f μ) (hg0 : g 0 = 0) (hgt : ∀b, g b < ∞): (f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) := begin have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf, simp only [← map_apply g f, lintegral_eq_lintegral], rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum], { refine finset.sum_congr rfl (λb hb, _), rw [smul_eq_mul, to_real_mul, mul_comm] }, { assume a ha, by_cases a0 : a = 0, { rw [a0, hg0, zero_mul], exact with_top.zero_lt_top }, { apply mul_lt_top (hgt a) (hf'.meas_preimage_singleton_ne_zero a0) } }, { simp [hg0] } end variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g): f.integral μ = g.integral μ := show ((pair f g).map prod.fst).integral μ = ((pair f g).map prod.snd).integral μ, from begin have inte := integrable_pair hf (hf.congr h), rw [map_integral (pair f g) _ inte prod.fst_zero, map_integral (pair f g) _ inte prod.snd_zero], refine finset.sum_congr rfl (assume p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : μ ((pair f g) ⁻¹' {(f a, g a)}) = 0, { refine measure_mono_null (assume a' ha', _) h, simp only [set.mem_preimage, mem_singleton_iff, pair_apply, prod.mk.inj_iff] at ha', show f a' ≠ g a', rwa [ha'.1, ha'.2] }, simp only [this, pair_apply, zero_smul, ennreal.zero_to_real] }, end /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. -/ lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) : f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) := begin have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) := h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm), rw [← integral_eq_lintegral' hf], { exact integral_congr hf this }, { exact ennreal.of_real_zero }, { assume b, rw ennreal.lt_top_iff_ne_top, exact ennreal.of_real_ne_top } end lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f + g) = integral μ f + integral μ g := calc integral μ (f + g) = ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • (x.fst + x.snd) : begin rw [add_eq_map₂, map_integral (pair f g)], { exact integrable_pair hf hg }, { simp only [add_zero, prod.fst_zero, prod.snd_zero] } end ... = ∑ x in (pair f g).range, (ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst + ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd) : finset.sum_congr rfl $ assume a ha, smul_add _ _ _ ... = ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst + ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd : by rw finset.sum_add_distrib ... = ((pair f g).map prod.fst).integral μ + ((pair f g).map prod.snd).integral μ : begin rw [map_integral (pair f g), map_integral (pair f g)], { exact integrable_pair hf hg }, { refl }, { exact integrable_pair hf hg }, { refl } end ... = integral μ f + integral μ g : rfl lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f := calc integral μ (-f) = integral μ (f.map (has_neg.neg)) : rfl ... = - integral μ f : begin rw [map_integral f _ hf neg_zero, integral, ← sum_neg_distrib], refine finset.sum_congr rfl (λx h, smul_neg _ _), end lemma integral_sub [borel_space E] {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f - g) = integral μ f - integral μ g := begin rw [sub_eq_add_neg, integral_add hf, integral_neg hg, sub_eq_add_neg], exact hg.neg end lemma integral_smul (c : 𝕜) {f : α →ₛ E} (hf : integrable f μ) : integral μ (c • f) = c • integral μ f := calc integral μ (c • f) = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • c • x : by rw [smul_eq_map c f, map_integral f _ hf (smul_zero _)] ... = ∑ x in f.range, c • (ennreal.to_real (μ (f ⁻¹' {x}))) • x : finset.sum_congr rfl $ λ b hb, by { exact smul_comm _ _ _} ... = c • integral μ f : by simp only [integral, smul_sum, smul_smul, mul_comm] lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) : ∥f.integral μ∥ ≤ (f.map norm).integral μ := begin rw [map_integral f norm hf norm_zero, integral], calc ∥∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • x∥ ≤ ∑ x in f.range, ∥ennreal.to_real (μ (f ⁻¹' {x})) • x∥ : norm_sum_le _ _ ... = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • ∥x∥ : begin refine finset.sum_congr rfl (λb hb, _), rw [norm_smul, smul_eq_mul, real.norm_eq_abs, abs_of_nonneg to_real_nonneg] end end lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) : f.integral (μ + ν) = f.integral μ + f.integral ν := begin simp only [integral_eq_sum_filter, ← sum_add_distrib, ← add_smul, measure.add_apply], refine sum_congr rfl (λ x hx, _), rw [to_real_add]; refine ne_of_lt ((integrable_iff_fin_meas_supp.1 _).meas_preimage_singleton_ne_zero (mem_filter.1 hx).2), exacts [hf.left_of_add_measure, hf.right_of_add_measure] end end integral end simple_func namespace L1 open ae_eq_fun variables [normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E] [normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F] {μ : measure α} variables (α E μ) /-- `L1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple function. -/ def simple_func : add_subgroup (Lp E 1 μ) := { carrier := {f : α →₁[μ] E | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_measurable : α →ₘ[μ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [←hs, ←ht, mk_add_mk, add_subgroup.coe_add, mk_eq_mk, simple_func.coe_add]⟩, neg_mem' := λ f ⟨s, hs⟩, ⟨-s, by simp only [←hs, neg_mk, simple_func.coe_neg, mk_eq_mk, add_subgroup.coe_neg]⟩ } variables {α E μ} notation α ` →₁ₛ[`:25 μ `] ` E := measure_theory.L1.simple_func α E μ namespace simple_func section instances /-! Simple functions in L1 space form a `normed_space`. -/ instance : has_coe (α →₁ₛ[μ] E) (α →₁[μ] E) := coe_subtype instance : has_coe_to_fun (α →₁ₛ[μ] E) := ⟨λ f, α → E, λ f, ⇑(f : α →₁[μ] E)⟩ @[simp, norm_cast] lemma coe_coe (f : α →₁ₛ[μ] E) : ⇑(f : α →₁[μ] E) = f := rfl protected lemma eq {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = (g : α →₁[μ] E) → f = g := subtype.eq protected lemma eq' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g := subtype.eq ∘ subtype.eq @[norm_cast] protected lemma eq_iff {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = g ↔ f = g := subtype.ext_iff.symm @[norm_cast] protected lemma eq_iff' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = g ↔ f = g := iff.intro (simple_func.eq') (congr_arg _) /-- L1 simple functions forms a `normed_group`, with the metric being inherited from L1 space, i.e., `dist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a)`). Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the Bochner integral. -/ protected def normed_group : normed_group (α →₁ₛ[μ] E) := by apply_instance local attribute [instance] simple_func.normed_group /-- Functions `α →₁ₛ[μ] E` form an additive commutative group. -/ instance : inhabited (α →₁ₛ[μ] E) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : α →₁ₛ[μ] E) : α →₁[μ] E) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : α →₁ₛ[μ] E) : ((f + g : α →₁ₛ[μ] E) : α →₁[μ] E) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : α →₁ₛ[μ] E) : ((-f : α →₁ₛ[μ] E) : α →₁[μ] E) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : α →₁ₛ[μ] E) : ((f - g : α →₁ₛ[μ] E) : α →₁[μ] E) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁ₛ[μ] E) : edist f g = edist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl @[simp] lemma dist_eq (f g : α →₁ₛ[μ] E) : dist f g = dist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl lemma norm_eq (f : α →₁ₛ[μ] E) : ∥f∥ = ∥(f : α →₁[μ] E)∥ := rfl variables [normed_field 𝕜] [normed_space 𝕜 E] /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def has_scalar : has_scalar 𝕜 (α →₁ₛ[μ] E) := ⟨λk f, ⟨k • f, begin rcases f with ⟨f, ⟨s, hs⟩⟩, use k • s, apply eq.trans (smul_mk k s s.ae_measurable).symm _, rw hs, refl, end ⟩⟩ local attribute [instance, priority 10000] simple_func.has_scalar @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁ₛ[μ] E) : ((c • f : α →₁ₛ[μ] E) : α →₁[μ] E) = c • (f : α →₁[μ] E) := rfl /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def semimodule : semimodule 𝕜 (α →₁ₛ[μ] E) := { one_smul := λf, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, simple_func.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, simple_func.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } local attribute [instance] simple_func.normed_group simple_func.semimodule /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner integral. -/ protected def normed_space : normed_space 𝕜 (α →₁ₛ[μ] E) := ⟨ λc f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.normed_group simple_func.normed_space section to_L1 /-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/ @[reducible] def to_L1 (f : α →ₛ E) (hf : integrable f μ) : (α →₁ₛ[μ] E) := ⟨hf.to_L1 f, ⟨f, rfl⟩⟩ lemma to_L1_eq_to_L1 (f : α →ₛ E) (hf : integrable f μ) : (to_L1 f hf : α →₁[μ] E) = hf.to_L1 f := rfl lemma to_L1_eq_mk (f : α →ₛ E) (hf : integrable f μ) : (to_L1 f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_measurable := rfl lemma to_L1_zero : to_L1 (0 : α →ₛ E) (integrable_zero α E μ) = 0 := rfl lemma to_L1_add (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f + g) (hf.add hg) = to_L1 f hf + to_L1 g hg := rfl lemma to_L1_neg (f : α →ₛ E) (hf : integrable f μ) : to_L1 (-f) hf.neg = -to_L1 f hf := rfl lemma to_L1_sub (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f - g) (hf.sub hg) = to_L1 f hf - to_L1 g hg := by { simp only [sub_eq_add_neg, ← to_L1_neg, ← to_L1_add], refl } variables [normed_field 𝕜] [normed_space 𝕜 E] lemma to_L1_smul (f : α →ₛ E) (hf : integrable f μ) (c : 𝕜) : to_L1 (c • f) (hf.smul c) = c • to_L1 f hf := rfl lemma norm_to_L1 (f : α →ₛ E) (hf : integrable f μ) : ∥to_L1 f hf∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := by simp [to_L1, integrable.norm_to_L1] end to_L1 section to_simple_func /-- Find a representative of a `L1.simple_func`. -/ def to_simple_func (f : α →₁ₛ[μ] E) : α →ₛ E := classical.some f.2 /-- `(to_simple_func f)` is measurable. -/ protected lemma measurable (f : α →₁ₛ[μ] E) : measurable (to_simple_func f) := (to_simple_func f).measurable protected lemma ae_measurable (f : α →₁ₛ[μ] E) : ae_measurable (to_simple_func f) μ := (simple_func.measurable f).ae_measurable /-- `to_simple_func f` is integrable. -/ protected lemma integrable (f : α →₁ₛ[μ] E) : integrable (to_simple_func f) μ := begin apply (integrable_mk (simple_func.ae_measurable f)).1, convert integrable_coe_fn f.val, exact classical.some_spec f.2 end lemma to_L1_to_simple_func (f : α →₁ₛ[μ] E) : to_L1 (to_simple_func f) (simple_func.integrable f) = f := by { rw ← simple_func.eq_iff', exact classical.some_spec f.2 } lemma to_simple_func_to_L1 (f : α →ₛ E) (hfi : integrable f μ) : to_simple_func (to_L1 f hfi) =ᵐ[μ] f := by { rw ← mk_eq_mk, exact classical.some_spec (to_L1 f hfi).2 } lemma to_simple_func_eq_to_fun (f : α →₁ₛ[μ] E) : to_simple_func f =ᵐ[μ] f := begin simp_rw [← integrable.to_L1_eq_to_L1_iff (to_simple_func f) f (simple_func.integrable f) (integrable_coe_fn ↑f), subtype.ext_iff], convert classical.some_spec f.coe_prop, exact integrable.to_L1_coe_fn _ _, end variables (E μ) lemma zero_to_simple_func : to_simple_func (0 : α →₁ₛ[μ] E) =ᵐ[μ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : α →₁ₛ[μ] E), Lp.coe_fn_zero E 1 μ], assume a h₁ h₂, rwa h₁, end variables {E μ} lemma add_to_simple_func (f g : α →₁ₛ[μ] E) : to_simple_func (f + g) =ᵐ[μ] to_simple_func f + to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : α →₁[μ] E) g], assume a, simp only [← coe_coe, coe_add, pi.add_apply], iterate 4 { assume h, rw h } end lemma neg_to_simple_func (f : α →₁ₛ[μ] E) : to_simple_func (-f) =ᵐ[μ] - to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, Lp.coe_fn_neg (f : α →₁[μ] E)], assume a, simp only [pi.neg_apply, coe_neg, ← coe_coe], repeat { assume h, rw h } end lemma sub_to_simple_func (f g : α →₁ₛ[μ] E) : to_simple_func (f - g) =ᵐ[μ] to_simple_func f - to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : α →₁[μ] E) g], assume a, simp only [coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h } end variables [normed_field 𝕜] [normed_space 𝕜 E] lemma smul_to_simple_func (k : 𝕜) (f : α →₁ₛ[μ] E) : to_simple_func (k • f) =ᵐ[μ] k • to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f, Lp.coe_fn_smul k (f : α →₁[μ] E)], assume a, simp only [pi.smul_apply, coe_smul, ← coe_coe], repeat { assume h, rw h } end lemma lintegral_edist_to_simple_func_lt_top (f g : α →₁ₛ[μ] E) : ∫⁻ (x : α), edist (to_simple_func f x) (to_simple_func g x) ∂μ < ∞ := begin rw lintegral_rw₂ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g), exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _) end lemma dist_to_simple_func (f g : α →₁ₛ[μ] E) : dist f g = ennreal.to_real (∫⁻ x, edist (to_simple_func f x) (to_simple_func g x) ∂μ) := begin rw [dist_eq, L1.dist_def, ennreal.to_real_eq_to_real], { rw lintegral_rw₂, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } }, { exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _) }, { exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_to_simple_func (f : α →₁ₛ[μ] E) : ∥f∥ = ennreal.to_real (∫⁻ (a : α), nnnorm ((to_simple_func f) a) ∂μ) := calc ∥f∥ = ennreal.to_real (∫⁻x, edist ((to_simple_func f) x) (to_simple_func (0 : α →₁ₛ[μ] E) x) ∂μ) : begin rw [← dist_zero_right, dist_to_simple_func] end ... = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) ((to_simple_func f) x) ∂μ) : begin rw lintegral_nnnorm_eq_lintegral_edist, have : ∫⁻ x, edist ((to_simple_func f) x) ((to_simple_func (0 : α →₁ₛ[μ] E)) x) ∂μ = ∫⁻ x, edist ((to_simple_func f) x) 0 ∂μ, { refine lintegral_congr_ae ((zero_to_simple_func E μ).mono (λ a h, _)), rw [h, pi.zero_apply] }, rw [ennreal.to_real_eq_to_real], { exact this }, { exact lintegral_edist_to_simple_func_lt_top _ _ }, { rw ← this, exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ∥f∥ = ((to_simple_func f).map norm).integral μ := begin rw [norm_to_simple_func, simple_func.integral_eq_lintegral], { simp only [simple_func.map_apply, of_real_norm_eq_coe_nnnorm] }, { exact (simple_func.integrable f).norm }, { exact eventually_of_forall (λ x, norm_nonneg _) } end end to_simple_func section coe_to_L1 protected lemma uniform_continuous : uniform_continuous (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding : dense_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := begin apply simple_func.uniform_embedding.dense_embedding, assume f, rw mem_closure_iff_seq_limit, have hfi' : integrable f μ := integrable_coe_fn f, refine ⟨λ n, ↑(to_L1 (simple_func.approx_on f (Lp.measurable f) univ 0 trivial n) (simple_func.integrable_approx_on_univ (Lp.measurable f) hfi' n)), λ n, mem_range_self _, _⟩, convert simple_func.tendsto_approx_on_univ_L1 (Lp.measurable f) hfi', rw integrable.to_L1_coe_fn end protected lemma dense_inducing : dense_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.dense_embedding.to_dense_inducing protected lemma dense_range : dense_range (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.dense_inducing.dense variables [normed_field 𝕜] [normed_space 𝕜 E] variables (α E 𝕜) /-- The uniform and dense embedding of L1 simple functions into L1 functions. -/ def coe_to_L1 : (α →₁ₛ[μ] E) →L[𝕜] (α →₁[μ] E) := { to_fun := (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)), map_add' := λf g, rfl, map_smul' := λk f, rfl, cont := L1.simple_func.uniform_continuous.continuous, } variables {α E 𝕜} end coe_to_L1 section pos_part /-- Positive part of a simple function in L1 space. -/ def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨Lp.pos_part (f : α →₁[μ] ℝ), begin rcases f with ⟨f, s, hsf⟩, use s.pos_part, simp only [subtype.coe_mk, Lp.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part, simple_func.coe_map] end ⟩ /-- Negative part of a simple function in L1 space. -/ def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (pos_part f : α →₁[μ] ℝ) = Lp.pos_part (f : α →₁[μ] ℝ) := rfl @[norm_cast] lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (neg_part f : α →₁[μ] ℝ) = Lp.neg_part (f : α →₁[μ] ℝ) := rfl end pos_part section simple_func_integral /-! Define the Bochner integral on `α →₁ₛ[μ] E` and prove basic properties of this integral. -/ variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] /-- The Bochner integral over simple functions in L1 space. -/ def integral (f : α →₁ₛ[μ] E) : E := ((to_simple_func f)).integral μ lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = ((to_simple_func f)).integral μ := rfl lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] (to_simple_func f)) : integral f = ennreal.to_real (∫⁻ a, ennreal.of_real ((to_simple_func f) a) ∂μ) := by rw [integral, simple_func.integral_eq_lintegral (simple_func.integrable f) h_pos] lemma integral_congr {f g : α →₁ₛ[μ] E} (h : to_simple_func f =ᵐ[μ] to_simple_func g) : integral f = integral g := simple_func.integral_congr (simple_func.integrable f) h lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g := begin simp only [integral], rw ← simple_func.integral_add (simple_func.integrable f) (simple_func.integrable g), apply measure_theory.simple_func.integral_congr (simple_func.integrable (f + g)), apply add_to_simple_func end lemma integral_smul (c : 𝕜) (f : α →₁ₛ[μ] E) : integral (c • f) = c • integral f := begin simp only [integral], rw ← simple_func.integral_smul _ (simple_func.integrable f), apply measure_theory.simple_func.integral_congr (simple_func.integrable (c • f)), apply smul_to_simple_func, repeat { assumption }, end lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ∥integral f∥ ≤ ∥f∥ := begin rw [integral, norm_eq_integral], exact (to_simple_func f).norm_integral_le_integral_norm (simple_func.integrable f) end variables (α E μ 𝕜) /-- The Bochner integral over simple functions in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁ₛ[μ] E) →L[𝕜] E := linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩ 1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul) /-- The Bochner integral over simple functions in L1 space as a continuous linear map over ℝ. -/ def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E := integral_clm' α E ℝ μ variables {α E μ 𝕜} local notation `Integral` := integral_clm α E μ open continuous_linear_map lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (zero_le_one) _ section pos_part lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : to_simple_func (pos_part f) =ᵐ[μ] (to_simple_func f).pos_part := begin have eq : ∀ a, (to_simple_func f).pos_part a = max ((to_simple_func f) a) 0 := λa, rfl, have ae_eq : ∀ᵐ a ∂μ, to_simple_func (pos_part f) a = max ((to_simple_func f) a) 0, { filter_upwards [to_simple_func_eq_to_fun (pos_part f), Lp.coe_fn_pos_part (f : α →₁[μ] ℝ), to_simple_func_eq_to_fun f], assume a h₁ h₂ h₃, rw [h₁, ← coe_coe, coe_pos_part, h₂, coe_coe, ← h₃] }, refine ae_eq.mono (assume a h, _), rw [h, eq] end lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : to_simple_func (neg_part f) =ᵐ[μ] (to_simple_func f).neg_part := begin rw [simple_func.neg_part, measure_theory.simple_func.neg_part], filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f], assume a h₁ h₂, rw h₁, show max _ _ = max _ _, rw h₂, refl end lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) : integral f = ∥pos_part f∥ - ∥neg_part f∥ := begin -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₁ : (to_simple_func f).pos_part =ᵐ[μ] (to_simple_func (pos_part f)).map norm, { filter_upwards [pos_part_to_simple_func f], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₂ : (to_simple_func f).neg_part =ᵐ[μ] (to_simple_func (neg_part f)).map norm, { filter_upwards [neg_part_to_simple_func f], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq : ∀ᵐ a ∂μ, (to_simple_func f).pos_part a - (to_simple_func f).neg_part a = (to_simple_func (pos_part f)).map norm a - (to_simple_func (neg_part f)).map norm a, { filter_upwards [ae_eq₁, ae_eq₂], assume a h₁ h₂, rw [h₁, h₂] }, rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub], { show (to_simple_func f).integral μ = ((to_simple_func (pos_part f)).map norm - (to_simple_func (neg_part f)).map norm).integral μ, apply measure_theory.simple_func.integral_congr (simple_func.integrable f), filter_upwards [ae_eq₁, ae_eq₂], assume a h₁ h₂, show _ = _ - _, rw [← h₁, ← h₂], have := (to_simple_func f).pos_part_sub_neg_part, conv_lhs {rw ← this}, refl }, { exact (simple_func.integrable f).max_zero.congr ae_eq₁ }, { exact (simple_func.integrable f).neg.max_zero.congr ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ variables [normed_space ℝ E] [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] [normed_space ℝ F] [complete_space E] section integration_in_L1 local notation `to_L1` := coe_to_L1 α E ℝ local attribute [instance] simple_func.normed_group simple_func.normed_space open continuous_linear_map variables (𝕜) /-- The Bochner integral in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁[μ] E) →L[𝕜] E := (integral_clm' α E 𝕜 μ).extend (coe_to_L1 α E 𝕜) simple_func.dense_range simple_func.uniform_inducing variables {𝕜} /-- The Bochner integral in L1 space as a continuous linear map over ℝ. -/ def integral_clm : (α →₁[μ] E) →L[ℝ] E := integral_clm' ℝ /-- The Bochner integral in L1 space -/ def integral (f : α →₁[μ] E) : E := integral_clm f lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl @[norm_cast] lemma simple_func.integral_L1_eq_integral (f : α →₁ₛ[μ] E) : integral (f : α →₁[μ] E) = (simple_func.integral f) := uniformly_extend_of_ind simple_func.uniform_inducing simple_func.dense_range (simple_func.integral_clm α E μ).uniform_continuous _ variables (α E) @[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 := map_zero integral_clm variables {α E} lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g := map_add integral_clm f g lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f := map_neg integral_clm f lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g := map_sub integral_clm f g lemma integral_smul (c : 𝕜) (f : α →₁[μ] E) : integral (c • f) = c • integral f := map_smul c (integral_clm' 𝕜) f local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ _ local notation `sIntegral` := @simple_func.integral_clm α E _ _ _ _ _ μ _ lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := calc ∥Integral∥ ≤ (1 : ℝ≥0) * ∥sIntegral∥ : op_norm_extend_le _ _ _ $ λs, by {rw [nnreal.coe_one, one_mul], refl} ... = ∥sIntegral∥ : one_mul _ ... ≤ 1 : norm_Integral_le_one lemma norm_integral_le (f : α →₁[μ] E) : ∥integral f∥ ≤ ∥f∥ := calc ∥integral f∥ = ∥Integral f∥ : rfl ... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _ ... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _ ... = ∥f∥ : one_mul _ @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), integral f) := by simp [L1.integral, L1.integral_clm.continuous] section pos_part local attribute [instance] fact_one_le_one_ennreal lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) : integral f = ∥Lp.pos_part f∥ - ∥Lp.neg_part f∥ := begin -- Use `is_closed_property` and `is_closed_eq` refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ)) (λ f : α →₁[μ] ℝ, integral f = ∥Lp.pos_part f∥ - ∥Lp.neg_part f∥) L1.simple_func.dense_range (is_closed_eq _ _) _ f, { exact cont _ }, { refine continuous.sub (continuous_norm.comp Lp.continuous_pos_part) (continuous_norm.comp Lp.continuous_neg_part) }, -- Show that the property holds for all simple functions in the `L¹` space. { assume s, norm_cast, rw [← simple_func.norm_eq, ← simple_func.norm_eq], exact simple_func.integral_eq_norm_pos_part_sub _} end end pos_part end integration_in_L1 end L1 variables [normed_group E] [second_countable_topology E] [normed_space ℝ E] [complete_space E] [measurable_space E] [borel_space E] [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] [normed_group F] [second_countable_topology F] [normed_space ℝ F] [complete_space F] [measurable_space F] [borel_space F] /-- The Bochner integral -/ def integral (μ : measure α) (f : α → E) : E := if hf : integrable f μ then L1.integral (hf.to_L1 f) else 0 /-! In the notation for integrals, an expression like `∫ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫ x, f x = 0` will be parsed incorrectly. -/ notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r section properties open continuous_linear_map measure_theory.simple_func variables {f g : α → E} {μ : measure α} lemma integral_eq (f : α → E) (hf : integrable f μ) : ∫ a, f a ∂μ = L1.integral (hf.to_L1 f) := dif_pos hf lemma L1.integral_eq_integral (f : α →₁[μ] E) : L1.integral f = ∫ a, f a ∂μ := by rw [integral_eq _ (L1.integrable_coe_fn f), integrable.to_L1_coe_fn] lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 := dif_neg h lemma integral_non_ae_measurable (h : ¬ ae_measurable f μ) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_left _ h variables (α E) lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 := by { rw [integral_eq _ (integrable_zero α E μ)], exact L1.integral_zero _ _ } @[simp] lemma integral_zero' : integral μ (0 : α → E) = 0 := integral_zero α E variables {α E} lemma integral_add (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := begin rw [integral_eq, integral_eq f hf, integral_eq g hg, ← L1.integral_add], { refl }, { exact hf.add hg } end lemma integral_add' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f + g) a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := integral_add hf hg lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, integral_eq (λa, - f a) hf.neg, ← L1.integral_neg], refl }, { rw [integral_undef hf, integral_undef, neg_zero], rwa [← integrable_neg_iff] at hf } end lemma integral_neg' (f : α → E) : ∫ a, (-f) a ∂μ = - ∫ a, f a ∂μ := integral_neg f lemma integral_sub (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := by { simp only [sub_eq_add_neg, ← integral_neg], exact integral_add hf hg.neg } lemma integral_sub' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f - g) a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := integral_sub hf hg lemma integral_smul (c : 𝕜) (f : α → E) : ∫ a, c • (f a) ∂μ = c • ∫ a, f a ∂μ := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, integral_eq (λa, c • (f a)), integrable.to_L1_smul, L1.integral_smul], }, { by_cases hr : c = 0, { simp only [hr, measure_theory.integral_zero, zero_smul] }, have hf' : ¬ integrable (λ x, c • f x) μ, { change ¬ integrable (c • f) μ, rwa [integrable_smul_iff hr f] }, rw [integral_undef hf, integral_undef hf', smul_zero] } end lemma integral_mul_left (r : ℝ) (f : α → ℝ) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ := integral_smul r f lemma integral_mul_right (r : ℝ) (f : α → ℝ) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r := by { simp only [mul_comm], exact integral_mul_left r f } lemma integral_div (r : ℝ) (f : α → ℝ) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r := integral_mul_right r⁻¹ f lemma integral_congr_ae (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ := begin by_cases hfi : integrable f μ, { have hgi : integrable g μ := hfi.congr h, rw [integral_eq f hfi, integral_eq g hgi, (integrable.to_L1_eq_to_L1_iff f g hfi hgi).2 h] }, { have hgi : ¬ integrable g μ, { rw integrable_congr h at hfi, exact hfi }, rw [integral_undef hfi, integral_undef hgi] }, end @[simp] lemma L1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) : ∫ a, (hf.to_L1 f) a ∂μ = ∫ a, f a ∂μ := integral_congr_ae $ by simp [integrable.coe_fn_to_L1] @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) := by { simp only [← L1.integral_eq_integral], exact L1.continuous_integral } lemma norm_integral_le_lintegral_norm (f : α → E) : ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, ← integrable.norm_to_L1_eq_lintegral_norm f hf], exact L1.norm_integral_le _ }, { rw [integral_undef hf, norm_zero], exact to_real_nonneg } end lemma ennnorm_integral_le_lintegral_ennnorm (f : α → E) : (nnnorm (∫ a, f a ∂μ) : ℝ≥0∞) ≤ ∫⁻ a, (nnnorm (f a)) ∂μ := by { simp_rw [← of_real_norm_eq_coe_nnnorm], apply ennreal.of_real_le_of_le_to_real, exact norm_integral_le_lintegral_norm f } lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 := if hfm : ae_measurable f μ then by simp [integral_congr_ae hf, integral_zero] else integral_non_ae_measurable hfm /-- If `f` has finite integral, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma has_finite_integral.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : has_finite_integral f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := begin rw [tendsto_zero_iff_norm_tendsto_zero], simp_rw [← coe_nnnorm, ← nnreal.coe_zero, nnreal.tendsto_coe, ← ennreal.tendsto_coe, ennreal.coe_zero], exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds (tendsto_set_lintegral_zero hf hs) (λ i, zero_le _) (λ i, ennnorm_integral_le_lintegral_ennnorm _) end /-- If `f` is integrable, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma integrable.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : integrable f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := hf.2.tendsto_set_integral_nhds_zero hs /-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x∂μ`. -/ lemma tendsto_integral_of_L1 {ι} (f : α → E) (hfi : integrable f μ) {F : ι → α → E} {l : filter ι} (hFi : ∀ᶠ i in l, integrable (F i) μ) (hF : tendsto (λ i, ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0)) : tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := begin rw [tendsto_iff_norm_tendsto_zero], replace hF : tendsto (λ i, ennreal.to_real $ ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0) := (ennreal.tendsto_to_real zero_ne_top).comp hF, refine squeeze_zero_norm' (hFi.mp $ hFi.mono $ λ i hFi hFm, _) hF, simp only [norm_norm, ← integral_sub hFi hfi, edist_dist, dist_eq_norm], apply norm_integral_le_lintegral_norm end /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their integrals. -/ theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ) (F_measurable : ∀ n, ae_measurable (F n) μ) (f_measurable : ae_measurable f μ) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) := begin /- To show `(∫ a, F n a) --> (∫ f)`, suffices to show `∥∫ a, F n a - ∫ f∥ --> 0` -/ rw tendsto_iff_norm_tendsto_zero, /- But `0 ≤ ∥∫ a, F n a - ∫ f∥ = ∥∫ a, (F n a - f a) ∥ ≤ ∫ a, ∥F n a - f a∥, and thus we apply the sandwich theorem and prove that `∫ a, ∥F n a - f a∥ --> 0` -/ have lintegral_norm_tendsto_zero : tendsto (λn, ennreal.to_real $ ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) := (tendsto_to_real zero_ne_top).comp (tendsto_lintegral_norm_of_dominated_convergence F_measurable f_measurable bound_integrable.has_finite_integral h_bound h_lim), -- Use the sandwich theorem refine squeeze_zero (λ n, norm_nonneg _) _ lintegral_norm_tendsto_zero, -- Show `∥∫ a, F n a - ∫ f∥ ≤ ∫ a, ∥F n a - f a∥` for all `n` { assume n, have h₁ : integrable (F n) μ := bound_integrable.mono' (F_measurable n) (h_bound _), have h₂ : integrable f μ := ⟨f_measurable, has_finite_integral_of_dominated_convergence bound_integrable.has_finite_integral h_bound h_lim⟩, rw ← integral_sub h₁ h₂, exact norm_integral_le_lintegral_norm _ } end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} {F : ι → α → E} {f : α → E} (bound : α → ℝ) (hl_cb : l.is_countably_generated) (hF_meas : ∀ᶠ n in l, ae_measurable (F n) μ) (f_measurable : ae_measurable f μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_integral_of_dominated_convergence _ _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { assumption }, { assumption }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { filter_upwards [h_lim], assume a h_lim, apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end /-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the integral of the positive part of `f` and the integral of the negative part of `f`. -/ lemma integral_eq_lintegral_max_sub_lintegral_min {f : α → ℝ} (hf : integrable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) - ennreal.to_real (∫⁻ a, (ennreal.of_real $ - min (f a) 0) ∂μ) := let f₁ := hf.to_L1 f in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) = ∥Lp.pos_part f₁∥ := begin rw L1.norm_def, congr' 1, apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_pos_part f₁, hf.coe_fn_to_L1], assume a h₁ h₂, rw [h₁, h₂, ennreal.of_real, nnnorm], congr' 1, apply nnreal.eq, simp [real.norm_of_nonneg, le_max_right, nnreal.coe_of_real] end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ -min (f a) 0) ∂μ) = ∥Lp.neg_part f₁∥ := begin rw L1.norm_def, congr' 1, apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_neg_part f₁, hf.coe_fn_to_L1], assume a h₁ h₂, rw [h₁, h₂, ennreal.of_real, nnnorm], congr' 1, apply nnreal.eq, simp [real.norm_of_nonneg, min_le_right, nnreal.coe_of_real, neg_nonneg], end, begin rw [eq₁, eq₂, integral, dif_pos], exact L1.integral_eq_norm_pos_part_sub _ end lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : ae_measurable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) := begin by_cases hfi : integrable f μ, { rw integral_eq_lintegral_max_sub_lintegral_min hfi, have h_min : ∫⁻ a, ennreal.of_real (-min (f a) 0) ∂μ = 0, { rw lintegral_eq_zero_iff', { refine hf.mono _, simp only [pi.zero_apply], assume a h, simp only [min_eq_right h, neg_zero, ennreal.of_real_zero] }, { exact measurable_of_real.comp_ae_measurable (measurable_id.neg.comp_ae_measurable $ hfm.min ae_measurable_const) } }, have h_max : ∫⁻ a, ennreal.of_real (max (f a) 0) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ, { refine lintegral_congr_ae (hf.mono (λ a h, _)), rw [pi.zero_apply] at h, rw max_eq_left h }, rw [h_min, h_max, zero_to_real, _root_.sub_zero] }, { rw integral_undef hfi, simp_rw [integrable, hfm, has_finite_integral_iff_norm, lt_top_iff_ne_top, ne.def, true_and, not_not] at hfi, have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ, { refine lintegral_congr_ae (hf.mono $ assume a h, _), rw [real.norm_eq_abs, abs_of_nonneg h] }, rw [this, hfi], refl } end lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ := begin by_cases hfm : ae_measurable f μ, { rw integral_eq_lintegral_of_nonneg_ae hf hfm, exact to_real_nonneg }, { rw integral_non_ae_measurable hfm } end lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : real)) μ) : ∫⁻ a, f a ∂μ = ennreal.of_real ∫ a, f a ∂μ := begin simp_rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, (f x).coe_nonneg)) hfi.ae_measurable, ← ennreal.coe_nnreal_eq], rw [ennreal.of_real_to_real], rw [← lt_top_iff_ne_top], convert hfi.has_finite_integral, ext1 x, rw [nnreal.nnnorm_eq] end lemma integral_to_real {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) (hf : ∀ᵐ x ∂μ, f x < ∞) : ∫ a, (f a).to_real ∂μ = (∫⁻ a, f a ∂μ).to_real := begin rw [integral_eq_lintegral_of_nonneg_ae _ hfm.to_real], { rw lintegral_congr_ae, refine hf.mp (eventually_of_forall _), intros x hx, rw [lt_top_iff_ne_top] at hx, simp [hx] }, { exact (eventually_of_forall $ λ x, ennreal.to_real_nonneg) } end lemma integral_nonneg {f : α → ℝ} (hf : 0 ≤ f) : 0 ≤ ∫ a, f a ∂μ := integral_nonneg_of_ae $ eventually_of_forall hf lemma integral_nonpos_of_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 := begin have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]), have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf, rwa [integral_neg, neg_nonneg] at this, end lemma integral_nonpos {f : α → ℝ} (hf : f ≤ 0) : ∫ a, f a ∂μ ≤ 0 := integral_nonpos_of_ae $ eventually_of_forall hf lemma integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := by simp_rw [integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_eq_zero_iff, lintegral_eq_zero_iff' (ennreal.measurable_of_real.comp_ae_measurable hfi.1), ← ennreal.not_lt_top, ← has_finite_integral_iff_of_real hf, hfi.2, not_true, or_false, ← hf.le_iff_eq, filter.eventually_eq, filter.eventually_le, (∘), pi.zero_apply, ennreal.of_real_eq_zero] lemma integral_eq_zero_iff_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := integral_eq_zero_iff_of_nonneg_ae (eventually_of_forall hf) hfi lemma integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := by simp_rw [(integral_nonneg_of_ae hf).lt_iff_ne, pos_iff_ne_zero, ne.def, @eq_comm ℝ 0, integral_eq_zero_iff_of_nonneg_ae hf hfi, filter.eventually_eq, ae_iff, pi.zero_apply, function.support] lemma integral_pos_iff_support_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := integral_pos_iff_support_of_nonneg_ae (eventually_of_forall hf) hfi section normed_group variables {H : Type*} [normed_group H] [second_countable_topology H] [measurable_space H] [borel_space H] lemma L1.norm_eq_integral_norm (f : α →₁[μ] H) : ∥f∥ = ∫ a, ∥f a∥ ∂μ := begin simp only [snorm, snorm', ennreal.one_to_real, ennreal.rpow_one, Lp.norm_def, if_false, ennreal.one_ne_top, one_ne_zero, _root_.div_one], rw integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (by simp [norm_nonneg])) (continuous_norm.measurable.comp_ae_measurable (Lp.ae_measurable f)), simp [of_real_norm_eq_coe_nnnorm] end lemma L1.norm_of_fun_eq_integral_norm {f : α → H} (hf : integrable f μ) : ∥hf.to_L1 f∥ = ∫ a, ∥f a∥ ∂μ := begin rw L1.norm_eq_integral_norm, refine integral_congr_ae _, apply hf.coe_fn_to_L1.mono, intros a ha, simp [ha] end end normed_group lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := le_of_sub_nonneg $ integral_sub hg hf ▸ integral_nonneg_of_ae $ h.mono (λ a, sub_nonneg_of_le) @[mono] lemma integral_mono {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := integral_mono_ae hf hg $ eventually_of_forall h lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgi : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := begin by_cases hfm : ae_measurable f μ, { refine integral_mono_ae ⟨hfm, _⟩ hgi h, refine (hgi.has_finite_integral.mono $ h.mp $ hf.mono $ λ x hf hfg, _), simpa [real.norm_eq_abs, abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] }, { rw [integral_non_ae_measurable hfm], exact integral_nonneg_of_ae (hf.trans h) } end lemma norm_integral_le_integral_norm (f : α → E) : ∥(∫ a, f a ∂μ)∥ ≤ ∫ a, ∥f a∥ ∂μ := have le_ae : ∀ᵐ a ∂μ, 0 ≤ ∥f a∥ := eventually_of_forall (λa, norm_nonneg _), classical.by_cases ( λh : ae_measurable f μ, calc ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) : norm_integral_le_lintegral_norm _ ... = ∫ a, ∥f a∥ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ ae_measurable.norm h).symm ) ( λh : ¬ae_measurable f μ, begin rw [integral_non_ae_measurable h, norm_zero], exact integral_nonneg_of_ae le_ae end ) lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hg : integrable g μ) (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : ∥∫ x, f x ∂μ∥ ≤ ∫ x, g x ∂μ := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ : norm_integral_le_integral_norm f ... ≤ ∫ x, g x ∂μ : integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hg h lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i, integrable (f i) μ) : ∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ := begin refine finset.induction_on s _ _, { simp only [integral_zero, finset.sum_empty] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], rw [integral_add (hf _) (integrable_finset_sum s hf), ih] } end lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) : f.integral μ = ∫ x, f x ∂μ := begin rw [integral_eq f hfi, ← L1.simple_func.to_L1_eq_to_L1, L1.simple_func.integral_L1_eq_integral, L1.simple_func.integral_eq_integral], exact simple_func.integral_congr hfi (L1.simple_func.to_simple_func_to_L1 _ _).symm end @[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c := begin by_cases hμ : μ univ < ∞, { haveI : finite_measure μ := ⟨hμ⟩, calc ∫ x : α, c ∂μ = (simple_func.const α c).integral μ : ((simple_func.const α c).integral_eq_integral (integrable_const _)).symm ... = _ : _, rw [simple_func.integral], by_cases ha : nonempty α, { resetI, simp [preimage_const_of_mem] }, { simp [μ.eq_zero_of_not_nonempty ha] } }, { by_cases hc : c = 0, { simp [hc, integral_zero] }, { have : ¬integrable (λ x : α, c) μ, { simp only [integrable_const_iff, not_or_distrib], exact ⟨hc, hμ⟩ }, simp only [not_lt, top_le_iff] at hμ, simp [integral_undef, *] } } end lemma norm_integral_le_of_norm_le_const [finite_measure μ] {f : α → E} {C : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : ∥∫ x, f x ∂μ∥ ≤ C * (μ univ).to_real := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le (integrable_const C) h ... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm] lemma tendsto_integral_approx_on_univ_of_measurable {f : α → E} (fmeas : measurable f) (hf : integrable f μ) : tendsto (λ n, (simple_func.approx_on f fmeas univ 0 trivial n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin have : tendsto (λ n, ∫ x, simple_func.approx_on f fmeas univ 0 trivial n x ∂μ) at_top (𝓝 $ ∫ x, f x ∂μ) := tendsto_integral_of_L1 _ hf (eventually_of_forall $ simple_func.integrable_approx_on_univ fmeas hf) (simple_func.tendsto_approx_on_univ_L1_edist fmeas hf), simpa only [simple_func.integral_eq_integral, simple_func.integrable_approx_on_univ fmeas hf] end variable {ν : measure α} private lemma integral_add_measure_of_measurable {f : α → E} (fmeas : measurable f) (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have hfi := hμ.add_measure hν, refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable fmeas hfi) _, simpa only [simple_func.integral_add_measure _ (simple_func.integrable_approx_on_univ fmeas hfi _)] using (tendsto_integral_approx_on_univ_of_measurable fmeas hμ).add (tendsto_integral_approx_on_univ_of_measurable fmeas hν) end lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have h : ae_measurable f (μ + ν) := hμ.ae_measurable.add_measure hν.ae_measurable, let g := h.mk f, have A : f =ᵐ[μ + ν] g := h.ae_eq_mk, have B : f =ᵐ[μ] g := A.filter_mono (ae_mono (measure.le_add_right (le_refl μ))), have C : f =ᵐ[ν] g := A.filter_mono (ae_mono (measure.le_add_left (le_refl ν))), calc ∫ x, f x ∂(μ + ν) = ∫ x, g x ∂(μ + ν) : integral_congr_ae A ... = ∫ x, g x ∂μ + ∫ x, g x ∂ν : integral_add_measure_of_measurable h.measurable_mk ((integrable_congr B).1 hμ) ((integrable_congr C).1 hν) ... = ∫ x, f x ∂μ + ∫ x, f x ∂ν : by { congr' 1, { exact integral_congr_ae B.symm }, { exact integral_congr_ae C.symm } } end @[simp] lemma integral_zero_measure (f : α → E) : ∫ x, f x ∂0 = 0 := norm_le_zero_iff.1 $ le_trans (norm_integral_le_lintegral_norm f) $ by simp private lemma integral_smul_measure_aux {f : α → E} {c : ℝ≥0∞} (h0 : 0 < c) (hc : c < ∞) (fmeas : measurable f) (hfi : integrable f μ) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin refine tendsto_nhds_unique _ (tendsto_const_nhds.smul (tendsto_integral_approx_on_univ_of_measurable fmeas hfi)), convert tendsto_integral_approx_on_univ_of_measurable fmeas (hfi.smul_measure hc), simp only [simple_func.integral, measure.smul_apply, finset.smul_sum, smul_smul, ennreal.to_real_mul] end @[simp] lemma integral_smul_measure (f : α → E) (c : ℝ≥0∞) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin -- First we consider “degenerate” cases: -- `c = 0` rcases (zero_le c).eq_or_lt with rfl|h0, { simp }, -- `f` is not almost everywhere measurable by_cases hfm : ae_measurable f μ, swap, { have : ¬ (ae_measurable f (c • μ)), by simpa [ne_of_gt h0] using hfm, simp [integral_non_ae_measurable, hfm, this] }, -- `c = ∞` rcases (le_top : c ≤ ∞).eq_or_lt with rfl|hc, { rw [ennreal.top_to_real, zero_smul], by_cases hf : f =ᵐ[μ] 0, { have : f =ᵐ[∞ • μ] 0 := ae_smul_measure hf ∞, exact integral_eq_zero_of_ae this }, { apply integral_undef, rw [integrable, has_finite_integral, iff_true_intro (hfm.smul_measure ∞), true_and, lintegral_smul_measure, top_mul, if_neg], { apply lt_irrefl }, { rw [lintegral_eq_zero_iff' hfm.ennnorm], refine λ h, hf (h.mono $ λ x, _), simp } } }, -- `f` is not integrable and `0 < c < ∞` by_cases hfi : integrable f μ, swap, { rw [integral_undef hfi, smul_zero], refine integral_undef (mt (λ h, _) hfi), convert h.smul_measure (ennreal.inv_lt_top.2 h0), rw [smul_smul, ennreal.inv_mul_cancel (ne_of_gt h0) (ne_of_lt hc), one_smul] }, -- Main case: `0 < c < ∞`, `f` is almost everywhere measurable and integrable let g := hfm.mk f, calc ∫ x, f x ∂(c • μ) = ∫ x, g x ∂(c • μ) : integral_congr_ae $ ae_smul_measure hfm.ae_eq_mk c ... = c.to_real • ∫ x, g x ∂μ : integral_smul_measure_aux h0 hc hfm.measurable_mk $ hfi.congr hfm.ae_eq_mk ... = c.to_real • ∫ x, f x ∂μ : by { congr' 1, exact integral_congr_ae (hfm.ae_eq_mk.symm) } end lemma integral_map_of_measurable {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : measurable f) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := begin by_cases hfi : integrable f (measure.map φ μ), swap, { rw [integral_undef hfi, integral_undef], rwa [← integrable_map_measure hfm.ae_measurable hφ] }, refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable hfm hfi) _, convert tendsto_integral_approx_on_univ_of_measurable (hfm.comp hφ) ((integrable_map_measure hfm.ae_measurable hφ).1 hfi), ext1 i, simp only [simple_func.approx_on_comp, simple_func.integral, measure.map_apply, hφ, simple_func.measurable_set_preimage, ← preimage_comp, simple_func.coe_comp], refine (finset.sum_subset (simple_func.range_comp_subset_range _ hφ) (λ y _ hy, _)).symm, rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty, simple_func.coe_comp] at hy, simp [hy] end lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : ae_measurable f (measure.map φ μ)) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := let g := hfm.mk f in calc ∫ y, f y ∂(measure.map φ μ) = ∫ y, g y ∂(measure.map φ μ) : integral_congr_ae hfm.ae_eq_mk ... = ∫ x, g (φ x) ∂μ : integral_map_of_measurable hφ hfm.measurable_mk ... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm lemma integral_dirac' (f : α → E) (a : α) (hfm : measurable f) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac' hfm ... = f a : by simp [measure.dirac_apply_of_mem] lemma integral_dirac [measurable_singleton_class α] (f : α → E) (a : α) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac f ... = f a : by simp [measure.dirac_apply_of_mem] end properties mk_simp_attribute integral_simps "Simp set for integral rules." attribute [integral_simps] integral_neg integral_smul L1.integral_add L1.integral_sub L1.integral_smul L1.integral_neg attribute [irreducible] integral L1.integral end measure_theory
9c41dffad443c4886c4accf2547e9781d08f02e7
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/predicate_logic/all.lean
6cb9ebdf363731a4ea098b310e417d25a1c298f1
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
1,430
lean
/- To show that something (such as n = n) is true for *all* values of some type (such as nat), assume that you have an *arbitrary* value of that type and in that context show that that something is true for that arbitrary n. Because it's true for an n selected arbitrarily it must be true for every n. (Think about that.) This is the introduction rule for ∀. In Lean, to prove a ∀ we define a function: one that *assumes* it's given an arbitrary argument of a given type and that, in that context, produces a value of a desired type. So you already know how to prove a ∀ proposition in Lean: write a function. -/ #check ∀ (n : nat), n = n lemma all1 : ∀ (n : nat), n = n := λ n, (eq.refl n) lemma all2 (n : nat) : n = n := rfl #check ∀ (n : nat), n = n -- Introduction rule for forall /- Now *given* a proof of a forall, you *use* it by *applying* it to get a proof for any particular instance of a universally quantified proposition. Suppse you know that ∀ n, n = n and you need a proof of 5 = 5. You can just *apply* the "proof" to 5 to get what you need. This is the elimination rule for ∀. -/ example : 5 = 5 := all1 5 #check all1 5 axioms (Person : Type) (Friendly : Person → Prop) (allFriendly : ∀ (p : Person), Friendly p) (John : Person) example : Friendly John := allFriendly John /- Proofs are just "programs" -- functions, data structures -/
f5676418bf8cdde4376c076821e643bcb6676268
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/geometry/manifold/smooth_manifold_with_corners_auto.lean
b7a2bd11419169aafcde2a52290a00d0c1e5f646
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
43,036
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.times_cont_diff import Mathlib.geometry.manifold.charted_space import Mathlib.PostPort universes u_1 u_2 u_3 l u_4 u v v' w w' u_6 u_8 u_7 u_5 namespace Mathlib /-! # Smooth manifolds (possibly with boundary or corners) A smooth manifold is a manifold modelled on a normed vector space, or a subset like a half-space (to get manifolds with boundaries) for which the changes of coordinates are smooth maps. We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in the vector space `E` (or more precisely as a structure containing all the relevant properties). Given such a model with corners `I` on `(E, H)`, we define the groupoid of local homeomorphisms of `H` which are smooth when read in `E` (for any regularity `n : with_top ℕ`). With this groupoid at hand and the general machinery of charted spaces, we thus get the notion of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. We also introduce a specific type class for `C^∞` manifolds as these are the most commonly used. ## Main definitions * `model_with_corners 𝕜 E H` : a structure containing informations on the way a space `H` embeds in a model vector space E over the field `𝕜`. This is all that is needed to define a smooth manifold with model space `H`, and model vector space `E`. * `model_with_corners_self 𝕜 E` : trivial model with corners structure on the space `E` embedded in itself by the identity. * `times_cont_diff_groupoid n I` : when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of local homeos of `H` which are of class `C^n` over the normed field `𝕜`, when read in `E`. * `smooth_manifold_with_corners I M` : a type class saying that the charted space `M`, modelled on the space `H`, has `C^∞` changes of coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just a shortcut for `has_groupoid M (times_cont_diff_groupoid ∞ I)`. * `ext_chart_at I x`: in a smooth manifold with corners with the model `I` on `(E, H)`, the charts take values in `H`, but often we may want to use their `E`-valued version, obtained by composing the charts with `I`. Since the target is in general not open, we can not register them as local homeomorphisms, but we register them as local equivs. `ext_chart_at I x` is the canonical such local equiv around `x`. As specific examples of models with corners, we define (in the file `real_instances.lean`) * `model_with_corners_self ℝ (euclidean_space (fin n))` for the model space used to define `n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners With these definitions at hand, to invoke an `n`-dimensional real manifold without boundary, one could use `variables {n : ℕ} {M : Type*} [topological_space M] [charted_space (euclidean_space (fin n)) M] [smooth_manifold_with_corners (𝓡 n) M]`. However, this is not the recommended way: a theorem proved using this assumption would not apply for instance to the tangent space of such a manifold, which is modelled on `(euclidean_space (fin n)) × (euclidean_space (fin n))` and not on `euclidean_space (fin (2 * n))`! In the same way, it would not apply to product manifolds, modelled on `(euclidean_space (fin n)) × (euclidean_space (fin m))`. The right invocation does not focus on one specific construction, but on all constructions sharing the right properties, like `variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {I : model_with_corners ℝ E E} [I.boundaryless] {M : Type*} [topological_space M] [charted_space E M] [smooth_manifold_with_corners I M]` Here, `I.boundaryless` is a typeclass property ensuring that there is no boundary (this is for instance the case for `model_with_corners_self`, or products of these). Note that one could consider as a natural assumption to only use the trivial model with corners `model_with_corners_self ℝ E`, but again in product manifolds the natural model with corners will not be this one but the product one (and they are not defeq as `(λp : E × F, (p.1, p.2))` is not defeq to the identity). So, it is important to use the above incantation to maximize the applicability of theorems. ## Implementation notes We want to talk about manifolds modelled on a vector space, but also on manifolds with boundary, modelled on a half space (or even manifolds with corners). For the latter examples, we still want to define smooth functions, tangent bundles, and so on. As smooth functions are well defined on vector spaces or subsets of these, one could take for model space a subtype of a vector space. With the drawback that the whole vector space itself (which is the most basic example) is not directly a subtype of itself: the inclusion of `univ : set E` in `set E` would show up in the definition, instead of `id`. A good abstraction covering both cases it to have a vector space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or `subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a model with corners, and we encompass all the relevant properties (in particular the fact that the image of `H` in `E` should have unique differentials) in the definition of `model_with_corners`. We concentrate on `C^∞` manifolds: all the definitions work equally well for `C^n` manifolds, but later on it is a pain to carry all over the smoothness parameter, especially when one wants to deal with `C^k` functions as there would be additional conditions `k ≤ n` everywhere. Since one deals almost all the time with `C^∞` (or analytic) manifolds, this seems to be a reasonable choice that one could revisit later if needed. `C^k` manifolds are still available, but they should be called using `has_groupoid M (times_cont_diff_groupoid k I)` where `I` is the model with corners. I have considered using the model with corners `I` as a typeclass argument, possibly `out_param`, to get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural model with corners, the trivial (identity) one, and the product one, and they are not defeq and one needs to indicate to Lean which one we want to use. This means that when talking on objects on manifolds one will most often need to specify the model with corners one is using. For instance, the tangent bundle will be `tangent_bundle I M` and the derivative will be `mfderiv I I' f`, instead of the more natural notations `tangent_bundle 𝕜 M` and `mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as real and complex manifolds). -/ /-! ### Models with corners. -/ /-- A structure containing informations on the way a space `H` embeds in a model vector space `E` over the field `𝕜`. This is all what is needed to define a smooth manifold with model space `H`, and model vector space `E`. -/ structure model_with_corners (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] (H : Type u_3) [topological_space H] extends local_equiv H E where source_eq : local_equiv.source _to_local_equiv = set.univ unique_diff' : unique_diff_on 𝕜 (set.range (local_equiv.to_fun _to_local_equiv)) continuous_to_fun : autoParam (continuous (local_equiv.to_fun _to_local_equiv)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.continuity'") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "continuity'") []) continuous_inv_fun : autoParam (continuous (local_equiv.inv_fun _to_local_equiv)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.continuity'") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "continuity'") []) /-- A vector space is a model with corners. -/ def model_with_corners_self (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : model_with_corners 𝕜 E E := model_with_corners.mk (local_equiv.mk id id set.univ set.univ sorry sorry sorry sorry) sorry sorry protected instance model_with_corners.has_coe_to_fun {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] : has_coe_to_fun (model_with_corners 𝕜 E H) := has_coe_to_fun.mk (fun (e : model_with_corners 𝕜 E H) => H → E) fun (e : model_with_corners 𝕜 E H) => local_equiv.to_fun (model_with_corners.to_local_equiv e) /-- The inverse to a model with corners, only registered as a local equiv. -/ protected def model_with_corners.symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : local_equiv E H := local_equiv.symm (model_with_corners.to_local_equiv I) /- Register a few lemmas to make sure that `simp` puts expressions in normal form -/ @[simp] theorem model_with_corners.to_local_equiv_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : ⇑(model_with_corners.to_local_equiv I) = ⇑I := rfl @[simp] theorem model_with_corners.mk_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (e : local_equiv H E) (a : local_equiv.source e = set.univ) (b : unique_diff_on 𝕜 (set.range (local_equiv.to_fun e))) (c : autoParam (continuous (local_equiv.to_fun e)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.continuity'") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "continuity'") [])) (d : autoParam (continuous (local_equiv.inv_fun e)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.continuity'") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "continuity'") [])) : ⇑(model_with_corners.mk e a b) = ⇑e := rfl @[simp] theorem model_with_corners.to_local_equiv_coe_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : ⇑(local_equiv.symm (model_with_corners.to_local_equiv I)) = ⇑(model_with_corners.symm I) := rfl @[simp] theorem model_with_corners.mk_coe_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (e : local_equiv H E) (a : local_equiv.source e = set.univ) (b : unique_diff_on 𝕜 (set.range (local_equiv.to_fun e))) (c : autoParam (continuous (local_equiv.to_fun e)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.continuity'") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "continuity'") [])) (d : autoParam (continuous (local_equiv.inv_fun e)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.continuity'") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "continuity'") [])) : ⇑(model_with_corners.symm (model_with_corners.mk e a b)) = ⇑(local_equiv.symm e) := rfl theorem model_with_corners.unique_diff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : unique_diff_on 𝕜 (set.range ⇑I) := model_with_corners.unique_diff' I protected theorem model_with_corners.continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : continuous ⇑I := model_with_corners.continuous_to_fun I theorem model_with_corners.continuous_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : continuous ⇑(model_with_corners.symm I) := model_with_corners.continuous_inv_fun I /-- In the trivial model with corners, the associated local equiv is the identity. -/ @[simp] theorem model_with_corners_self_local_equiv (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : model_with_corners.to_local_equiv (model_with_corners_self 𝕜 E) = local_equiv.refl E := rfl @[simp] theorem model_with_corners_self_coe (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : ⇑(model_with_corners_self 𝕜 E) = id := rfl @[simp] theorem model_with_corners_self_coe_symm (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : ⇑(model_with_corners.symm (model_with_corners_self 𝕜 E)) = id := rfl @[simp] theorem model_with_corners.target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : local_equiv.target (model_with_corners.to_local_equiv I) = set.range ⇑I := sorry @[simp] theorem model_with_corners.left_inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) (x : H) : coe_fn (model_with_corners.symm I) (coe_fn I x) = x := sorry @[simp] theorem model_with_corners.left_inv' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : ⇑(model_with_corners.symm I) ∘ ⇑I = id := funext fun (x : H) => model_with_corners.left_inv I x @[simp] theorem model_with_corners.right_inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {x : E} (hx : x ∈ set.range ⇑I) : coe_fn I (coe_fn (model_with_corners.symm I) x) = x := sorry theorem model_with_corners.image {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) (s : set H) : ⇑I '' s = ⇑(model_with_corners.symm I) ⁻¹' s ∩ set.range ⇑I := sorry theorem model_with_corners.unique_diff_preimage {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {s : set H} (hs : is_open s) : unique_diff_on 𝕜 (⇑(model_with_corners.symm I) ⁻¹' s ∩ set.range ⇑I) := sorry theorem model_with_corners.unique_diff_preimage_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {β : Type u_4} [topological_space β] {e : local_homeomorph H β} : unique_diff_on 𝕜 (⇑(model_with_corners.symm I) ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv e) ∩ set.range ⇑I) := model_with_corners.unique_diff_preimage I (local_homeomorph.open_source e) theorem model_with_corners.unique_diff_at_image {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {x : H} : unique_diff_within_at 𝕜 (set.range ⇑I) (coe_fn I x) := model_with_corners.unique_diff I (coe_fn I x) (set.mem_range_self x) /-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with corners `I.prod I'` on `(E × E', H × H')`. This appears in particular for the manifold structure on the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. -/ def model_with_corners.prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') : model_with_corners 𝕜 (E × E') (model_prod H H') := model_with_corners.mk (local_equiv.mk (fun (p : model_prod H H') => (coe_fn I (prod.fst p), coe_fn I' (prod.snd p))) (fun (p : E × E') => (coe_fn (model_with_corners.symm I) (prod.fst p), coe_fn (model_with_corners.symm I') (prod.snd p))) set.univ (set.prod (set.range ⇑I) (set.range ⇑I')) sorry sorry sorry sorry) sorry sorry /-- Special case of product model with corners, which is trivial on the second factor. This shows up as the model to tangent bundles. -/ def model_with_corners.tangent {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) : model_with_corners 𝕜 (E × E) (model_prod H E) := model_with_corners.prod I (model_with_corners_self 𝕜 E) @[simp] theorem model_with_corners_prod_to_local_equiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_4} [normed_group F] [normed_space 𝕜 F] {H : Type u_6} [topological_space H] {G : Type u_8} [topological_space G] {I : model_with_corners 𝕜 E H} {J : model_with_corners 𝕜 F G} : model_with_corners.to_local_equiv (model_with_corners.prod I J) = local_equiv.prod (model_with_corners.to_local_equiv I) (model_with_corners.to_local_equiv J) := sorry @[simp] theorem model_with_corners_prod_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {E' : Type u_3} [normed_group E'] [normed_space 𝕜 E'] {H : Type u_6} [topological_space H] {H' : Type u_7} [topological_space H'] (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : ⇑(model_with_corners.prod I I') = prod.map ⇑I ⇑I' := rfl @[simp] theorem model_with_corners_prod_coe_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {E' : Type u_3} [normed_group E'] [normed_space 𝕜 E'] {H : Type u_6} [topological_space H] {H' : Type u_7} [topological_space H'] (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : ⇑(model_with_corners.symm (model_with_corners.prod I I')) = prod.map ⇑(model_with_corners.symm I) ⇑(model_with_corners.symm I') := rfl /-- Property ensuring that the model with corners `I` defines manifolds without boundary. -/ class model_with_corners.boundaryless {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) where range_eq_univ : set.range ⇑I = set.univ /-- The trivial model with corners has no boundary -/ protected instance model_with_corners_self_boundaryless (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : model_with_corners.boundaryless (model_with_corners_self 𝕜 E) := model_with_corners.boundaryless.mk (eq.mpr (id (Eq.trans ((fun (a a_1 : set E) (e_1 : a = a_1) (ᾰ ᾰ_1 : set E) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (set.range ⇑(model_with_corners_self 𝕜 E)) set.univ (Eq.trans ((fun (f f_1 : E → E) (e_1 : f = f_1) => congr_arg set.range e_1) (⇑(model_with_corners_self 𝕜 E)) id (model_with_corners_self_coe 𝕜 E)) set.range_id) set.univ set.univ (Eq.refl set.univ)) (propext (eq_self_iff_true set.univ)))) trivial) /-- If two model with corners are boundaryless, their product also is -/ protected instance model_with_corners.range_eq_univ_prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) [model_with_corners.boundaryless I] {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') [model_with_corners.boundaryless I'] : model_with_corners.boundaryless (model_with_corners.prod I I') := model_with_corners.boundaryless.mk (id (eq.mpr (id (Eq._oldrec (Eq.refl ((set.range fun (p : H × H') => (coe_fn I (prod.fst p), coe_fn I' (prod.snd p))) = set.univ)) (Eq.symm set.prod_range_range_eq))) (eq.mpr (id (Eq._oldrec (Eq.refl (set.prod (set.range ⇑I) (set.range ⇑I') = set.univ)) model_with_corners.boundaryless.range_eq_univ)) (eq.mpr (id (Eq._oldrec (Eq.refl (set.prod set.univ (set.range ⇑I') = set.univ)) model_with_corners.boundaryless.range_eq_univ)) (eq.mpr (id (Eq._oldrec (Eq.refl (set.prod set.univ set.univ = set.univ)) set.univ_prod_univ)) (Eq.refl set.univ)))))) /-! ### Smooth functions on models with corners -/ /-- Given a model with corners `(E, H)`, we define the groupoid of `C^n` transformations of `H` as the maps that are `C^n` when read in `E` through `I`. -/ def times_cont_diff_groupoid (n : with_top ℕ) {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : structure_groupoid H := pregroupoid.groupoid (pregroupoid.mk (fun (f : H → H) (s : set H) => times_cont_diff_on 𝕜 n (⇑I ∘ f ∘ ⇑(model_with_corners.symm I)) (⇑(model_with_corners.symm I) ⁻¹' s ∩ set.range ⇑I)) sorry sorry sorry sorry) /-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when `m ≤ n` -/ theorem times_cont_diff_groupoid_le {m : with_top ℕ} {n : with_top ℕ} {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) (h : m ≤ n) : times_cont_diff_groupoid n I ≤ times_cont_diff_groupoid m I := sorry /-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all local homeomorphisms -/ theorem times_cont_diff_groupoid_zero_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : times_cont_diff_groupoid 0 I = continuous_groupoid H := sorry /-- An identity local homeomorphism belongs to the `C^n` groupoid. -/ theorem of_set_mem_times_cont_diff_groupoid (n : with_top ℕ) {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {s : set H} (hs : is_open s) : local_homeomorph.of_set s hs ∈ times_cont_diff_groupoid n I := sorry /-- The composition of a local homeomorphism from `H` to `M` and its inverse belongs to the `C^n` groupoid. -/ theorem symm_trans_mem_times_cont_diff_groupoid (n : with_top ℕ) {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] (e : local_homeomorph M H) : local_homeomorph.trans (local_homeomorph.symm e) e ∈ times_cont_diff_groupoid n I := structure_groupoid.eq_on_source (times_cont_diff_groupoid n I) (of_set_mem_times_cont_diff_groupoid n I (local_homeomorph.open_target e)) (local_homeomorph.trans_symm_self e) /-- The product of two smooth local homeomorphisms is smooth. -/ theorem times_cont_diff_groupoid_prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {H' : Type u_6} [topological_space H'] {I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'} {e : local_homeomorph H H} {e' : local_homeomorph H' H'} (he : e ∈ times_cont_diff_groupoid ⊤ I) (he' : e' ∈ times_cont_diff_groupoid ⊤ I') : local_homeomorph.prod e e' ∈ times_cont_diff_groupoid ⊤ (model_with_corners.prod I I') := sorry /-- The `C^n` groupoid is closed under restriction. -/ protected instance times_cont_diff_groupoid.closed_under_restriction (n : with_top ℕ) {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) : closed_under_restriction (times_cont_diff_groupoid n I) := iff.mpr (closed_under_restriction_iff_id_le (times_cont_diff_groupoid n I)) (iff.mpr structure_groupoid.le_iff fun (e : local_homeomorph H H) (ᾰ : e ∈ id_restr_groupoid) => Exists.dcases_on ᾰ fun (s : set H) (ᾰ_h : ∃ (h : is_open s), e ≈ local_homeomorph.of_set s h) => Exists.dcases_on ᾰ_h fun (hs : is_open s) (hes : e ≈ local_homeomorph.of_set s hs) => structure_groupoid.eq_on_source' (times_cont_diff_groupoid n I) (local_homeomorph.of_set s hs) e (of_set_mem_times_cont_diff_groupoid n I hs) hes) /-! ### Smooth manifolds with corners -/ /-- Typeclass defining smooth manifolds with corners with respect to a model with corners, over a field `𝕜` and with infinite smoothness to simplify typeclass search and statements later on. -/ class smooth_manifold_with_corners {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type u_4) [topological_space M] [charted_space H M] extends has_groupoid M (times_cont_diff_groupoid ⊤ I) where theorem smooth_manifold_with_corners_of_times_cont_diff_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type u_4) [topological_space M] [charted_space H M] (h : ∀ (e e' : local_homeomorph M H), e ∈ charted_space.atlas H M → e' ∈ charted_space.atlas H M → times_cont_diff_on 𝕜 ⊤ (⇑I ∘ ⇑(local_homeomorph.trans (local_homeomorph.symm e) e') ∘ ⇑(model_with_corners.symm I)) (⇑(model_with_corners.symm I) ⁻¹' local_equiv.source (local_homeomorph.to_local_equiv (local_homeomorph.trans (local_homeomorph.symm e) e')) ∩ set.range ⇑I)) : smooth_manifold_with_corners I M := smooth_manifold_with_corners.mk (structure_groupoid.compatible (times_cont_diff_groupoid ⊤ I)) /-- For any model with corners, the model space is a smooth manifold -/ protected instance model_space_smooth {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] {I : model_with_corners 𝕜 E H} : smooth_manifold_with_corners I H := smooth_manifold_with_corners.mk (has_groupoid.compatible (times_cont_diff_groupoid ⊤ I)) namespace smooth_manifold_with_corners /- We restate in the namespace `smooth_manifolds_with_corners` some lemmas that hold for general charted space with a structure groupoid, avoiding the need to specify the groupoid `times_cont_diff_groupoid ∞ I` explicitly. -/ /-- The maximal atlas of `M` for the smooth manifold with corners structure corresponding to the model with corners `I`. -/ def maximal_atlas {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type u_4) [topological_space M] [charted_space H M] : set (local_homeomorph M H) := structure_groupoid.maximal_atlas M (times_cont_diff_groupoid ⊤ I) theorem mem_maximal_atlas_of_mem_atlas {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {e : local_homeomorph M H} (he : e ∈ charted_space.atlas H M) : e ∈ maximal_atlas I M := structure_groupoid.mem_maximal_atlas_of_mem_atlas (times_cont_diff_groupoid ⊤ I) he theorem chart_mem_maximal_atlas {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (x : M) : charted_space.chart_at H x ∈ maximal_atlas I M := structure_groupoid.chart_mem_maximal_atlas (times_cont_diff_groupoid ⊤ I) x theorem compatible_of_mem_maximal_atlas {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type u_4} [topological_space M] [charted_space H M] {e : local_homeomorph M H} {e' : local_homeomorph M H} (he : e ∈ maximal_atlas I M) (he' : e' ∈ maximal_atlas I M) : local_homeomorph.trans (local_homeomorph.symm e) e' ∈ times_cont_diff_groupoid ⊤ I := structure_groupoid.compatible_of_mem_maximal_atlas he he' /-- The product of two smooth manifolds with corners is naturally a smooth manifold with corners. -/ protected instance prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {E' : Type u_3} [normed_group E'] [normed_space 𝕜 E'] {H : Type u_4} [topological_space H] {I : model_with_corners 𝕜 E H} {H' : Type u_5} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} (M : Type u_6) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (M' : Type u_7) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] : smooth_manifold_with_corners (model_with_corners.prod I I') (M × M') := sorry end smooth_manifold_with_corners /-! ### Extended charts In a smooth manifold with corners, the model space is the space `H`. However, we will also need to use extended charts taking values in the model vector space `E`. These extended charts are not `local_homeomorph` as the target is not open in `E` in general, but we can still register them as `local_equiv`. -/ /-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood of `x` to the model vector space. -/ @[simp] def ext_chart_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : local_equiv M E := local_equiv.trans (local_homeomorph.to_local_equiv (charted_space.chart_at H x)) (model_with_corners.to_local_equiv I) theorem ext_chart_at_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : local_equiv.source (ext_chart_at I x) = local_equiv.source (local_homeomorph.to_local_equiv (charted_space.chart_at H x)) := sorry theorem ext_chart_at_open_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : is_open (local_equiv.source (ext_chart_at I x)) := eq.mpr (id (Eq._oldrec (Eq.refl (is_open (local_equiv.source (ext_chart_at I x)))) (ext_chart_at_source I x))) (local_homeomorph.open_source (charted_space.chart_at H x)) theorem mem_ext_chart_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : x ∈ local_equiv.source (ext_chart_at I x) := sorry theorem ext_chart_at_to_inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : coe_fn (local_equiv.symm (ext_chart_at I x)) (coe_fn (ext_chart_at I x) x) = x := sorry theorem ext_chart_at_source_mem_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : local_equiv.source (ext_chart_at I x) ∈ nhds x := mem_nhds_sets (ext_chart_at_open_source I x) (mem_ext_chart_source I x) theorem ext_chart_at_continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : continuous_on (⇑(ext_chart_at I x)) (local_equiv.source (ext_chart_at I x)) := sorry theorem ext_chart_at_continuous_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : continuous_at (⇑(ext_chart_at I x)) x := continuous_within_at.continuous_at (ext_chart_at_continuous_on I x x (mem_ext_chart_source I x)) (ext_chart_at_source_mem_nhds I x) theorem ext_chart_at_continuous_on_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : continuous_on (⇑(local_equiv.symm (ext_chart_at I x))) (local_equiv.target (ext_chart_at I x)) := sorry theorem ext_chart_at_target_mem_nhds_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : local_equiv.target (ext_chart_at I x) ∈ nhds_within (coe_fn (ext_chart_at I x) x) (set.range ⇑I) := sorry theorem ext_chart_at_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) (p : M) : coe_fn (ext_chart_at I x) p = coe_fn I (coe_fn (charted_space.chart_at H x) p) := rfl theorem ext_chart_at_coe_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) (p : E) : coe_fn (local_equiv.symm (ext_chart_at I x)) p = coe_fn (local_homeomorph.symm (charted_space.chart_at H x)) (coe_fn (model_with_corners.symm I) p) := rfl theorem nhds_within_ext_chart_target_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : nhds_within (coe_fn (ext_chart_at I x) x) (local_equiv.target (ext_chart_at I x)) = nhds_within (coe_fn (ext_chart_at I x) x) (set.range ⇑I) := sorry theorem ext_chart_continuous_at_symm' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) {x' : M} (h : x' ∈ local_equiv.source (ext_chart_at I x)) : continuous_at (⇑(local_equiv.symm (ext_chart_at I x))) (coe_fn (ext_chart_at I x) x') := sorry theorem ext_chart_continuous_at_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) : continuous_at (⇑(local_equiv.symm (ext_chart_at I x))) (coe_fn (ext_chart_at I x) x) := ext_chart_continuous_at_symm' I x (mem_ext_chart_source I x) /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ theorem ext_chart_preimage_mem_nhds_within' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) {s : set M} {t : set M} {x' : M} (h : x' ∈ local_equiv.source (ext_chart_at I x)) (ht : t ∈ nhds_within x' s) : ⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' t ∈ nhds_within (coe_fn (ext_chart_at I x) x') (⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' s ∩ set.range ⇑I) := sorry /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the base point is a neighborhood of the preimage, within a set. -/ theorem ext_chart_preimage_mem_nhds_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) {s : set M} {t : set M} (ht : t ∈ nhds_within x s) : ⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' t ∈ nhds_within (coe_fn (ext_chart_at I x) x) (⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' s ∩ set.range ⇑I) := ext_chart_preimage_mem_nhds_within' I x (mem_ext_chart_source I x) ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point is a neighborhood of the preimage. -/ theorem ext_chart_preimage_mem_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) {t : set M} (ht : t ∈ nhds x) : ⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' t ∈ nhds (coe_fn (ext_chart_at I x) x) := sorry /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ theorem ext_chart_preimage_inter_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {H : Type u_3} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type u_4} [topological_space M] [charted_space H M] (x : M) {s : set M} {t : set M} : ⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' (s ∩ t) ∩ set.range ⇑I = ⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' s ∩ set.range ⇑I ∩ ⇑(local_equiv.symm (ext_chart_at I x)) ⁻¹' t := sorry /-- In the case of the manifold structure on a vector space, the extended charts are just the identity.-/ theorem ext_chart_model_space_eq_id (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] (x : E) : ext_chart_at (model_with_corners_self 𝕜 E) x = local_equiv.refl E := sorry end Mathlib
ca4fd346bbe770def3c36237b8a122388b293715
6329dd15b8fd567a4737f2dacd02bd0e8c4b3ae4
/src/game/world1/level8.lean
94188d487bc847837b81050391dc179f965e3ca4
[ "Apache-2.0" ]
permissive
agusakov/mathematics_in_lean_game
76e455a688a8826b05160c16c0490b9e3d39f071
ad45fd42148f2203b973537adec7e8a48677ba2a
refs/heads/master
1,666,147,402,274
1,592,119,137,000
1,592,119,137,000
272,111,226
0
0
null
null
null
null
UTF-8
Lean
false
false
470
lean
import data.real.basic --imports the real numbers import tactic.maths_in_lean_game -- hide namespace calculating -- hide /- #Calculating ## Level 8: Practice Now try this problem: -/ /- Lemma : no-side-bar For all natural numbers $a$, we have $$a + \operatorname{succ}(0) = \operatorname{succ}(a).$$ -/ lemma example8 (a b c d e f : ℝ) (h : b * c = e * f) : a * b * c * d = a * e * f * d := begin [maths_in_lean_game] sorry end end calculating -- hide
6b4e2735fdd99c2ab03d23bb0e99bd2e20144606
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/algebra/order_functions.lean
e3322b5be2fa4ed40c18ef60aacffb948bcf0613
[ "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
8,352
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 algebra.ordered_group order.lattice open lattice universes u v variables {α : Type u} {β : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right section variables [decidable_linear_order α] [decidable_linear_order β] {f : α → β} {a b c d : α} -- translate from lattices to linear orders (sup → max, inf → min) @[simp] lemma le_min_iff : c ≤ min a b ↔ c ≤ a ∧ c ≤ b := le_inf_iff @[simp] lemma max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf lemma le_max_left_of_le : a ≤ b → a ≤ max b c := le_sup_left_of_le lemma le_max_right_of_le : a ≤ c → a ≤ max b c := le_sup_right_of_le lemma min_le_left_of_le : a ≤ c → min a b ≤ c := inf_le_left_of_le lemma min_le_right_of_le : b ≤ c → min a b ≤ c := inf_le_right_of_le lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right instance max_idem : is_idempotent α max := by apply_instance instance min_idem : is_idempotent α min := by apply_instance @[simp] lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c := have a ≤ b → (a ≤ c ∨ b ≤ c ↔ a ≤ c), from assume h, or_iff_left_of_imp $ le_trans h, have b ≤ a → (a ≤ c ∨ b ≤ c ↔ b ≤ c), from assume h, or_iff_right_of_imp $ le_trans h, by cases le_total a b; simp * @[simp] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := have b ≤ c → (a ≤ b ∨ a ≤ c ↔ a ≤ c), from assume h, or_iff_right_of_imp $ assume h', le_trans h' h, have c ≤ b → (a ≤ b ∨ a ≤ c ↔ a ≤ b), from assume h, or_iff_left_of_imp $ assume h', le_trans h' h, by cases le_total b c; simp * @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := by rw [lt_iff_not_ge]; simp [(≥), le_max_iff, not_or_distrib] @[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := by rw [lt_iff_not_ge]; simp [(≥), min_le_iff, not_or_distrib] @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := by rw [lt_iff_not_ge]; simp [(≥), max_le_iff, not_and_distrib] @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := by rw [lt_iff_not_ge]; simp [(≥), le_min_iff, not_and_distrib] lemma max_lt_max (h₁ : a < c) (h₂ : b < d) : max a b < max c d := by apply max_lt; simp [lt_max_iff, h₁, h₂] lemma min_lt_min (h₁ : a < c) (h₂ : b < d) : min a b < min c d := by apply lt_min; simp [min_lt_iff, h₁, h₂] theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma max_distrib_of_monotone (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma min_distrib_of_monotone (hf : monotone f) : f (min a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] theorem min_choice (a b : α) : min a b = a ∨ min a b = b := by by_cases h : a ≤ b; simp [min, h] theorem max_choice (a b : α) : max a b = a ∨ max a b = b := by by_cases h : a ≤ b; simp [max, h] lemma le_of_max_le_left {a b c : α} (h : max a b ≤ c) : a ≤ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : α} (h : max a b ≤ c) : b ≤ c := le_trans (le_max_right _ _) h end lemma min_add {α : Type u} [decidable_linear_ordered_comm_group α] (a b c : α) : min a b + c = min (a + c) (b + c) := if hle : a ≤ b then have a - c ≤ b - c, from sub_le_sub hle (le_refl _), by simp * at * else have b - c ≤ a - c, from sub_le_sub (le_of_lt (lt_of_not_ge hle)) (le_refl _), by simp * at * lemma min_sub {α : Type u} [decidable_linear_ordered_comm_group α] (a b c : α) : min a b - c = min (a - c) (b - c) := by simp [min_add, sub_eq_add_neg] section decidable_linear_ordered_comm_group variables [decidable_linear_ordered_comm_group α] {a b c : α} attribute [simp] abs_zero abs_neg def abs_add := @abs_add_le_abs_add_abs theorem abs_le : abs a ≤ b ↔ - b ≤ a ∧ a ≤ b := ⟨assume h, ⟨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) h⟩, assume ⟨h₁, h₂⟩, abs_le_of_le_of_neg_le h₂ $ neg_le_of_neg_le h₁⟩ lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := ⟨assume h, ⟨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) h⟩, assume ⟨h₁, h₂⟩, abs_lt_of_lt_of_neg_lt h₂ $ neg_lt_of_neg_lt h₁⟩ lemma abs_sub_le_iff : abs (a - b) ≤ c ↔ a - b ≤ c ∧ b - a ≤ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] def sub_abs_le_abs_sub := @abs_sub_abs_le_abs_sub lemma abs_abs_sub_le_abs_sub (a b : α) : abs (abs a - abs b) ≤ abs (a - b) := abs_sub_le_iff.2 ⟨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_sub⟩ lemma abs_eq (hb : b ≥ 0) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := ⟨eq_zero_of_abs_eq_zero, λ e, e.symm ▸ abs_zero⟩ lemma abs_pos_iff {a : α} : 0 < abs a ↔ a ≠ 0 := ⟨λ h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zero⟩ lemma abs_le_max_abs_abs (hab : a ≤ b) (hbc : b ≤ c) : abs b ≤ max (abs a) (abs c) := abs_le_of_le_of_neg_le (by simp [le_max_iff, le_trans hbc (le_abs_self c)]) (by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)]) theorem abs_le_abs {α : Type*} [decidable_linear_ordered_comm_group α] {a b : α} (h₀ : a ≤ b) (h₁ : -a ≤ b) : abs a ≤ abs b := calc abs a ≤ b : by { apply abs_le_of_le_of_neg_le; assumption } ... ≤ abs b : le_abs_self _ lemma min_le_add_of_nonneg_right {a b : α} (hb : b ≥ 0) : min a b ≤ a + b := calc min a b ≤ a : by apply min_le_left ... ≤ a + b : le_add_of_nonneg_right hb lemma min_le_add_of_nonneg_left {a b : α} (ha : a ≥ 0) : min a b ≤ a + b := calc min a b ≤ b : by apply min_le_right ... ≤ a + b : le_add_of_nonneg_left ha lemma max_le_add_of_nonneg {a b : α} (ha : a ≥ 0) (hb : b ≥ 0) : max a b ≤ a + b := max_le_iff.2 (by split; simpa) end decidable_linear_ordered_comm_group section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring α] {a b c d : α} @[simp] lemma abs_one : abs (1 : α) = 1 := abs_of_pos zero_lt_one lemma monotone_mul_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) := assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha lemma mul_max_of_nonneg (b c : α) (ha : 0 ≤ a) : a * max b c = max (a * b) (a * c) := max_distrib_of_monotone (monotone_mul_of_nonneg ha) lemma mul_min_of_nonneg (b c : α) (ha : 0 ≤ a) : a * min b c = min (a * b) (a * c) := min_distrib_of_monotone (monotone_mul_of_nonneg ha) lemma max_mul_mul_le_max_mul_max (b c : α) (ha : 0 ≤ a) (hd: 0 ≤ d) : max (a * b) (d * c) ≤ max a c * max d b := have ba : b * a ≤ max d b * max c a, from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≤ max a c * max b d, from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) end decidable_linear_ordered_comm_ring
5566919164def4625ea712ea6b91863df6366d45
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/covering/besicovitch_vector_space.lean
02288e290a3628d34dc24412c696258afa92458b
[ "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
26,116
lean
/- Copyright (c) 2021 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 measure_theory.measure.haar_lebesgue import measure_theory.covering.besicovitch /-! # Satellite configurations for Besicovitch covering lemma in vector spaces The Besicovitch covering theorem ensures that, in a nice metric space, there exists a number `N` such that, from any family of balls with bounded radii, one can extract `N` families, each made of disjoint balls, covering together all the centers of the initial family. A key tool in the proof of this theorem is the notion of a satellite configuration, i.e., a family of `N + 1` balls, where the first `N` balls all intersect the last one, but none of them contains the center of another one and their radii are controlled. This is a technical notion, but it shows up naturally in the proof of the Besicovitch theorem (which goes through a greedy algorithm): to ensure that in the end one needs at most `N` families of balls, the crucial property of the underlying metric space is that there should be no satellite configuration of `N + 1` points. This file is devoted to the study of this property in vector spaces: we prove the main result of [Füredi and Loeb, On the best constant for the Besicovitch covering theorem][furedi-loeb1994], which shows that the optimal such `N` in a vector space coincides with the maximal number of points one can put inside the unit ball of radius `2` under the condition that their distances are bounded below by `1`. In particular, this number is bounded by `5 ^ dim` by a straightforward measure argument. ## Main definitions and results * `multiplicity E` is the maximal number of points one can put inside the unit ball of radius `2` in the vector space `E`, under the condition that their distances are bounded below by `1`. * `multiplicity_le E` shows that `multiplicity E ≤ 5 ^ (dim E)`. * `good_τ E` is a constant `> 1`, but close enough to `1` that satellite configurations with this parameter `τ` are not worst than for `τ = 1`. * `is_empty_satellite_config_multiplicity` is the main theorem, saying that there are no satellite configurations of `(multiplicity E) + 1` points, for the parameter `good_τ E`. -/ universe u open metric set finite_dimensional measure_theory filter fin open_locale ennreal topological_space noncomputable theory namespace besicovitch variables {E : Type*} [normed_add_comm_group E] namespace satellite_config variables [normed_space ℝ E] {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) /-- Rescaling a satellite configuration in a vector space, to put the basepoint at `0` and the base radius at `1`. -/ def center_and_rescale : satellite_config E N τ := { c := λ i, (a.r (last N))⁻¹ • (a.c i - a.c (last N)), r := λ i, (a.r (last N))⁻¹ * a.r i, rpos := λ i, mul_pos (inv_pos.2 (a.rpos _)) (a.rpos _), h := λ i j hij, begin rcases a.h i j hij with H|H, { left, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } }, { right, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } }, end, hlast := λ i hi, begin have H := a.hlast i hi, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } end, inter := λ i hi, begin have H := a.inter i hi, rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le), ← mul_add], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw dist_eq_norm at H, convert H using 2, abel end } lemma center_and_rescale_center : a.center_and_rescale.c (last N) = 0 := by simp [satellite_config.center_and_rescale] lemma center_and_rescale_radius {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) : a.center_and_rescale.r (last N) = 1 := by simp [satellite_config.center_and_rescale, inv_mul_cancel (a.rpos _).ne'] end satellite_config /-! ### Disjoint balls of radius close to `1` in the radius `2` ball. -/ /-- The maximum cardinality of a `1`-separated set in the ball of radius `2`. This is also the optimal number of families in the Besicovitch covering theorem. -/ def multiplicity (E : Type*) [normed_add_comm_group E] := Sup {N | ∃ s : finset E, s.card = N ∧ (∀ c ∈ s, ∥c∥ ≤ 2) ∧ (∀ c ∈ s, ∀ d ∈ s, c ≠ d → 1 ≤ ∥c - d∥)} section variables [normed_space ℝ E] [finite_dimensional ℝ E] /-- Any `1`-separated set in the ball of radius `2` has cardinality at most `5 ^ dim`. This is useful to show that the supremum in the definition of `besicovitch.multiplicity E` is well behaved. -/ lemma card_le_of_separated (s : finset E) (hs : ∀ c ∈ s, ∥c∥ ≤ 2) (h : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ∥c - d∥) : s.card ≤ 5 ^ (finrank ℝ E) := begin /- We consider balls of radius `1/2` around the points in `s`. They are disjoint, and all contained in the ball of radius `5/2`. A volume argument gives `s.card * (1/2)^dim ≤ (5/2)^dim`, i.e., `s.card ≤ 5^dim`. -/ borelize E, let μ : measure E := measure.add_haar, let δ : ℝ := (1 : ℝ)/2, let ρ : ℝ := (5 : ℝ)/2, have ρpos : 0 < ρ := by norm_num [ρ], set A := ⋃ (c ∈ s), ball (c : E) δ with hA, have D : set.pairwise (s : set E) (disjoint on (λ c, ball (c : E) δ)), { rintros c hc d hd hcd, apply ball_disjoint_ball, rw dist_eq_norm, convert h c hc d hd hcd, norm_num }, have A_subset : A ⊆ ball (0 : E) ρ, { refine Union₂_subset (λ x hx, _), apply ball_subset_ball', calc δ + dist x 0 ≤ δ + 2 : by { rw dist_zero_right, exact add_le_add le_rfl (hs x hx) } ... = 5 / 2 : by norm_num [δ] }, have I : (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) * μ (ball 0 1) ≤ ennreal.of_real (ρ ^ (finrank ℝ E)) * μ (ball 0 1) := calc (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) * μ (ball 0 1) = μ A : begin rw [hA, measure_bUnion_finset D (λ c hc, measurable_set_ball)], have I : 0 < δ, by norm_num [δ], simp only [μ.add_haar_ball_of_pos _ I, one_div, one_pow, finset.sum_const, nsmul_eq_mul, div_pow, mul_assoc] end ... ≤ μ (ball (0 : E) ρ) : measure_mono A_subset ... = ennreal.of_real (ρ ^ (finrank ℝ E)) * μ (ball 0 1) : by simp only [μ.add_haar_ball_of_pos _ ρpos], have J : (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) ≤ ennreal.of_real (ρ ^ (finrank ℝ E)) := (ennreal.mul_le_mul_right (measure_ball_pos _ _ zero_lt_one).ne' measure_ball_lt_top.ne).1 I, have K : (s.card : ℝ) ≤ (5 : ℝ) ^ finrank ℝ E, by simpa [ennreal.to_real_mul, div_eq_mul_inv] using ennreal.to_real_le_of_le_of_real (pow_nonneg ρpos.le _) J, exact_mod_cast K, end lemma multiplicity_le : multiplicity E ≤ 5 ^ (finrank ℝ E) := begin apply cSup_le, { refine ⟨0, ⟨∅, by simp⟩⟩ }, { rintros _ ⟨s, ⟨rfl, h⟩⟩, exact besicovitch.card_le_of_separated s h.1 h.2 } end lemma card_le_multiplicity {s : finset E} (hs : ∀ c ∈ s, ∥c∥ ≤ 2) (h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ∥c - d∥) : s.card ≤ multiplicity E := begin apply le_cSup, { refine ⟨5 ^ (finrank ℝ E), _⟩, rintros _ ⟨s, ⟨rfl, h⟩⟩, exact besicovitch.card_le_of_separated s h.1 h.2 }, { simp only [mem_set_of_eq, ne.def], exact ⟨s, rfl, hs, h's⟩ } end variable (E) /-- If `δ` is small enough, a `(1-δ)`-separated set in the ball of radius `2` also has cardinality at most `multiplicity E`. -/ lemma exists_good_δ : ∃ (δ : ℝ), 0 < δ ∧ δ < 1 ∧ ∀ (s : finset E), (∀ c ∈ s, ∥c∥ ≤ 2) → (∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - δ ≤ ∥c - d∥) → s.card ≤ multiplicity E := begin /- This follows from a compactness argument: otherwise, one could extract a converging subsequence, to obtain a `1`-separated set in the ball of radius `2` with cardinality `N = multiplicity E + 1`. To formalize this, we work with functions `fin N → E`. -/ classical, by_contra' h, set N := multiplicity E + 1 with hN, have : ∀ (δ : ℝ), 0 < δ → ∃ f : fin N → E, (∀ (i : fin N), ∥f i∥ ≤ 2) ∧ (∀ i j, i ≠ j → 1 - δ ≤ ∥f i - f j∥), { assume δ hδ, rcases lt_or_le δ 1 with hδ'|hδ', { rcases h δ hδ hδ' with ⟨s, hs, h's, s_card⟩, obtain ⟨f, f_inj, hfs⟩ : ∃ (f : fin N → E), function.injective f ∧ range f ⊆ ↑s, { have : fintype.card (fin N) ≤ s.card, { simp only [fintype.card_fin], exact s_card }, rcases function.embedding.exists_of_card_le_finset this with ⟨f, hf⟩, exact ⟨f, f.injective, hf⟩ }, simp only [range_subset_iff, finset.mem_coe] at hfs, refine ⟨f, λ i, hs _ (hfs i), λ i j hij, h's _ (hfs i) _ (hfs j) (f_inj.ne hij)⟩ }, { exact ⟨λ i, 0, λ i, by simp, λ i j hij, by simpa only [norm_zero, sub_nonpos, sub_self]⟩ } }, -- For `δ > 0`, `F δ` is a function from `fin N` to the ball of radius `2` for which two points -- in the image are separated by `1 - δ`. choose! F hF using this, -- Choose a converging subsequence when `δ → 0`. have : ∃ f : fin N → E, (∀ (i : fin N), ∥f i∥ ≤ 2) ∧ (∀ i j, i ≠ j → 1 ≤ ∥f i - f j∥), { obtain ⟨u, u_mono, zero_lt_u, hu⟩ : ∃ (u : ℕ → ℝ), (∀ (m n : ℕ), m < n → u n < u m) ∧ (∀ (n : ℕ), 0 < u n) ∧ filter.tendsto u filter.at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), have A : ∀ n, F (u n) ∈ closed_ball (0 : fin N → E) 2, { assume n, simp only [pi_norm_le_iff zero_le_two, mem_closed_ball, dist_zero_right, (hF (u n) (zero_lt_u n)).left, forall_const], }, obtain ⟨f, fmem, φ, φ_mono, hf⟩ : ∃ (f ∈ closed_ball (0 : fin N → E) 2) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto ((F ∘ u) ∘ φ) at_top (𝓝 f) := is_compact.tendsto_subseq (is_compact_closed_ball _ _) A, refine ⟨f, λ i, _, λ i j hij, _⟩, { simp only [pi_norm_le_iff zero_le_two, mem_closed_ball, dist_zero_right] at fmem, exact fmem i }, { have A : tendsto (λ n, ∥F (u (φ n)) i - F (u (φ n)) j∥) at_top (𝓝 (∥f i - f j∥)) := ((hf.apply i).sub (hf.apply j)).norm, have B : tendsto (λ n, 1 - u (φ n)) at_top (𝓝 (1 - 0)) := tendsto_const_nhds.sub (hu.comp φ_mono.tendsto_at_top), rw sub_zero at B, exact le_of_tendsto_of_tendsto' B A (λ n, (hF (u (φ n)) (zero_lt_u _)).2 i j hij) } }, rcases this with ⟨f, hf, h'f⟩, -- the range of `f` contradicts the definition of `multiplicity E`. have finj : function.injective f, { assume i j hij, by_contra, have : 1 ≤ ∥f i - f j∥ := h'f i j h, simp only [hij, norm_zero, sub_self] at this, exact lt_irrefl _ (this.trans_lt zero_lt_one) }, let s := finset.image f finset.univ, have s_card : s.card = N, by { rw finset.card_image_of_injective _ finj, exact finset.card_fin N }, have hs : ∀ c ∈ s, ∥c∥ ≤ 2, by simp only [hf, forall_apply_eq_imp_iff', forall_const, forall_exists_index, finset.mem_univ, finset.mem_image], have h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ∥c - d∥, { simp only [s, forall_apply_eq_imp_iff', forall_exists_index, finset.mem_univ, finset.mem_image, ne.def, exists_true_left, forall_apply_eq_imp_iff', forall_true_left], assume i j hij, have : i ≠ j := λ h, by { rw h at hij, exact hij rfl }, exact h'f i j this }, have : s.card ≤ multiplicity E := card_le_multiplicity hs h's, rw [s_card, hN] at this, exact lt_irrefl _ ((nat.lt_succ_self (multiplicity E)).trans_le this), end /-- A small positive number such that any `1 - δ`-separated set in the ball of radius `2` has cardinality at most `besicovitch.multiplicity E`. -/ def good_δ : ℝ := (exists_good_δ E).some lemma good_δ_lt_one : good_δ E < 1 := (exists_good_δ E).some_spec.2.1 /-- A number `τ > 1`, but chosen close enough to `1` so that the construction in the Besicovitch covering theorem using this parameter `τ` will give the smallest possible number of covering families. -/ def good_τ : ℝ := 1 + (good_δ E) / 4 lemma one_lt_good_τ : 1 < good_τ E := by { dsimp [good_τ, good_δ], linarith [(exists_good_δ E).some_spec.1] } variable {E} lemma card_le_multiplicity_of_δ {s : finset E} (hs : ∀ c ∈ s, ∥c∥ ≤ 2) (h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - good_δ E ≤ ∥c - d∥) : s.card ≤ multiplicity E := (classical.some_spec (exists_good_δ E)).2.2 s hs h's lemma le_multiplicity_of_δ_of_fin {n : ℕ} (f : fin n → E) (h : ∀ i, ∥f i∥ ≤ 2) (h' : ∀ i j, i ≠ j → 1 - good_δ E ≤ ∥f i - f j∥) : n ≤ multiplicity E := begin classical, have finj : function.injective f, { assume i j hij, by_contra, have : 1 - good_δ E ≤ ∥f i - f j∥ := h' i j h, simp only [hij, norm_zero, sub_self] at this, linarith [good_δ_lt_one E] }, let s := finset.image f finset.univ, have s_card : s.card = n, by { rw finset.card_image_of_injective _ finj, exact finset.card_fin n }, have hs : ∀ c ∈ s, ∥c∥ ≤ 2, by simp only [h, forall_apply_eq_imp_iff', forall_const, forall_exists_index, finset.mem_univ, finset.mem_image, implies_true_iff], have h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - good_δ E ≤ ∥c - d∥, { simp only [s, forall_apply_eq_imp_iff', forall_exists_index, finset.mem_univ, finset.mem_image, ne.def, exists_true_left, forall_apply_eq_imp_iff', forall_true_left], assume i j hij, have : i ≠ j := λ h, by { rw h at hij, exact hij rfl }, exact h' i j this }, have : s.card ≤ multiplicity E := card_le_multiplicity_of_δ hs h's, rwa [s_card] at this, end end namespace satellite_config /-! ### Relating satellite configurations to separated points in the ball of radius `2`. We prove that the number of points in a satellite configuration is bounded by the maximal number of `1`-separated points in the ball of radius `2`. For this, start from a satellite congifuration `c`. Without loss of generality, one can assume that the last ball is centered at `0` and of radius `1`. Define `c' i = c i` if `∥c i∥ ≤ 2`, and `c' i = (2/∥c i∥) • c i` if `∥c i∥ > 2`. It turns out that these points are `1 - δ`-separated, where `δ` is arbitrarily small if `τ` is close enough to `1`. The number of such configurations is bounded by `multiplicity E` if `δ` is suitably small. To check that the points `c' i` are `1 - δ`-separated, one treats separately the cases where both `∥c i∥` and `∥c j∥` are `≤ 2`, where one of them is `≤ 2` and the other one is `> 2`, and where both of them are `> 2`. -/ lemma exists_normalized_aux1 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) (i j : fin N.succ) (inej : i ≠ j) : 1 - δ ≤ ∥a.c i - a.c j∥ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ∥a.c i - a.c j∥ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ∥a.c j - a.c i∥ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have D : 0 ≤ 1 - δ / 4, by linarith only [hδ2], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have I : (1 - δ / 4) * τ ≤ 1 := calc (1 - δ / 4) * τ ≤ (1 - δ / 4) * (1 + δ / 4) : mul_le_mul_of_nonneg_left hδ1 D ... = 1 - δ^2 / 16 : by ring ... ≤ 1 : (by linarith only [sq_nonneg δ]), have J : 1 - δ ≤ 1 - δ / 4, by linarith only [δnonneg], have K : 1 - δ / 4 ≤ τ⁻¹, by { rw [inv_eq_one_div, le_div_iff τpos], exact I }, suffices L : τ⁻¹ ≤ ∥a.c i - a.c j∥, by linarith only [J, K, L], have hτ' : ∀ k, τ⁻¹ ≤ a.r k, { assume k, rw [inv_eq_one_div, div_le_iff τpos, ← lastr, mul_comm], exact a.hlast' k hτ }, rcases ah i j inej with H|H, { apply le_trans _ H.1, exact hτ' i }, { rw norm_sub_rev, apply le_trans _ H.1, exact hτ' j } end variable [normed_space ℝ E] lemma exists_normalized_aux2 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) (i j : fin N.succ) (inej : i ≠ j) (hi : ∥a.c i∥ ≤ 2) (hj : 2 < ∥a.c j∥) : 1 - δ ≤ ∥a.c i - (2 / ∥a.c j∥) • a.c j∥ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ∥a.c i - a.c j∥ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ∥a.c j - a.c i∥ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have D : 0 ≤ 1 - δ / 4, by linarith only [hδ2], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have hcrj : ∥a.c j∥ ≤ a.r j + 1, by simpa only [lastc, lastr, dist_zero_right] using a.inter' j, have I : a.r i ≤ 2, { rcases lt_or_le i (last N) with H|H, { apply (a.hlast i H).1.trans, simpa only [dist_eq_norm, lastc, sub_zero] using hi }, { have : i = last N := top_le_iff.1 H, rw [this, lastr], exact one_le_two } }, have J : (1 - δ / 4) * τ ≤ 1 := calc (1 - δ / 4) * τ ≤ (1 - δ / 4) * (1 + δ / 4) : mul_le_mul_of_nonneg_left hδ1 D ... = 1 - δ^2 / 16 : by ring ... ≤ 1 : (by linarith only [sq_nonneg δ]), have A : a.r j - δ ≤ ∥a.c i - a.c j∥, { rcases ah j i inej.symm with H|H, { rw norm_sub_rev, linarith [H.1] }, have C : a.r j ≤ 4 := calc a.r j ≤ τ * a.r i : H.2 ... ≤ τ * 2 : mul_le_mul_of_nonneg_left I τpos.le ... ≤ (5/4) * 2 : mul_le_mul_of_nonneg_right (by linarith only [hδ1, hδ2]) zero_le_two ... ≤ 4 : by norm_num, calc a.r j - δ ≤ a.r j - (a.r j / 4) * δ : begin refine sub_le_sub le_rfl _, refine mul_le_of_le_one_left δnonneg _, linarith only [C], end ... = (1 - δ / 4) * a.r j : by ring ... ≤ (1 - δ / 4) * (τ * a.r i) : mul_le_mul_of_nonneg_left (H.2) D ... ≤ 1 * a.r i : by { rw [← mul_assoc], apply mul_le_mul_of_nonneg_right J (a.rpos _).le } ... ≤ ∥a.c i - a.c j∥ : by { rw [one_mul], exact H.1 } }, set d := (2 / ∥a.c j∥) • a.c j with hd, have : a.r j - δ ≤ ∥a.c i - d∥ + (a.r j - 1) := calc a.r j - δ ≤ ∥a.c i - a.c j∥ : A ... ≤ ∥a.c i - d∥ + ∥d - a.c j∥ : by simp only [← dist_eq_norm, dist_triangle] ... ≤ ∥a.c i - d∥ + (a.r j - 1) : begin apply add_le_add_left, have A : 0 ≤ 1 - 2 / ∥a.c j∥, by simpa [div_le_iff (zero_le_two.trans_lt hj)] using hj.le, rw [← one_smul ℝ (a.c j), hd, ← sub_smul, norm_smul, norm_sub_rev, real.norm_eq_abs, abs_of_nonneg A, sub_mul], field_simp [(zero_le_two.trans_lt hj).ne'], linarith only [hcrj] end, linarith only [this] end lemma exists_normalized_aux3 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (i j : fin N.succ) (inej : i ≠ j) (hi : 2 < ∥a.c i∥) (hij : ∥a.c i∥ ≤ ∥a.c j∥) : 1 - δ ≤ ∥(2 / ∥a.c i∥) • a.c i - (2 / ∥a.c j∥) • a.c j∥ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ∥a.c i - a.c j∥ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ∥a.c j - a.c i∥ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have hcrj : ∥a.c j∥ ≤ a.r j + 1, by simpa only [lastc, lastr, dist_zero_right] using a.inter' j, have A : a.r i ≤ ∥a.c i∥, { have : i < last N, { apply lt_top_iff_ne_top.2, assume iN, change i = last N at iN, rw [iN, lastc, norm_zero] at hi, exact lt_irrefl _ (zero_le_two.trans_lt hi) }, convert (a.hlast i this).1, rw [dist_eq_norm, lastc, sub_zero] }, have hj : 2 < ∥a.c j∥ := hi.trans_le hij, set s := ∥a.c i∥ with hs, have spos : 0 < s := zero_lt_two.trans hi, set d := (s/∥a.c j∥) • a.c j with hd, have I : ∥a.c j - a.c i∥ ≤ ∥a.c j∥ - s + ∥d - a.c i∥ := calc ∥a.c j - a.c i∥ ≤ ∥a.c j - d∥ + ∥d - a.c i∥ : by simp [← dist_eq_norm, dist_triangle] ... = ∥a.c j∥ - ∥a.c i∥ + ∥d - a.c i∥ : begin nth_rewrite 0 ← one_smul ℝ (a.c j), rw [add_left_inj, hd, ← sub_smul, norm_smul, real.norm_eq_abs, abs_of_nonneg, sub_mul, one_mul, div_mul_cancel _ (zero_le_two.trans_lt hj).ne'], rwa [sub_nonneg, div_le_iff (zero_lt_two.trans hj), one_mul], end, have J : a.r j - ∥a.c j - a.c i∥ ≤ s / 2 * δ := calc a.r j - ∥a.c j - a.c i∥ ≤ s * (τ - 1) : begin rcases ah j i inej.symm with H|H, { calc a.r j - ∥a.c j - a.c i∥ ≤ 0 : sub_nonpos.2 H.1 ... ≤ s * (τ - 1) : mul_nonneg spos.le (sub_nonneg.2 hτ) }, { rw norm_sub_rev at H, calc a.r j - ∥a.c j - a.c i∥ ≤ τ * a.r i - a.r i : sub_le_sub H.2 H.1 ... = a.r i * (τ - 1) : by ring ... ≤ s * (τ - 1) : mul_le_mul_of_nonneg_right A (sub_nonneg.2 hτ) } end ... ≤ s * (δ / 2) : mul_le_mul_of_nonneg_left (by linarith only [δnonneg, hδ1]) spos.le ... = s / 2 * δ : by ring, have invs_nonneg : 0 ≤ 2 / s := (div_nonneg zero_le_two (zero_le_two.trans hi.le)), calc 1 - δ = (2 / s) * (s / 2 - (s / 2) * δ) : by { field_simp [spos.ne'], ring } ... ≤ (2 / s) * ∥d - a.c i∥ : mul_le_mul_of_nonneg_left (by linarith only [hcrj, I, J, hi]) invs_nonneg ... = ∥(2 / s) • a.c i - (2 / ∥a.c j∥) • a.c j∥ : begin conv_lhs { rw [norm_sub_rev, ← abs_of_nonneg invs_nonneg] }, rw [← real.norm_eq_abs, ← norm_smul, smul_sub, hd, smul_smul], congr' 3, field_simp [spos.ne'], end end lemma exists_normalized {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) : ∃ (c' : fin N.succ → E), (∀ n, ∥c' n∥ ≤ 2) ∧ (∀ i j, i ≠ j → 1 - δ ≤ ∥c' i - c' j∥) := begin let c' : fin N.succ → E := λ i, if ∥a.c i∥ ≤ 2 then a.c i else (2 / ∥a.c i∥) • a.c i, have norm_c'_le : ∀ i, ∥c' i∥ ≤ 2, { assume i, simp only [c'], split_ifs, { exact h }, by_cases hi : ∥a.c i∥ = 0; field_simp [norm_smul, hi] }, refine ⟨c', λ n, norm_c'_le n, λ i j inej, _⟩, -- up to exchanging `i` and `j`, one can assume `∥c i∥ ≤ ∥c j∥`. wlog hij : ∥a.c i∥ ≤ ∥a.c j∥ := le_total (∥a.c i∥) (∥a.c j∥) using [i j, j i] tactic.skip, swap, { assume i_ne_j, rw norm_sub_rev, exact this i_ne_j.symm }, rcases le_or_lt (∥a.c j∥) 2 with Hj|Hj, -- case `∥c j∥ ≤ 2` (and therefore also `∥c i∥ ≤ 2`) { simp_rw [c', Hj, hij.trans Hj, if_true], exact exists_normalized_aux1 a lastr hτ δ hδ1 hδ2 i j inej }, -- case `2 < ∥c j∥` { have H'j : (∥a.c j∥ ≤ 2) ↔ false, by simpa only [not_le, iff_false] using Hj, rcases le_or_lt (∥a.c i∥) 2 with Hi|Hi, { -- case `∥c i∥ ≤ 2` simp_rw [c', Hi, if_true, H'j, if_false], exact exists_normalized_aux2 a lastc lastr hτ δ hδ1 hδ2 i j inej Hi Hj }, { -- case `2 < ∥c i∥` have H'i : (∥a.c i∥ ≤ 2) ↔ false, by simpa only [not_le, iff_false] using Hi, simp_rw [c', H'i, if_false, H'j, if_false], exact exists_normalized_aux3 a lastc lastr hτ δ hδ1 i j inej Hi hij } } end end satellite_config variables (E) [normed_space ℝ E] [finite_dimensional ℝ E] /-- In a normed vector space `E`, there can be no satellite configuration with `multiplicity E + 1` points and the parameter `good_τ E`. This will ensure that in the inductive construction to get the Besicovitch covering families, there will never be more than `multiplicity E` nonempty families. -/ theorem is_empty_satellite_config_multiplicity : is_empty (satellite_config E (multiplicity E) (good_τ E)) := ⟨begin assume a, let b := a.center_and_rescale, rcases b.exists_normalized (a.center_and_rescale_center) (a.center_and_rescale_radius) (one_lt_good_τ E).le (good_δ E) le_rfl (good_δ_lt_one E).le with ⟨c', c'_le_two, hc'⟩, exact lt_irrefl _ ((nat.lt_succ_self _).trans_le (le_multiplicity_of_δ_of_fin c' c'_le_two hc')) end⟩ @[priority 100] instance : has_besicovitch_covering E := ⟨⟨multiplicity E, good_τ E, one_lt_good_τ E, is_empty_satellite_config_multiplicity E⟩⟩ end besicovitch
85348eabae11ce4121591cb03c185ab03b2d6907
b0d97c09d47e3b0a68128c64cad26ab132d23108
/src/connected_space.lean
0c50737658a4b29b54d322660c2c1c3d86e729c5
[]
no_license
jmvlangen/RIP-seminar
2ad32c1c6fceb59ac7ae3b2251baf08f4c0c0caa
ed6771404dd4bcec298de2dfdc89d5e9cfd331bb
refs/heads/master
1,585,865,817,237
1,546,865,823,000
1,546,865,823,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,159
lean
import analysis.topology.topological_space import analysis.topology.continuity import analysis.topology.topological_structures import data.set.basic import analysis.real open set open function open tactic universes u v variables {α: Type u} {β : Type v} variables [topological_space α] variables [topological_space β] variables {a : α} {b : β} variables {s s₁ s₂ : set α} {r r₁ r₂ : set β} variables {f : α → β} --- Usefull lemma's about the inclusion map lemma image_preimage_subtype_val {s : set α} (t : set α) : subtype.val '' (@subtype.val α s ⁻¹' t) = s ∩ t := begin rw [image_preimage_eq_inter_range, inter_comm, subtype_val_range], exact set_of_mem, end lemma subtype_val_univ_eq (s : set α) : (subtype.val) '' (@univ s) = s := calc subtype.val '' (@univ s) = range subtype.val : image_univ ... = s : subtype_val_range lemma preimage_subtype_val_eq_univ (s : set α) : @univ s = (@subtype.val _ s) ⁻¹' s := let lift := @subtype.val α s in calc @univ s = lift ⁻¹' univ : by rw set.preimage_univ ... = lift ⁻¹' (lift '' (lift ⁻¹' univ)) : by rw set.preimage_image_preimage ... = lift ⁻¹' (s ∩ univ) : by rw image_preimage_subtype_val ... = lift ⁻¹' s : by rw inter_univ s lemma preimage_subtype_val_empty_iff {s : set α} (t : set α) : @subtype.val α s ⁻¹' t = ∅ ↔ s ∩ t = ∅ := by rw [←image_preimage_subtype_val, image_eq_empty] lemma preimage_subtype_val_ne_empty_iff {s : set α} (t : set α) : @subtype.val α s ⁻¹' t ≠ ∅ ↔ s ∩ t ≠ ∅ := not_iff_not_of_iff (preimage_subtype_val_empty_iff t) lemma mem_image_inter_iff {f : α → β} : (∃ x : α, x ∈ s ∩ (f ⁻¹' r)) ↔ ∃ y : β, y ∈ (f '' s) ∩ r := iff.intro (assume h : ∃ x, x ∈ s ∩ (f ⁻¹' r), let ⟨x, _, _⟩ := h in show ∃ y, y ∈ (f '' s) ∩ r, from ⟨f x, ⟨x, ‹x ∈ s›, rfl⟩, ‹x ∈ f ⁻¹' r›⟩) (assume h : ∃ y, y ∈ (f '' s) ∩ r, let ⟨y, _, _⟩ := h in have ∃ x, x ∈ s ∧ f x = y, from ‹y ∈ f '' s›, let ⟨x, _, _⟩ := this in have x ∈ f ⁻¹' r, by rw [mem_preimage_eq, ‹f x = y›]; assumption, show ∃ x, x ∈ s ∩ (f ⁻¹' r), from ⟨x, ‹x ∈ s›, ‹x ∈ f ⁻¹' r›⟩) lemma image_connected [continuous f] [is_connected s] : is_connected (f '' s) := -- We take arbitrary sets U and V of β assume U : set β, assume V : set β, -- Assuming U is open we get that f ⁻¹' U is open assume _ : is_open U, have h₁ : is_open (f ⁻¹' U), from ‹continuous f› U ‹is_open U›, -- Assuming V is open we get that f ⁻¹' V is open assume _ : is_open V, have h₂ : is_open (f ⁻¹' V), from ‹continuous f› V ‹is_open V›, -- Assuming that (f '' s) ⊆ U ∪ V we get that s ⊆ (f ⁻¹' U) ∪ (f ⁻¹' V) assume _ : f '' s ⊆ U ∪ V, have h₃ : s ⊆ (f ⁻¹' U) ∪ (f ⁻¹' V), by rw [←preimage_union, ←image_subset_iff]; assumption, -- Assuming (f '' s) ∩ U is non-empty we get that s ∩ (f ⁻¹' U) is non-empty assume h₄' : ∃ y : β, y ∈ (f '' s) ∩ U, have h₄ : ∃ x : α, x ∈ s ∩ (f ⁻¹' U), by rw mem_image_inter_iff; assumption, -- Assuming (f '' s) ∩ V is non-empty we get that s ∩ (f ⁻¹' V) is non-empty assume _ : ∃ y : β, y ∈ (f '' s) ∩ V, have h₅ : ∃ x : α, x ∈ s ∩ (f ⁻¹' V), by rw mem_image_inter_iff; assumption, -- Applying connectedness of s to f ⁻¹' U and f ⁻¹' V with our assumptions -- tells us that s ∩ (f ⁻¹' U) ∩ (f ⁻¹' V) is non-empty have ∃ x : α, x ∈ s ∩ ((f ⁻¹' U) ∩ (f ⁻¹' V)), from ‹is_connected s› (f ⁻¹' U) (f ⁻¹' V) h₁ h₂ h₃ h₄ h₅, show ∃ y : β, y ∈ (f '' s) ∩ (U ∩ V), begin rw ←mem_image_inter_iff, fapply exists_imp_exists, exact λ x, x ∈ s ∩ ((f ⁻¹' U) ∩ (f ⁻¹' V)), intros, rw preimage_inter, assumption, assumption, end -- This proves that f '' s is connected. set_option eqn_compiler.zeta true instance connected_subset [is_connected s] : connected_space s := ⟨let lift := @subtype.val _ s in -- Start with arbitrary subsets of s assume U : set s, assume V : set s, -- We have an open set U' containing U if U is open in s assume _ : is_open U, have ∃ U' : set α, is_open U' ∧ U = lift ⁻¹' U', from ‹is_open U›, let ⟨U', h₁, _⟩ := this in -- We have an open set V' containing V if V is open in s assume _ : is_open V, have ∃ V' : set α, is_open V' ∧ V = lift ⁻¹' V', from ‹is_open V›, let ⟨V', h₂, _⟩ := this in -- Assuming the universe of s is contained in the union of U and V, -- s will be contained in the union of U' and V' assume _ : univ ⊆ U ∪ V, have h₃ : s ⊆ U' ∪ V', by rw [←(subtype_val_univ_eq s), image_subset_iff, preimage_union, ←‹U = lift ⁻¹' U'›, ←‹V = lift ⁻¹' V'›]; assumption, -- Assuming that U is non-empty we get that U' intersected s is non-empty assume _ :∃ x, x ∈ univ ∩ U, have h₄ : ∃ y, y ∈ s ∩ U', by rw [←(subtype_val_univ_eq s), ←mem_image_inter_iff, ←‹U = lift ⁻¹' U'›]; assumption, -- Assuming that V is non-empty we get that V' intersected s is non-empty assume _ : ∃ x, x ∈ univ ∩ V, have h₅ : ∃ y, y ∈ s ∩ V', by rw [←(subtype_val_univ_eq s), ←mem_image_inter_iff, ←‹V = lift ⁻¹' V'›]; assumption, -- We can now deduce that there is an element in s ∩ (U' ∩ V') -- which in turn gives an element of univ ∩ (U ∩ V) have ∃ y, y ∈ s ∩ (U' ∩ V'), from ‹is_connected s› U' V' h₁ h₂ h₃ h₄ h₅, show ∃ x, x ∈ univ ∩ (U ∩ V), by rw [‹U = lift ⁻¹' U'›, ‹V = lift ⁻¹' V'›, ←preimage_inter, mem_image_inter_iff, subtype_val_univ_eq]; assumption⟩ lemma connected_subspace_iff : connected_space s ↔ is_connected s := let lift := @subtype.val _ s in iff.intro (assume h : connected_space s, have is_connected (@univ s), from h.is_connected_univ, have is_connected (lift '' univ), from @image_connected _ _ _ _ (@univ s) lift continuous_subtype_val ‹is_connected (@univ s)›, show is_connected s, by rw ←(subtype_val_univ_eq s); assumption) (@connected_subset _ _ s) section old_connected lemma preimage_empty_of_empty {f : α → β} {r : set β} (hf : surjective f): f⁻¹' r = ∅ → r = ∅ := suffices (∀a, a ∉ f ⁻¹' r) → ∀b, b ∉ r, by simpa only [eq_empty_iff_forall_not_mem], assume h b, let ⟨a, eq⟩ := hf b in eq ▸ h a lemma preimage_ne_empty_of_ne_empty {f : α → β } (_ : surjective f) {s : set β} (_ : s ≠ ∅ ) : f ⁻¹' s ≠ ∅ := let ⟨y, _⟩ := exists_mem_of_ne_empty ‹s ≠ ∅›, ⟨x, _⟩ := ‹surjective f› y in have f x ∈ s, from (eq.symm ‹f x = y›) ▸ ‹y ∈ s›, show f⁻¹' s ≠ ∅, from ne_empty_of_mem ‹x ∈ f⁻¹' s› -- Separations of a topological space: write α as the disjoint union of non empty, open subsets def separation (s₁ s₂ : set α) : Prop := is_open s₁ ∧ is_open s₂ ∧ s₁ ≠ ∅ ∧ s₂ ≠ ∅ ∧ s₁ ∩ s₂ = ∅ ∧ s₁ ∪ s₂ = univ -- Separations are symmetric: if s₁ s₂ is a separation of α, then so is s₂ s₁. lemma sep_symm (h : separation s₁ s₂) : separation s₂ s₁ := let ⟨ho1, ho2, hne1, hne2, hce, huu⟩ := h in ⟨ho2, ho1, hne2, hne1, (inter_comm s₁ s₂) ▸ hce, (union_comm s₁ s₂) ▸ huu⟩ lemma sep_neg (sep : separation s₁ s₂) : s₁ = -s₂ := let ⟨_, _, _, _, _, _⟩ := sep in have s₁ ⊆ -s₂, by rw [subset_compl_iff_disjoint]; assumption, have -s₂ ⊆ s₁, by rw [compl_subset_iff_union, union_comm]; assumption, antisymm ‹s₁ ⊆ -s₂› ‹-s₂ ⊆ s₁› lemma sep_sets_closed (h : separation s₁ s₂) : is_closed s₁ ∧ is_closed s₂ := let ⟨ho1, ho2, _, _, hce, huu⟩ := h in have he1 : -s₂ = s₁, from eq.symm (sep_neg h), have he2 : -s₁ = s₂, from eq.symm (sep_neg (sep_symm h)), ⟨he1 ▸ is_closed_compl_iff.mpr ho2, he2 ▸ is_closed_compl_iff.mpr ho1⟩ --Connected topological spaces --class connected_space (α) [topological_space α] : Prop := --(connected : ¬∃ s₁ s₂ : set α, separation s₁ s₂) theorem is_connected_image {f : α → β} [connected_space α] (_ : continuous f) : is_connected (f '' univ) := assume u v hu hv hcsuv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, sorry /-- The image of a connected space under a surjective map is connected. -/ /-theorem im_connected {f : α → β} [connected_space α] (_ : continuous f) (_ : surjective f) : connected_space β := connected_space.mk $ -- a space is connected if there exists no separation, so we assume there is one and derive false -- we do this by constructing a 'preimage separation' assume _ : ∃ r₁ r₂ : set β, separation r₁ r₂, -- supose we have a separation r₁ ∪ r₂ = β let ⟨r₁, r₂, _, _, _, _, _, _⟩ := ‹∃ r₁ r₂ : set β, separation r₁ r₂› in -- we claim that then (s₁=f⁻¹r₁) ∪ (s₂=f⁻¹r₂) is a separation of α let s₁ := f⁻¹' r₁, s₂ := f⁻¹' r₂ in -- we now need to show that s_i are open, disjoint, nonempty, and span α have is_open s₁, from ‹continuous f› r₁ ‹is_open r₁›, have is_open s₂, from ‹continuous f› r₂ ‹is_open r₂›, have s₁ ≠ ∅, from preimage_ne_empty_of_ne_empty ‹surjective f› ‹r₁ ≠ ∅›, have s₂ ≠ ∅, from preimage_ne_empty_of_ne_empty ‹surjective f› ‹r₂ ≠ ∅›, have s₁ ∩ s₂ = ∅, by rw ←(@preimage_empty α β f); rw ←‹r₁ ∩ r₂ = ∅›; simp, have s₁ ∪ s₂ = univ, by rw ←(@preimage_univ α β f); rw ←‹r₁ ∪ r₂ = univ›; simp, -- with this preparation at hand, we construct a separation have separation s₁ s₂, from ⟨‹is_open s₁›, ‹is_open s₂›, ‹s₁ ≠ ∅›, ‹s₂ ≠ ∅›, ‹s₁ ∩ s₂ = ∅›, ‹s₁ ∪ s₂ = univ›⟩, -- which contradicts the fact that α is connected show false, from connected_space'.connected α ⟨s₁, s₂, ‹separation s₁ s₂›⟩ --Separations of a subset of a topological space def subset_separation [topological_space α] (s s₁ s₂ : set α) : Prop := is_open s₁ ∧ is_open s₂ ∧ s₁ ∩ s ≠ ∅ ∧ s₂ ∩ s ≠ ∅ ∧ s₁ ∩ s₂ ∩ s = ∅ ∧ s ⊆ s₁ ∪ s₂ lemma subset_sep_symm {t : topological_space α} {s s₁ s₂ : set α} (h : subset_separation s s₁ s₂) : subset_separation s s₂ s₁ := let ⟨ho1, ho2, hne1, hne2, hce, huu⟩ := h in ⟨ho2, ho1, hne2, hne1, (inter_comm s₁ s₂) ▸ hce, (union_comm s₁ s₂) ▸ huu⟩ lemma sep_of_subset_sep {s s₁ s₂ : set α} : subset_separation s s₁ s₂ → @separation s _ (subtype.val⁻¹' s₁) (subtype.val⁻¹' s₂) := let lift := @subtype.val _ s in assume h : subset_separation s s₁ s₂, let ⟨_, _, _, _, _, _⟩ := h in let s₁' := lift ⁻¹' s₁ in let s₂' := lift ⁻¹' s₂ in have is_open s₁', from ⟨s₁, ‹is_open s₁›, eq.refl s₁'⟩, have is_open s₂', from ⟨s₂, ‹is_open s₂›, eq.refl s₂'⟩, have s₁' ≠ ∅, by rw [preimage_subtype_val_ne_empty_iff, inter_comm]; assumption, have s₂' ≠ ∅, by rw [preimage_subtype_val_ne_empty_iff, inter_comm]; assumption, have s₁' ∩ s₂' = ∅, by rw [←preimage_inter, preimage_subtype_val_empty_iff, inter_comm]; assumption, have s₁' ∪ s₂' = univ, from (have s₁' ∪ s₂' ⊆ univ, from subset_univ (s₁' ∪ s₂'), have univ ⊆ s₁' ∪ s₂', from calc univ = lift ⁻¹' s : by rw preimage_subtype_val_eq_univ ... ⊆ lift ⁻¹' (s₁ ∪ s₂) : preimage_mono ‹s ⊆ s₁ ∪ s₂› ... = (lift ⁻¹' s₁) ∪ (lift ⁻¹' s₂) : by rw preimage_union ... = s₁' ∪ s₂' : rfl, show s₁' ∪ s₂' = univ, from eq_of_subset_of_subset ‹s₁' ∪ s₂' ⊆ univ› ‹univ ⊆ s₁' ∪ s₂'› ), show separation s₁' s₂', from ⟨‹is_open s₁'›, ‹is_open s₂'›, ‹s₁' ≠ ∅›, ‹s₂' ≠ ∅›, ‹s₁' ∩ s₂' = ∅›, ‹s₁' ∪ s₂' = univ›⟩ lemma exists_subset_sep_of_sep {s : set α} {s₁ s₂ : set s} : separation s₁ s₂ → ∃ s₁' s₂' : set α, subset_separation s s₁' s₂' := let lift := @subtype.val _ s in assume h : separation s₁ s₂, let ⟨_, _, _, _, _, _⟩ := h in have ∃ s₁' : set α, is_open s₁' ∧ s₁ = (lift) ⁻¹' s₁', from ‹is_open s₁›, let ⟨s₁', _, _⟩ := this in have ∃ s₂' : set α, is_open s₂' ∧ s₂ = (lift) ⁻¹' s₂', from ‹is_open s₂›, let ⟨s₂', _, _⟩ := this in have s₁' ∩ s ≠ ∅, by rw [←inter_comm, ←preimage_subtype_val_ne_empty_iff, ←‹s₁ = lift ⁻¹' s₁'›]; assumption, have s₂' ∩ s ≠ ∅, by rw [←inter_comm, ←preimage_subtype_val_ne_empty_iff, ←‹s₂ = lift ⁻¹' s₂'›]; assumption, have s₁' ∩ s₂' ∩ s = ∅, by rw [←inter_comm, ←preimage_subtype_val_empty_iff, preimage_inter, ←‹s₁ = lift ⁻¹' s₁'›, ←‹s₂ = lift ⁻¹' s₂'›]; assumption, have s ⊆ s₁' ∪ s₂', from (calc s = lift '' (univ) : by rw (subtype_val_univ_eq s) ... = lift '' (s₁ ∪ s₂) : by rw ‹s₁ ∪ s₂ = univ› ... = lift '' s₁ ∪ lift '' s₂ : by rw image_union ... = (lift '' (lift ⁻¹' s₁')) ∪ (lift '' (lift ⁻¹' s₂')) : by rw [‹s₁ = lift ⁻¹' s₁'›, ‹s₂ = lift ⁻¹' s₂'›] ... ⊆ s₁' ∪ s₂' : union_subset_union (image_preimage_subset lift s₁') (image_preimage_subset lift s₂') ), show ∃s₁' s₂' : set α, subset_separation s s₁' s₂', from ⟨s₁', s₂', ‹is_open s₁'›, ‹is_open s₂'›, ‹s₁' ∩ s ≠ ∅›, ‹s₂' ∩ s ≠ ∅›, ‹s₁' ∩ s₂' ∩ s = ∅›, ‹s ⊆ s₁' ∪ s₂'›⟩ --Connected subsets of a topological space def disconnected_subset (s : set α) : Prop := ∃s₁ s₂ : set α, subset_separation s s₁ s₂ def connected_subset' (s : set α) : Prop := ¬(disconnected_subset s) lemma connected_space_iff : connected_space' α ↔ ¬∃ s₁ s₂ : set α, separation s₁ s₂ := begin constructor, apply connected_space'.connected, apply connected_space'.mk end theorem subtype_connected_iff_subset_connected {s : set α} : connected_space' s ↔ connected_subset' s := suffices h₀ : (∃ s₁ s₂ : set s, separation s₁ s₂) ↔ disconnected_subset s, by rw connected_space_iff; apply not_iff_not_of_iff; assumption, let lift := @subtype.val α s in iff.intro (assume h : ∃ s₁ s₂ : set s, separation s₁ s₂, let ⟨s₁, s₂, h₁⟩ := h in show ∃ s₁' s₂' : set α, subset_separation s s₁' s₂', from exists_subset_sep_of_sep h₁ ) (assume h : disconnected_subset s, let ⟨s₁, s₂, h₁⟩ := h in show ∃ s₁ s₂ : set s, separation s₁ s₂, from ⟨lift ⁻¹' s₁, lift ⁻¹' s₂, sep_of_subset_sep h₁⟩ )-/ end old_connected
ab8656a0de19c95bac4e102cf37f4bfab317c8ad
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/structure_segfault.lean
15a08dd3b144bfdf4495e45f9ca1ca87edf38fa6
[ "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
246
lean
universes u namespace ex structure group (α : Type u) : Type u structure T (α : extends group α := () structure ring (α : Type u) : Type u class T (α : Type*) extends ring x α class S (α : Type*) extends ((λ x, group x) α) end ex
f9d952f8d991df30cdd90bc013417fea1c0d6104
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/standard/string.lean
18d67d3a5c67a1165bd6819d579738b3274b73c9
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
589
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura import bool using bool namespace string inductive char : Type := | ascii : bool → bool → bool → bool → bool → bool → bool → bool → char inductive string : Type := | empty : string | str : char → string → string theorem inhabited_char [instance] : inhabited char := inhabited_intro (ascii b0 b0 b0 b0 b0 b0 b0 b0) theorem inhabited_string [instance] : inhabited string := inhabited_intro empty end
feae180140d0d03807926e4ac7ef6b3b355df1e9
a7dd8b83f933e72c40845fd168dde330f050b1c9
/src/category_theory/concrete_category.lean
5607c5b213da2de9edd1bd644f263843d72fd11d
[ "Apache-2.0" ]
permissive
NeilStrickland/mathlib
10420e92ee5cb7aba1163c9a01dea2f04652ed67
3efbd6f6dff0fb9b0946849b43b39948560a1ffe
refs/heads/master
1,589,043,046,346
1,558,938,706,000
1,558,938,706,000
181,285,984
0
0
Apache-2.0
1,568,941,848,000
1,555,233,833,000
Lean
UTF-8
Lean
false
false
3,424
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, Reid Barton, Sean Leather Bundled type and structure. -/ import category_theory.functor import category_theory.types universes u v namespace category_theory variables {c d : Sort u → Sort v} {α : Sort u} /-- `concrete_category @hom` collects the evidence that a type constructor `c` and a morphism predicate `hom` can be thought of as a concrete category. In a typical example, `c` is the type class `topological_space` and `hom` is `continuous`. -/ structure concrete_category (hom : out_param $ ∀ {α β}, c α → c β → (α → β) → Prop) := (hom_id : ∀ {α} (ia : c α), hom ia ia id) (hom_comp : ∀ {α β γ} (ia : c α) (ib : c β) (ic : c γ) {g f}, hom ib ic g → hom ia ib f → hom ia ic (g ∘ f)) attribute [class] concrete_category /-- `bundled` is a type bundled with a type class instance for that type. Only the type class is exposed as a parameter. -/ structure bundled (c : Sort u → Sort v) : Sort (max (u+1) v) := (α : Sort u) (str : c α) def mk_ob {c : Sort u → Sort v} (α : Sort u) [str : c α] : bundled c := ⟨α, str⟩ namespace bundled instance : has_coe_to_sort (bundled c) := { S := Sort u, coe := bundled.α } /-- Map over the bundled structure -/ def map (f : ∀ {α}, c α → d α) (b : bundled c) : bundled d := ⟨b.α, f b.str⟩ section concrete_category variables (hom : ∀ {α β : Sort u}, c α → c β → (α → β) → Prop) variables [h : concrete_category @hom] include h instance : category (bundled c) := { hom := λ a b, subtype (hom a.2 b.2), id := λ a, ⟨@id a.1, h.hom_id a.2⟩, comp := λ a b c f g, ⟨g.1 ∘ f.1, h.hom_comp a.2 b.2 c.2 g.2 f.2⟩ } variables {X Y Z : bundled c} @[simp] lemma concrete_category_id (X : bundled c) : subtype.val (𝟙 X) = id := rfl @[simp] lemma concrete_category_comp (f : X ⟶ Y) (g : Y ⟶ Z) : subtype.val (f ≫ g) = g.val ∘ f.val := rfl instance : has_coe_to_fun (X ⟶ Y) := { F := λ f, X → Y, coe := λ f, f.1 } @[extensionality] lemma bundled_hom.ext {f g : X ⟶ Y} : (∀ x : X, f x = g x) → f = g := λ w, subtype.ext.2 $ funext w @[simp] lemma coe_id {X : bundled c} : ((𝟙 X) : X → X) = id := rfl @[simp] lemma bundled_hom_coe (val : X → Y) (prop) (x : X) : (⟨val, prop⟩ : X ⟶ Y) x = val x := rfl end concrete_category end bundled def concrete_functor {C : Sort u → Sort v} {hC : ∀{α β}, C α → C β → (α → β) → Prop} [concrete_category @hC] {D : Sort u → Sort v} {hD : ∀{α β}, D α → D β → (α → β) → Prop} [concrete_category @hD] (m : ∀{α}, C α → D α) (h : ∀{α β} {ia : C α} {ib : C β} {f}, hC ia ib f → hD (m ia) (m ib) f) : bundled C ⥤ bundled D := { obj := bundled.map @m, map := λ X Y f, ⟨ f, h f.2 ⟩ } section forget variables {C : Sort u → Sort v} {hom : ∀α β, C α → C β → (α → β) → Prop} [i : concrete_category hom] include i /-- The forgetful functor from a bundled category to `Sort`. -/ def forget : bundled C ⥤ Sort u := { obj := bundled.α, map := λ a b h, h.1 } instance forget.faithful : faithful (forget : bundled C ⥤ Sort u) := { injectivity' := begin rintros X Y ⟨f,_⟩ ⟨g,_⟩ p, congr, exact p, end } end forget end category_theory
96c09f42e22915864070774f08637d536ba2478b
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/generalize1.lean
98b189ca5037214977a44d439468f890d41b34dd
[ "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
351
lean
open tactic add_key_equivalence has_add.add nat.succ set_option pp.binder_types true example (a b c : nat) : a + 1 = b + 1 → b + 1 = a + 1 := by do a ← get_local `a, b ← get_local `b, e₁ ← mk_app `nat.succ [a], e₂ ← mk_app `nat.succ [b], generalize e₁ `x, generalize e₂ `y, trace_state, intros, symmetry, assumption
60957f760ae4bd22b62d4e7c9cc412a9a73e0b8b
ca1ad81c8733787aba30f7a8d63f418508e12812
/clfrags/src/hilbert/wr/dc_pt.lean
78cd5d272c6d55169d9a6cd868efe677ae96fd57
[]
no_license
greati/hilbert-classical-fragments
5cdbe07851e979c8a03c621a5efd4d24bbfa333a
18a21ac6b2e890060eb4ae65752fc0245394d226
refs/heads/master
1,591,973,117,184
1,573,822,710,000
1,573,822,710,000
194,334,439
2
0
null
null
null
null
UTF-8
Lean
false
false
1,477
lean
import core.connectives import hilbert.wr.pt import hilbert.wr.proofs.pt import hilbert.wr.dc import hilbert.wr.proofs.dc namespace clfrags namespace hilbert namespace wr namespace dc_pt axiom dcpt₁ : Π {a b c d e : Prop}, dc a b (pt c d e) → pt (dc a b c) (dc a b d) (dc a b e) axiom dcpt₂ : Π {a b c d e : Prop}, pt (dc a b c) (dc a b d) (dc a b e) → dc a b (pt c d e) axiom dcpt₃ : Π {a b c d e : Prop}, pt a b (dc c d e) → dc (pt a b c) (pt a b d) (pt a b e) axiom dcpt₄ : Π {a b c d e : Prop}, dc (pt a b c) (pt a b d) (pt a b e) → pt a b (dc c d e) -- dcpt₃_dc axiom dcpt₅ : Π {a b c d e f g : Prop}, dc f g (pt a b (dc c d e)) → dc f g (dc (pt a b c) (pt a b d) (pt a b e)) -- dcpt₄_dc axiom dcpt₆ : Π {a b c d e f g : Prop}, dc f g (dc (pt a b c) (pt a b d) (pt a b e)) → dc f g (pt a b (dc c d e)) -- dcpt₁_pt axiom dcpt₇ : Π {a b c d e f g : Prop}, pt f g (dc a b (pt c d e)) → pt f g (pt (dc a b c) (dc a b d) (dc a b e)) -- dcpt₂_pt axiom dcpt₈ : Π {a b c d e f g : Prop}, pt f g (pt (dc a b c) (dc a b d) (dc a b e)) → pt f g (dc a b (pt c d e)) end dc_pt end wr end hilbert end clfrags
674e78fc83037bde684785ff26e34a427a9ac190
09b3e1beaeff2641ac75019c9f735d79d508071d
/Mathlib/Logic/Function/Basic.lean
6f4711c279e8c81faa4fe43d362744200b2497d7
[ "Apache-2.0" ]
permissive
abentkamp/mathlib4
b819079cc46426b3c5c77413504b07541afacc19
f8294a67548f8f3d1f5913677b070a2ef5bcf120
refs/heads/master
1,685,309,252,764
1,623,232,534,000
1,623,232,534,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
685
lean
/- Copyright (c) 2016 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 Mathlib.Logic.Basic import Mathlib.Function namespace Function theorem left_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := λ a => show h (f (g (i a))) = a by rw [hf (i a), hh a] theorem right_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf
29861a0927cd72a18c7791349cfd1a9a053261b0
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/list/perm.lean
6e1ce76ea2a49196f63c4881b8daf2cf841239cd
[ "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
55,451
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import data.list.dedup import data.list.lattice import data.list.permutation import data.list.zip import logic.relation /-! # List Permutations This file introduces the `list.perm` relation, which is true if two lists are permutations of one another. ## Notation The notation `~` is used for permutation equivalence. -/ open_locale nat universes uu vv namespace list variables {α : Type uu} {β : Type vv} {l₁ l₂ : list α} /-- `perm l₁ l₂` or `l₁ ~ l₂` asserts that `l₁` and `l₂` are permutations of each other. This is defined by induction using pairwise swaps. -/ inductive perm : list α → list α → Prop | nil : perm [] [] | cons : Π (x : α) {l₁ l₂ : list α}, perm l₁ l₂ → perm (x::l₁) (x::l₂) | swap : Π (x y : α) (l : list α), perm (y::x::l) (x::y::l) | trans : Π {l₁ l₂ l₃ : list α}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ open perm (swap) infix ` ~ `:50 := perm @[refl] protected theorem perm.refl : ∀ (l : list α), l ~ l | [] := perm.nil | (x::xs) := (perm.refl xs).cons x @[symm] protected theorem perm.symm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₂ ~ l₁ := perm.rec_on p perm.nil (λ x l₁ l₂ p₁ r₁, r₁.cons x) (λ x y l, swap y x l) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, r₂.trans r₁) theorem perm_comm {l₁ l₂ : list α} : l₁ ~ l₂ ↔ l₂ ~ l₁ := ⟨perm.symm, perm.symm⟩ theorem perm.swap' (x y : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : y::x::l₁ ~ x::y::l₂ := (swap _ _ _).trans ((p.cons _).cons _) attribute [trans] perm.trans theorem perm.eqv (α) : equivalence (@perm α) := mk_equivalence (@perm α) (@perm.refl α) (@perm.symm α) (@perm.trans α) instance is_setoid (α) : setoid (list α) := setoid.mk (@perm α) (perm.eqv α) theorem perm.subset {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁ ⊆ l₂ := λ a, perm.rec_on p (λ h, h) (λ x l₁ l₂ p₁ r₁ i, or.elim i (λ ax, by simp [ax]) (λ al₁, or.inr (r₁ al₁))) (λ x y l ayxl, or.elim ayxl (λ ay, by simp [ay]) (λ axl, or.elim axl (λ ax, by simp [ax]) (λ al, or.inr (or.inr al)))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ ainl₁, r₂ (r₁ ainl₁)) theorem perm.mem_iff {a : α} {l₁ l₂ : list α} (h : l₁ ~ l₂) : a ∈ l₁ ↔ a ∈ l₂ := iff.intro (λ m, h.subset m) (λ m, h.symm.subset m) theorem perm.append_right {l₁ l₂ : list α} (t₁ : list α) (p : l₁ ~ l₂) : l₁++t₁ ~ l₂++t₁ := perm.rec_on p (perm.refl ([] ++ t₁)) (λ x l₁ l₂ p₁ r₁, r₁.cons x) (λ x y l, swap x y _) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, r₁.trans r₂) theorem perm.append_left {t₁ t₂ : list α} : ∀ (l : list α), t₁ ~ t₂ → l++t₁ ~ l++t₂ | [] p := p | (x::xs) p := (perm.append_left xs p).cons x theorem perm.append {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁++t₁ ~ l₂++t₂ := (p₁.append_right t₁).trans (p₂.append_left l₂) theorem perm.append_cons (a : α) {h₁ h₂ t₁ t₂ : list α} (p₁ : h₁ ~ h₂) (p₂ : t₁ ~ t₂) : h₁ ++ a::t₁ ~ h₂ ++ a::t₂ := p₁.append (p₂.cons a) @[simp] theorem perm_middle {a : α} : ∀ {l₁ l₂ : list α}, l₁++a::l₂ ~ a::(l₁++l₂) | [] l₂ := perm.refl _ | (b::l₁) l₂ := ((@perm_middle l₁ l₂).cons _).trans (swap a b _) @[simp] theorem perm_append_singleton (a : α) (l : list α) : l ++ [a] ~ a::l := perm_middle.trans $ by rw [append_nil] theorem perm_append_comm : ∀ {l₁ l₂ : list α}, (l₁++l₂) ~ (l₂++l₁) | [] l₂ := by simp | (a::t) l₂ := (perm_append_comm.cons _).trans perm_middle.symm theorem concat_perm (l : list α) (a : α) : concat l a ~ a :: l := by simp theorem perm.length_eq {l₁ l₂ : list α} (p : l₁ ~ l₂) : length l₁ = length l₂ := perm.rec_on p rfl (λ x l₁ l₂ p r, by simp[r]) (λ x y l, by simp) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) theorem perm.eq_nil {l : list α} (p : l ~ []) : l = [] := eq_nil_of_length_eq_zero p.length_eq theorem perm.nil_eq {l : list α} (p : [] ~ l) : [] = l := p.symm.eq_nil.symm @[simp] theorem perm_nil {l₁ : list α} : l₁ ~ [] ↔ l₁ = [] := ⟨λ p, p.eq_nil, λ e, e ▸ perm.refl _⟩ @[simp] theorem nil_perm {l₁ : list α} : [] ~ l₁ ↔ l₁ = [] := perm_comm.trans perm_nil theorem not_perm_nil_cons (x : α) (l : list α) : ¬ [] ~ x::l | p := by injection p.symm.eq_nil @[simp] theorem reverse_perm : ∀ (l : list α), reverse l ~ l | [] := perm.nil | (a::l) := by { rw reverse_cons, exact (perm_append_singleton _ _).trans ((reverse_perm l).cons a) } theorem perm_cons_append_cons {l l₁ l₂ : list α} (a : α) (p : l ~ l₁++l₂) : a::l ~ l₁++(a::l₂) := (p.cons a).trans perm_middle.symm @[simp] theorem perm_repeat {a : α} {n : ℕ} {l : list α} : l ~ repeat a n ↔ l = repeat a n := ⟨λ p, (eq_repeat.2 ⟨p.length_eq.trans $ length_repeat _ _, λ b m, eq_of_mem_repeat $ p.subset m⟩), λ h, h ▸ perm.refl _⟩ @[simp] theorem repeat_perm {a : α} {n : ℕ} {l : list α} : repeat a n ~ l ↔ repeat a n = l := (perm_comm.trans perm_repeat).trans eq_comm @[simp] theorem perm_singleton {a : α} {l : list α} : l ~ [a] ↔ l = [a] := @perm_repeat α a 1 l @[simp] theorem singleton_perm {a : α} {l : list α} : [a] ~ l ↔ [a] = l := @repeat_perm α a 1 l theorem perm.eq_singleton {a : α} {l : list α} (p : l ~ [a]) : l = [a] := perm_singleton.1 p theorem perm.singleton_eq {a : α} {l : list α} (p : [a] ~ l) : [a] = l := p.symm.eq_singleton.symm theorem singleton_perm_singleton {a b : α} : [a] ~ [b] ↔ a = b := by simp theorem perm_cons_erase [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) : l ~ a :: l.erase a := let ⟨l₁, l₂, _, e₁, e₂⟩ := exists_erase_eq h in e₂.symm ▸ e₁.symm ▸ perm_middle @[elab_as_eliminator] theorem perm_induction_on {P : list α → list α → Prop} {l₁ l₂ : list α} (p : l₁ ~ l₂) (h₁ : P [] []) (h₂ : ∀ x l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (x::l₁) (x::l₂)) (h₃ : ∀ x y l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (y::x::l₁) (x::y::l₂)) (h₄ : ∀ l₁ l₂ l₃, l₁ ~ l₂ → l₂ ~ l₃ → P l₁ l₂ → P l₂ l₃ → P l₁ l₃) : P l₁ l₂ := have P_refl : ∀ l, P l l, from assume l, list.rec_on l h₁ (λ x xs ih, h₂ x xs xs (perm.refl xs) ih), perm.rec_on p h₁ h₂ (λ x y l, h₃ x y l l (perm.refl l) (P_refl l)) h₄ @[congr] theorem perm.filter_map (f : α → option β) {l₁ l₂ : list α} (p : l₁ ~ l₂) : filter_map f l₁ ~ filter_map f l₂ := begin induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂, { simp }, { simp only [filter_map], cases f x with a; simp [filter_map, IH, perm.cons] }, { simp only [filter_map], cases f x with a; cases f y with b; simp [filter_map, swap] }, { exact IH₁.trans IH₂ } end @[congr] theorem perm.map (f : α → β) {l₁ l₂ : list α} (p : l₁ ~ l₂) : map f l₁ ~ map f l₂ := filter_map_eq_map f ▸ p.filter_map _ theorem perm.pmap {p : α → Prop} (f : Π a, p a → β) {l₁ l₂ : list α} (p : l₁ ~ l₂) {H₁ H₂} : pmap f l₁ H₁ ~ pmap f l₂ H₂ := begin induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂, { simp }, { simp [IH, perm.cons] }, { simp [swap] }, { refine IH₁.trans IH₂, exact λ a m, H₂ a (p₂.subset m) } end theorem perm.filter (p : α → Prop) [decidable_pred p] {l₁ l₂ : list α} (s : l₁ ~ l₂) : filter p l₁ ~ filter p l₂ := by rw ← filter_map_eq_filter; apply s.filter_map _ theorem filter_append_perm (p : α → Prop) [decidable_pred p] (l : list α) : filter p l ++ filter (λ x, ¬ p x) l ~ l := begin induction l with x l ih, { refl }, { by_cases h : p x, { simp only [h, filter_cons_of_pos, filter_cons_of_neg, not_true, not_false_iff, cons_append], exact ih.cons x }, { simp only [h, filter_cons_of_neg, not_false_iff, filter_cons_of_pos], refine perm.trans _ (ih.cons x), exact perm_append_comm.trans (perm_append_comm.cons _), } } end theorem exists_perm_sublist {l₁ l₂ l₂' : list α} (s : l₁ <+ l₂) (p : l₂ ~ l₂') : ∃ l₁' ~ l₁, l₁' <+ l₂' := begin induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂ generalizing l₁ s, { exact ⟨[], eq_nil_of_sublist_nil s ▸ perm.refl _, nil_sublist _⟩ }, { cases s with _ _ _ s l₁ _ _ s, { exact let ⟨l₁', p', s'⟩ := IH s in ⟨l₁', p', s'.cons _ _ _⟩ }, { exact let ⟨l₁', p', s'⟩ := IH s in ⟨x::l₁', p'.cons x, s'.cons2 _ _ _⟩ } }, { cases s with _ _ _ s l₁ _ _ s; cases s with _ _ _ s l₁ _ _ s, { exact ⟨l₁, perm.refl _, (s.cons _ _ _).cons _ _ _⟩ }, { exact ⟨x::l₁, perm.refl _, (s.cons _ _ _).cons2 _ _ _⟩ }, { exact ⟨y::l₁, perm.refl _, (s.cons2 _ _ _).cons _ _ _⟩ }, { exact ⟨x::y::l₁, perm.swap _ _ _, (s.cons2 _ _ _).cons2 _ _ _⟩ } }, { exact let ⟨m₁, pm, sm⟩ := IH₁ s, ⟨r₁, pr, sr⟩ := IH₂ sm in ⟨r₁, pr.trans pm, sr⟩ } end theorem perm.sizeof_eq_sizeof [has_sizeof α] {l₁ l₂ : list α} (h : l₁ ~ l₂) : l₁.sizeof = l₂.sizeof := begin induction h with hd l₁ l₂ h₁₂ h_sz₁₂ a b l l₁ l₂ l₃ h₁₂ h₂₃ h_sz₁₂ h_sz₂₃, { refl }, { simp only [list.sizeof, h_sz₁₂] }, { simp only [list.sizeof, add_left_comm] }, { simp only [h_sz₁₂, h_sz₂₃] } end section rel open relator variables {γ : Type*} {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop} local infixr ` ∘r ` : 80 := relation.comp lemma perm_comp_perm : (perm ∘r perm : list α → list α → Prop) = perm := begin funext a c, apply propext, split, { exact assume ⟨b, hab, hba⟩, perm.trans hab hba }, { exact assume h, ⟨a, perm.refl a, h⟩ } end lemma perm_comp_forall₂ {l u v} (hlu : perm l u) (huv : forall₂ r u v) : (forall₂ r ∘r perm) l v := begin induction hlu generalizing v, case perm.nil { cases huv, exact ⟨[], forall₂.nil, perm.nil⟩ }, case perm.cons : a l u hlu ih { cases huv with _ b _ v hab huv', rcases ih huv' with ⟨l₂, h₁₂, h₂₃⟩, exact ⟨b::l₂, forall₂.cons hab h₁₂, h₂₃.cons _⟩ }, case perm.swap : a₁ a₂ l₁ l₂ h₂₃ { cases h₂₃ with _ b₁ _ l₂ h₁ hr_₂₃, cases hr_₂₃ with _ b₂ _ l₂ h₂ h₁₂, exact ⟨b₂::b₁::l₂, forall₂.cons h₂ (forall₂.cons h₁ h₁₂), perm.swap _ _ _⟩ }, case perm.trans : la₁ la₂ la₃ _ _ ih₁ ih₂ { rcases ih₂ huv with ⟨lb₂, hab₂, h₂₃⟩, rcases ih₁ hab₂ with ⟨lb₁, hab₁, h₁₂⟩, exact ⟨lb₁, hab₁, perm.trans h₁₂ h₂₃⟩ } end lemma forall₂_comp_perm_eq_perm_comp_forall₂ : forall₂ r ∘r perm = perm ∘r forall₂ r := begin funext l₁ l₃, apply propext, split, { assume h, rcases h with ⟨l₂, h₁₂, h₂₃⟩, have : forall₂ (flip r) l₂ l₁, from h₁₂.flip , rcases perm_comp_forall₂ h₂₃.symm this with ⟨l', h₁, h₂⟩, exact ⟨l', h₂.symm, h₁.flip⟩ }, { exact assume ⟨l₂, h₁₂, h₂₃⟩, perm_comp_forall₂ h₁₂ h₂₃ } end lemma rel_perm_imp (hr : right_unique r) : (forall₂ r ⇒ forall₂ r ⇒ implies) perm perm := assume a b h₁ c d h₂ h, have (flip (forall₂ r) ∘r (perm ∘r forall₂ r)) b d, from ⟨a, h₁, c, h, h₂⟩, have ((flip (forall₂ r) ∘r forall₂ r) ∘r perm) b d, by rwa [← forall₂_comp_perm_eq_perm_comp_forall₂, ← relation.comp_assoc] at this, let ⟨b', ⟨c', hbc, hcb⟩, hbd⟩ := this in have b' = b, from right_unique_forall₂' hr hcb hbc, this ▸ hbd lemma rel_perm (hr : bi_unique r) : (forall₂ r ⇒ forall₂ r ⇒ (↔)) perm perm := assume a b hab c d hcd, iff.intro (rel_perm_imp hr.2 hab hcd) (rel_perm_imp hr.left.flip hab.flip hcd.flip) end rel section subperm /-- `subperm l₁ l₂`, denoted `l₁ <+~ l₂`, means that `l₁` is a sublist of a permutation of `l₂`. This is an analogue of `l₁ ⊆ l₂` which respects multiplicities of elements, and is used for the `≤` relation on multisets. -/ def subperm (l₁ l₂ : list α) : Prop := ∃ l ~ l₁, l <+ l₂ infix ` <+~ `:50 := subperm theorem nil_subperm {l : list α} : [] <+~ l := ⟨[], perm.nil, by simp⟩ theorem perm.subperm_left {l l₁ l₂ : list α} (p : l₁ ~ l₂) : l <+~ l₁ ↔ l <+~ l₂ := suffices ∀ {l₁ l₂ : list α}, l₁ ~ l₂ → l <+~ l₁ → l <+~ l₂, from ⟨this p, this p.symm⟩, λ l₁ l₂ p ⟨u, pu, su⟩, let ⟨v, pv, sv⟩ := exists_perm_sublist su p in ⟨v, pv.trans pu, sv⟩ theorem perm.subperm_right {l₁ l₂ l : list α} (p : l₁ ~ l₂) : l₁ <+~ l ↔ l₂ <+~ l := ⟨λ ⟨u, pu, su⟩, ⟨u, pu.trans p, su⟩, λ ⟨u, pu, su⟩, ⟨u, pu.trans p.symm, su⟩⟩ theorem sublist.subperm {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁ <+~ l₂ := ⟨l₁, perm.refl _, s⟩ theorem perm.subperm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁ <+~ l₂ := ⟨l₂, p.symm, sublist.refl _⟩ @[refl] theorem subperm.refl (l : list α) : l <+~ l := (perm.refl _).subperm @[trans] theorem subperm.trans {l₁ l₂ l₃ : list α} : l₁ <+~ l₂ → l₂ <+~ l₃ → l₁ <+~ l₃ | s ⟨l₂', p₂, s₂⟩ := let ⟨l₁', p₁, s₁⟩ := p₂.subperm_left.2 s in ⟨l₁', p₁, s₁.trans s₂⟩ theorem subperm.length_le {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₁ ≤ length l₂ | ⟨l, p, s⟩ := p.length_eq ▸ length_le_of_sublist s theorem subperm.perm_of_length_le {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₂ ≤ length l₁ → l₁ ~ l₂ | ⟨l, p, s⟩ h := suffices l = l₂, from this ▸ p.symm, eq_of_sublist_of_length_le s $ p.symm.length_eq ▸ h theorem subperm.antisymm {l₁ l₂ : list α} (h₁ : l₁ <+~ l₂) (h₂ : l₂ <+~ l₁) : l₁ ~ l₂ := h₁.perm_of_length_le h₂.length_le theorem subperm.subset {l₁ l₂ : list α} : l₁ <+~ l₂ → l₁ ⊆ l₂ | ⟨l, p, s⟩ := subset.trans p.symm.subset s.subset lemma subperm.filter (p : α → Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l <+~ l') : filter p l <+~ filter p l' := begin obtain ⟨xs, hp, h⟩ := h, exact ⟨_, hp.filter p, h.filter p⟩ end end subperm theorem sublist.exists_perm_append : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → ∃ l, l₂ ~ l₁ ++ l | ._ ._ sublist.slnil := ⟨nil, perm.refl _⟩ | ._ ._ (sublist.cons l₁ l₂ a s) := let ⟨l, p⟩ := sublist.exists_perm_append s in ⟨a::l, (p.cons a).trans perm_middle.symm⟩ | ._ ._ (sublist.cons2 l₁ l₂ a s) := let ⟨l, p⟩ := sublist.exists_perm_append s in ⟨l, p.cons a⟩ theorem perm.countp_eq (p : α → Prop) [decidable_pred p] {l₁ l₂ : list α} (s : l₁ ~ l₂) : countp p l₁ = countp p l₂ := by rw [countp_eq_length_filter, countp_eq_length_filter]; exact (s.filter _).length_eq theorem subperm.countp_le (p : α → Prop) [decidable_pred p] {l₁ l₂ : list α} : l₁ <+~ l₂ → countp p l₁ ≤ countp p l₂ | ⟨l, p', s⟩ := p'.countp_eq p ▸ s.countp_le p theorem perm.countp_congr (s : l₁ ~ l₂) {p p' : α → Prop} [decidable_pred p] [decidable_pred p'] (hp : ∀ x ∈ l₁, p x = p' x) : l₁.countp p = l₂.countp p' := begin rw ← s.countp_eq p', clear s, induction l₁ with y s hs, { refl }, { simp only [mem_cons_iff, forall_eq_or_imp] at hp, simp only [countp_cons, hs hp.2, hp.1], }, end theorem countp_eq_countp_filter_add (l : list α) (p q : α → Prop) [decidable_pred p] [decidable_pred q] : l.countp p = (l.filter q).countp p + (l.filter (λ a, ¬ q a)).countp p := by { rw [← countp_append], exact perm.countp_eq _ (filter_append_perm _ _).symm } theorem perm.count_eq [decidable_eq α] {l₁ l₂ : list α} (p : l₁ ~ l₂) (a) : count a l₁ = count a l₂ := p.countp_eq _ theorem subperm.count_le [decidable_eq α] {l₁ l₂ : list α} (s : l₁ <+~ l₂) (a) : count a l₁ ≤ count a l₂ := s.countp_le _ theorem perm.foldl_eq' {f : β → α → β} {l₁ l₂ : list α} (p : l₁ ~ l₂) : (∀ (x ∈ l₁) (y ∈ l₁) z, f (f z x) y = f (f z y) x) → ∀ b, foldl f b l₁ = foldl f b l₂ := perm_induction_on p (λ H b, rfl) (λ x t₁ t₂ p r H b, r (λ x hx y hy, H _ (or.inr hx) _ (or.inr hy)) _) (λ x y t₁ t₂ p r H b, begin simp only [foldl], rw [H x (or.inr $ or.inl rfl) y (or.inl rfl)], exact r (λ x hx y hy, H _ (or.inr $ or.inr hx) _ (or.inr $ or.inr hy)) _ end) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ H b, eq.trans (r₁ H b) (r₂ (λ x hx y hy, H _ (p₁.symm.subset hx) _ (p₁.symm.subset hy)) b)) theorem perm.foldl_eq {f : β → α → β} {l₁ l₂ : list α} (rcomm : right_commutative f) (p : l₁ ~ l₂) : ∀ b, foldl f b l₁ = foldl f b l₂ := p.foldl_eq' $ λ x hx y hy z, rcomm z x y theorem perm.foldr_eq {f : α → β → β} {l₁ l₂ : list α} (lcomm : left_commutative f) (p : l₁ ~ l₂) : ∀ b, foldr f b l₁ = foldr f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, by simp; rw [r b]) (λ x y t₁ t₂ p r b, by simp; rw [lcomm, r b]) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ a, eq.trans (r₁ a) (r₂ a)) lemma perm.rec_heq {β : list α → Sort*} {f : Πa l, β l → β (a::l)} {b : β []} {l l' : list α} (hl : perm l l') (f_congr : ∀{a l l' b b'}, perm l l' → b == b' → f a l b == f a l' b') (f_swap : ∀{a a' l b}, f a (a'::l) (f a' l b) == f a' (a::l) (f a l b)) : @list.rec α β b f l == @list.rec α β b f l' := begin induction hl, case list.perm.nil { refl }, case list.perm.cons : a l l' h ih { exact f_congr h ih }, case list.perm.swap : a a' l { exact f_swap }, case list.perm.trans : l₁ l₂ l₃ h₁ h₂ ih₁ ih₂ { exact heq.trans ih₁ ih₂ } end section variables {op : α → α → α} [is_associative α op] [is_commutative α op] local notation a * b := op a b local notation l <*> a := foldl op a l lemma perm.fold_op_eq {l₁ l₂ : list α} {a : α} (h : l₁ ~ l₂) : l₁ <*> a = l₂ <*> a := h.foldl_eq (right_comm _ is_commutative.comm is_associative.assoc) _ end section comm_monoid /-- If elements of a list commute with each other, then their product does not depend on the order of elements-/ @[to_additive] lemma perm.prod_eq' [monoid α] {l₁ l₂ : list α} (h : l₁ ~ l₂) (hc : l₁.pairwise (λ x y, x * y = y * x)) : l₁.prod = l₂.prod := h.foldl_eq' (pairwise.forall_of_forall (λ x y h z, (h z).symm) (λ x hx z, rfl) $ hc.imp $ λ x y h z, by simp only [mul_assoc, h]) _ variable [comm_monoid α] @[to_additive] lemma perm.prod_eq {l₁ l₂ : list α} (h : perm l₁ l₂) : prod l₁ = prod l₂ := h.fold_op_eq @[to_additive] lemma prod_reverse (l : list α) : prod l.reverse = prod l := (reverse_perm l).prod_eq end comm_monoid theorem perm_inv_core {a : α} {l₁ l₂ r₁ r₂ : list α} : l₁++a::r₁ ~ l₂++a::r₂ → l₁++r₁ ~ l₂++r₂ := begin generalize e₁ : l₁++a::r₁ = s₁, generalize e₂ : l₂++a::r₂ = s₂, intro p, revert l₁ l₂ r₁ r₂ e₁ e₂, refine perm_induction_on p _ (λ x t₁ t₂ p IH, _) (λ x y t₁ t₂ p IH, _) (λ t₁ t₂ t₃ p₁ p₂ IH₁ IH₂, _); intros l₁ l₂ r₁ r₂ e₁ e₂, { apply (not_mem_nil a).elim, rw ← e₁, simp }, { cases l₁ with y l₁; cases l₂ with z l₂; dsimp at e₁ e₂; injections; subst x, { substs t₁ t₂, exact p }, { substs z t₁ t₂, exact p.trans perm_middle }, { substs y t₁ t₂, exact perm_middle.symm.trans p }, { substs z t₁ t₂, exact (IH rfl rfl).cons y } }, { rcases l₁ with _|⟨y, _|⟨z, l₁⟩⟩; rcases l₂ with _|⟨u, _|⟨v, l₂⟩⟩; dsimp at e₁ e₂; injections; substs x y, { substs r₁ r₂, exact p.cons a }, { substs r₁ r₂, exact p.cons u }, { substs r₁ v t₂, exact (p.trans perm_middle).cons u }, { substs r₁ r₂, exact p.cons y }, { substs r₁ r₂ y u, exact p.cons a }, { substs r₁ u v t₂, exact ((p.trans perm_middle).cons y).trans (swap _ _ _) }, { substs r₂ z t₁, exact (perm_middle.symm.trans p).cons y }, { substs r₂ y z t₁, exact (swap _ _ _).trans ((perm_middle.symm.trans p).cons u) }, { substs u v t₁ t₂, exact (IH rfl rfl).swap' _ _ } }, { substs t₁ t₃, have : a ∈ t₂ := p₁.subset (by simp), rcases mem_split this with ⟨l₂, r₂, e₂⟩, subst t₂, exact (IH₁ rfl rfl).trans (IH₂ rfl rfl) } end theorem perm.cons_inv {a : α} {l₁ l₂ : list α} : a::l₁ ~ a::l₂ → l₁ ~ l₂ := @perm_inv_core _ _ [] [] _ _ @[simp] theorem perm_cons (a : α) {l₁ l₂ : list α} : a::l₁ ~ a::l₂ ↔ l₁ ~ l₂ := ⟨perm.cons_inv, perm.cons a⟩ theorem perm_append_left_iff {l₁ l₂ : list α} : ∀ l, l++l₁ ~ l++l₂ ↔ l₁ ~ l₂ | [] := iff.rfl | (a::l) := (perm_cons a).trans (perm_append_left_iff l) theorem perm_append_right_iff {l₁ l₂ : list α} (l) : l₁++l ~ l₂++l ↔ l₁ ~ l₂ := ⟨λ p, (perm_append_left_iff _).1 $ perm_append_comm.trans $ p.trans perm_append_comm, perm.append_right _⟩ theorem perm_option_to_list {o₁ o₂ : option α} : o₁.to_list ~ o₂.to_list ↔ o₁ = o₂ := begin refine ⟨λ p, _, λ e, e ▸ perm.refl _⟩, cases o₁ with a; cases o₂ with b, {refl}, { cases p.length_eq }, { cases p.length_eq }, { exact option.mem_to_list.1 (p.symm.subset $ by simp) } end theorem subperm_cons (a : α) {l₁ l₂ : list α} : a::l₁ <+~ a::l₂ ↔ l₁ <+~ l₂ := ⟨λ ⟨l, p, s⟩, begin cases s with _ _ _ s' u _ _ s', { exact (p.subperm_left.2 $ (sublist_cons _ _).subperm).trans s'.subperm }, { exact ⟨u, p.cons_inv, s'⟩ } end, λ ⟨l, p, s⟩, ⟨a::l, p.cons a, s.cons2 _ _ _⟩⟩ alias subperm_cons ↔ subperm.of_cons subperm.cons attribute [protected] subperm.cons theorem cons_subperm_of_mem {a : α} {l₁ l₂ : list α} (d₁ : nodup l₁) (h₁ : a ∉ l₁) (h₂ : a ∈ l₂) (s : l₁ <+~ l₂) : a :: l₁ <+~ l₂ := begin rcases s with ⟨l, p, s⟩, induction s generalizing l₁, case list.sublist.slnil { cases h₂ }, case list.sublist.cons : r₁ r₂ b s' ih { simp at h₂, cases h₂ with e m, { subst b, exact ⟨a::r₁, p.cons a, s'.cons2 _ _ _⟩ }, { rcases ih m d₁ h₁ p with ⟨t, p', s'⟩, exact ⟨t, p', s'.cons _ _ _⟩ } }, case list.sublist.cons2 : r₁ r₂ b s' ih { have bm : b ∈ l₁ := (p.subset $ mem_cons_self _ _), have am : a ∈ r₂ := h₂.resolve_left (λ e, h₁ $ e.symm ▸ bm), rcases mem_split bm with ⟨t₁, t₂, rfl⟩, have st : t₁ ++ t₂ <+ t₁ ++ b :: t₂ := by simp, rcases ih am (d₁.sublist st) (mt (λ x, st.subset x) h₁) (perm.cons_inv $ p.trans perm_middle) with ⟨t, p', s'⟩, exact ⟨b::t, (p'.cons b).trans $ (swap _ _ _).trans (perm_middle.symm.cons a), s'.cons2 _ _ _⟩ } end theorem subperm_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+~ l++l₂ ↔ l₁ <+~ l₂ | [] := iff.rfl | (a::l) := (subperm_cons a).trans (subperm_append_left l) theorem subperm_append_right {l₁ l₂ : list α} (l) : l₁++l <+~ l₂++l ↔ l₁ <+~ l₂ := (perm_append_comm.subperm_left.trans perm_append_comm.subperm_right).trans (subperm_append_left l) theorem subperm.exists_of_length_lt {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₁ < length l₂ → ∃ a, a :: l₁ <+~ l₂ | ⟨l, p, s⟩ h := suffices length l < length l₂ → ∃ (a : α), a :: l <+~ l₂, from (this $ p.symm.length_eq ▸ h).imp (λ a, (p.cons a).subperm_right.1), begin clear subperm.exists_of_length_lt p h l₁, rename l₂ u, induction s with l₁ l₂ a s IH _ _ b s IH; intro h, { cases h }, { cases lt_or_eq_of_le (nat.le_of_lt_succ h : length l₁ ≤ length l₂) with h h, { exact (IH h).imp (λ a s, s.trans (sublist_cons _ _).subperm) }, { exact ⟨a, eq_of_sublist_of_length_eq s h ▸ subperm.refl _⟩ } }, { exact (IH $ nat.lt_of_succ_lt_succ h).imp (λ a s, (swap _ _ _).subperm_right.1 $ (subperm_cons _).2 s) } end protected lemma nodup.subperm (d : nodup l₁) (H : l₁ ⊆ l₂) : l₁ <+~ l₂ := begin induction d with a l₁' h d IH, { exact ⟨nil, perm.nil, nil_sublist _⟩ }, { cases forall_mem_cons.1 H with H₁ H₂, simp at h, exact cons_subperm_of_mem d h H₁ (IH H₂) } end theorem perm_ext {l₁ l₂ : list α} (d₁ : nodup l₁) (d₂ : nodup l₂) : l₁ ~ l₂ ↔ ∀a, a ∈ l₁ ↔ a ∈ l₂ := ⟨λ p a, p.mem_iff, λ H, (d₁.subperm $ λ a, (H a).1).antisymm $ d₂.subperm $ λ a, (H a).2⟩ theorem nodup.sublist_ext {l₁ l₂ l : list α} (d : nodup l) (s₁ : l₁ <+ l) (s₂ : l₂ <+ l) : l₁ ~ l₂ ↔ l₁ = l₂ := ⟨λ h, begin induction s₂ with l₂ l a s₂ IH l₂ l a s₂ IH generalizing l₁, { exact h.eq_nil }, { simp at d, cases s₁ with _ _ _ s₁ l₁ _ _ s₁, { exact IH d.2 s₁ h }, { apply d.1.elim, exact subperm.subset ⟨_, h.symm, s₂⟩ (mem_cons_self _ _) } }, { simp at d, cases s₁ with _ _ _ s₁ l₁ _ _ s₁, { apply d.1.elim, exact subperm.subset ⟨_, h, s₁⟩ (mem_cons_self _ _) }, { rw IH d.2 s₁ h.cons_inv } } end, λ h, by rw h⟩ section variable [decidable_eq α] -- attribute [congr] theorem perm.erase (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁.erase a ~ l₂.erase a := if h₁ : a ∈ l₁ then have h₂ : a ∈ l₂, from p.subset h₁, perm.cons_inv $ (perm_cons_erase h₁).symm.trans $ p.trans (perm_cons_erase h₂) else have h₂ : a ∉ l₂, from mt p.mem_iff.2 h₁, by rw [erase_of_not_mem h₁, erase_of_not_mem h₂]; exact p theorem subperm_cons_erase (a : α) (l : list α) : l <+~ a :: l.erase a := begin by_cases h : a ∈ l, { exact (perm_cons_erase h).subperm }, { rw [erase_of_not_mem h], exact (sublist_cons _ _).subperm } end theorem erase_subperm (a : α) (l : list α) : l.erase a <+~ l := (erase_sublist _ _).subperm theorem subperm.erase {l₁ l₂ : list α} (a : α) (h : l₁ <+~ l₂) : l₁.erase a <+~ l₂.erase a := let ⟨l, hp, hs⟩ := h in ⟨l.erase a, hp.erase _, hs.erase _⟩ theorem perm.diff_right {l₁ l₂ : list α} (t : list α) (h : l₁ ~ l₂) : l₁.diff t ~ l₂.diff t := by induction t generalizing l₁ l₂ h; simp [*, perm.erase] theorem perm.diff_left (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : l.diff t₁ = l.diff t₂ := by induction h generalizing l; simp [*, perm.erase, erase_comm] <|> exact (ih_1 _).trans (ih_2 _) theorem perm.diff {l₁ l₂ t₁ t₂ : list α} (hl : l₁ ~ l₂) (ht : t₁ ~ t₂) : l₁.diff t₁ ~ l₂.diff t₂ := ht.diff_left l₂ ▸ hl.diff_right _ theorem subperm.diff_right {l₁ l₂ : list α} (h : l₁ <+~ l₂) (t : list α) : l₁.diff t <+~ l₂.diff t := by induction t generalizing l₁ l₂ h; simp [*, subperm.erase] theorem erase_cons_subperm_cons_erase (a b : α) (l : list α) : (a :: l).erase b <+~ a :: l.erase b := begin by_cases h : a = b, { subst b, rw [erase_cons_head], apply subperm_cons_erase }, { rw [erase_cons_tail _ h] } end theorem subperm_cons_diff {a : α} : ∀ {l₁ l₂ : list α}, (a :: l₁).diff l₂ <+~ a :: l₁.diff l₂ | l₁ [] := ⟨a::l₁, by simp⟩ | l₁ (b::l₂) := begin simp only [diff_cons], refine ((erase_cons_subperm_cons_erase a b l₁).diff_right l₂).trans _, apply subperm_cons_diff end theorem subset_cons_diff {a : α} {l₁ l₂ : list α} : (a :: l₁).diff l₂ ⊆ a :: l₁.diff l₂ := subperm_cons_diff.subset theorem perm.bag_inter_right {l₁ l₂ : list α} (t : list α) (h : l₁ ~ l₂) : l₁.bag_inter t ~ l₂.bag_inter t := begin induction h with x _ _ _ _ x y _ _ _ _ _ _ ih_1 ih_2 generalizing t, {simp}, { by_cases x ∈ t; simp [*, perm.cons] }, { by_cases x = y, {simp [h]}, by_cases xt : x ∈ t; by_cases yt : y ∈ t, { simp [xt, yt, mem_erase_of_ne h, mem_erase_of_ne (ne.symm h), erase_comm, swap] }, { simp [xt, yt, mt mem_of_mem_erase, perm.cons] }, { simp [xt, yt, mt mem_of_mem_erase, perm.cons] }, { simp [xt, yt] } }, { exact (ih_1 _).trans (ih_2 _) } end theorem perm.bag_inter_left (l : list α) {t₁ t₂ : list α} (p : t₁ ~ t₂) : l.bag_inter t₁ = l.bag_inter t₂ := begin induction l with a l IH generalizing t₁ t₂ p, {simp}, by_cases a ∈ t₁, { simp [h, p.subset h, IH (p.erase _)] }, { simp [h, mt p.mem_iff.2 h, IH p] } end theorem perm.bag_inter {l₁ l₂ t₁ t₂ : list α} (hl : l₁ ~ l₂) (ht : t₁ ~ t₂) : l₁.bag_inter t₁ ~ l₂.bag_inter t₂ := ht.bag_inter_left l₂ ▸ hl.bag_inter_right _ theorem cons_perm_iff_perm_erase {a : α} {l₁ l₂ : list α} : a::l₁ ~ l₂ ↔ a ∈ l₂ ∧ l₁ ~ l₂.erase a := ⟨λ h, have a ∈ l₂, from h.subset (mem_cons_self a l₁), ⟨this, (h.trans $ perm_cons_erase this).cons_inv⟩, λ ⟨m, h⟩, (h.cons a).trans (perm_cons_erase m).symm⟩ theorem perm_iff_count {l₁ l₂ : list α} : l₁ ~ l₂ ↔ ∀ a, count a l₁ = count a l₂ := ⟨perm.count_eq, λ H, begin induction l₁ with a l₁ IH generalizing l₂, { cases l₂ with b l₂, {refl}, specialize H b, simp at H, contradiction }, { have : a ∈ l₂ := count_pos.1 (by rw ← H; simp; apply nat.succ_pos), refine ((IH $ λ b, _).cons a).trans (perm_cons_erase this).symm, specialize H b, rw (perm_cons_erase this).count_eq at H, by_cases b = a; simp [h] at H ⊢; assumption } end⟩ lemma subperm.cons_right {α : Type*} {l l' : list α} (x : α) (h : l <+~ l') : l <+~ x :: l' := h.trans (sublist_cons x l').subperm /-- The list version of `add_tsub_cancel_of_le` for multisets. -/ lemma subperm_append_diff_self_of_count_le {l₁ l₂ : list α} (h : ∀ x ∈ l₁, count x l₁ ≤ count x l₂) : l₁ ++ l₂.diff l₁ ~ l₂ := begin induction l₁ with hd tl IH generalizing l₂, { simp }, { have : hd ∈ l₂, { rw ←count_pos, exact lt_of_lt_of_le (count_pos.mpr (mem_cons_self _ _)) (h hd (mem_cons_self _ _)) }, replace this : l₂ ~ hd :: l₂.erase hd := perm_cons_erase this, refine perm.trans _ this.symm, rw [cons_append, diff_cons, perm_cons], refine IH (λ x hx, _), specialize h x (mem_cons_of_mem _ hx), rw (perm_iff_count.mp this) at h, by_cases hx : x = hd, { subst hd, simpa [nat.succ_le_succ_iff] using h }, { simpa [hx] using h } }, end /-- The list version of `multiset.le_iff_count`. -/ lemma subperm_ext_iff {l₁ l₂ : list α} : l₁ <+~ l₂ ↔ ∀ x ∈ l₁, count x l₁ ≤ count x l₂ := begin refine ⟨λ h x hx, subperm.count_le h x, λ h, _⟩, suffices : l₁ <+~ (l₂.diff l₁ ++ l₁), { refine this.trans (perm.subperm _), exact perm_append_comm.trans (subperm_append_diff_self_of_count_le h) }, convert (subperm_append_right _).mpr nil_subperm using 1 end @[simp] lemma subperm_singleton_iff {α} {l : list α} {a : α} : [a] <+~ l ↔ a ∈ l := ⟨λ ⟨s, hla, h⟩, by rwa [perm_singleton.mp hla, singleton_sublist] at h, λ h, ⟨[a], perm.refl _, singleton_sublist.mpr h⟩⟩ lemma subperm.cons_left {l₁ l₂ : list α} (h : l₁ <+~ l₂) (x : α) (hx : count x l₁ < count x l₂) : x :: l₁ <+~ l₂ := begin rw subperm_ext_iff at h ⊢, intros y hy, by_cases hy' : y = x, { subst x, simpa using nat.succ_le_of_lt hx }, { rw count_cons_of_ne hy', refine h y _, simpa [hy'] using hy } end instance decidable_perm : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂) | [] [] := is_true $ perm.refl _ | [] (b::l₂) := is_false $ λ h, by have := h.nil_eq; contradiction | (a::l₁) l₂ := by haveI := decidable_perm l₁ (l₂.erase a); exact decidable_of_iff' _ cons_perm_iff_perm_erase -- @[congr] theorem perm.dedup {l₁ l₂ : list α} (p : l₁ ~ l₂) : dedup l₁ ~ dedup l₂ := perm_iff_count.2 $ λ a, if h : a ∈ l₁ then by simp [nodup_dedup, h, p.subset h] else by simp [h, mt p.mem_iff.2 h] -- attribute [congr] theorem perm.insert (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : insert a l₁ ~ insert a l₂ := if h : a ∈ l₁ then by simpa [h, p.subset h] using p else by simpa [h, mt p.mem_iff.2 h] using p.cons a theorem perm_insert_swap (x y : α) (l : list α) : insert x (insert y l) ~ insert y (insert x l) := begin by_cases xl : x ∈ l; by_cases yl : y ∈ l; simp [xl, yl], by_cases xy : x = y, { simp [xy] }, simp [not_mem_cons_of_ne_of_not_mem xy xl, not_mem_cons_of_ne_of_not_mem (ne.symm xy) yl], constructor end theorem perm_insert_nth {α} (x : α) (l : list α) {n} (h : n ≤ l.length) : insert_nth n x l ~ x :: l := begin induction l generalizing n, { cases n, refl, cases h }, cases n, { simp [insert_nth] }, { simp only [insert_nth, modify_nth_tail], transitivity, { apply perm.cons, apply l_ih, apply nat.le_of_succ_le_succ h }, { apply perm.swap } } end theorem perm.union_right {l₁ l₂ : list α} (t₁ : list α) (h : l₁ ~ l₂) : l₁ ∪ t₁ ~ l₂ ∪ t₁ := begin induction h with a _ _ _ ih _ _ _ _ _ _ _ _ ih_1 ih_2; try {simp}, { exact ih.insert a }, { apply perm_insert_swap }, { exact ih_1.trans ih_2 } end theorem perm.union_left (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : l ∪ t₁ ~ l ∪ t₂ := by induction l; simp [*, perm.insert] -- @[congr] theorem perm.union {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁ ∪ t₁ ~ l₂ ∪ t₂ := (p₁.union_right t₁).trans (p₂.union_left l₂) theorem perm.inter_right {l₁ l₂ : list α} (t₁ : list α) : l₁ ~ l₂ → l₁ ∩ t₁ ~ l₂ ∩ t₁ := perm.filter _ theorem perm.inter_left (l : list α) {t₁ t₂ : list α} (p : t₁ ~ t₂) : l ∩ t₁ = l ∩ t₂ := filter_congr' (λ a _, p.mem_iff) -- @[congr] theorem perm.inter {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁ ∩ t₁ ~ l₂ ∩ t₂ := p₂.inter_left l₂ ▸ p₁.inter_right t₁ theorem perm.inter_append {l t₁ t₂ : list α} (h : disjoint t₁ t₂) : l ∩ (t₁ ++ t₂) ~ l ∩ t₁ ++ l ∩ t₂ := begin induction l, case list.nil { simp }, case list.cons : x xs l_ih { by_cases h₁ : x ∈ t₁, { have h₂ : x ∉ t₂ := h h₁, simp * }, by_cases h₂ : x ∈ t₂, { simp only [*, inter_cons_of_not_mem, false_or, mem_append, inter_cons_of_mem, not_false_iff], transitivity, { apply perm.cons _ l_ih, }, change [x] ++ xs ∩ t₁ ++ xs ∩ t₂ ~ xs ∩ t₁ ++ ([x] ++ xs ∩ t₂), rw [← list.append_assoc], solve_by_elim [perm.append_right, perm_append_comm] }, { simp * } }, end end theorem perm.pairwise_iff {R : α → α → Prop} (S : symmetric R) : ∀ {l₁ l₂ : list α} (p : l₁ ~ l₂), pairwise R l₁ ↔ pairwise R l₂ := suffices ∀ {l₁ l₂}, l₁ ~ l₂ → pairwise R l₁ → pairwise R l₂, from λ l₁ l₂ p, ⟨this p, this p.symm⟩, λ l₁ l₂ p d, begin induction d with a l₁ h d IH generalizing l₂, { rw ← p.nil_eq, constructor }, { have : a ∈ l₂ := p.subset (mem_cons_self _ _), rcases mem_split this with ⟨s₂, t₂, rfl⟩, have p' := (p.trans perm_middle).cons_inv, refine (pairwise_middle S).2 (pairwise_cons.2 ⟨λ b m, _, IH _ p'⟩), exact h _ (p'.symm.subset m) } end theorem perm.nodup_iff {l₁ l₂ : list α} : l₁ ~ l₂ → (nodup l₁ ↔ nodup l₂) := perm.pairwise_iff $ @ne.symm α theorem perm.bind_right {l₁ l₂ : list α} (f : α → list β) (p : l₁ ~ l₂) : l₁.bind f ~ l₂.bind f := begin induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp}, { simp, exact IH.append_left _ }, { simp, rw [← append_assoc, ← append_assoc], exact perm_append_comm.append_right _ }, { exact IH₁.trans IH₂ } end theorem perm.bind_left (l : list α) {f g : α → list β} (h : ∀ a, f a ~ g a) : l.bind f ~ l.bind g := by induction l with a l IH; simp; exact (h a).append IH theorem bind_append_perm (l : list α) (f g : α → list β) : l.bind f ++ l.bind g ~ l.bind (λ x, f x ++ g x) := begin induction l with a l IH; simp, refine (perm.trans _ (IH.append_left _)).append_left _, rw [← append_assoc, ← append_assoc], exact perm_append_comm.append_right _ end theorem perm.product_right {l₁ l₂ : list α} (t₁ : list β) (p : l₁ ~ l₂) : product l₁ t₁ ~ product l₂ t₁ := p.bind_right _ theorem perm.product_left (l : list α) {t₁ t₂ : list β} (p : t₁ ~ t₂) : product l t₁ ~ product l t₂ := perm.bind_left _ $ λ a, p.map _ @[congr] theorem perm.product {l₁ l₂ : list α} {t₁ t₂ : list β} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : product l₁ t₁ ~ product l₂ t₂ := (p₁.product_right t₁).trans (p₂.product_left l₂) theorem sublists_cons_perm_append (a : α) (l : list α) : sublists (a :: l) ~ sublists l ++ map (cons a) (sublists l) := begin simp only [sublists, sublists_aux_cons_cons, cons_append, perm_cons], refine (perm.cons _ _).trans perm_middle.symm, induction sublists_aux l cons with b l IH; simp, exact (IH.cons _).trans perm_middle.symm end theorem sublists_perm_sublists' : ∀ l : list α, sublists l ~ sublists' l | [] := perm.refl _ | (a::l) := let IH := sublists_perm_sublists' l in by rw sublists'_cons; exact (sublists_cons_perm_append _ _).trans (IH.append (IH.map _)) theorem revzip_sublists (l : list α) : ∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists → l₁ ++ l₂ ~ l := begin rw revzip, apply list.reverse_rec_on l, { intros l₁ l₂ h, simp at h, simp [h] }, { intros l a IH l₁ l₂ h, rw [sublists_concat, reverse_append, zip_append, ← map_reverse, zip_map_right, zip_map_left] at h; [skip, {simp}], simp only [prod.mk.inj_iff, mem_map, mem_append, prod.map_mk, prod.exists] at h, rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', l₂, h, rfl, rfl⟩, { rw ← append_assoc, exact (IH _ _ h).append_right _ }, { rw append_assoc, apply (perm_append_comm.append_left _).trans, rw ← append_assoc, exact (IH _ _ h).append_right _ } } end theorem revzip_sublists' (l : list α) : ∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists' → l₁ ++ l₂ ~ l := begin rw revzip, induction l with a l IH; intros l₁ l₂ h, { simp at h, simp [h] }, { rw [sublists'_cons, reverse_append, zip_append, ← map_reverse, zip_map_right, zip_map_left] at h; [simp at h, simp], rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', h, rfl⟩, { exact perm_middle.trans ((IH _ _ h).cons _) }, { exact (IH _ _ h).cons _ } } end theorem perm_lookmap (f : α → option α) {l₁ l₂ : list α} (H : pairwise (λ a b, ∀ (c ∈ f a) (d ∈ f b), a = b ∧ c = d) l₁) (p : l₁ ~ l₂) : lookmap f l₁ ~ lookmap f l₂ := begin let F := λ a b, ∀ (c ∈ f a) (d ∈ f b), a = b ∧ c = d, change pairwise F l₁ at H, induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp}, { cases h : f a, { simp [h], exact IH (pairwise_cons.1 H).2 }, { simp [lookmap_cons_some _ _ h, p] } }, { cases h₁ : f a with c; cases h₂ : f b with d, { simp [h₁, h₂], apply swap }, { simp [h₁, lookmap_cons_some _ _ h₂], apply swap }, { simp [lookmap_cons_some _ _ h₁, h₂], apply swap }, { simp [lookmap_cons_some _ _ h₁, lookmap_cons_some _ _ h₂], rcases (pairwise_cons.1 H).1 _ (or.inl rfl) _ h₂ _ h₁ with ⟨rfl, rfl⟩, refl } }, { refine (IH₁ H).trans (IH₂ ((p₁.pairwise_iff _).1 H)), exact λ a b h c h₁ d h₂, (h d h₂ c h₁).imp eq.symm eq.symm } end theorem perm.erasep (f : α → Prop) [decidable_pred f] {l₁ l₂ : list α} (H : pairwise (λ a b, f a → f b → false) l₁) (p : l₁ ~ l₂) : erasep f l₁ ~ erasep f l₂ := begin let F := λ a b, f a → f b → false, change pairwise F l₁ at H, induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp}, { by_cases h : f a, { simp [h, p] }, { simp [h], exact IH (pairwise_cons.1 H).2 } }, { by_cases h₁ : f a; by_cases h₂ : f b; simp [h₁, h₂], { cases (pairwise_cons.1 H).1 _ (or.inl rfl) h₂ h₁ }, { apply swap } }, { refine (IH₁ H).trans (IH₂ ((p₁.pairwise_iff _).1 H)), exact λ a b h h₁ h₂, h h₂ h₁ } end lemma perm.take_inter {α} [decidable_eq α] {xs ys : list α} (n : ℕ) (h : xs ~ ys) (h' : ys.nodup) : xs.take n ~ ys.inter (xs.take n) := begin simp only [list.inter] at *, induction h generalizing n, case list.perm.nil : n { simp only [not_mem_nil, filter_false, take_nil] }, case list.perm.cons : h_x h_l₁ h_l₂ h_a h_ih n { cases n; simp only [mem_cons_iff, true_or, eq_self_iff_true, filter_cons_of_pos, perm_cons, take, not_mem_nil, filter_false], cases h' with _ _ h₁ h₂, convert h_ih h₂ n using 1, apply filter_congr', introv h, simp only [(h₁ x h).symm, false_or], }, case list.perm.swap : h_x h_y h_l n { cases h' with _ _ h₁ h₂, cases h₂ with _ _ h₂ h₃, have := h₁ _ (or.inl rfl), cases n; simp only [mem_cons_iff, not_mem_nil, filter_false, take], cases n; simp only [mem_cons_iff, false_or, true_or, filter, *, nat.nat_zero_eq_zero, if_true, not_mem_nil, eq_self_iff_true, or_false, if_false, perm_cons, take], { rw filter_eq_nil.2, intros, solve_by_elim [ne.symm], }, { convert perm.swap _ _ _, rw @filter_congr' _ _ (∈ take n h_l), { clear h₁, induction n generalizing h_l, { simp }, cases h_l; simp only [mem_cons_iff, true_or, eq_self_iff_true, filter_cons_of_pos, true_and, take, not_mem_nil, filter_false, take_nil], cases h₃ with _ _ h₃ h₄, rwa [@filter_congr' _ _ (∈ take n_n h_l_tl), n_ih], { introv h, apply h₂ _ (or.inr h), }, { introv h, simp only [(h₃ x h).symm, false_or], }, }, { introv h, simp only [(h₂ x h).symm, (h₁ x (or.inr h)).symm, false_or], } } }, case list.perm.trans : h_l₁ h_l₂ h_l₃ h₀ h₁ h_ih₀ h_ih₁ n { transitivity, { apply h_ih₀, rwa h₁.nodup_iff }, { apply perm.filter _ h₁, } }, end lemma perm.drop_inter {α} [decidable_eq α] {xs ys : list α} (n : ℕ) (h : xs ~ ys) (h' : ys.nodup) : xs.drop n ~ ys.inter (xs.drop n) := begin by_cases h'' : n ≤ xs.length, { let n' := xs.length - n, have h₀ : n = xs.length - n', { dsimp [n'], rwa tsub_tsub_cancel_of_le, } , have h₁ : n' ≤ xs.length, { apply tsub_le_self }, have h₂ : xs.drop n = (xs.reverse.take n').reverse, { rw [reverse_take _ h₁, h₀, reverse_reverse], }, rw [h₂], apply (reverse_perm _).trans, rw inter_reverse, apply perm.take_inter _ _ h', apply (reverse_perm _).trans; assumption, }, { have : drop n xs = [], { apply eq_nil_of_length_eq_zero, rw [length_drop, tsub_eq_zero_iff_le], apply le_of_not_ge h'' }, simp [this, list.inter], } end lemma perm.slice_inter {α} [decidable_eq α] {xs ys : list α} (n m : ℕ) (h : xs ~ ys) (h' : ys.nodup) : list.slice n m xs ~ ys ∩ (list.slice n m xs) := begin simp only [slice_eq], have : n ≤ n + m := nat.le_add_right _ _, have := h.nodup_iff.2 h', apply perm.trans _ (perm.inter_append _).symm; solve_by_elim [perm.append, perm.drop_inter, perm.take_inter, disjoint_take_drop, h, h'] { max_depth := 7 }, end /- enumerating permutations -/ section permutations theorem perm_of_mem_permutations_aux : ∀ {ts is l : list α}, l ∈ permutations_aux ts is → l ~ ts ++ is := begin refine permutations_aux.rec (by simp) _, introv IH1 IH2 m, rw [permutations_aux_cons, permutations, mem_foldr_permutations_aux2] at m, rcases m with m | ⟨l₁, l₂, m, _, e⟩, { exact (IH1 m).trans perm_middle }, { subst e, have p : l₁ ++ l₂ ~ is, { simp [permutations] at m, cases m with e m, {simp [e]}, exact is.append_nil ▸ IH2 m }, exact ((perm_middle.trans (p.cons _)).append_right _).trans (perm_append_comm.cons _) } end theorem perm_of_mem_permutations {l₁ l₂ : list α} (h : l₁ ∈ permutations l₂) : l₁ ~ l₂ := (eq_or_mem_of_mem_cons h).elim (λ e, e ▸ perm.refl _) (λ m, append_nil l₂ ▸ perm_of_mem_permutations_aux m) theorem length_permutations_aux : ∀ ts is : list α, length (permutations_aux ts is) + is.length! = (length ts + length is)! := begin refine permutations_aux.rec (by simp) _, intros t ts is IH1 IH2, have IH2 : length (permutations_aux is nil) + 1 = is.length!, { simpa using IH2 }, simp [-add_comm, nat.factorial, nat.add_succ, mul_comm] at IH1, rw [permutations_aux_cons, length_foldr_permutations_aux2' _ _ _ _ _ (λ l m, (perm_of_mem_permutations m).length_eq), permutations, length, length, IH2, nat.succ_add, nat.factorial_succ, mul_comm (nat.succ _), ← IH1, add_comm (_*_), add_assoc, nat.mul_succ, mul_comm] end theorem length_permutations (l : list α) : length (permutations l) = (length l)! := length_permutations_aux l [] theorem mem_permutations_of_perm_lemma {is l : list α} (H : l ~ [] ++ is → (∃ ts' ~ [], l = ts' ++ is) ∨ l ∈ permutations_aux is []) : l ~ is → l ∈ permutations is := by simpa [permutations, perm_nil] using H theorem mem_permutations_aux_of_perm : ∀ {ts is l : list α}, l ~ is ++ ts → (∃ is' ~ is, l = is' ++ ts) ∨ l ∈ permutations_aux ts is := begin refine permutations_aux.rec (by simp) _, intros t ts is IH1 IH2 l p, rw [permutations_aux_cons, mem_foldr_permutations_aux2], rcases IH1 (p.trans perm_middle) with ⟨is', p', e⟩ | m, { clear p, subst e, rcases mem_split (p'.symm.subset (mem_cons_self _ _)) with ⟨l₁, l₂, e⟩, subst is', have p := (perm_middle.symm.trans p').cons_inv, cases l₂ with a l₂', { exact or.inl ⟨l₁, by simpa using p⟩ }, { exact or.inr (or.inr ⟨l₁, a::l₂', mem_permutations_of_perm_lemma IH2 p, by simp⟩) } }, { exact or.inr (or.inl m) } end @[simp] theorem mem_permutations {s t : list α} : s ∈ permutations t ↔ s ~ t := ⟨perm_of_mem_permutations, mem_permutations_of_perm_lemma mem_permutations_aux_of_perm⟩ theorem perm_permutations'_aux_comm (a b : α) (l : list α) : (permutations'_aux a l).bind (permutations'_aux b) ~ (permutations'_aux b l).bind (permutations'_aux a) := begin induction l with c l ih, {simp [swap]}, simp [permutations'_aux], apply perm.swap', have : ∀ a b, (map (cons c) (permutations'_aux a l)).bind (permutations'_aux b) ~ map (cons b ∘ cons c) (permutations'_aux a l) ++ map (cons c) ((permutations'_aux a l).bind (permutations'_aux b)), { intros, simp only [map_bind, permutations'_aux], refine (bind_append_perm _ (λ x, [_]) _).symm.trans _, rw [← map_eq_bind, ← bind_map] }, refine (((this _ _).append_left _).trans _).trans ((this _ _).append_left _).symm, rw [← append_assoc, ← append_assoc], exact perm_append_comm.append (ih.map _), end theorem perm.permutations' {s t : list α} (p : s ~ t) : permutations' s ~ permutations' t := begin induction p with a s t p IH a b l s t u p₁ p₂ IH₁ IH₂, {simp}, { simp only [permutations'], exact IH.bind_right _ }, { simp only [permutations'], rw [bind_assoc, bind_assoc], apply perm.bind_left, apply perm_permutations'_aux_comm }, { exact IH₁.trans IH₂ } end theorem permutations_perm_permutations' (ts : list α) : ts.permutations ~ ts.permutations' := begin obtain ⟨n, h⟩ : ∃ n, length ts < n := ⟨_, nat.lt_succ_self _⟩, induction n with n IH generalizing ts, {cases h}, refine list.reverse_rec_on ts (λ h, _) (λ ts t _ h, _) h, {simp [permutations]}, rw [← concat_eq_append, length_concat, nat.succ_lt_succ_iff] at h, have IH₂ := (IH ts.reverse (by rwa [length_reverse])).trans (reverse_perm _).permutations', simp only [permutations_append, foldr_permutations_aux2, permutations_aux_nil, permutations_aux_cons, append_nil], refine (perm_append_comm.trans ((IH₂.bind_right _).append ((IH _ h).map _))).trans (perm.trans _ perm_append_comm.permutations'), rw [map_eq_bind, singleton_append, permutations'], convert bind_append_perm _ _ _, funext ys, rw [permutations'_aux_eq_permutations_aux2, permutations_aux2_append] end @[simp] theorem mem_permutations' {s t : list α} : s ∈ permutations' t ↔ s ~ t := (permutations_perm_permutations' _).symm.mem_iff.trans mem_permutations theorem perm.permutations {s t : list α} (h : s ~ t) : permutations s ~ permutations t := (permutations_perm_permutations' _).trans $ h.permutations'.trans (permutations_perm_permutations' _).symm @[simp] theorem perm_permutations_iff {s t : list α} : permutations s ~ permutations t ↔ s ~ t := ⟨λ h, mem_permutations.1 $ h.mem_iff.1 $ mem_permutations.2 (perm.refl _), perm.permutations⟩ @[simp] theorem perm_permutations'_iff {s t : list α} : permutations' s ~ permutations' t ↔ s ~ t := ⟨λ h, mem_permutations'.1 $ h.mem_iff.1 $ mem_permutations'.2 (perm.refl _), perm.permutations'⟩ lemma nth_le_permutations'_aux (s : list α) (x : α) (n : ℕ) (hn : n < length (permutations'_aux x s)) : (permutations'_aux x s).nth_le n hn = s.insert_nth n x := begin induction s with y s IH generalizing n, { simp only [length, permutations'_aux, nat.lt_one_iff] at hn, simp [hn] }, { cases n, { simp }, { simpa using IH _ _ } } end lemma count_permutations'_aux_self [decidable_eq α] (l : list α) (x : α) : count (x :: l) (permutations'_aux x l) = length (take_while ((=) x) l) + 1 := begin induction l with y l IH generalizing x, { simp [take_while], }, { rw [permutations'_aux, count_cons_self], by_cases hx : x = y, { subst hx, simpa [take_while, nat.succ_inj'] using IH _ }, { rw take_while, rw if_neg hx, cases permutations'_aux x l with a as, { simp }, { rw [count_eq_zero_of_not_mem, length, zero_add], simp [hx, ne.symm hx] } } } end @[simp] lemma length_permutations'_aux (s : list α) (x : α) : length (permutations'_aux x s) = length s + 1 := begin induction s with y s IH, { simp }, { simpa using IH } end @[simp] lemma permutations'_aux_nth_le_zero (s : list α) (x : α) (hn : 0 < length (permutations'_aux x s) := by simp) : (permutations'_aux x s).nth_le 0 hn = x :: s := nth_le_permutations'_aux _ _ _ _ lemma injective_permutations'_aux (x : α) : function.injective (permutations'_aux x) := begin intros s t h, apply insert_nth_injective s.length x, have hl : s.length = t.length := by simpa using congr_arg length h, rw [←nth_le_permutations'_aux s x s.length (by simp), ←nth_le_permutations'_aux t x s.length (by simp [hl])], simp [h, hl] end lemma nodup_permutations'_aux_of_not_mem (s : list α) (x : α) (hx : x ∉ s) : nodup (permutations'_aux x s) := begin induction s with y s IH, { simp }, { simp only [not_or_distrib, mem_cons_iff] at hx, simp only [not_and, exists_eq_right_right, mem_map, permutations'_aux, nodup_cons], refine ⟨λ _, ne.symm hx.left, _⟩, rw nodup_map_iff, { exact IH hx.right }, { simp } } end lemma nodup_permutations'_aux_iff {s : list α} {x : α} : nodup (permutations'_aux x s) ↔ x ∉ s := begin refine ⟨λ h, _, nodup_permutations'_aux_of_not_mem _ _⟩, intro H, obtain ⟨k, hk, hk'⟩ := nth_le_of_mem H, rw nodup_iff_nth_le_inj at h, suffices : k = k + 1, { simpa using this }, refine h k (k + 1) _ _ _, { simpa [nat.lt_succ_iff] using hk.le }, { simpa using hk }, rw [nth_le_permutations'_aux, nth_le_permutations'_aux], have hl : length (insert_nth k x s) = length (insert_nth (k + 1) x s), { rw [length_insert_nth _ _ hk.le, length_insert_nth _ _ (nat.succ_le_of_lt hk)] }, refine ext_le hl (λ n hn hn', _), rcases lt_trichotomy n k with H|rfl|H, { rw [nth_le_insert_nth_of_lt _ _ _ _ H (H.trans hk), nth_le_insert_nth_of_lt _ _ _ _ (H.trans (nat.lt_succ_self _))] }, { rw [nth_le_insert_nth_self _ _ _ hk.le, nth_le_insert_nth_of_lt _ _ _ _ (nat.lt_succ_self _) hk, hk'] }, { rcases (nat.succ_le_of_lt H).eq_or_lt with rfl|H', { rw [nth_le_insert_nth_self _ _ _ (nat.succ_le_of_lt hk)], convert hk' using 1, convert nth_le_insert_nth_add_succ _ _ _ 0 _, simpa using hk }, { obtain ⟨m, rfl⟩ := nat.exists_eq_add_of_lt H', rw [length_insert_nth _ _ hk.le, nat.succ_lt_succ_iff, nat.succ_add] at hn, rw nth_le_insert_nth_add_succ, convert nth_le_insert_nth_add_succ s x k m.succ _ using 2, { simp [nat.add_succ, nat.succ_add] }, { simp [add_left_comm, add_comm] }, { simpa [nat.add_succ] using hn }, { simpa [nat.succ_add] using hn } } } end lemma nodup_permutations (s : list α) (hs : nodup s) : nodup s.permutations := begin rw (permutations_perm_permutations' s).nodup_iff, induction hs with x l h h' IH, { simp }, { rw [permutations'], rw nodup_bind, split, { intros ys hy, rw mem_permutations' at hy, rw [nodup_permutations'_aux_iff, hy.mem_iff], exact λ H, h x H rfl }, { refine IH.pairwise_of_forall_ne (λ as ha bs hb H, _), rw disjoint_iff_ne, rintro a ha' b hb' rfl, obtain ⟨n, hn, hn'⟩ := nth_le_of_mem ha', obtain ⟨m, hm, hm'⟩ := nth_le_of_mem hb', rw mem_permutations' at ha hb, have hl : as.length = bs.length := (ha.trans hb.symm).length_eq, simp only [nat.lt_succ_iff, length_permutations'_aux] at hn hm, rw nth_le_permutations'_aux at hn' hm', have hx : nth_le (insert_nth n x as) m (by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff, hl]) = x, { simp [hn', ←hm', hm] }, have hx' : nth_le (insert_nth m x bs) n (by rwa [length_insert_nth _ _ hm, nat.lt_succ_iff, ←hl]) = x, { simp [hm', ←hn', hn] }, rcases lt_trichotomy n m with ht|ht|ht, { suffices : x ∈ bs, { exact h x (hb.subset this) rfl }, rw [←hx', nth_le_insert_nth_of_lt _ _ _ _ ht (ht.trans_le hm)], exact nth_le_mem _ _ _ }, { simp only [ht] at hm' hn', rw ←hm' at hn', exact H (insert_nth_injective _ _ hn') }, { suffices : x ∈ as, { exact h x (ha.subset this) rfl }, rw [←hx, nth_le_insert_nth_of_lt _ _ _ _ ht (ht.trans_le hn)], exact nth_le_mem _ _ _ } } } end -- TODO: `nodup s.permutations ↔ nodup s` -- TODO: `count s s.permutations = (zip_with count s s.tails).prod` end permutations end list
dd16d7490fee256d23c5774aef73429cbcda9c4c
735bb6d9c54e20a6bdc031c27bff1717e68886b9
/data/seq/seq.lean
d56e1d4b696de13c18b11834dccff82d0521f136
[]
no_license
digama0/library_dev
3ea441564c4d7eca54a562b701febaa4db6a1061
56520d5d1dda46d87c98bf3acdf850672fdab00f
refs/heads/master
1,611,047,574,219
1,500,469,648,000
1,500,469,648,000
87,738,883
0
0
null
1,491,771,880,000
1,491,771,879,000
null
UTF-8
Lean
false
false
22,006
lean
import data.stream data.lazy_list data.seq.computation logic.basic pending universes u v w /- coinductive seq (α : Type u) : Type u | nil : seq α | cons : α → seq α → seq α -/ def seq (α : Type u) : Type u := { f : stream (option α) // ∀ {n}, f n = none → f (n+1) = none } def seq1 (α) := α × seq α namespace seq variables {α : Type u} {β : Type v} {γ : Type w} def nil : seq α := ⟨stream.const none, λn h, rfl⟩ def cons (a : α) : seq α → seq α | ⟨f, al⟩ := ⟨some a :: f, λn h, by {cases n with n, contradiction, exact al h}⟩ def nth : seq α → ℕ → option α := subtype.val def omap (f : β → γ) : option (α × β) → option (α × γ) | none := none | (some (a, b)) := some (a, f b) attribute [simp] omap def head (s : seq α) : option α := nth s 0 def tail : seq α → seq α | ⟨f, al⟩ := ⟨f.tail, λ n, al⟩ protected def mem (a : α) (s : seq α) := some a ∈ s.1 instance : has_mem α (seq α) := ⟨seq.mem⟩ theorem le_stable (s : seq α) {m n} (h : m ≤ n) : s.1 m = none → s.1 n = none := by {cases s with f al, induction h with n h IH, exacts [id, λ h2, al (IH h2)]} theorem not_mem_nil (a : α) : a ∉ @nil α := λ ⟨n, (h : some a = none)⟩, by injection h lemma mem_cons (a : α) : ∀ (s : seq α), a ∈ cons a s | ⟨f, al⟩ := stream.mem_cons (some a) _ lemma mem_cons_of_mem (y : α) {a : α} : ∀ {s : seq α}, a ∈ s → a ∈ cons y s | ⟨f, al⟩ := stream.mem_cons_of_mem (some y) lemma eq_or_mem_of_mem_cons {a b : α} : ∀ {s : seq α}, a ∈ cons b s → a = b ∨ a ∈ s | ⟨f, al⟩ h := or_of_or_of_implies_left (stream.eq_or_mem_of_mem_cons h) (λh, by injection h) @[simp] lemma mem_cons_iff {a b : α} {s : seq α} : a ∈ cons b s ↔ a = b ∨ a ∈ s := ⟨eq_or_mem_of_mem_cons, λo, by cases o with e m; [{rw e, apply mem_cons}, exact mem_cons_of_mem _ m]⟩ def destruct (s : seq α) : option (seq1 α) := (λa', (a', s.tail)) <$> nth s 0 theorem destruct_eq_nil {s : seq α} : destruct s = none → s = nil := begin dsimp [destruct], ginduction nth s 0 with f0; intro h, { apply subtype.eq, apply funext, dsimp [nil], intro n, induction n with n IH, exacts [f0, s.2 IH] }, { contradiction } end theorem destruct_eq_cons {s : seq α} {a s'} : destruct s = some (a, s') → s = cons a s' := begin dsimp [destruct], ginduction nth s 0 with f0 a'; intro h, { contradiction }, { unfold has_map.map at h, dsimp [option_map, option_bind] at h, cases s with f al, injections with _ h1 h2, rw ←h2, apply subtype.eq, dsimp [tail, cons], rw h1 at f0, rw ←f0, exact (stream.eta f).symm } end @[simp] theorem destruct_nil : destruct (nil : seq α) = none := rfl @[simp] theorem destruct_cons (a : α) : ∀ s, destruct (cons a s) = some (a, s) | ⟨f, al⟩ := begin unfold cons destruct has_map.map, apply congr_arg (λ s, some (a, s)), apply subtype.eq, dsimp [tail], rw [stream.tail_cons] end theorem head_eq_destruct (s : seq α) : head s = prod.fst <$> destruct s := by unfold destruct head; cases nth s 0; refl @[simp] theorem head_nil : head (nil : seq α) = none := rfl @[simp] theorem head_cons (a : α) (s) : head (cons a s) = some a := by rw [head_eq_destruct, destruct_cons]; refl @[simp] theorem tail_nil : tail (nil : seq α) = nil := rfl @[simp] theorem tail_cons (a : α) (s) : tail (cons a s) = s := by cases s with f al; apply subtype.eq; dsimp [tail, cons]; rw [stream.tail_cons] def cases_on {C : seq α → Sort v} (s : seq α) (h1 : C nil) (h2 : ∀ x s, C (cons x s)) : C s := begin ginduction destruct s with H, { rw destruct_eq_nil H, apply h1 }, { cases a with a s', rw destruct_eq_cons H, apply h2 } end theorem mem_rec_on {C : seq α → Prop} {a s} (M : a ∈ s) (h1 : ∀ b s', (a = b ∨ C s') → C (cons b s')) : C s := begin cases M with k e, unfold stream.nth at e, revert s, induction k with k IH; intros s e, { have TH : s = cons a (tail s), { apply destruct_eq_cons, unfold destruct nth has_map.map, rw ←e, refl }, rw TH, apply h1 _ _ (or.inl rfl) }, revert e, apply s.cases_on _ (λ b s', _); intro e, { injection e }, { rw [show (cons b s').val (nat.succ k) = s'.val k, by cases s'; refl] at e, apply h1 _ _ (or.inr (IH e)) } end def corec.F (f : β → option (α × β)) : option β → option α × option β | none := (none, none) | (some b) := match f b with none := (none, none) | some (a, b') := (some a, some b') end def corec (f : β → option (α × β)) (b : β) : seq α := begin refine ⟨stream.corec' (corec.F f) (some b), λn h, _⟩, rw stream.corec'_eq, change stream.corec' (corec.F f) (corec.F f (some b)).2 n = none, revert h, generalize (some b) o, induction n with n IH; intro o, { change (corec.F f o).1 = none → (corec.F f (corec.F f o).2).1 = none, cases o with b; intro h, { refl }, dsimp [corec.F] at h, dsimp [corec.F], cases f b with s, { refl }, { cases s with a b', contradiction } }, { rw [stream.corec'_eq (corec.F f) (corec.F f o).2, stream.corec'_eq (corec.F f) o], exact IH (corec.F f o).2 } end @[simp] def corec_eq (f : β → option (α × β)) (b : β) : destruct (corec f b) = omap (corec f) (f b) := begin dsimp [corec, destruct, nth], change stream.corec' (corec.F f) (some b) 0 with (corec.F f (some b)).1, unfold has_map.map, dsimp [corec.F], ginduction f b with h s, { refl }, cases s with a b', dsimp [corec.F, option_bind], apply congr_arg (λ b', some (a, b')), apply subtype.eq, dsimp [corec, tail], rw [stream.corec'_eq, stream.tail_cons], dsimp [corec.F], rw h, refl end def of_list (l : list α) : seq α := ⟨list.nth l, λn h, begin revert n, induction l with a l IH; intros, refl, dsimp [list.nth], cases n with n; dsimp [list.nth] at h, { contradiction }, { apply IH _ h } end⟩ instance coe_list : has_coe (list α) (seq α) := ⟨of_list⟩ section bisim variable (R : seq α → seq α → Prop) local infix ~ := R def bisim_o : option (seq1 α) → option (seq1 α) → Prop | none none := true | (some (a, s)) (some (a', s')) := a = a' ∧ R s s' | _ _ := false attribute [simp] bisim_o def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → bisim_o R (destruct s₁) (destruct s₂) -- If two streams are bisimilar, then they are equal lemma eq_of_bisim (bisim : is_bisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s₁ = s₂ := begin apply subtype.eq, apply stream.eq_of_bisim (λx y, ∃ s s' : seq α, s.1 = x ∧ s'.1 = y ∧ R s s'), dsimp [stream.is_bisimulation], intros t₁ t₂ e, exact match t₁, t₂, e with ._, ._, ⟨s, s', rfl, rfl, r⟩ := suffices head s = head s' ∧ R (tail s) (tail s'), from and.imp id (λr, ⟨tail s, tail s', by cases s; refl, by cases s'; refl, r⟩) this, begin have := bisim r, revert r this, apply cases_on s _ _; intros; apply cases_on s' _ _; intros; intros r this, { constructor, refl, assumption }, { rw [destruct_nil, destruct_cons] at this, exact false.elim this }, { rw [destruct_nil, destruct_cons] at this, exact false.elim this }, { rw [destruct_cons, destruct_cons] at this, rw [head_cons, head_cons, tail_cons, tail_cons], cases this with h1 h2, constructor, rw h1, exact h2 } end end, exact ⟨s₁, s₂, rfl, rfl, r⟩ end end bisim lemma coinduction : ∀ {s₁ s₂ : seq α}, head s₁ = head s₂ → (∀ (β : Type u) (fr : seq α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂ | ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ hh ht := subtype.eq (stream.coinduction hh (λ β fr, ht β (λs, fr s.1))) lemma coinduction2 (s) (f g : seq α → seq β) (H : ∀ s, bisim_o (λ (s1 s2 : seq β), ∃ (s : seq α), s1 = f s ∧ s2 = g s) (destruct (f s)) (destruct (g s))) : f s = g s := begin refine eq_of_bisim (λ s1 s2, ∃ s, s1 = f s ∧ s2 = g s) _ ⟨s, rfl, rfl⟩, intros s1 s2 h, cases h with s h, cases h with h1 h2, rw [h1, h2], apply H end def of_stream (s : stream α) : seq α := ⟨s.map some, λn h, by contradiction⟩ instance coe_stream : has_coe (stream α) (seq α) := ⟨of_stream⟩ def of_lazy_list : lazy_list α → seq α := corec (λl, match l with | lazy_list.nil := none | lazy_list.cons a l' := some (a, l' ()) end) instance coe_lazy_list : has_coe (lazy_list α) (seq α) := ⟨of_lazy_list⟩ meta def to_lazy_list : seq α → lazy_list α | s := match destruct s with | none := lazy_list.nil | some (a, s') := lazy_list.cons a (to_lazy_list s') end meta def force_to_list (s : seq α) : list α := (to_lazy_list s).to_list def append (s₁ s₂ : seq α) : seq α := @corec α (seq α × seq α) (λ⟨s₁, s₂⟩, match destruct s₁ with | none := omap (λs₂, (nil, s₂)) (destruct s₂) | some (a, s₁') := some (a, s₁', s₂) end) (s₁, s₂) def map (f : α → β) : seq α → seq β | ⟨s, al⟩ := ⟨s.map (option_map f), λn, begin dsimp [stream.map, stream.nth], ginduction s n with e; intro, { rw al e, assumption }, { contradiction } end⟩ def join : seq (seq1 α) → seq α := corec (λS, match destruct S with | none := none | some ((a, s), S') := some (a, match destruct s with | none := S' | some s' := cons s' S' end) end) def drop (s : seq α) : ℕ → seq α | 0 := s | (n+1) := tail (drop n) attribute [simp] drop def take : ℕ → seq α → list α | 0 s := [] | (n+1) s := match destruct s with | none := [] | some (x, r) := list.cons x (take n r) end def split_at : ℕ → seq α → list α × seq α | 0 s := ([], s) | (n+1) s := match destruct s with | none := ([], nil) | some (x, s') := let (l, r) := split_at n s' in (list.cons x l, r) end def zip_with (f : α → β → γ) : seq α → seq β → seq γ | ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ := ⟨λn, match f₁ n, f₂ n with | some a, some b := some (f a b) | _, _ := none end, λn, begin ginduction f₁ n with h1, { intro H, rw a₁ h1, refl }, ginduction f₂ n with h2; dsimp [seq.zip_with._match_1]; intro H, { rw a₂ h2, cases f₁ (n + 1); refl }, { contradiction } end⟩ def zip : seq α → seq β → seq (α × β) := zip_with prod.mk def unzip (s : seq (α × β)) : seq α × seq β := (map prod.fst s, map prod.snd s) def to_list (s : seq α) (h : ∃ n, ¬ (nth s n).is_some) : list α := take (nat.find h) s def to_stream (s : seq α) (h : ∀ n, (nth s n).is_some) : stream α := λn, option.get (h n) def to_list_or_stream (s : seq α) [decidable (∃ n, ¬ (nth s n).is_some)] : list α ⊕ stream α := if h : ∃ n, ¬ (nth s n).is_some then sum.inl (to_list s h) else sum.inr (to_stream s (λn, decidable.by_contradiction (λ hn, h ⟨n, hn⟩))) @[simp] lemma nil_append (s : seq α) : append nil s = s := begin apply coinduction2, intro s, dsimp [append], rw [corec_eq], dsimp [append], apply cases_on s _ _, { trivial }, { intros x s, rw [destruct_cons], dsimp, exact ⟨rfl, s, rfl, rfl⟩ } end @[simp] lemma cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) := destruct_eq_cons $ begin dsimp [append], rw [corec_eq], dsimp [append], rw [destruct_cons], dsimp [append], refl end @[simp] lemma append_nil (s : seq α) : append s nil = s := begin apply coinduction2 s, intro s, apply cases_on s _ _, { trivial }, { intros x s, rw [cons_append, destruct_cons, destruct_cons], dsimp, exact ⟨rfl, s, rfl, rfl⟩ } end @[simp] lemma append_assoc (s t u : seq α) : append (append s t) u = append s (append t u) := begin apply eq_of_bisim (λs1 s2, ∃ s t u, s1 = append (append s t) u ∧ s2 = append s (append t u)), { intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, u, rfl, rfl⟩ := begin apply cases_on s; simp, { apply cases_on t; simp, { apply cases_on u; simp, { intros x u, refine ⟨nil, nil, u, _, _⟩; simp } }, { intros x t, refine ⟨nil, t, u, _, _⟩; simp } }, { intros x s, exact ⟨s, t, u, rfl, rfl⟩ } end end }, { exact ⟨s, t, u, rfl, rfl⟩ } end @[simp] lemma map_nil (f : α → β) : map f nil = nil := rfl @[simp] lemma map_cons (f : α → β) (a) : ∀ s, map f (cons a s) = cons (f a) (map f s) | ⟨s, al⟩ := by apply subtype.eq; dsimp [cons, map]; rw stream.map_cons; refl @[simp] lemma map_id : ∀ (s : seq α), map id s = s | ⟨s, al⟩ := begin apply subtype.eq; dsimp [map], rw [option.map_id, stream.map_id]; refl end @[simp] lemma map_tail (f : α → β) : ∀ s, map f (tail s) = tail (map f s) | ⟨s, al⟩ := by apply subtype.eq; dsimp [tail, map]; rw stream.map_tail; refl lemma map_comp (f : α → β) (g : β → γ) : ∀ (s : seq α), map (g ∘ f) s = map g (map f s) | ⟨s, al⟩ := begin apply subtype.eq; dsimp [map], rw stream.map_map, apply congr_arg (λ f : _ → option γ, stream.map f s), apply funext, intro, cases x with x; refl end @[simp] theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s) (map f t) := begin apply eq_of_bisim (λs1 s2, ∃ s t, s1 = map f (append s t) ∧ s2 = append (map f s) (map f t)) _ ⟨s, t, rfl, rfl⟩, intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, rfl, rfl⟩ := begin apply cases_on s; simp, { apply cases_on t; simp, { intros x t, refine ⟨nil, t, _, _⟩; simp } }, { intros x s, refine ⟨s, t, rfl, rfl⟩ } end end end @[simp] lemma map_nth (f : α → β) : ∀ s n, nth (map f s) n = option_map f (nth s n) | ⟨s, al⟩ n := rfl instance : functor seq := { map := @map, id_map := @map_id, map_comp := @map_comp } @[simp] lemma join_nil : join nil = (nil : seq α) := destruct_eq_nil rfl @[simp] lemma join_cons_nil (a : α) (S) : join (cons (a, nil) S) = cons a (join S) := destruct_eq_cons $ by simp [join] @[simp] lemma join_cons_cons (a b : α) (s S) : join (cons (a, cons b s) S) = cons a (join (cons (b, s) S)) := destruct_eq_cons $ by simp [join] @[simp] lemma join_cons (a : α) (s S) : join (cons (a, s) S) = cons a (append s (join S)) := begin apply eq_of_bisim (λs1 s2, s1 = s2 ∨ ∃ a s S, s1 = join (cons (a, s) S) ∧ s2 = cons a (append s (join S))) _ (or.inr ⟨a, s, S, rfl, rfl⟩), intros s1 s2 h, exact match s1, s2, h with | ._, s, (or.inl rfl) := begin apply cases_on s, { trivial }, { intros x s, rw [destruct_cons], exact ⟨rfl, or.inl rfl⟩ } end | ._, ._, (or.inr ⟨a, s, S, rfl, rfl⟩) := begin apply cases_on s, { simp }, { intros x s, simp, refine or.inr ⟨x, s, S, rfl, rfl⟩ } end end end @[simp] lemma join_append (S T : seq (seq1 α)) : join (append S T) = append (join S) (join T) := begin apply eq_of_bisim (λs1 s2, ∃ s S T, s1 = append s (join (append S T)) ∧ s2 = append s (append (join S) (join T))), { intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, T, rfl, rfl⟩ := begin apply cases_on s; simp, { apply cases_on S; simp, { apply cases_on T, { simp }, { intros s T, cases s with a s; simp, refine ⟨s, nil, T, _, _⟩; simp } }, { intros s S, cases s with a s; simp, exact ⟨s, S, T, rfl, rfl⟩ } }, { intros x s, exact ⟨s, S, T, rfl, rfl⟩ } end end }, { refine ⟨nil, S, T, _, _⟩; simp } end @[simp] def of_list_nil : of_list [] = (nil : seq α) := rfl @[simp] def of_list_cons (a : α) (l) : of_list (a :: l) = cons a (of_list l) := begin apply subtype.eq, simp [of_list, cons], apply funext, intro n, cases n; simp [list.nth, stream.cons] end @[simp] def of_stream_cons (a : α) (s) : of_stream (a :: s) = cons a (of_stream s) := by apply subtype.eq; simp [of_stream, cons]; rw stream.map_cons @[simp] def of_list_append (l l' : list α) : of_list (l ++ l') = append (of_list l) (of_list l') := by induction l; simp [*] @[simp] def of_stream_append (l : list α) (s : stream α) : of_stream (l ++ₛ s) = append (of_list l) (of_stream s) := by induction l; simp [*, stream.nil_append_stream, stream.cons_append_stream] def to_list' {α} (s : seq α) : computation (list α) := @computation.corec (list α) (list α × seq α) (λ⟨l, s⟩, match destruct s with | none := sum.inl l.reverse | some (a, s') := sum.inr (a::l, s') end) ([], s) theorem dropn_add (s : seq α) (m) : ∀ n, drop s (m + n) = drop (drop s m) n | 0 := rfl | (n+1) := congr_arg tail (dropn_add n) theorem dropn_tail (s : seq α) (n) : drop (tail s) n = drop s (n + 1) := by rw add_comm; symmetry; apply dropn_add theorem nth_tail : ∀ (s : seq α) n, nth (tail s) n = nth s (n + 1) | ⟨f, al⟩ n := rfl @[simp] theorem head_dropn (s : seq α) (n) : head (drop s n) = nth s n := begin revert s, induction n with n IH; intro, { refl }, rw [nat.succ_eq_add_one, ←nth_tail, ←dropn_tail], apply IH end theorem mem_map (f : α → β) {a : α} : ∀ {s : seq α}, a ∈ s → f a ∈ map f s | ⟨g, al⟩ := stream.mem_map (option_map f) lemma exists_of_mem_map {f} {b : β} : ∀ {s : seq α}, b ∈ map f s → ∃ a, a ∈ s ∧ f a = b | ⟨g, al⟩ h := let ⟨o, om, oe⟩ := stream.exists_of_mem_map h in by cases o; injection oe; exact ⟨a, om, h⟩ def of_mem_append {s₁ s₂ : seq α} {a : α} (h : a ∈ append s₁ s₂) : a ∈ s₁ ∨ a ∈ s₂ := begin have := h, revert this, generalize2 (append s₁ s₂) ss e, intro h, revert s₁, apply mem_rec_on h _, intros b s' o s₁, apply s₁.cases_on _ (λ c t₁, _); intros m e; have := congr_arg destruct e; simp at this; injections with i1 i2 i3, { simp at m, exact or.inr m }, { simp at m, cases m with e' m, { rw e', exact or.inl (mem_cons _ _) }, { rw i2, cases o with e' IH, { rw e', exact or.inl (mem_cons _ _) }, { exact or.imp_left (mem_cons_of_mem _) (IH m i3) } } } end def mem_append_left {s₁ s₂ : seq α} {a : α} (h : a ∈ s₁) : a ∈ append s₁ s₂ := by apply mem_rec_on h; intros; simp [*] end seq namespace seq1 variables {α : Type u} {β : Type v} {γ : Type w} open seq def to_seq : seq1 α → seq α | (a, s) := cons a s instance coe_seq : has_coe (seq1 α) (seq α) := ⟨to_seq⟩ def map (f : α → β) : seq1 α → seq1 β | (a, s) := (f a, seq.map f s) theorem map_id : ∀ (s : seq1 α), map id s = s | ⟨a, s⟩ := by simp [map] def join : seq1 (seq1 α) → seq1 α | ((a, s), S) := match destruct s with | none := (a, seq.join S) | some s' := (a, seq.join (cons s' S)) end @[simp] lemma join_nil (a : α) (S) : join ((a, nil), S) = (a, seq.join S) := rfl @[simp] lemma join_cons (a b : α) (s S) : join ((a, cons b s), S) = (a, seq.join (cons (b, s) S)) := by dsimp [join]; rw [destruct_cons]; refl def ret (a : α) : seq1 α := (a, nil) def bind (s : seq1 α) (f : α → seq1 β) : seq1 β := join (map f s) @[simp] theorem join_map_ret (s : seq α) : seq.join (seq.map ret s) = s := begin apply coinduction2 s, intro s, apply cases_on s; simp [ret], { intros x s, exact ⟨_, rfl, rfl⟩ } end @[simp] theorem bind_ret (f : α → β) : ∀ s, bind s (ret ∘ f) = map f s | ⟨a, s⟩ := begin dsimp [bind, map], change (λx, ret (f x)) with (ret ∘ f), rw [map_comp], simp [function.comp, ret] end @[simp] theorem ret_bind (a : α) (f : α → seq1 β) : bind (ret a) f = f a := begin simp [ret, bind, map], cases f a with a s, apply cases_on s; intros; simp end @[simp] theorem map_join' (f : α → β) (S) : seq.map f (seq.join S) = seq.join (seq.map (map f) S) := begin apply eq_of_bisim (λs1 s2, ∃ s S, s1 = append s (seq.map f (seq.join S)) ∧ s2 = append s (seq.join (seq.map (map f) S))), { intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, rfl, rfl⟩ := begin apply cases_on s; simp, { apply cases_on S; simp, { intros x S, cases x with a s; simp [map], exact ⟨_, _, rfl, rfl⟩ } }, { intros x s, refine ⟨s, S, rfl, rfl⟩ } end end }, { refine ⟨nil, S, _, _⟩; simp } end @[simp] theorem map_join (f : α → β) : ∀ S, map f (join S) = join (map (map f) S) | ((a, s), S) := by apply cases_on s; intros; simp [map] @[simp] theorem join_join (SS : seq (seq1 (seq1 α))) : seq.join (seq.join SS) = seq.join (seq.map join SS) := begin apply eq_of_bisim (λs1 s2, ∃ s SS, s1 = seq.append s (seq.join (seq.join SS)) ∧ s2 = seq.append s (seq.join (seq.map join SS))), { intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, SS, rfl, rfl⟩ := begin apply cases_on s; simp, { apply cases_on SS; simp, { intros S SS, cases S with s S; cases s with x s; simp [map], apply cases_on s; simp, { exact ⟨_, _, rfl, rfl⟩ }, { intros x s, refine ⟨cons x (append s (seq.join S)), SS, _, _⟩; simp } } }, { intros x s, exact ⟨s, SS, rfl, rfl⟩ } end end }, { refine ⟨nil, SS, _, _⟩; simp } end @[simp] theorem bind_assoc (s : seq1 α) (f : α → seq1 β) (g : β → seq1 γ) : bind (bind s f) g = bind s (λ (x : α), bind (f x) g) := begin cases s with a s, simp [bind, map], rw [←map_comp], change (λ x, join (map g (f x))) with (join ∘ ((map g) ∘ f)), rw [map_comp _ join], generalize (seq.map (map g ∘ f) s) SS, intro SS, cases map g (f a) with s S, cases s with a s, apply cases_on s; intros; apply cases_on S; intros; simp, { cases x with x t, apply cases_on t; intros; simp }, { cases x_1 with y t; simp } end instance : monad seq1 := { map := @map, pure := @ret, bind := @bind, id_map := @map_id, bind_pure_comp_eq_map := @bind_ret, pure_bind := @ret_bind, bind_assoc := @bind_assoc } end seq1
b4f9cb6140f1157c8749483f407f65be2168d5c0
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/measure_theory/prod.lean
0ac2730268a032051d4002b8eccaa7d56f6815bf
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
46,281
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.giry_monad import measure_theory.set_integral /-! # The product measure In this file we define and prove properties about the binary product measure. If `α` and `β` have σ-finite measures `μ` resp. `ν` then `α × β` can be equipped with a σ-finite measure `μ.prod ν` that satisfies `(μ.prod ν) s = ∫⁻ x, ν {y | (x, y) ∈ s} ∂μ`. We also have `(μ.prod ν) (s.prod t) = μ s * ν t`, i.e. the measure of a rectangle is the product of the measures of the sides. We also prove Tonelli's theorem and Fubini's theorem. ## Main definition * `measure_theory.measure.prod`: The product of two measures. ## Main results * `measure_theory.measure.prod_apply` states `μ.prod ν s = ∫⁻ x, ν {y | (x, y) ∈ s} ∂μ` for measurable `s`. `measure_theory.measure.prod_apply_symm` is the reversed version. * `measure_theory.measure.prod_prod` states `μ.prod ν (s.prod t) = μ s * ν t` for measurable sets `s` and `t`. * `measure_theory.lintegral_prod`: Tonelli's theorem. It states that for a measurable function `α × β → ℝ≥0∞` we have `∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ`. The version for functions `α → β → ℝ≥0∞` is reversed, and called `lintegral_lintegral`. Both versions have a variant with `_symm` appended, where the order of integration is reversed. The lemma `measurable.lintegral_prod_right'` states that the inner integral of the right-hand side is measurable. * `measure_theory.integrable_prod_iff` states that a binary function is integrable iff both * `y ↦ f (x, y)` is integrable for almost every `x`, and * the function `x ↦ ∫ ∥f (x, y)∥ dy` is integrable. * `measure_theory.integral_prod`: Fubini's theorem. It states that for a integrable function `α × β → E` (where `E` is a second countable Banach space) we have `∫ z, f z ∂(μ.prod ν) = ∫ x, ∫ y, f (x, y) ∂ν ∂μ`. This theorem has the same variants as Tonelli's theorem. The lemma `measure_theory.integrable.integral_prod_right` states that the inner integral of the right-hand side is integrable. ## Implementation Notes Many results are proven twice, once for functions in curried form (`α → β → γ`) and one for functions in uncurried form (`α × β → γ`). The former often has an assumption `measurable (uncurry f)`, which could be inconvenient to discharge, but for the latter it is more common that the function has to be given explicitly, since Lean cannot synthesize the function by itself. We name the lemmas about the uncurried form with a prime. Tonelli's theorem and Fubini's theorem have a different naming scheme, since the version for the uncurried version is reversed. ## Tags product measure, Fubini's theorem, Tonelli's theorem, Fubini-Tonelli theorem -/ noncomputable theory open_locale classical topological_space ennreal open set function real ennreal open measure_theory measurable_space measure_theory.measure open topological_space (hiding generate_from) open filter (hiding prod_eq map) variables {α α' β β' γ E : Type*} /-- Rectangles formed by π-systems form a π-system. -/ lemma is_pi_system.prod {C : set (set α)} {D : set (set β)} (hC : is_pi_system C) (hD : is_pi_system D) : is_pi_system (image2 set.prod C D) := begin rintro _ _ ⟨s₁, t₁, hs₁, ht₁, rfl⟩ ⟨s₂, t₂, hs₂, ht₂, rfl⟩ hst, rw [prod_inter_prod] at hst ⊢, rw [prod_nonempty_iff] at hst, exact mem_image2_of_mem (hC _ _ hs₁ hs₂ hst.1) (hD _ _ ht₁ ht₂ hst.2) end /-- Rectangles of countably spanning sets are countably spanning. -/ lemma is_countably_spanning.prod {C : set (set α)} {D : set (set β)} (hC : is_countably_spanning C) (hD : is_countably_spanning D) : is_countably_spanning (image2 set.prod C D) := begin rcases ⟨hC, hD⟩ with ⟨⟨s, h1s, h2s⟩, t, h1t, h2t⟩, refine ⟨λ n, (s n.unpair.1).prod (t n.unpair.2), λ n, mem_image2_of_mem (h1s _) (h1t _), _⟩, rw [Union_unpair_prod, h2s, h2t, univ_prod_univ] end variables [measurable_space α] [measurable_space α'] [measurable_space β] [measurable_space β'] variables [measurable_space γ] variables {μ : measure α} {ν : measure β} {τ : measure γ} variables [normed_group E] [measurable_space E] /-! ### Measurability Before we define the product measure, we can talk about the measurability of operations on binary functions. We show that if `f` is a binary measurable function, then the function that integrates along one of the variables (using either the Lebesgue or Bochner integral) is measurable. -/ /-- The product of generated σ-algebras is the one generated by rectangles, if both generating sets are countably spanning. -/ lemma generate_from_prod_eq {α β} {C : set (set α)} {D : set (set β)} (hC : is_countably_spanning C) (hD : is_countably_spanning D) : @prod.measurable_space _ _ (generate_from C) (generate_from D) = generate_from (image2 set.prod C D) := begin apply le_antisymm, { refine sup_le _ _; rw [comap_generate_from]; apply generate_from_le; rintro _ ⟨s, hs, rfl⟩, { rcases hD with ⟨t, h1t, h2t⟩, rw [← prod_univ, ← h2t, prod_Union], apply measurable_set.Union, intro n, apply measurable_set_generate_from, exact ⟨s, t n, hs, h1t n, rfl⟩ }, { rcases hC with ⟨t, h1t, h2t⟩, rw [← univ_prod, ← h2t, Union_prod], apply measurable_set.Union, rintro n, apply measurable_set_generate_from, exact mem_image2_of_mem (h1t n) hs } }, { apply generate_from_le, rintro _ ⟨s, t, hs, ht, rfl⟩, rw [prod_eq], apply (measurable_fst _).inter (measurable_snd _), { exact measurable_set_generate_from hs }, { exact measurable_set_generate_from ht } } end /-- If `C` and `D` generate the σ-algebras on `α` resp. `β`, then rectangles formed by `C` and `D` generate the σ-algebra on `α × β`. -/ lemma generate_from_eq_prod {C : set (set α)} {D : set (set β)} (hC : generate_from C = ‹_›) (hD : generate_from D = ‹_›) (h2C : is_countably_spanning C) (h2D : is_countably_spanning D) : generate_from (image2 set.prod C D) = prod.measurable_space := by rw [← hC, ← hD, generate_from_prod_eq h2C h2D] /-- The product σ-algebra is generated from boxes, i.e. `s.prod t` for sets `s : set α` and `t : set β`. -/ lemma generate_from_prod : generate_from (image2 set.prod {s : set α | measurable_set s} {t : set β | measurable_set t}) = prod.measurable_space := generate_from_eq_prod generate_from_measurable_set generate_from_measurable_set is_countably_spanning_measurable_set is_countably_spanning_measurable_set /-- Rectangles form a π-system. -/ lemma is_pi_system_prod : is_pi_system (image2 set.prod {s : set α | measurable_set s} {t : set β | measurable_set t}) := is_pi_system_measurable_set.prod is_pi_system_measurable_set /-- If `ν` is a finite measure, and `s ⊆ α × β` is measurable, then `x ↦ ν { y | (x, y) ∈ s }` is a measurable function. `measurable_measure_prod_mk_left` is strictly more general. -/ lemma measurable_measure_prod_mk_left_finite [finite_measure ν] {s : set (α × β)} (hs : measurable_set s) : measurable (λ x, ν (prod.mk x ⁻¹' s)) := begin refine induction_on_inter generate_from_prod.symm is_pi_system_prod _ _ _ _ hs, { simp [measurable_zero, const_def] }, { rintro _ ⟨s, t, hs, ht, rfl⟩, simp only [mk_preimage_prod_right_eq_if, measure_if], exact measurable_const.indicator hs }, { intros t ht h2t, simp_rw [preimage_compl, measure_compl (measurable_prod_mk_left ht) (measure_lt_top ν _)], exact measurable_const.ennreal_sub h2t }, { intros f h1f h2f h3f, simp_rw [preimage_Union], have : ∀ b, ν (⋃ i, prod.mk b ⁻¹' f i) = ∑' i, ν (prod.mk b ⁻¹' f i) := λ b, measure_Union (λ i j hij, disjoint.preimage _ (h1f i j hij)) (λ i, measurable_prod_mk_left (h2f i)), simp_rw [this], apply measurable.ennreal_tsum h3f }, end /-- If `ν` is a σ-finite measure, and `s ⊆ α × β` is measurable, then `x ↦ ν { y | (x, y) ∈ s }` is a measurable function. -/ lemma measurable_measure_prod_mk_left [sigma_finite ν] {s : set (α × β)} (hs : measurable_set s) : measurable (λ x, ν (prod.mk x ⁻¹' s)) := begin have : ∀ x, measurable_set (prod.mk x ⁻¹' s) := λ x, measurable_prod_mk_left hs, simp only [← @supr_restrict_spanning_sets _ _ ν, this], apply measurable_supr, intro i, haveI : fact _ := measure_spanning_sets_lt_top ν i, exact measurable_measure_prod_mk_left_finite hs end /-- If `μ` is a σ-finite measure, and `s ⊆ α × β` is measurable, then `y ↦ μ { x | (x, y) ∈ s }` is a measurable function. -/ lemma measurable_measure_prod_mk_right {μ : measure α} [sigma_finite μ] {s : set (α × β)} (hs : measurable_set s) : measurable (λ y, μ ((λ x, (x, y)) ⁻¹' s)) := measurable_measure_prod_mk_left (measurable_set_swap_iff.mpr hs) lemma measurable.map_prod_mk_left [sigma_finite ν] : measurable (λ x : α, map (prod.mk x) ν) := begin apply measurable_of_measurable_coe, intros s hs, simp_rw [map_apply measurable_prod_mk_left hs], exact measurable_measure_prod_mk_left hs end lemma measurable.map_prod_mk_right {μ : measure α} [sigma_finite μ] : measurable (λ y : β, map (λ x : α, (x, y)) μ) := begin apply measurable_of_measurable_coe, intros s hs, simp_rw [map_apply measurable_prod_mk_right hs], exact measurable_measure_prod_mk_right hs end /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) Tonelli's theorem is measurable. -/ lemma measurable.lintegral_prod_right' [sigma_finite ν] : ∀ {f : α × β → ℝ≥0∞} (hf : measurable f), measurable (λ x, ∫⁻ y, f (x, y) ∂ν) := begin have m := @measurable_prod_mk_left, refine measurable.ennreal_induction _ _ _, { intros c s hs, simp only [← indicator_comp_right], suffices : measurable (λ x, c * ν (prod.mk x ⁻¹' s)), { simpa [lintegral_indicator _ (m hs)] }, exact measurable_const.ennreal_mul (measurable_measure_prod_mk_left hs) }, { rintro f g - hf hg h2f h2g, simp_rw [pi.add_apply, lintegral_add (hf.comp m) (hg.comp m)], exact h2f.add h2g }, { intros f hf h2f h3f, have := measurable_supr h3f, have : ∀ x, monotone (λ n y, f n (x, y)) := λ x i j hij y, h2f hij (x, y), simpa [lintegral_supr (λ n, (hf n).comp m), this] } end /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) Tonelli's theorem is measurable. This version has the argument `f` in curried form. -/ lemma measurable.lintegral_prod_right [sigma_finite ν] {f : α → β → ℝ≥0∞} (hf : measurable (uncurry f)) : measurable (λ x, ∫⁻ y, f x y ∂ν) := hf.lintegral_prod_right' /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Tonelli's theorem is measurable. -/ lemma measurable.lintegral_prod_left' [sigma_finite μ] {f : α × β → ℝ≥0∞} (hf : measurable f) : measurable (λ y, ∫⁻ x, f (x, y) ∂μ) := (measurable_swap_iff.mpr hf).lintegral_prod_right' /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Tonelli's theorem is measurable. This version has the argument `f` in curried form. -/ lemma measurable.lintegral_prod_left [sigma_finite μ] {f : α → β → ℝ≥0∞} (hf : measurable (uncurry f)) : measurable (λ y, ∫⁻ x, f x y ∂μ) := hf.lintegral_prod_left' lemma measurable_set_integrable [sigma_finite ν] [opens_measurable_space E] ⦃f : α → β → E⦄ (hf : measurable (uncurry f)) : measurable_set { x | integrable (f x) ν } := begin simp_rw [integrable, hf.of_uncurry_left.ae_measurable, true_and], exact measurable_set_lt (measurable.lintegral_prod_right hf.ennnorm) measurable_const end section variables [second_countable_topology E] [normed_space ℝ E] [complete_space E] [borel_space E] /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is measurable. This version has `f` in curried form. -/ lemma measurable.integral_prod_right [sigma_finite ν] ⦃f : α → β → E⦄ (hf : measurable (uncurry f)) : measurable (λ x, ∫ y, f x y ∂ν) := begin let s : ℕ → simple_func (α × β) E := simple_func.approx_on _ hf univ _ (mem_univ 0), let s' : ℕ → α → simple_func β E := λ n x, (s n).comp (prod.mk x) measurable_prod_mk_left, let f' : ℕ → α → E := λ n, {x | integrable (f x) ν}.indicator (λ x, (s' n x).integral ν), have hf' : ∀ n, measurable (f' n), { intro n, refine measurable.indicator _ (measurable_set_integrable hf), have : ∀ x, (s' n x).range.filter (λ x, x ≠ 0) ⊆ (s n).range, { intros x, refine finset.subset.trans (finset.filter_subset _ _) _, intro y, simp_rw [simple_func.mem_range], rintro ⟨z, rfl⟩, exact ⟨(x, z), rfl⟩ }, simp only [simple_func.integral_eq_sum_of_subset (this _)], refine finset.measurable_sum _ _, intro x, refine (measurable.to_real _).smul measurable_const, simp only [simple_func.coe_comp, preimage_comp] {single_pass := tt}, apply measurable_measure_prod_mk_left, exact (s n).measurable_set_fiber x }, have h2f' : tendsto f' at_top (𝓝 (λ (x : α), ∫ (y : β), f x y ∂ν)), { rw [tendsto_pi], intro x, by_cases hfx : integrable (f x) ν, { have : ∀ n, integrable (s' n x) ν, { intro n, apply (hfx.norm.add hfx.norm).mono' (s' n x).measurable.ae_measurable, apply eventually_of_forall, intro y, simp_rw [s', simple_func.coe_comp], exact simple_func.norm_approx_on_zero_le _ _ (x, y) n }, simp only [f', hfx, simple_func.integral_eq_integral _ (this _), indicator_of_mem, mem_set_of_eq], refine tendsto_integral_of_dominated_convergence (λ y, ∥f x y∥ + ∥f x y∥) (λ n, (s' n x).ae_measurable) hf.of_uncurry_left.ae_measurable (hfx.norm.add hfx.norm) _ _, { exact λ n, eventually_of_forall (λ y, simple_func.norm_approx_on_zero_le _ _ (x, y) n) }, { exact eventually_of_forall (λ y, simple_func.tendsto_approx_on _ _ (by simp)) } }, { simpa [f', hfx, integral_undef] using @tendsto_const_nhds _ _ _ (0 : E) _, } }, exact measurable_of_tendsto_metric hf' h2f' end /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is measurable. -/ lemma measurable.integral_prod_right' [sigma_finite ν] ⦃f : α × β → E⦄ (hf : measurable f) : measurable (λ x, ∫ y, f (x, y) ∂ν) := by { rw [← uncurry_curry f] at hf, exact hf.integral_prod_right } /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Fubini's theorem is measurable. This version has `f` in curried form. -/ lemma measurable.integral_prod_left [sigma_finite μ] ⦃f : α → β → E⦄ (hf : measurable (uncurry f)) : measurable (λ y, ∫ x, f x y ∂μ) := (hf.comp measurable_swap).integral_prod_right' /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Fubini's theorem is measurable. -/ lemma measurable.integral_prod_left' [sigma_finite μ] ⦃f : α × β → E⦄ (hf : measurable f) : measurable (λ y, ∫ x, f (x, y) ∂μ) := (hf.comp measurable_swap).integral_prod_right' end /-! ### The product measure -/ namespace measure_theory namespace measure /-- The binary product of measures. They are defined for arbitrary measures, but we basically prove all properties under the assumption that at least one of them is σ-finite. -/ @[irreducible] protected def prod (μ : measure α) (ν : measure β) : measure (α × β) := bind μ $ λ x : α, map (prod.mk x) ν instance prod.measure_space {α β} [measure_space α] [measure_space β] : measure_space (α × β) := { volume := volume.prod volume } variables {μ ν} [sigma_finite ν] lemma prod_apply {s : set (α × β)} (hs : measurable_set s) : μ.prod ν s = ∫⁻ x, ν (prod.mk x ⁻¹' s) ∂μ := by simp_rw [measure.prod, bind_apply hs measurable.map_prod_mk_left, map_apply measurable_prod_mk_left hs] @[simp] lemma prod_prod {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) : μ.prod ν (s.prod t) = μ s * ν t := by simp_rw [prod_apply (hs.prod ht), mk_preimage_prod_right_eq_if, measure_if, lintegral_indicator _ hs, lintegral_const, restrict_apply measurable_set.univ, univ_inter, mul_comm] local attribute [instance] nonempty_measurable_superset /-- If we don't assume measurability of `s` and `t`, we can bound the measure of their product. -/ lemma prod_prod_le (s : set α) (t : set β) : μ.prod ν (s.prod t) ≤ μ s * ν t := begin by_cases hs0 : μ s = 0, { rcases (exists_measurable_superset_of_null hs0) with ⟨s', hs', h2s', h3s'⟩, convert measure_mono (prod_mono hs' (subset_univ _)), simp_rw [hs0, prod_prod h2s' measurable_set.univ, h3s', zero_mul] }, by_cases hti : ν t = ∞, { convert le_top, simp_rw [hti, ennreal.mul_top, hs0, if_false] }, rw [measure_eq_infi' μ], simp_rw [ennreal.infi_mul hti], refine le_infi _, rintro ⟨s', h1s', h2s'⟩, rw [subtype.coe_mk], by_cases ht0 : ν t = 0, { rcases (exists_measurable_superset_of_null ht0) with ⟨t', ht', h2t', h3t'⟩, convert measure_mono (prod_mono (subset_univ _) ht'), simp_rw [ht0, prod_prod measurable_set.univ h2t', h3t', mul_zero] }, by_cases hsi : μ s' = ∞, { convert le_top, simp_rw [hsi, ennreal.top_mul, ht0, if_false] }, rw [measure_eq_infi' ν], simp_rw [ennreal.mul_infi hsi], refine le_infi _, rintro ⟨t', h1t', h2t'⟩, convert measure_mono (prod_mono h1s' h1t'), simp [prod_prod h2s' h2t'], end lemma ae_measure_lt_top {s : set (α × β)} (hs : measurable_set s) (h2s : (μ.prod ν) s < ∞) : ∀ᵐ x ∂μ, ν (prod.mk x ⁻¹' s) < ∞ := by { simp_rw [prod_apply hs] at h2s, refine ae_lt_top (measurable_measure_prod_mk_left hs) h2s } lemma integrable_measure_prod_mk_left {s : set (α × β)} (hs : measurable_set s) (h2s : (μ.prod ν) s < ∞) : integrable (λ x, (ν (prod.mk x ⁻¹' s)).to_real) μ := begin refine ⟨(measurable_measure_prod_mk_left hs).to_real.ae_measurable, _⟩, simp_rw [has_finite_integral, ennnorm_eq_of_real to_real_nonneg], convert h2s using 1, simp_rw [prod_apply hs], apply lintegral_congr_ae, refine (ae_measure_lt_top hs h2s).mp _, apply eventually_of_forall, intros x hx, rw [lt_top_iff_ne_top] at hx, simp [of_real_to_real, hx], end /-- Note: the assumption `hs` cannot be dropped. For a counterexample, see Walter Rudin *Real and Complex Analysis*, example (c) in section 8.9. -/ lemma measure_prod_null {s : set (α × β)} (hs : measurable_set s) : μ.prod ν s = 0 ↔ (λ x, ν (prod.mk x ⁻¹' s)) =ᵐ[μ] 0 := by simp_rw [prod_apply hs, lintegral_eq_zero_iff (measurable_measure_prod_mk_left hs)] /-- Note: the converse is not true without assuming that `s` is measurable. For a counterexample, see Walter Rudin *Real and Complex Analysis*, example (c) in section 8.9. -/ lemma measure_ae_null_of_prod_null {s : set (α × β)} (h : μ.prod ν s = 0) : (λ x, ν (prod.mk x ⁻¹' s)) =ᵐ[μ] 0 := begin obtain ⟨t, hst, mt, ht⟩ := exists_measurable_superset_of_null h, simp_rw [measure_prod_null mt] at ht, rw [eventually_le_antisymm_iff], exact ⟨eventually_le.trans_eq (eventually_of_forall $ λ x, (measure_mono (preimage_mono hst) : _)) ht, eventually_of_forall $ λ x, zero_le _⟩ end /-- Note: the converse is not true. For a counterexample, see Walter Rudin *Real and Complex Analysis*, example (c) in section 8.9. -/ lemma ae_ae_of_ae_prod {p : α × β → Prop} (h : ∀ᵐ z ∂μ.prod ν, p z) : ∀ᵐ x ∂ μ, ∀ᵐ y ∂ ν, p (x, y) := measure_ae_null_of_prod_null h /-- `μ.prod ν` has finite spanning sets in rectangles of finite spanning sets. -/ def finite_spanning_sets_in.prod {ν : measure β} {C : set (set α)} {D : set (set β)} (hμ : μ.finite_spanning_sets_in C) (hν : ν.finite_spanning_sets_in D) (hC : ∀ s ∈ C, measurable_set s) (hD : ∀ t ∈ D, measurable_set t) : (μ.prod ν).finite_spanning_sets_in (image2 set.prod C D) := begin haveI := hν.sigma_finite hD, refine ⟨λ n, (hμ.set n.unpair.1).prod (hν.set n.unpair.2), λ n, mem_image2_of_mem (hμ.set_mem _) (hν.set_mem _), λ n, _, _⟩, { simp_rw [prod_prod (hC _ (hμ.set_mem _)) (hD _ (hν.set_mem _))], exact mul_lt_top (hμ.finite _) (hν.finite _) }, { simp_rw [Union_unpair_prod, hμ.spanning, hν.spanning, univ_prod_univ] } end lemma prod_fst_absolutely_continuous : map prod.fst (μ.prod ν) ≪ μ := begin refine absolutely_continuous.mk (λ s hs h2s, _), simp_rw [map_apply measurable_fst hs, ← prod_univ, prod_prod hs measurable_set.univ], rw [h2s, zero_mul] -- for some reason `simp_rw [h2s]` doesn't work end lemma prod_snd_absolutely_continuous : map prod.snd (μ.prod ν) ≪ ν := begin refine absolutely_continuous.mk (λ s hs h2s, _), simp_rw [map_apply measurable_snd hs, ← univ_prod, prod_prod measurable_set.univ hs], rw [h2s, mul_zero] -- for some reason `simp_rw [h2s]` doesn't work end variables [sigma_finite μ] instance prod.sigma_finite : sigma_finite (μ.prod ν) := ⟨⟨(μ.to_finite_spanning_sets_in.prod ν.to_finite_spanning_sets_in (λ _, id) (λ _, id)).mono $ by { rintro _ ⟨s, t, hs, ht, rfl⟩, exact hs.prod ht }⟩⟩ /-- Measures on a product space are equal the product measure if they are equal on rectangles with as sides sets that generate the corresponding σ-algebras. -/ lemma prod_eq_generate_from {μ : measure α} {ν : measure β} {C : set (set α)} {D : set (set β)} (hC : generate_from C = ‹_›) (hD : generate_from D = ‹_›) (h2C : is_pi_system C) (h2D : is_pi_system D) (h3C : μ.finite_spanning_sets_in C) (h3D : ν.finite_spanning_sets_in D) {μν : measure (α × β)} (h₁ : ∀ (s ∈ C) (t ∈ D), μν (set.prod s t) = μ s * ν t) : μ.prod ν = μν := begin have h4C : ∀ (s : set α), s ∈ C → measurable_set s, { intros s hs, rw [← hC], exact measurable_set_generate_from hs }, have h4D : ∀ (t : set β), t ∈ D → measurable_set t, { intros t ht, rw [← hD], exact measurable_set_generate_from ht }, refine (h3C.prod h3D h4C h4D).ext (generate_from_eq_prod hC hD h3C.is_countably_spanning h3D.is_countably_spanning).symm (h2C.prod h2D) _, { rintro _ ⟨s, t, hs, ht, rfl⟩, haveI := h3D.sigma_finite h4D, simp_rw [h₁ s hs t ht, prod_prod (h4C s hs) (h4D t ht)] } end /-- Measures on a product space are equal to the product measure if they are equal on rectangles. -/ lemma prod_eq {μν : measure (α × β)} (h : ∀ s t, measurable_set s → measurable_set t → μν (s.prod t) = μ s * ν t) : μ.prod ν = μν := prod_eq_generate_from generate_from_measurable_set generate_from_measurable_set is_pi_system_measurable_set is_pi_system_measurable_set μ.to_finite_spanning_sets_in ν.to_finite_spanning_sets_in (λ s hs t ht, h s t hs ht) lemma prod_swap : map prod.swap (μ.prod ν) = ν.prod μ := begin refine (prod_eq _).symm, intros s t hs ht, simp_rw [map_apply measurable_swap (hs.prod ht), preimage_swap_prod, prod_prod ht hs, mul_comm] end lemma prod_apply_symm {s : set (α × β)} (hs : measurable_set s) : μ.prod ν s = ∫⁻ y, μ ((λ x, (x, y)) ⁻¹' s) ∂ν := by { rw [← prod_swap, map_apply measurable_swap hs], simp only [prod_apply (measurable_swap hs)], refl } lemma prod_assoc_prod [sigma_finite τ] : map measurable_equiv.prod_assoc ((μ.prod ν).prod τ) = μ.prod (ν.prod τ) := begin refine (prod_eq_generate_from generate_from_measurable_set generate_from_prod is_pi_system_measurable_set is_pi_system_prod μ.to_finite_spanning_sets_in (ν.to_finite_spanning_sets_in.prod τ.to_finite_spanning_sets_in (λ _, id) (λ _, id)) _).symm, rintro s hs _ ⟨t, u, ht, hu, rfl⟩, rw [mem_set_of_eq] at hs ht hu, simp_rw [map_apply (measurable_equiv.measurable _) (hs.prod (ht.prod hu)), prod_prod ht hu, measurable_equiv.prod_assoc, measurable_equiv.coe_eq, equiv.prod_assoc_preimage, prod_prod (hs.prod ht) hu, prod_prod hs ht, mul_assoc] end /-! ### The product of specific measures -/ lemma prod_restrict {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) : (μ.restrict s).prod (ν.restrict t) = (μ.prod ν).restrict (s.prod t) := begin refine prod_eq (λ s' t' hs' ht', _), simp_rw [restrict_apply (hs'.prod ht'), prod_inter_prod, prod_prod (hs'.inter hs) (ht'.inter ht), restrict_apply hs', restrict_apply ht'] end lemma restrict_prod_eq_prod_univ {s : set α} (hs : measurable_set s) : (μ.restrict s).prod ν = (μ.prod ν).restrict (s.prod univ) := begin have : ν = ν.restrict set.univ := measure.restrict_univ.symm, rwa [this, measure.prod_restrict, ← this], exact measurable_set.univ, end lemma prod_dirac (y : β) : μ.prod (dirac y) = map (λ x, (x, y)) μ := begin refine prod_eq (λ s t hs ht, _), simp_rw [map_apply measurable_prod_mk_right (hs.prod ht), mk_preimage_prod_left_eq_if, measure_if, dirac_apply' _ ht, ← indicator_mul_right _ (λ x, μ s), pi.one_apply, mul_one] end lemma dirac_prod (x : α) : (dirac x).prod ν = map (prod.mk x) ν := begin refine prod_eq (λ s t hs ht, _), simp_rw [map_apply measurable_prod_mk_left (hs.prod ht), mk_preimage_prod_right_eq_if, measure_if, dirac_apply' _ hs, ← indicator_mul_left _ _ (λ x, ν t), pi.one_apply, one_mul] end lemma dirac_prod_dirac {x : α} {y : β} : (dirac x).prod (dirac y) = dirac (x, y) := by rw [prod_dirac, map_dirac measurable_prod_mk_right] lemma prod_sum {ι : Type*} [fintype ι] (ν : ι → measure β) [∀ i, sigma_finite (ν i)] : μ.prod (sum ν) = sum (λ i, μ.prod (ν i)) := begin refine prod_eq (λ s t hs ht, _), simp_rw [sum_apply _ (hs.prod ht), sum_apply _ ht, prod_prod hs ht, tsum_fintype, finset.mul_sum] end lemma sum_prod {ι : Type*} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] : (sum μ).prod ν = sum (λ i, (μ i).prod ν) := begin refine prod_eq (λ s t hs ht, _), simp_rw [sum_apply _ (hs.prod ht), sum_apply _ hs, prod_prod hs ht, tsum_fintype, finset.sum_mul] end lemma prod_add (ν' : measure β) [sigma_finite ν'] : μ.prod (ν + ν') = μ.prod ν + μ.prod ν' := by { refine prod_eq (λ s t hs ht, _), simp_rw [add_apply, prod_prod hs ht, left_distrib] } lemma add_prod (μ' : measure α) [sigma_finite μ'] : (μ + μ').prod ν = μ.prod ν + μ'.prod ν := by { refine prod_eq (λ s t hs ht, _), simp_rw [add_apply, prod_prod hs ht, right_distrib] } end measure end measure_theory open measure_theory.measure section lemma ae_measurable.prod_swap [sigma_finite μ] [sigma_finite ν] {f : β × α → γ} (hf : ae_measurable f (ν.prod μ)) : ae_measurable (λ (z : α × β), f z.swap) (μ.prod ν) := by { rw ← prod_swap at hf, exact hf.comp_measurable measurable_swap } lemma ae_measurable.fst [sigma_finite ν] {f : α → γ} (hf : ae_measurable f μ) : ae_measurable (λ (z : α × β), f z.1) (μ.prod ν) := hf.comp_measurable' measurable_fst prod_fst_absolutely_continuous lemma ae_measurable.snd [sigma_finite ν] {f : β → γ} (hf : ae_measurable f ν) : ae_measurable (λ (z : α × β), f z.2) (μ.prod ν) := hf.comp_measurable' measurable_snd prod_snd_absolutely_continuous /-- The Bochner integral is a.e.-measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is a.e.-measurable. -/ lemma ae_measurable.integral_prod_right' [sigma_finite ν] [second_countable_topology E] [normed_space ℝ E] [borel_space E] [complete_space E] ⦃f : α × β → E⦄ (hf : ae_measurable f (μ.prod ν)) : ae_measurable (λ x, ∫ y, f (x, y) ∂ν) μ := ⟨λ x, ∫ y, hf.mk f (x, y) ∂ν, hf.measurable_mk.integral_prod_right', begin filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk], assume x hx, exact integral_congr_ae hx end⟩ lemma ae_measurable.prod_mk_left [sigma_finite ν] {f : α × β → γ} (hf : ae_measurable f (μ.prod ν)) : ∀ᵐ x ∂μ, ae_measurable (λ y, f (x, y)) ν := begin filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk], intros x hx, exact ⟨λ y, hf.mk f (x, y), hf.measurable_mk.comp measurable_prod_mk_left, hx⟩ end end namespace measure_theory /-! ### The Lebesgue integral on a product -/ variables [sigma_finite ν] lemma lintegral_prod_swap [sigma_finite μ] (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z.swap ∂(ν.prod μ) = ∫⁻ z, f z ∂(μ.prod ν) := by { rw ← prod_swap at hf, rw [← lintegral_map' hf measurable_swap, prod_swap] } /-- Tonelli's Theorem: For `ℝ≥0∞`-valued measurable functions on `α × β`, the integral of `f` is equal to the iterated integral. -/ lemma lintegral_prod_of_measurable : ∀ (f : α × β → ℝ≥0∞) (hf : measurable f), ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ := begin have m := @measurable_prod_mk_left, refine measurable.ennreal_induction _ _ _, { intros c s hs, simp only [← indicator_comp_right], simp [lintegral_indicator, m hs, hs, lintegral_const_mul, measurable_measure_prod_mk_left hs, prod_apply] }, { rintro f g - hf hg h2f h2g, simp [lintegral_add, measurable.lintegral_prod_right', hf.comp m, hg.comp m, hf, hg, h2f, h2g] }, { intros f hf h2f h3f, have kf : ∀ x n, measurable (λ y, f n (x, y)) := λ x n, (hf n).comp m, have k2f : ∀ x, monotone (λ n y, f n (x, y)) := λ x i j hij y, h2f hij (x, y), have lf : ∀ n, measurable (λ x, ∫⁻ y, f n (x, y) ∂ν) := λ n, (hf n).lintegral_prod_right', have l2f : monotone (λ n x, ∫⁻ y, f n (x, y) ∂ν) := λ i j hij x, lintegral_mono (k2f x hij), simp only [lintegral_supr hf h2f, lintegral_supr (kf _), k2f, lintegral_supr lf l2f, h3f] }, end /-- Tonelli's Theorem: For `ℝ≥0∞`-valued almost everywhere measurable functions on `α × β`, the integral of `f` is equal to the iterated integral. -/ lemma lintegral_prod (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ := begin have A : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ z, hf.mk f z ∂(μ.prod ν) := lintegral_congr_ae hf.ae_eq_mk, have B : ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ = ∫⁻ x, ∫⁻ y, hf.mk f (x, y) ∂ν ∂μ, { apply lintegral_congr_ae, filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk], assume a ha, exact lintegral_congr_ae ha }, rw [A, B, lintegral_prod_of_measurable _ hf.measurable_mk], apply_instance end /-- The symmetric verion of Tonelli's Theorem: For `ℝ≥0∞`-valued almost everywhere measurable functions on `α × β`, the integral of `f` is equal to the iterated integral, in reverse order. -/ lemma lintegral_prod_symm' [sigma_finite μ] (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ y, ∫⁻ x, f (x, y) ∂μ ∂ν := by { simp_rw [← lintegral_prod_swap f hf], exact lintegral_prod _ hf.prod_swap } /-- The symmetric verion of Tonelli's Theorem: For `ℝ≥0∞`-valued measurable functions on `α × β`, the integral of `f` is equal to the iterated integral, in reverse order. -/ lemma lintegral_prod_symm [sigma_finite μ] (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ y, ∫⁻ x, f (x, y) ∂μ ∂ν := lintegral_prod_symm' f hf /-- The reversed version of Tonelli's Theorem. In this version `f` is in curried form, which makes it easier for the elaborator to figure out `f` automatically. -/ lemma lintegral_lintegral ⦃f : α → β → ℝ≥0∞⦄ (hf : ae_measurable (uncurry f) (μ.prod ν)) : ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ = ∫⁻ z, f z.1 z.2 ∂(μ.prod ν) := (lintegral_prod _ hf).symm /-- The reversed version of Tonelli's Theorem (symmetric version). In this version `f` is in curried form, which makes it easier for the elaborator to figure out `f` automatically. -/ lemma lintegral_lintegral_symm [sigma_finite μ] ⦃f : α → β → ℝ≥0∞⦄ (hf : ae_measurable (uncurry f) (μ.prod ν)) : ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ = ∫⁻ z, f z.2 z.1 ∂(ν.prod μ) := (lintegral_prod_symm _ hf.prod_swap).symm /-- Change the order of Lebesgue integration. -/ lemma lintegral_lintegral_swap [sigma_finite μ] ⦃f : α → β → ℝ≥0∞⦄ (hf : ae_measurable (uncurry f) (μ.prod ν)) : ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ = ∫⁻ y, ∫⁻ x, f x y ∂μ ∂ν := (lintegral_lintegral hf).trans (lintegral_prod_symm _ hf) lemma lintegral_prod_mul {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g ν) : ∫⁻ z, f z.1 * g z.2 ∂(μ.prod ν) = ∫⁻ x, f x ∂μ * ∫⁻ y, g y ∂ν := by simp [lintegral_prod _ (hf.fst.ennreal_mul hg.snd), lintegral_lintegral_mul hf hg] /-! ### Integrability on a product -/ section variables [opens_measurable_space E] lemma integrable.swap [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (f ∘ prod.swap) (ν.prod μ) := ⟨hf.ae_measurable.prod_swap, (lintegral_prod_swap _ hf.ae_measurable.ennnorm : _).le.trans_lt hf.has_finite_integral⟩ lemma integrable_swap_iff [sigma_finite μ] ⦃f : α × β → E⦄ : integrable (f ∘ prod.swap) (ν.prod μ) ↔ integrable f (μ.prod ν) := ⟨λ hf, by { convert hf.swap, ext ⟨x, y⟩, refl }, λ hf, hf.swap⟩ lemma has_finite_integral_prod_iff ⦃f : α × β → E⦄ (h1f : measurable f) : has_finite_integral f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, has_finite_integral (λ y, f (x, y)) ν) ∧ has_finite_integral (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := begin simp only [has_finite_integral, lintegral_prod_of_measurable _ h1f.ennnorm], have : ∀ x, ∀ᵐ y ∂ν, 0 ≤ ∥f (x, y)∥ := λ x, eventually_of_forall (λ y, norm_nonneg _), simp_rw [integral_eq_lintegral_of_nonneg_ae (this _) (h1f.norm.comp measurable_prod_mk_left).ae_measurable, ennnorm_eq_of_real to_real_nonneg, of_real_norm_eq_coe_nnnorm], -- this fact is probably too specialized to be its own lemma have : ∀ {p q r : Prop} (h1 : r → p), (r ↔ p ∧ q) ↔ (p → (r ↔ q)) := λ p q r h1, by rw [← and.congr_right_iff, and_iff_right_of_imp h1], rw [this], { intro h2f, rw lintegral_congr_ae, refine h2f.mp _, apply eventually_of_forall, intros x hx, dsimp only, rw [of_real_to_real], rw [← lt_top_iff_ne_top], exact hx }, { intro h2f, refine ae_lt_top _ h2f, exact h1f.ennnorm.lintegral_prod_right' }, end lemma has_finite_integral_prod_iff' ⦃f : α × β → E⦄ (h1f : ae_measurable f (μ.prod ν)) : has_finite_integral f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, has_finite_integral (λ y, f (x, y)) ν) ∧ has_finite_integral (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := begin rw [has_finite_integral_congr h1f.ae_eq_mk, has_finite_integral_prod_iff h1f.measurable_mk], apply and_congr, { apply eventually_congr, filter_upwards [ae_ae_of_ae_prod h1f.ae_eq_mk.symm], assume x hx, exact has_finite_integral_congr hx }, { apply has_finite_integral_congr, filter_upwards [ae_ae_of_ae_prod h1f.ae_eq_mk.symm], assume x hx, exact integral_congr_ae (eventually_eq.fun_comp hx _) }, { apply_instance } end /-- A binary function is integrable if the function `y ↦ f (x, y)` is integrable for almost every `x` and the function `x ↦ ∫ ∥f (x, y)∥ dy` is integrable. -/ lemma integrable_prod_iff ⦃f : α × β → E⦄ (h1f : ae_measurable f (μ.prod ν)) : integrable f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, integrable (λ y, f (x, y)) ν) ∧ integrable (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := by simp [integrable, h1f, has_finite_integral_prod_iff', h1f.norm.integral_prod_right', h1f.prod_mk_left] /-- A binary function is integrable if the function `x ↦ f (x, y)` is integrable for almost every `y` and the function `y ↦ ∫ ∥f (x, y)∥ dx` is integrable. -/ lemma integrable_prod_iff' [sigma_finite μ] ⦃f : α × β → E⦄ (h1f : ae_measurable f (μ.prod ν)) : integrable f (μ.prod ν) ↔ (∀ᵐ y ∂ ν, integrable (λ x, f (x, y)) μ) ∧ integrable (λ y, ∫ x, ∥f (x, y)∥ ∂μ) ν := by { convert integrable_prod_iff (h1f.prod_swap) using 1, rw [integrable_swap_iff] } lemma integrable.prod_left_ae [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : ∀ᵐ y ∂ ν, integrable (λ x, f (x, y)) μ := ((integrable_prod_iff' hf.ae_measurable).mp hf).1 lemma integrable.prod_right_ae [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : ∀ᵐ x ∂ μ, integrable (λ y, f (x, y)) ν := hf.swap.prod_left_ae lemma integrable.integral_norm_prod_left ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := ((integrable_prod_iff hf.ae_measurable).mp hf).2 lemma integrable.integral_norm_prod_right [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ y, ∫ x, ∥f (x, y)∥ ∂μ) ν := hf.swap.integral_norm_prod_left end variables [second_countable_topology E] [normed_space ℝ E] [complete_space E] [borel_space E] lemma integrable.integral_prod_left ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ x, ∫ y, f (x, y) ∂ν) μ := integrable.mono hf.integral_norm_prod_left hf.ae_measurable.integral_prod_right' $ eventually_of_forall $ λ x, (norm_integral_le_integral_norm _).trans_eq $ (norm_of_nonneg $ integral_nonneg_of_ae $ eventually_of_forall $ λ y, (norm_nonneg _ : _)).symm lemma integrable.integral_prod_right [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ y, ∫ x, f (x, y) ∂μ) ν := hf.swap.integral_prod_left /-! ### The Bochner integral on a product -/ variables [sigma_finite μ] lemma integral_prod_swap (f : α × β → E) (hf : ae_measurable f (μ.prod ν)) : ∫ z, f z.swap ∂(ν.prod μ) = ∫ z, f z ∂(μ.prod ν) := begin rw ← prod_swap at hf, rw [← integral_map measurable_swap hf, prod_swap] end variables {E' : Type*} [measurable_space E'] [normed_group E'] [borel_space E'] [complete_space E'] [normed_space ℝ E'] [second_countable_topology E'] /-! Some rules about the sum/difference of double integrals. They follow from `integral_add`, but we separate them out as separate lemmas, because they involve quite some steps. -/ /-- Integrals commute with addition inside another integral. `F` can be any function. -/ lemma integral_fn_integral_add ⦃f g : α × β → E⦄ (F : E → E') (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, F (∫ y, f (x, y) + g (x, y) ∂ν) ∂μ = ∫ x, F (∫ y, f (x, y) ∂ν + ∫ y, g (x, y) ∂ν) ∂μ := begin refine integral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae], intros x h2f h2g, simp [integral_add h2f h2g], end /-- Integrals commute with subtraction inside another integral. `F` can be any measurable function. -/ lemma integral_fn_integral_sub ⦃f g : α × β → E⦄ (F : E → E') (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, F (∫ y, f (x, y) - g (x, y) ∂ν) ∂μ = ∫ x, F (∫ y, f (x, y) ∂ν - ∫ y, g (x, y) ∂ν) ∂μ := begin refine integral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae], intros x h2f h2g, simp [integral_sub h2f h2g] end /-- Integrals commute with subtraction inside a lower Lebesgue integral. `F` can be any function. -/ lemma lintegral_fn_integral_sub ⦃f g : α × β → E⦄ (F : E → ℝ≥0∞) (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫⁻ x, F (∫ y, f (x, y) - g (x, y) ∂ν) ∂μ = ∫⁻ x, F (∫ y, f (x, y) ∂ν - ∫ y, g (x, y) ∂ν) ∂μ := begin refine lintegral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae], intros x h2f h2g, simp [integral_sub h2f h2g] end /-- Double integrals commute with addition. -/ lemma integral_integral_add ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, f (x, y) + g (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ + ∫ x, ∫ y, g (x, y) ∂ν ∂μ := (integral_fn_integral_add id hf hg).trans $ integral_add hf.integral_prod_left hg.integral_prod_left /-- Double integrals commute with addition. This is the version with `(f + g) (x, y)` (instead of `f (x, y) + g (x, y)`) in the LHS. -/ lemma integral_integral_add' ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, (f + g) (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ + ∫ x, ∫ y, g (x, y) ∂ν ∂μ := integral_integral_add hf hg /-- Double integrals commute with subtraction. -/ lemma integral_integral_sub ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, f (x, y) - g (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ - ∫ x, ∫ y, g (x, y) ∂ν ∂μ := (integral_fn_integral_sub id hf hg).trans $ integral_sub hf.integral_prod_left hg.integral_prod_left /-- Double integrals commute with subtraction. This is the version with `(f - g) (x, y)` (instead of `f (x, y) - g (x, y)`) in the LHS. -/ lemma integral_integral_sub' ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, (f - g) (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ - ∫ x, ∫ y, g (x, y) ∂ν ∂μ := integral_integral_sub hf hg /-- The map that sends an L¹-function `f : α × β → E` to `∫∫f` is continuous. -/ lemma continuous_integral_integral : continuous (λ (f : α × β →₁[μ.prod ν] E), ∫ x, ∫ y, f (x, y) ∂ν ∂μ) := begin rw [continuous_iff_continuous_at], intro g, refine tendsto_integral_of_L1 _ (L1.integrable_coe_fn g).integral_prod_left (eventually_of_forall $ λ h, (L1.integrable_coe_fn h).integral_prod_left) _, simp_rw [edist_eq_coe_nnnorm_sub, ← lintegral_fn_integral_sub (λ x, (nnnorm x : ℝ≥0∞)) (L1.integrable_coe_fn _) (L1.integrable_coe_fn g)], refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds _ (λ i, zero_le _) _, { exact λ i, ∫⁻ x, ∫⁻ y, nnnorm (i (x, y) - g (x, y)) ∂ν ∂μ }, swap, { exact λ i, lintegral_mono (λ x, ennnorm_integral_le_lintegral_ennnorm _) }, show tendsto (λ (i : α × β →₁[μ.prod ν] E), ∫⁻ x, ∫⁻ (y : β), nnnorm (i (x, y) - g (x, y)) ∂ν ∂μ) (𝓝 g) (𝓝 0), have : ∀ (i : α × β →₁[μ.prod ν] E), measurable (λ z, (nnnorm (i z - g z) : ℝ≥0∞)) := λ i, ((Lp.measurable i).sub (Lp.measurable g)).ennnorm, simp_rw [← lintegral_prod_of_measurable _ (this _), ← L1.of_real_norm_sub_eq_lintegral, ← of_real_zero], refine (continuous_of_real.tendsto 0).comp _, rw [← tendsto_iff_norm_tendsto_zero], exact tendsto_id end /-- Fubini's Theorem: For integrable functions on `α × β`, the Bochner integral of `f` is equal to the iterated Bochner integral. `integrable_prod_iff` can be useful to show that the function in question in integrable. `measure_theory.integrable.integral_prod_right` is useful to show that the inner integral of the right-hand side is integrable. -/ lemma integral_prod : ∀ (f : α × β → E) (hf : integrable f (μ.prod ν)), ∫ z, f z ∂(μ.prod ν) = ∫ x, ∫ y, f (x, y) ∂ν ∂μ := begin apply integrable.induction, { intros c s hs h2s, simp_rw [integral_indicator hs, ← indicator_comp_right, function.comp, integral_indicator (measurable_prod_mk_left hs), set_integral_const, integral_smul_const, integral_to_real (measurable_measure_prod_mk_left hs).ae_measurable (ae_measure_lt_top hs h2s), prod_apply hs] }, { intros f g hfg i_f i_g hf hg, simp_rw [integral_add' i_f i_g, integral_integral_add' i_f i_g, hf, hg] }, { exact is_closed_eq continuous_integral continuous_integral_integral }, { intros f g hfg i_f hf, convert hf using 1, { exact integral_congr_ae hfg.symm }, { refine integral_congr_ae _, refine (ae_ae_of_ae_prod hfg).mp _, apply eventually_of_forall, intros x hfgx, exact integral_congr_ae (ae_eq_symm hfgx) } } end /-- Symmetric version of Fubini's Theorem: For integrable functions on `α × β`, the Bochner integral of `f` is equal to the iterated Bochner integral. This version has the integrals on the right-hand side in the other order. -/ lemma integral_prod_symm (f : α × β → E) (hf : integrable f (μ.prod ν)) : ∫ z, f z ∂(μ.prod ν) = ∫ y, ∫ x, f (x, y) ∂μ ∂ν := by { simp_rw [← integral_prod_swap f hf.ae_measurable], exact integral_prod _ hf.swap } /-- Reversed version of Fubini's Theorem. -/ lemma integral_integral {f : α → β → E} (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ z, f z.1 z.2 ∂(μ.prod ν) := (integral_prod _ hf).symm /-- Reversed version of Fubini's Theorem (symmetric version). -/ lemma integral_integral_symm {f : α → β → E} (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ z, f z.2 z.1 ∂(ν.prod μ) := (integral_prod_symm _ hf.swap).symm /-- Change the order of Bochner integration. -/ lemma integral_integral_swap ⦃f : α → β → E⦄ (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ y, ∫ x, f x y ∂μ ∂ν := (integral_integral hf).trans (integral_prod_symm _ hf) end measure_theory
04b1bfb16da0de2d98dab114555a0e3f162bf8f5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/struct_instance_in_eqn.lean
f1090569b1df0af6ce43baa72fbb590405161317
[ "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
118
lean
structure S := (x : Nat) (y : Bool) (z : Nat) (w : Nat) def g : S → S | s@{ x := x, ..} => { s with x := x + 1 }
2354f71f604b6668c21222df299ca2fe087d6380
ec62863c729b7eedee77b86d974f2c529fa79d25
/6/a.lean
8aa664c4107a5b9c63c44507adbe4203dea75289
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
265
lean
def solve1 (s : String) : Int := ("abcdefghijklmnopqrstuvwxyz".toList.filter (λ c => s.toList.elem c)).length def main : IO Unit := do let input ← IO.FS.readFile "a.in" let paras := input.splitOn "\n\n" IO.print s!"{(paras.map solve1).foldl Int.add 0}\n"
57a7f7577a44382ad3a5013f64b1415b6c89ed36
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Tactic/Unfold.lean
fc0936d15471f61396c1cab09012f895544970bf
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,092
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.Meta.Tactic.Unfold import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.Location namespace Lean.Elab.Tactic open Meta def unfoldLocalDecl (declName : Name) (fvarId : FVarId) : TacticM Unit := do replaceMainGoal [← Meta.unfoldLocalDecl (← getMainGoal) fvarId declName] def unfoldTarget (declName : Name) : TacticM Unit := do replaceMainGoal [← Meta.unfoldTarget (← getMainGoal) declName] /-- "unfold " ident+ (location)? -/ @[builtin_tactic Lean.Parser.Tactic.unfold] def evalUnfold : Tactic := fun stx => do let loc := expandOptLocation stx[2] for declNameId in stx[1].getArgs do go declNameId loc where go (declNameId : Syntax) (loc : Location) : TacticM Unit := do let declName ← resolveGlobalConstNoOverloadWithInfo declNameId withLocation loc (unfoldLocalDecl declName) (unfoldTarget declName) (throwTacticEx `unfold · m!"did not unfold '{declName}'") end Lean.Elab.Tactic
1c86e59f40ec3137722abe555ca506d1f0b17b21
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/control/bitraversable/instances.lean
2314d1f381b9f5756f178dbb9ec3dc6c4c70f6c6
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
4,378
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author(s): Simon Hudon -/ import control.bitraversable.lemmas import control.traversable.lemmas /-! # bitraversable instances ## Instances * prod * sum * const * flip * bicompl * bicompr ## References * Hackage: <https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html> ## Tags traversable bitraversable functor bifunctor applicative -/ universes u v w variables {t : Type u → Type u → Type u} [bitraversable t] section variables {F : Type u → Type u} [applicative F] def prod.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : α × β → F (α' × β') | (x,y) := prod.mk <$> f x <*> f' y instance : bitraversable prod := { bitraverse := @prod.bitraverse } instance : is_lawful_bitraversable prod := by constructor; introsI; cases x; simp [bitraverse,prod.bitraverse] with functor_norm; refl open functor def sum.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : α ⊕ β → F (α' ⊕ β') | (sum.inl x) := sum.inl <$> f x | (sum.inr x) := sum.inr <$> f' x instance : bitraversable sum := { bitraverse := @sum.bitraverse } instance : is_lawful_bitraversable sum := by constructor; introsI; cases x; simp [bitraverse,sum.bitraverse] with functor_norm; refl def const.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : const α β → F (const α' β') := f instance bitraversable.const : bitraversable const := { bitraverse := @const.bitraverse } instance is_lawful_bitraversable.const : is_lawful_bitraversable const := by constructor; introsI; simp [bitraverse,const.bitraverse] with functor_norm; refl def flip.bitraverse {α α' β β'} (f : α → F α') (f' : β → F β') : flip t α β → F (flip t α' β') := (bitraverse f' f : t β α → F (t β' α')) instance bitraversable.flip : bitraversable (flip t) := { bitraverse := @flip.bitraverse t _ } open is_lawful_bitraversable instance is_lawful_bitraversable.flip [is_lawful_bitraversable t] : is_lawful_bitraversable (flip t) := by constructor; intros; unfreezingI { casesm is_lawful_bitraversable t }; apply_assumption open bitraversable functor @[priority 10] instance bitraversable.traversable {α} : traversable (t α) := { traverse := @tsnd t _ _ } @[priority 10] instance bitraversable.is_lawful_traversable [is_lawful_bitraversable t] {α} : is_lawful_traversable (t α) := by { constructor; introsI; simp [traverse,comp_tsnd] with functor_norm, { refl }, { simp [tsnd_eq_snd_id], refl }, { simp [tsnd,binaturality,function.comp] with functor_norm } } end open bifunctor traversable is_lawful_traversable is_lawful_bitraversable open function (bicompl bicompr) section bicompl variables (F G : Type u → Type u) [traversable F] [traversable G] def bicompl.bitraverse {m} [applicative m] {α β α' β'} (f : α → m β) (f' : α' → m β') : bicompl t F G α α' → m (bicompl t F G β β') := (bitraverse (traverse f) (traverse f') : t (F α) (G α') → m _) instance : bitraversable (bicompl t F G) := { bitraverse := @bicompl.bitraverse t _ F G _ _ } instance [is_lawful_traversable F] [is_lawful_traversable G] [is_lawful_bitraversable t] : is_lawful_bitraversable (bicompl t F G) := begin constructor; introsI; simp [bitraverse,bicompl.bitraverse,bimap,traverse_id,bitraverse_id_id,comp_bitraverse] with functor_norm, { simp [traverse_eq_map_id',bitraverse_eq_bimap_id], }, { revert x, dunfold bicompl, simp [binaturality,naturality_pf] } end end bicompl section bicompr variables (F : Type u → Type u) [traversable F] def bicompr.bitraverse {m} [applicative m] {α β α' β'} (f : α → m β) (f' : α' → m β') : bicompr F t α α' → m (bicompr F t β β') := (traverse (bitraverse f f') : F (t α α') → m _) instance : bitraversable (bicompr F t) := { bitraverse := @bicompr.bitraverse t _ F _ } instance [is_lawful_traversable F] [is_lawful_bitraversable t] : is_lawful_bitraversable (bicompr F t) := begin constructor; introsI; simp [bitraverse,bicompr.bitraverse,bitraverse_id_id] with functor_norm, { simp [bitraverse_eq_bimap_id',traverse_eq_map_id'], refl }, { revert x, dunfold bicompr, intro, simp [naturality,binaturality'] } end end bicompr
d8bc8f8d7600bb9907ec5b9fc32cae5bafb62446
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_492.lean
2e5b854e289cb110becfcef62474613638a1deb6
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
510
lean
import algebra.ring variables (R : Type*) [ring R] #check (add_assoc : ∀ a b c : R, a + b + c = a + (b + c)) #check (add_comm : ∀ a b : R, a + b = b + a) #check (zero_add : ∀ a : R, 0 + a = a) #check (add_left_neg : ∀ a : R, -a + a = 0) #check (mul_assoc : ∀ a b c : R, a * b * c = a * (b * c)) #check (mul_one : ∀ a : R, a * 1 = a) #check (one_mul : ∀ a : R, 1 * a = a) #check (mul_add : ∀ a b c : R, a * (b + c) = a * b + a * c) #check (add_mul : ∀ a b c : R, (a + b) * c = a * c + b * c)
16967d7a79a053ee0ef0ccdc31c74a8b931fc538
56af0912bd25910f5caae91d6dd0603b0c032989
/src/complex/kb_solutions/Level_06_alg_closed.lean
837000a2d8f752fe66ad99320d89530ca052f8c5
[ "Apache-2.0" ]
permissive
isabella232/complex-number-game
ae36e0b1df9761d9df07049ca29c91ae44dbdc2d
3d767f14041f9002e435bed3a3527fdd297c166d
refs/heads/master
1,679,305,953,116
1,606,397,567,000
1,606,397,567,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
958
lean
/- Copyright (c) 2020 The Xena project. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kevin Buzzard Thanks: Imperial College London, leanprover-community -/ -- Import levels 1 to 5 import complex.Level_05_field -- Import the theory polynomials in one variable over rings import data.polynomial /-! # Level 6: The complex numbers are algebraically closed -/ namespace complex open polynomial lemma exists_root {f : polynomial ℂ} (hf : 0 < degree f) : ∃ z : ℂ, is_root f z := begin sorry end end complex /- Chris Hughes, an undergraduate mathematician at Imperial College London, proved this result in Lean and PR'ed it to Lean's maths library. Here is the link to the docs https://leanprover-community.github.io/mathlib_docs/analysis/complex/polynomial.html and here is the code https://github.com/leanprover-community/mathlib/blob/3710744/src/analysis/complex/polynomial.lean#L34 -/
da65ffd0ae2167faecaa119aa42f263b7c9a5800
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/tactic/transform_decl.lean
e676e45412c2df40d502063ba6d424ce2bca29fb
[ "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
5,283
lean
/- Copyright (c) 2017 Mario Carneiro All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn -/ import tactic.core namespace tactic open expr /-- Auxilliary function for `additive_test`. The bool argument *only* matters when applied to exactly a constant. -/ meta def additive_test_aux (f : name → option name) (ignore : name_map $ list ℕ) : bool → expr → bool | b (var n) := tt | b (sort l) := tt | b (const n ls) := b || (f n).is_some | b (mvar n m t) := tt | b (local_const n m bi t) := tt | b (app e f) := additive_test_aux tt e && -- this might be inefficient. -- If it becomes a performance problem: we can give this info for the recursive call to `e`. match ignore.find e.get_app_fn.const_name with | some l := if e.get_app_num_args + 1 ∈ l then tt else additive_test_aux ff f | none := additive_test_aux ff f end | b (lam n bi e t) := additive_test_aux ff t | b (pi n bi e t) := additive_test_aux ff t | b (elet n g e f) := additive_test_aux ff e && additive_test_aux ff f | b (macro d args) := tt /-- `additive_test f replace_all ignore e` tests whether the expression `e` contains no constant `nm` that is not applied to any arguments, and such that `f nm = none`. This is used in `@[to_additive]` for deciding which subexpressions to transform: we only transform constants if `additive_test` applied to their first argument returns `tt`. This means we will replace expression applied to e.g. `α` or `α × β`, but not when applied to e.g. `ℕ` or `ℝ × α`. `f` is the dictionary of declarations that are in the `to_additive` dictionary. We ignore all arguments specified in the `name_map` `ignore`. If `replace_all` is `tt` the test always return `tt`. -/ meta def additive_test (f : name → option name) (replace_all : bool) (ignore : name_map $ list ℕ) (e : expr) : bool := if replace_all then tt else additive_test_aux f ignore ff e /-- transform the declaration `src` and all declarations `pre._proof_i` occurring in `src` using the dictionary `f`. `replace_all`, `trace`, `ignore` and `reorder` are configuration options. `pre` is the declaration that got the `@[to_additive]` attribute and `tgt_pre` is the target of this declaration. -/ meta def transform_decl_with_prefix_fun_aux (f : name → option name) (replace_all trace : bool) (ignore reorder : name_map $ list ℕ) (pre tgt_pre : name) (attrs : list name) : name → command := λ src, do -- if this declaration is not `pre` or an internal declaration, we do nothing. tt ← return (src = pre ∨ src.is_internal : bool) | if (f src).is_some then skip else fail!("@[to_additive] failed. The declaration {pre} depends on the declaration {src} which is in the namespace {pre}, but " ++ "does not have the `@[to_additive]` attribute. This is not supported. Workaround: move {src} to " ++ "a different namespace."), env ← get_env, -- we find the additive name of `src` let tgt := src.map_prefix (λ n, if n = pre then some tgt_pre else none), -- we skip if we already transformed this declaration before ff ← return $ env.contains tgt | skip, decl ← get_decl src, -- we first transform all the declarations of the form `pre._proof_i` (decl.type.list_names_with_prefix pre).mfold () (λ n _, transform_decl_with_prefix_fun_aux n), (decl.value.list_names_with_prefix pre).mfold () (λ n _, transform_decl_with_prefix_fun_aux n), -- we transform `decl` using `f` and the configuration options. let decl := decl.update_with_fun env (name.map_prefix f) (additive_test f replace_all ignore) reorder tgt, pp_decl ← pp decl, when trace $ trace!"[to_additive] > generating\n{pp_decl}", decorate_error (format!"@[to_additive] failed. Type mismatch in additive declaration. For help, see the docstring of `to_additive.attr`, section `Troubleshooting`. Failed to add declaration\n{pp_decl} Nested error message:\n").to_string $ -- the empty line is intentional if env.is_protected src then add_protected_decl decl else add_decl decl, attrs.mmap' $ λ n, copy_attribute n src tgt /-- Make a new copy of a declaration, replacing fragments of the names of identifiers in the type and the body using the function `f`. This is used to implement `@[to_additive]`. -/ meta def transform_decl_with_prefix_fun (f : name → option name) (replace_all trace : bool) (ignore reorder : name_map $ list ℕ) (src tgt : name) (attrs : list name) : command := do transform_decl_with_prefix_fun_aux f replace_all trace ignore reorder src tgt attrs src, ls ← get_eqn_lemmas_for tt src, ls.mmap' $ transform_decl_with_prefix_fun_aux f replace_all trace ignore reorder src tgt attrs /-- Make a new copy of a declaration, replacing fragments of the names of identifiers in the type and the body using the dictionary `dict`. This is used to implement `@[to_additive]`. -/ meta def transform_decl_with_prefix_dict (dict : name_map name) (replace_all trace : bool) (ignore reorder : name_map $ list ℕ) (src tgt : name) (attrs : list name) : command := transform_decl_with_prefix_fun dict.find replace_all trace ignore reorder src tgt attrs end tactic
1d4b7d124c6010cc609b0de9b6da37496a76e330
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/analysis/convex/basic.lean
77b46a64abfe4b3da3f785f40e5bafc916ce831d
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,551
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov, Yaël Dillies -/ import algebra.order.module import linear_algebra.affine_space.affine_subspace /-! # Convex sets and functions in vector spaces In a 𝕜-vector space, we define the following objects and properties. * `segment 𝕜 x y`: Closed segment joining `x` and `y`. * `open_segment 𝕜 x y`: Open segment joining `x` and `y`. * `convex 𝕜 s`: A set `s` is convex if for any two points `x y ∈ s` it includes `segment 𝕜 x y`. * `std_simplex 𝕜 ι`: The standard simplex in `ι → 𝕜` (currently requires `fintype ι`). It is the intersection of the positive quadrant with the hyperplane `s.sum = 1`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex. ## Notations We provide the following notation: * `[x -[𝕜] y] = segment 𝕜 x y` in locale `convex` ## TODO Generalize all this file to affine spaces. Should we rename `segment` and `open_segment` to `convex.Icc` and `convex.Ioo`? Should we also define `clopen_segment`/`convex.Ico`/`convex.Ioc`? -/ variables {𝕜 E F β : Type*} open linear_map set open_locale big_operators classical pointwise /-! ### Segment -/ section ordered_semiring variables [ordered_semiring 𝕜] [add_comm_monoid E] section has_scalar variables (𝕜) [has_scalar 𝕜 E] /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | ∃ (a b : 𝕜) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} /-- Open segment in a vector space. Note that `open_segment 𝕜 x x = {x}` instead of being `∅` when the base semiring has some element between `0` and `1`. -/ def open_segment (x y : E) : set E := {z : E | ∃ (a b : 𝕜) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1), a • x + b • y = z} localized "notation `[` x ` -[` 𝕜 `] ` y `]` := segment 𝕜 x y" in convex lemma segment_symm (x y : E) : [x -[𝕜] y] = [y -[𝕜] x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_symm (x y : E) : open_segment 𝕜 x y = open_segment 𝕜 y x := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_subset_segment (x y : E) : open_segment 𝕜 x y ⊆ [x -[𝕜] y] := λ z ⟨a, b, ha, hb, hab, hz⟩, ⟨a, b, ha.le, hb.le, hab, hz⟩ end has_scalar open_locale convex section mul_action_with_zero variables (𝕜) [mul_action_with_zero 𝕜 E] lemma left_mem_segment (x y : E) : x ∈ [x -[𝕜] y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x -[𝕜] y] := segment_symm 𝕜 y x ▸ left_mem_segment 𝕜 y x end mul_action_with_zero section module variables (𝕜) [module 𝕜 E] lemma segment_same (x : E) : [x -[𝕜] x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment 𝕜 z z⟩ lemma mem_open_segment_of_ne_left_right {x y z : E} (hx : x ≠ z) (hy : y ≠ z) (hz : z ∈ [x -[𝕜] y]) : z ∈ open_segment 𝕜 x y := begin obtain ⟨a, b, ha, hb, hab, hz⟩ := hz, by_cases ha' : a = 0, { rw [ha', zero_add] at hab, rw [ha', hab, zero_smul, one_smul, zero_add] at hz, exact (hy hz).elim }, by_cases hb' : b = 0, { rw [hb', add_zero] at hab, rw [hb', hab, zero_smul, one_smul, add_zero] at hz, exact (hx hz).elim }, exact ⟨a, b, ha.lt_of_ne (ne.symm ha'), hb.lt_of_ne (ne.symm hb'), hab, hz⟩, end variables {𝕜} lemma open_segment_subset_iff_segment_subset {x y : E} {s : set E} (hx : x ∈ s) (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s ↔ [x -[𝕜] y] ⊆ s := begin refine ⟨λ h z hz, _, (open_segment_subset_segment 𝕜 x y).trans⟩, obtain rfl | hxz := eq_or_ne x z, { exact hx }, obtain rfl | hyz := eq_or_ne y z, { exact hy }, exact h (mem_open_segment_of_ne_left_right 𝕜 hxz hyz hz), end lemma convex.combo_self {a b : 𝕜} (h : a + b = 1) (x : E) : a • x + b • x = x := by rw [←add_smul, h, one_smul] end module end ordered_semiring open_locale convex section ordered_ring variables [ordered_ring 𝕜] section add_comm_group variables (𝕜) [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] section densely_ordered variables [nontrivial 𝕜] [densely_ordered 𝕜] @[simp] lemma open_segment_same (x : E) : open_segment 𝕜 x x = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [← add_smul, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ (h : z = x), begin obtain ⟨a, ha₀, ha₁⟩ := densely_ordered.dense (0 : 𝕜) 1 zero_lt_one, refine ⟨a, 1 - a, ha₀, sub_pos_of_lt ha₁, add_sub_cancel'_right _ _, _⟩, rw [←add_smul, add_sub_cancel'_right, one_smul, h], end⟩ end densely_ordered lemma segment_eq_image (x y : E) : [x -[𝕜] y] = (λ θ : 𝕜, (1 - θ) • x + θ • y) '' Icc (0 : 𝕜) 1 := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma open_segment_eq_image (x y : E) : open_segment 𝕜 x y = (λ (θ : 𝕜), (1 - θ) • x + θ • y) '' Ioo (0 : 𝕜) 1 := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ lt_add_of_pos_left _ ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1 - θ, θ, sub_pos.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image₂ (x y : E) : [x -[𝕜] y] = (λ p : 𝕜 × 𝕜, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma open_segment_eq_image₂ (x y : E) : open_segment 𝕜 x y = (λ p : 𝕜 × 𝕜, p.1 • x + p.2 • y) '' {p | 0 < p.1 ∧ 0 < p.2 ∧ p.1 + p.2 = 1} := by simp only [open_segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_image' (x y : E) : [x -[𝕜] y] = (λ (θ : 𝕜), x + θ • (y - x)) '' Icc (0 : 𝕜) 1 := by { convert segment_eq_image 𝕜 x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma open_segment_eq_image' (x y : E) : open_segment 𝕜 x y = (λ (θ : 𝕜), x + θ • (y - x)) '' Ioo (0 : 𝕜) 1 := by { convert open_segment_eq_image 𝕜 x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_image (f : E →ₗ[𝕜] F) (a b : E) : f '' [a -[𝕜] b] = [f a -[𝕜] f b] := set.ext (λ x, by simp_rw [segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) @[simp] lemma open_segment_image (f : E →ₗ[𝕜] F) (a b : E) : f '' open_segment 𝕜 a b = open_segment 𝕜 (f a) (f b) := set.ext (λ x, by simp_rw [open_segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b -[𝕜] a + c] ↔ x ∈ [b -[𝕜] c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end @[simp] lemma mem_open_segment_translate (a : E) {x b c : E} : a + x ∈ open_segment 𝕜 (a + b) (a + c) ↔ x ∈ open_segment 𝕜 b c := begin rw [open_segment_eq_image', open_segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b -[𝕜] a + c] = [b -[𝕜] c] := set.ext $ λ x, mem_segment_translate 𝕜 a lemma open_segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' open_segment 𝕜 (a + b) (a + c) = open_segment 𝕜 b c := set.ext $ λ x, mem_open_segment_translate 𝕜 a lemma segment_translate_image (a b c : E) : (λ x, a + x) '' [b -[𝕜] c] = [a + b -[𝕜] a + c] := segment_translate_preimage 𝕜 a b c ▸ image_preimage_eq _ $ add_left_surjective a lemma open_segment_translate_image (a b c : E) : (λ x, a + x) '' open_segment 𝕜 b c = open_segment 𝕜 (a + b) (a + c) := open_segment_translate_preimage 𝕜 a b c ▸ image_preimage_eq _ $ add_left_surjective a end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] @[simp] lemma left_mem_open_segment_iff [no_zero_smul_divisors 𝕜 E] {x y : E} : x ∈ open_segment 𝕜 x y ↔ x = y := begin split, { rintro ⟨a, b, ha, hb, hab, hx⟩, refine smul_right_injective _ hb.ne' ((add_right_inj (a • x)).1 _), rw [hx, ←add_smul, hab, one_smul] }, { rintro rfl, rw open_segment_same, exact mem_singleton _ } end @[simp] lemma right_mem_open_segment_iff {x y : E} : y ∈ open_segment 𝕜 x y ↔ x = y := by rw [open_segment_symm, left_mem_open_segment_iff, eq_comm] end add_comm_group end linear_ordered_field /-! #### Segments in an ordered space Relates `segment`, `open_segment` and `set.Icc`, `set.Ico`, `set.Ioc`, `set.Ioo` -/ section ordered_semiring variables [ordered_semiring 𝕜] section ordered_add_comm_monoid variables [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] lemma segment_subset_Icc {x y : E} (h : x ≤ y) : [x -[𝕜] y] ⊆ Icc x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a • x + b • x :(convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg h hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg h ha) _ ... = y : convex.combo_self hab _, end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] lemma open_segment_subset_Ioo {x y : E} (h : x < y) : open_segment 𝕜 x y ⊆ Ioo x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a • x + b • x : (convex.combo_self hab _).symm ... < a • x + b • y : add_lt_add_left (smul_lt_smul_of_pos h hb) _, calc a • x + b • y < a • y + b • y : add_lt_add_right (smul_lt_smul_of_pos h ha) _ ... = y : convex.combo_self hab _, end end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {𝕜} lemma segment_subset_interval (x y : E) : [x -[𝕜] y] ⊆ interval x y := begin cases le_total x y, { rw interval_of_le h, exact segment_subset_Icc h }, { rw [interval_of_ge h, segment_symm], exact segment_subset_Icc h } end lemma convex.min_le_combo (x y : E) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min x y ≤ a • x + b • y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).1 lemma convex.combo_le_max (x y : E) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : a • x + b • y ≤ max x y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).2 end linear_ordered_add_comm_monoid end ordered_semiring section linear_ordered_field variables [linear_ordered_field 𝕜] lemma Icc_subset_segment {x y : 𝕜} : Icc x y ⊆ [x -[𝕜] y] := begin rintro z ⟨hxz, hyz⟩, obtain rfl | h := (hxz.trans hyz).eq_or_lt, { rw segment_same, exact hyz.antisymm hxz }, rw ←sub_nonneg at hxz hyz, rw ←sub_pos at h, refine ⟨(y - z) / (y - x), (z - x) / (y - x), div_nonneg hyz h.le, div_nonneg hxz h.le, _, _⟩, { rw [←add_div, sub_add_sub_cancel, div_self h.ne'] }, { rw [smul_eq_mul, smul_eq_mul, ←mul_div_right_comm, ←mul_div_right_comm, ←add_div, div_eq_iff h.ne', add_comm, sub_mul, sub_mul, mul_comm x, sub_add_sub_cancel, mul_sub] } end @[simp] lemma segment_eq_Icc {x y : 𝕜} (h : x ≤ y) : [x -[𝕜] y] = Icc x y := (segment_subset_Icc h).antisymm Icc_subset_segment lemma Ioo_subset_open_segment {x y : 𝕜} : Ioo x y ⊆ open_segment 𝕜 x y := λ z hz, mem_open_segment_of_ne_left_right _ hz.1.ne hz.2.ne' (Icc_subset_segment $ Ioo_subset_Icc_self hz) @[simp] lemma open_segment_eq_Ioo {x y : 𝕜} (h : x < y) : open_segment 𝕜 x y = Ioo x y := (open_segment_subset_Ioo h).antisymm Ioo_subset_open_segment lemma segment_eq_Icc' (x y : 𝕜) : [x -[𝕜] y] = Icc (min x y) (max x y) := begin cases le_total x y, { rw [segment_eq_Icc h, max_eq_right h, min_eq_left h] }, { rw [segment_symm, segment_eq_Icc h, max_eq_left h, min_eq_right h] } end lemma open_segment_eq_Ioo' {x y : 𝕜} (hxy : x ≠ y) : open_segment 𝕜 x y = Ioo (min x y) (max x y) := begin cases hxy.lt_or_lt, { rw [open_segment_eq_Ioo h, max_eq_right h.le, min_eq_left h.le] }, { rw [open_segment_symm, open_segment_eq_Ioo h, max_eq_left h.le, min_eq_right h.le] } end lemma segment_eq_interval (x y : 𝕜) : [x -[𝕜] y] = interval x y := segment_eq_Icc' _ _ /-- A point is in an `Icc` iff it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc {x y : 𝕜} (h : x ≤ y) {z : 𝕜} : z ∈ Icc x y ↔ ∃ (a b : 𝕜), 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ a * x + b * y = z := begin rw ←segment_eq_Icc h, simp_rw [←exists_prop], refl, end /-- A point is in an `Ioo` iff it can be expressed as a strict convex combination of the endpoints. -/ lemma convex.mem_Ioo {x y : 𝕜} (h : x < y) {z : 𝕜} : z ∈ Ioo x y ↔ ∃ (a b : 𝕜), 0 < a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin rw ←open_segment_eq_Ioo h, simp_rw [←exists_prop], refl, end /-- A point is in an `Ioc` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ioc {x y : 𝕜} (h : x < y) {z : 𝕜} : z ∈ Ioc x y ↔ ∃ (a b : 𝕜), 0 ≤ a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin split, { rintro hz, obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ioc_subset_Icc_self hz), obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rw [hab, one_mul, zero_mul, add_zero] at hz, exact (hz.1.ne rfl).elim }, { exact ⟨a, b, ha, hb', hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_mul, zero_mul, zero_add, right_mem_Ioc] }, { exact Ioo_subset_Ioc_self ((convex.mem_Ioo h).2 ⟨a, b, ha', hb, hab, rfl⟩) } } end /-- A point is in an `Ico` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ico {x y : 𝕜} (h : x < y) {z : 𝕜} : z ∈ Ico x y ↔ ∃ (a b : 𝕜), 0 < a ∧ 0 ≤ b ∧ a + b = 1 ∧ a * x + b * y = z := begin split, { rintro hz, obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ico_subset_Icc_self hz), obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rw [hab, one_mul, zero_mul, zero_add] at hz, exact (hz.2.ne rfl).elim }, { exact ⟨a, b, ha', hb, hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_mul, zero_mul, add_zero, left_mem_Ico] }, { exact Ioo_subset_Ico_self ((convex.mem_Ioo h).2 ⟨a, b, ha, hb', hab, rfl⟩) } } end end linear_ordered_field /-! ### Convexity of sets -/ section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_scalar variables (𝕜) [has_scalar 𝕜 E] [has_scalar 𝕜 F] (s : set E) /-- Convexity of sets. -/ def convex : Prop := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s variables {𝕜 s} lemma convex_iff_segment_subset : convex 𝕜 s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x -[𝕜] y] ⊆ s := begin split, { rintro h x y hx hy z ⟨a, b, ha, hb, hab, rfl⟩, exact h hx hy ha hb hab }, { rintro h x y hx hy a b ha hb hab, exact h hx hy ⟨a, b, ha, hb, hab, rfl⟩ } end lemma convex.segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : [x -[𝕜] y] ⊆ s := convex_iff_segment_subset.1 h hx hy lemma convex.open_segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s := (open_segment_subset_segment 𝕜 x y).trans (h.segment_subset hx hy) /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset : convex 𝕜 s ↔ ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintro hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) lemma convex_empty : convex 𝕜 (∅ : set E) := by finish lemma convex_univ : convex 𝕜 (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex 𝕜 s) : convex 𝕜 (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s : ι → set E} (h : ∀ i : ι, convex 𝕜 (s i)) : convex 𝕜 (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma directed.convex_Union {ι : Sort*} {s : ι → set E} (hdir : directed (⊆) s) (hc : ∀ ⦃i : ι⦄, convex 𝕜 (s i)) : convex 𝕜 (⋃ i, s i) := begin rintro x y hx hy a b ha hb hab, rw mem_Union at ⊢ hx hy, obtain ⟨i, hx⟩ := hx, obtain ⟨j, hy⟩ := hy, obtain ⟨k, hik, hjk⟩ := hdir i j, exact ⟨k, hc (hik hx) (hjk hy) ha hb hab⟩, end lemma directed_on.convex_sUnion {c : set (set E)} (hdir : directed_on (⊆) c) (hc : ∀ ⦃A : set E⦄, A ∈ c → convex 𝕜 A) : convex 𝕜 (⋃₀c) := begin rw sUnion_eq_Union, exact (directed_on_iff_directed.1 hdir).convex_Union (λ A, hc A.2), end end has_scalar section module variables [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex_iff_forall_pos : convex 𝕜 s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy ha.le hb.le hab, _⟩, intros h x y hx hy a b ha hb hab, cases ha.eq_or_lt with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases hb.eq_or_lt with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_pairwise_pos : convex 𝕜 s ↔ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s) := begin refine ⟨λ h x hx y hy _ a b ha hb hab, h hx hy ha.le hb.le hab, _⟩, intros h x y hx hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, rwa [hab, zero_smul, one_smul, zero_add] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, rwa [hab, zero_smul, one_smul, add_zero] }, obtain rfl | hxy := eq_or_ne x y, { rwa convex.combo_self hab }, exact h _ hx _ hy hxy ha' hb' hab, end lemma convex_iff_open_segment_subset : convex 𝕜 s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → open_segment 𝕜 x y ⊆ s := begin rw convex_iff_segment_subset, exact forall₂_congr (λ x y, forall₂_congr $ λ hx hy, (open_segment_subset_iff_segment_subset hx hy).symm), end lemma convex_singleton (c : E) : convex 𝕜 ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex.linear_image (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) : convex 𝕜 (s.image f) := begin intros x y hx hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, exact ⟨a • x' + b • y', hs hx' hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end lemma convex.is_linear_image (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) : convex 𝕜 (f '' s) := hs.linear_image $ hf.mk' f lemma convex.linear_preimage {s : set F} (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) : convex 𝕜 (s.preimage f) := begin intros x y hx hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hx hy ha hb hab, end lemma convex.is_linear_preimage {s : set F} (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) : convex 𝕜 (preimage f s) := hs.linear_preimage $ hf.mk' f lemma convex.add {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s + t) := by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.translate (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) '' s) := begin intros x y hx hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, refine ⟨a • x' + b • y', hs hx' hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) ⁻¹' s) := begin intros x y hx hy a b ha hb hab, have h := hs hx hy ha hb hab, rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h, end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, x + z) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right z section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_Iic (r : β) : convex 𝕜 (Iic r) := λ x y hx hy a b ha hb hab, calc a • x + b • y ≤ a • r + b • r : add_le_add (smul_le_smul_of_nonneg hx ha) (smul_le_smul_of_nonneg hy hb) ... = r : convex.combo_self hab _ lemma convex_Ici (r : β) : convex 𝕜 (Ici r) := @convex_Iic 𝕜 (order_dual β) _ _ _ _ r lemma convex_Icc (r s : β) : convex 𝕜 (Icc r s) := Ici_inter_Iic.subst ((convex_Ici r).inter $ convex_Iic s) lemma convex_halfspace_le {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_ge {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | f w = r} := begin simp_rw le_antisymm_iff, exact (convex_halfspace_le h r).inter (convex_halfspace_ge h r), end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_Iio (r : β) : convex 𝕜 (Iio r) := begin intros x y hx hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [zero_smul, zero_add, hab, one_smul] }, rw mem_Iio at hx hy, calc a • x + b • y < a • r + b • r : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx ha') (smul_le_smul_of_nonneg hy.le hb) ... = r : convex.combo_self hab _ end lemma convex_Ioi (r : β) : convex 𝕜 (Ioi r) := @convex_Iio 𝕜 (order_dual β) _ _ _ _ r lemma convex_Ioo (r s : β) : convex 𝕜 (Ioo r s) := Ioi_inter_Iio.subst ((convex_Ioi r).inter $ convex_Iio s) lemma convex_Ico (r s : β) : convex 𝕜 (Ico r s) := Ici_inter_Iio.subst ((convex_Ici r).inter $ convex_Iio s) lemma convex_Ioc (r s : β) : convex 𝕜 (Ioc r s) := Ioi_inter_Iic.subst ((convex_Ioi r).inter $ convex_Iic s) lemma convex_halfspace_lt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_gt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | r < f w} := (convex_Ioi r).is_linear_preimage h end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_interval (r s : β) : convex 𝕜 (interval r s) := convex_Icc _ _ end linear_ordered_add_comm_monoid end module end add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} {f : E → β} lemma monotone_on.convex_le (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := λ x y hx hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_lt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x < r} := λ x y hx hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans_lt (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_ge (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := @monotone_on.convex_le 𝕜 (order_dual E) (order_dual β) _ _ _ _ _ _ _ hf.dual hs r lemma monotone_on.convex_gt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r < f x} := @monotone_on.convex_lt 𝕜 (order_dual E) (order_dual β) _ _ _ _ _ _ _ hf.dual hs r lemma antitone_on.convex_le (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := @monotone_on.convex_ge 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_lt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x < r} := @monotone_on.convex_gt 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_ge (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := @monotone_on.convex_le 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_gt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r < f x} := @monotone_on.convex_lt 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma monotone.convex_le (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_lt (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_ge (hf : monotone f ) (r : β) : convex 𝕜 {x | r ≤ f x} := set.sep_univ.subst ((hf.monotone_on univ).convex_ge convex_univ r) lemma monotone.convex_gt (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma antitone.convex_le (hf : antitone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.antitone_on univ).convex_le convex_univ r) lemma antitone.convex_lt (hf : antitone f) (r : β) : convex 𝕜 {x | f x < r} := set.sep_univ.subst ((hf.antitone_on univ).convex_lt convex_univ r) lemma antitone.convex_ge (hf : antitone f) (r : β) : convex 𝕜 {x | r ≤ f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_ge convex_univ r) lemma antitone.convex_gt (hf : antitone f) (r : β) : convex 𝕜 {x | r < f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_gt convex_univ r) end linear_ordered_add_comm_monoid section add_comm_group variables [add_comm_group E] [module 𝕜 E] {s t : set E} lemma convex.combo_eq_vadd {a b : 𝕜} {x y : E} (h : a + b = 1) : a • x + b • y = b • (y - x) + x := calc a • x + b • y = (b • y - b • x) + (a • x + b • x) : by abel ... = b • (y - x) + x : by rw [smul_sub, convex.combo_self h] lemma convex.sub (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 ((λ x : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex_segment (x y : E) : convex 𝕜 [x -[𝕜] y] := begin rintro p q ⟨ap, bp, hap, hbp, habp, rfl⟩ ⟨aq, bq, haq, hbq, habq, rfl⟩ a b ha hb hab, refine ⟨a * ap + b * aq, a * bp + b * bq, add_nonneg (mul_nonneg ha hap) (mul_nonneg hb haq), add_nonneg (mul_nonneg ha hbp) (mul_nonneg hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end lemma convex_open_segment (a b : E) : convex 𝕜 (open_segment 𝕜 a b) := begin rw convex_iff_open_segment_subset, rintro p q ⟨ap, bp, hap, hbp, habp, rfl⟩ ⟨aq, bq, haq, hbq, habq, rfl⟩ z ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨a * ap + b * aq, a * bp + b * bq, add_pos (mul_pos ha hap) (mul_pos hb haq), add_pos (mul_pos ha hbp) (mul_pos hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end end add_comm_group end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex.smul (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 (c • s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.affinity (hs : convex 𝕜 s) (z : E) (c : 𝕜) : convex 𝕜 ((λ x, z + c • x) '' s) := begin have h := (hs.smul c).translate z, rwa [←image_smul, image_image] at h, end end add_comm_monoid end ordered_comm_semiring section ordered_ring variables [ordered_ring 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex.add_smul_mem (hs : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : x + y ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • y ∈ s := begin have h : x + t • y = (1 - t) • x + t • (x + y), { rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] }, rw h, exact hs hx hy (sub_nonneg_of_le ht.2) ht.1 (sub_add_cancel _ _), end lemma convex.smul_mem_of_zero_mem (hs : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : t • x ∈ s := by simpa using hs.add_smul_mem zero_mem (by simpa using hx) ht lemma convex.add_smul_sub_mem (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • (y - x) ∈ s := begin apply h.segment_subset hx hy, rw segment_eq_image', exact mem_image_of_mem _ ht, end /-- Affine subspaces are convex. -/ lemma affine_subspace.convex (Q : affine_subspace 𝕜 E) : convex 𝕜 (Q : set E) := begin intros x y hx hy a b ha hb hab, rw [eq_sub_of_add_eq hab, ← affine_map.line_map_apply_module], exact affine_map.line_map_mem b hx hy, end /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ lemma convex.combo_affine_apply {a b : 𝕜} {x y : E} {f : E →ᵃ[𝕜] F} (h : a + b = 1) : f (a • x + b • y) = a • f x + b • f y := begin simp only [convex.combo_eq_vadd h, ← vsub_eq_sub], exact f.apply_line_map _ _ _, end /-- The preimage of a convex set under an affine map is convex. -/ lemma convex.affine_preimage (f : E →ᵃ[𝕜] F) {s : set F} (hs : convex 𝕜 s) : convex 𝕜 (f ⁻¹' s) := begin intros x y xs ys a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs xs ys ha hb hab, end /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᵃ[𝕜] F) {s : set E} (hs : convex 𝕜 s) : convex 𝕜 (f '' s) := begin rintro x y ⟨x', ⟨hx', hx'f⟩⟩ ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x' + b • y', ⟨hs hx' hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hx'f, hy'f] end lemma convex.neg (hs : convex 𝕜 s) : convex 𝕜 ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex 𝕜 s) : convex 𝕜 ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s : set E} /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div : convex 𝕜 s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨λ h x y hx hy a b ha hb hab, begin apply h hx hy, { have ha', from mul_le_mul_of_nonneg_left ha (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at ha' }, { have hb', from mul_le_mul_of_nonneg_left hb (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at hb' }, { rw ←add_div, exact div_self hab.ne' } end, λ h x y hx hy a b ha hb hab, begin have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma convex.mem_smul_of_zero_mem (h : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : 𝕜} (ht : 1 ≤ t) : x ∈ t • s := begin rw mem_smul_set_iff_inv_smul_mem₀ (zero_lt_one.trans_le ht).ne', exact h.smul_mem_of_zero_mem zero_mem hx ⟨inv_nonneg.2 (zero_le_one.trans ht), inv_le_one ht⟩, end lemma convex.add_smul (h_conv : convex 𝕜 s) {p q : 𝕜} (hp : 0 ≤ p) (hq : 0 ≤ q) : (p + q) • s = p • s + q • s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { simp_rw [smul_set_empty, add_empty] }, obtain rfl | hp' := hp.eq_or_lt, { rw [zero_add, zero_smul_set hs, zero_add] }, obtain rfl | hq' := hq.eq_or_lt, { rw [add_zero, zero_smul_set hs, add_zero] }, ext, split, { rintro ⟨v, hv, rfl⟩, exact ⟨p • v, q • v, smul_mem_smul_set hv, smul_mem_smul_set hv, (add_smul _ _ _).symm⟩ }, { rintro ⟨v₁, v₂, ⟨v₁₁, h₁₂, rfl⟩, ⟨v₂₁, h₂₂, rfl⟩, rfl⟩, have hpq := add_pos hp' hq', exact mem_smul_set.2 ⟨_, h_conv h₁₂ h₂₂ (div_pos hp' hpq).le (div_pos hq' hpq).le (by rw [←div_self hpq.ne', add_div] : p / (p + q) + q / (p + q) = 1), by simp only [← mul_smul, smul_add, mul_div_cancel' _ hpq.ne']⟩ } end end add_comm_group end linear_ordered_field /-! #### Convex sets in an ordered space Relates `convex` and `ord_connected`. -/ section lemma set.ord_connected.convex_of_chain [ordered_semiring 𝕜] [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) (h : zorn.chain (≤) s) : convex 𝕜 s := begin intros x y hx hy a b ha hb hab, obtain hxy | hyx := h.total_of_refl hx hy, { refine hs.out hx hy (mem_Icc.2 ⟨_, _⟩), calc x = a • x + b • x : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg hxy hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg hxy ha) _ ... = y : convex.combo_self hab _ }, { refine hs.out hy hx (mem_Icc.2 ⟨_, _⟩), calc y = a • y + b • y : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_right (smul_le_smul_of_nonneg hyx ha) _, calc a • x + b • y ≤ a • x + b • x : add_le_add_left (smul_le_smul_of_nonneg hyx hb) _ ... = x : convex.combo_self hab _ } end lemma set.ord_connected.convex [ordered_semiring 𝕜] [linear_ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) : convex 𝕜 s := hs.convex_of_chain (zorn.chain_of_trichotomous s) lemma convex_iff_ord_connected [linear_ordered_field 𝕜] {s : set 𝕜} : convex 𝕜 s ↔ s.ord_connected := begin simp_rw [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset], exact forall_congr (λ x, forall_swap) end alias convex_iff_ord_connected ↔ convex.ord_connected _ end /-! #### Convexity of submodules/subspaces -/ section submodule open submodule lemma submodule.convex [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] (K : submodule 𝕜 E) : convex 𝕜 (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex [linear_ordered_field 𝕜] [add_comm_group E] [module 𝕜 E] (K : subspace 𝕜 E) : convex 𝕜 (↑K : set E) := K.convex end submodule /-! ### Simplex -/ section simplex variables (𝕜) (ι : Type*) [ordered_semiring 𝕜] [fintype ι] /-- The standard simplex in the space of functions `ι → 𝕜` is the set of vectors with non-negative coordinates with total sum `1`. This is the free object in the category of convex spaces. -/ def std_simplex : set (ι → 𝕜) := {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} lemma std_simplex_eq_inter : std_simplex 𝕜 ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex 𝕜 (std_simplex 𝕜 ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:𝕜) 0) ∈ std_simplex 𝕜 ι := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩ end simplex
b2fe84e7b3cb54b25dac2238a5151188d9d36dd7
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/field_theory/is_alg_closed/basic.lean
11296fead530e1f6a642c1b11028483b7461b2e0
[ "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
18,263
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import field_theory.splitting_field /-! # Algebraically Closed Field In this file we define the typeclass for algebraically closed fields and algebraic closures, and prove some of their properties. ## Main Definitions - `is_alg_closed k` is the typeclass saying `k` is an algebraically closed field, i.e. every polynomial in `k` splits. - `is_alg_closure R K` is the typeclass saying `K` is an algebraic closure of `R`, where `R` is a commutative ring. This means that the map from `R` to `K` is injective, and `K` is algebraically closed and algebraic over `R` - `is_alg_closed.lift` is a map from an algebraic extension `L` of `R`, into any algebraically closed extension of `R`. - `is_alg_closure.equiv` is a proof that any two algebraic closures of the same field are isomorphic. ## Tags algebraic closure, algebraically closed -/ universes u v w open_locale classical big_operators open polynomial variables (k : Type u) [field k] /-- Typeclass for algebraically closed fields. To show `polynomial.splits p f` for an arbitrary ring homomorphism `f`, see `is_alg_closed.splits_codomain` and `is_alg_closed.splits_domain`. -/ class is_alg_closed : Prop := (splits : ∀ p : polynomial k, p.splits $ ring_hom.id k) /-- Every polynomial splits in the field extension `f : K →+* k` if `k` is algebraically closed. See also `is_alg_closed.splits_domain` for the case where `K` is algebraically closed. -/ theorem is_alg_closed.splits_codomain {k K : Type*} [field k] [is_alg_closed k] [field K] {f : K →+* k} (p : polynomial K) : p.splits f := by { convert is_alg_closed.splits (p.map f), simp [splits_map_iff] } /-- Every polynomial splits in the field extension `f : K →+* k` if `K` is algebraically closed. See also `is_alg_closed.splits_codomain` for the case where `k` is algebraically closed. -/ theorem is_alg_closed.splits_domain {k K : Type*} [field k] [is_alg_closed k] [field K] {f : k →+* K} (p : polynomial k) : p.splits f := polynomial.splits_of_splits_id _ $ is_alg_closed.splits _ namespace is_alg_closed variables {k} theorem exists_root [is_alg_closed k] (p : polynomial k) (hp : p.degree ≠ 0) : ∃ x, is_root p x := exists_root_of_splits _ (is_alg_closed.splits p) hp lemma exists_pow_nat_eq [is_alg_closed k] (x : k) {n : ℕ} (hn : 0 < n) : ∃ z, z ^ n = x := begin rcases exists_root (X ^ n - C x) _ with ⟨z, hz⟩, swap, { rw degree_X_pow_sub_C hn x, exact ne_of_gt (with_bot.coe_lt_coe.2 hn) }, use z, simp only [eval_C, eval_X, eval_pow, eval_sub, is_root.def] at hz, exact sub_eq_zero.1 hz end lemma exists_eq_mul_self [is_alg_closed k] (x : k) : ∃ z, x = z * z := begin rcases exists_pow_nat_eq x zero_lt_two with ⟨z, rfl⟩, exact ⟨z, sq z⟩ end lemma roots_eq_zero_iff [is_alg_closed k] {p : polynomial k} : p.roots = 0 ↔ p = polynomial.C (p.coeff 0) := begin refine ⟨λ h, _, λ hp, by rw [hp, roots_C]⟩, cases (le_or_lt (degree p) 0) with hd hd, { exact eq_C_of_degree_le_zero hd }, { obtain ⟨z, hz⟩ := is_alg_closed.exists_root p hd.ne', rw [←mem_roots (ne_zero_of_degree_gt hd), h] at hz, simpa using hz } end theorem exists_eval₂_eq_zero_of_injective {R : Type*} [ring R] [is_alg_closed k] (f : R →+* k) (hf : function.injective f) (p : polynomial R) (hp : p.degree ≠ 0) : ∃ x, p.eval₂ f x = 0 := let ⟨x, hx⟩ := exists_root (p.map f) (by rwa [degree_map_eq_of_injective hf]) in ⟨x, by rwa [eval₂_eq_eval_map, ← is_root]⟩ theorem exists_eval₂_eq_zero {R : Type*} [field R] [is_alg_closed k] (f : R →+* k) (p : polynomial R) (hp : p.degree ≠ 0) : ∃ x, p.eval₂ f x = 0 := exists_eval₂_eq_zero_of_injective f f.injective p hp variables (k) theorem exists_aeval_eq_zero_of_injective {R : Type*} [comm_ring R] [is_alg_closed k] [algebra R k] (hinj : function.injective (algebra_map R k)) (p : polynomial R) (hp : p.degree ≠ 0) : ∃ x : k, aeval x p = 0 := exists_eval₂_eq_zero_of_injective (algebra_map R k) hinj p hp theorem exists_aeval_eq_zero {R : Type*} [field R] [is_alg_closed k] [algebra R k] (p : polynomial R) (hp : p.degree ≠ 0) : ∃ x : k, aeval x p = 0 := exists_eval₂_eq_zero (algebra_map R k) p hp theorem of_exists_root (H : ∀ p : polynomial k, p.monic → irreducible p → ∃ x, p.eval x = 0) : is_alg_closed k := ⟨λ p, or.inr $ λ q hq hqp, have irreducible (q * C (leading_coeff q)⁻¹), by { rw ← coe_norm_unit_of_ne_zero hq.ne_zero, exact (associated_normalize _).irreducible hq }, let ⟨x, hx⟩ := H (q * C (leading_coeff q)⁻¹) (monic_mul_leading_coeff_inv hq.ne_zero) this in degree_mul_leading_coeff_inv q hq.ne_zero ▸ degree_eq_one_of_irreducible_of_root this hx⟩ lemma degree_eq_one_of_irreducible [is_alg_closed k] {p : polynomial k} (hp : irreducible p) : p.degree = 1 := degree_eq_one_of_irreducible_of_splits hp (is_alg_closed.splits_codomain _) lemma algebra_map_surjective_of_is_integral {k K : Type*} [field k] [ring K] [is_domain K] [hk : is_alg_closed k] [algebra k K] (hf : algebra.is_integral k K) : function.surjective (algebra_map k K) := begin refine λ x, ⟨-((minpoly k x).coeff 0), _⟩, have hq : (minpoly k x).leading_coeff = 1 := minpoly.monic (hf x), have h : (minpoly k x).degree = 1 := degree_eq_one_of_irreducible k (minpoly.irreducible (hf x)), have : (aeval x (minpoly k x)) = 0 := minpoly.aeval k x, rw [eq_X_add_C_of_degree_eq_one h, hq, C_1, one_mul, aeval_add, aeval_X, aeval_C, add_eq_zero_iff_eq_neg] at this, exact (ring_hom.map_neg (algebra_map k K) ((minpoly k x).coeff 0)).symm ▸ this.symm, end lemma algebra_map_surjective_of_is_integral' {k K : Type*} [field k] [comm_ring K] [is_domain K] [hk : is_alg_closed k] (f : k →+* K) (hf : f.is_integral) : function.surjective f := @algebra_map_surjective_of_is_integral k K _ _ _ _ f.to_algebra hf lemma algebra_map_surjective_of_is_algebraic {k K : Type*} [field k] [ring K] [is_domain K] [hk : is_alg_closed k] [algebra k K] (hf : algebra.is_algebraic k K) : function.surjective (algebra_map k K) := algebra_map_surjective_of_is_integral (algebra.is_algebraic_iff_is_integral.mp hf) end is_alg_closed /-- Typeclass for an extension being an algebraic closure. -/ class is_alg_closure (R : Type u) (K : Type v) [comm_ring R] [field K] [algebra R K] [no_zero_smul_divisors R K] : Prop := (alg_closed : is_alg_closed K) (algebraic : algebra.is_algebraic R K) theorem is_alg_closure_iff (K : Type v) [field K] [algebra k K] : is_alg_closure k K ↔ is_alg_closed K ∧ algebra.is_algebraic k K := ⟨λ h, ⟨h.1, h.2⟩, λ h, ⟨h.1, h.2⟩⟩ namespace lift /- In this section, the homomorphism from any algebraic extension into an algebraically closed extension is proven to exist. The assumption that M is algebraically closed could probably easily be switched to an assumption that M contains all the roots of polynomials in K -/ variables {K : Type u} {L : Type v} {M : Type w} [field K] [field L] [algebra K L] [field M] [algebra K M] [is_alg_closed M] (hL : algebra.is_algebraic K L) variables (K L M) include hL open zorn subalgebra alg_hom function /-- This structure is used to prove the existence of a homomorphism from any algebraic extension into an algebraic closure -/ structure subfield_with_hom := (carrier : subalgebra K L) (emb : carrier →ₐ[K] M) variables {K L M hL} namespace subfield_with_hom variables {E₁ E₂ E₃ : subfield_with_hom K L M hL} instance : has_le (subfield_with_hom K L M hL) := { le := λ E₁ E₂, ∃ h : E₁.carrier ≤ E₂.carrier, ∀ x, E₂.emb (inclusion h x) = E₁.emb x } noncomputable instance : inhabited (subfield_with_hom K L M hL) := ⟨{ carrier := ⊥, emb := (algebra.of_id K M).comp (algebra.bot_equiv K L).to_alg_hom }⟩ lemma le_def : E₁ ≤ E₂ ↔ ∃ h : E₁.carrier ≤ E₂.carrier, ∀ x, E₂.emb (inclusion h x) = E₁.emb x := iff.rfl lemma compat (h : E₁ ≤ E₂) : ∀ x, E₂.emb (inclusion h.fst x) = E₁.emb x := by { rw le_def at h, cases h, assumption } instance : preorder (subfield_with_hom K L M hL) := { le := (≤), le_refl := λ E, ⟨le_rfl, by simp⟩, le_trans := λ E₁ E₂ E₃ h₁₂ h₂₃, ⟨le_trans h₁₂.fst h₂₃.fst, λ _, by erw [← inclusion_inclusion h₁₂.fst h₂₃.fst, compat, compat]⟩ } open lattice lemma maximal_subfield_with_hom_chain_bounded (c : set (subfield_with_hom K L M hL)) (hc : chain (≤) c) : ∃ ub : subfield_with_hom K L M hL, ∀ N, N ∈ c → N ≤ ub := if hcn : c.nonempty then let ub : subfield_with_hom K L M hL := by haveI : nonempty c := set.nonempty.to_subtype hcn; exact { carrier := ⨆ i : c, (i : subfield_with_hom K L M hL).carrier, emb := subalgebra.supr_lift (λ i : c, (i : subfield_with_hom K L M hL).carrier) (λ i j, let ⟨k, hik, hjk⟩ := directed_on_iff_directed.1 hc.directed_on i j in ⟨k, hik.fst, hjk.fst⟩) (λ i, (i : subfield_with_hom K L M hL).emb) begin assume i j h, ext x, cases hc.total i.prop j.prop with hij hji, { simp [← hij.snd x] }, { erw [alg_hom.comp_apply, ← hji.snd (inclusion h x), inclusion_inclusion, inclusion_self, alg_hom.id_apply x] } end _ rfl } in ⟨ub, λ N hN, ⟨(le_supr (λ i : c, (i : subfield_with_hom K L M hL).carrier) ⟨N, hN⟩ : _), begin intro x, simp [ub], refl end⟩⟩ else by { rw [set.not_nonempty_iff_eq_empty] at hcn, simp [hcn], } variables (hL M) lemma exists_maximal_subfield_with_hom : ∃ E : subfield_with_hom K L M hL, ∀ N, E ≤ N → N ≤ E := zorn.exists_maximal_of_chains_bounded maximal_subfield_with_hom_chain_bounded (λ _ _ _, le_trans) /-- The maximal `subfield_with_hom`. We later prove that this is equal to `⊤`. -/ noncomputable def maximal_subfield_with_hom : subfield_with_hom K L M hL := classical.some (exists_maximal_subfield_with_hom M hL) lemma maximal_subfield_with_hom_is_maximal : ∀ (N : subfield_with_hom K L M hL), (maximal_subfield_with_hom M hL) ≤ N → N ≤ (maximal_subfield_with_hom M hL) := classical.some_spec (exists_maximal_subfield_with_hom M hL) lemma maximal_subfield_with_hom_eq_top : (maximal_subfield_with_hom M hL).carrier = ⊤ := begin rw [eq_top_iff], intros x _, let p := minpoly K x, let N : subalgebra K L := (maximal_subfield_with_hom M hL).carrier, letI : field N := is_field.to_field _ (subalgebra.is_field_of_algebraic N hL), letI : algebra N M := (maximal_subfield_with_hom M hL).emb.to_ring_hom.to_algebra, cases is_alg_closed.exists_aeval_eq_zero M (minpoly N x) (ne_of_gt (minpoly.degree_pos (is_algebraic_iff_is_integral.1 (algebra.is_algebraic_of_larger_base _ _ hL x)))) with y hy, let O : subalgebra N L := algebra.adjoin N {(x : L)}, let larger_emb := ((adjoin_root.lift_hom (minpoly N x) y hy).comp (alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly N x).to_alg_hom), have hNO : N ≤ O.restrict_scalars K, { intros z hz, show algebra_map N L ⟨z, hz⟩ ∈ O, exact O.algebra_map_mem _ }, let O' : subfield_with_hom K L M hL := { carrier := O.restrict_scalars K, emb := larger_emb.restrict_scalars K }, have hO' : maximal_subfield_with_hom M hL ≤ O', { refine ⟨hNO, _⟩, intros z, show O'.emb (algebra_map N O z) = algebra_map N M z, simp only [O', restrict_scalars_apply, alg_hom.commutes] }, refine (maximal_subfield_with_hom_is_maximal M hL O' hO').fst _, exact algebra.subset_adjoin (set.mem_singleton x), end end subfield_with_hom end lift namespace is_alg_closed variables {K : Type u} [field K] {L : Type v} {M : Type w} [field L] [algebra K L] [field M] [algebra K M] [is_alg_closed M] (hL : algebra.is_algebraic K L) variables (K L M) include hL /-- Less general version of `lift`. -/ @[irreducible] private noncomputable def lift_aux : L →ₐ[K] M := (lift.subfield_with_hom.maximal_subfield_with_hom M hL).emb.comp $ eq.rec_on (lift.subfield_with_hom.maximal_subfield_with_hom_eq_top M hL).symm algebra.to_top omit hL variables {R : Type u} [comm_ring R] variables {S : Type v} [comm_ring S] [is_domain S] [algebra R S] [algebra R M] [no_zero_smul_divisors R S] [no_zero_smul_divisors R M] (hS : algebra.is_algebraic R S) variables {M} include hS /-- A (random) homomorphism from an algebraic extension of R into an algebraically closed extension of R. -/ @[irreducible] noncomputable def lift : S →ₐ[R] M := begin letI : is_domain R := (no_zero_smul_divisors.algebra_map_injective R S).is_domain _, have hfRfS : algebra.is_algebraic (fraction_ring R) (fraction_ring S), from λ x, (is_fraction_ring.is_algebraic_iff R (fraction_ring R) (fraction_ring S)).1 ((is_fraction_ring.is_algebraic_iff' R S (fraction_ring S)).1 hS x), let f : fraction_ring S →ₐ[fraction_ring R] M := lift_aux (fraction_ring R) (fraction_ring S) M hfRfS, exact (f.restrict_scalars R).comp ((algebra.of_id S (fraction_ring S)).restrict_scalars R), end end is_alg_closed namespace is_alg_closure variables (K : Type*) (J : Type*) (R : Type u) (S : Type*) [field K] [field J] [comm_ring R] (L : Type v) (M : Type w) [field L] [field M] [algebra R M] [no_zero_smul_divisors R M] [is_alg_closure R M] [algebra K M] [is_alg_closure K M] [comm_ring S] [algebra S L] [no_zero_smul_divisors S L] [is_alg_closure S L] local attribute [instance] is_alg_closure.alg_closed section variables [algebra R L] [no_zero_smul_divisors R L] [is_alg_closure R L] /-- A (random) isomorphism between two algebraic closures of `R`. -/ noncomputable def equiv : L ≃ₐ[R] M := let f : L →ₐ[R] M := is_alg_closed.lift is_alg_closure.algebraic in alg_equiv.of_bijective f ⟨ring_hom.injective f.to_ring_hom, begin letI : algebra L M := ring_hom.to_algebra f, letI : is_scalar_tower R L M := is_scalar_tower.of_algebra_map_eq (by simp [ring_hom.algebra_map_to_algebra]), show function.surjective (algebra_map L M), exact is_alg_closed.algebra_map_surjective_of_is_algebraic (algebra.is_algebraic_of_larger_base_of_injective (no_zero_smul_divisors.algebra_map_injective R _) is_alg_closure.algebraic), end⟩ end section equiv_of_algebraic variables [algebra R S] [algebra R L] [is_scalar_tower R S L] variables [algebra K J] [algebra J L] [is_alg_closure J L] [algebra K L] [is_scalar_tower K J L] /-- A (random) isomorphism between an algebraic closure of `R` and an algebraic closure of an algebraic extension of `R` -/ noncomputable def equiv_of_algebraic' [nontrivial S] [no_zero_smul_divisors R S] (hRL : algebra.is_algebraic R L) : L ≃ₐ[R] M := begin letI : no_zero_smul_divisors R L := no_zero_smul_divisors.of_algebra_map_injective begin rw [is_scalar_tower.algebra_map_eq R S L], exact function.injective.comp (no_zero_smul_divisors.algebra_map_injective _ _) (no_zero_smul_divisors.algebra_map_injective _ _) end, letI : is_alg_closure R L := { alg_closed := by apply_instance, algebraic := hRL }, exact is_alg_closure.equiv _ _ _ end /-- A (random) isomorphism between an algebraic closure of `K` and an algebraic closure of an algebraic extension of `K` -/ noncomputable def equiv_of_algebraic (hKJ : algebra.is_algebraic K J) : L ≃ₐ[K] M := equiv_of_algebraic' K J _ _ (algebra.is_algebraic_trans hKJ is_alg_closure.algebraic) end equiv_of_algebraic section equiv_of_equiv variables {R S} /-- Used in the definition of `equiv_of_equiv` -/ noncomputable def equiv_of_equiv_aux (hSR : S ≃+* R) : { e : L ≃+* M // e.to_ring_hom.comp (algebra_map S L) = (algebra_map R M).comp hSR.to_ring_hom }:= begin letI : algebra R S := ring_hom.to_algebra hSR.symm.to_ring_hom, letI : algebra S R := ring_hom.to_algebra hSR.to_ring_hom, letI : is_domain R := (no_zero_smul_divisors.algebra_map_injective R M).is_domain _, letI : is_domain S := (no_zero_smul_divisors.algebra_map_injective S L).is_domain _, have : algebra.is_algebraic R S, from λ x, begin rw [← ring_equiv.symm_apply_apply hSR x], exact is_algebraic_algebra_map _ end, letI : algebra R L := ring_hom.to_algebra ((algebra_map S L).comp (algebra_map R S)), haveI : is_scalar_tower R S L := is_scalar_tower.of_algebra_map_eq (λ _, rfl), haveI : is_scalar_tower S R L := is_scalar_tower.of_algebra_map_eq (by simp [ring_hom.algebra_map_to_algebra]), haveI : no_zero_smul_divisors R S := no_zero_smul_divisors.of_algebra_map_injective hSR.symm.injective, refine ⟨equiv_of_algebraic' R S L M (algebra.is_algebraic_of_larger_base_of_injective (show function.injective (algebra_map S R), from hSR.injective) is_alg_closure.algebraic) , _⟩, ext, simp only [ring_equiv.to_ring_hom_eq_coe, function.comp_app, ring_hom.coe_comp, alg_equiv.coe_ring_equiv, ring_equiv.coe_to_ring_hom], conv_lhs { rw [← hSR.symm_apply_apply x] }, show equiv_of_algebraic' R S L M _ (algebra_map R L (hSR x)) = _, rw [alg_equiv.commutes] end /-- Algebraic closure of isomorphic fields are isomorphic -/ noncomputable def equiv_of_equiv (hSR : S ≃+* R) : L ≃+* M := equiv_of_equiv_aux L M hSR @[simp] lemma equiv_of_equiv_comp_algebra_map (hSR : S ≃+* R) : (↑(equiv_of_equiv L M hSR) : L →+* M).comp (algebra_map S L) = (algebra_map R M).comp hSR := (equiv_of_equiv_aux L M hSR).2 @[simp] lemma equiv_of_equiv_algebra_map (hSR : S ≃+* R) (s : S): equiv_of_equiv L M hSR (algebra_map S L s) = algebra_map R M (hSR s) := ring_hom.ext_iff.1 (equiv_of_equiv_comp_algebra_map L M hSR) s @[simp] lemma equiv_of_equiv_symm_algebra_map (hSR : S ≃+* R) (r : R): (equiv_of_equiv L M hSR).symm (algebra_map R M r) = algebra_map S L (hSR.symm r) := (equiv_of_equiv L M hSR).injective (by simp) @[simp] lemma equiv_of_equiv_symm_comp_algebra_map (hSR : S ≃+* R) : ((equiv_of_equiv L M hSR).symm : M →+* L).comp (algebra_map R M) = (algebra_map S L).comp hSR.symm := ring_hom.ext_iff.2 (equiv_of_equiv_symm_algebra_map L M hSR) end equiv_of_equiv end is_alg_closure
5d467a3015abd5486aea6765873372c99784cd61
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/algebraic_geometry/prime_spectrum.lean
d9dcbf14bf06d3b081875211f1f7d7d1d6e3e7a5
[ "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
13,575
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import topology.opens import ring_theory.ideal.operations import linear_algebra.finsupp import algebra.punit_instances /-! # Prime spectrum of a commutative ring The prime spectrum of a commutative ring is the type of all prime ideals. It is naturally endowed with a topology: the Zariski topology. (It is also naturally endowed with a sheaf of rings, which is constructed in `algebraic_geometry.structure_sheaf`.) ## Main definitions * `prime_spectrum R`: The prime spectrum of a commutative ring `R`, i.e., the set of all prime ideals of `R`. * `zero_locus s`: The zero locus of a subset `s` of `R` is the subset of `prime_spectrum R` consisting of all prime ideals that contain `s`. * `vanishing_ideal t`: The vanishing ideal of a subset `t` of `prime_spectrum R` is the intersection of points in `t` (viewed as prime ideals). ## Conventions We denote subsets of rings with `s`, `s'`, etc... whereas we denote subsets of prime spectra with `t`, `t'`, etc... ## Inspiration/contributors The contents of this file draw inspiration from <https://github.com/ramonfmir/lean-scheme> which has contributions from Ramon Fernandez Mir, Kevin Buzzard, Kenny Lau, and Chris Hughes (on an earlier repository). -/ noncomputable theory open_locale classical universe variables u v variables (R : Type u) [comm_ring R] /-- The prime spectrum of a commutative ring `R` is the type of all prime ideals of `R`. It is naturally endowed with a topology (the Zariski topology), and a sheaf of commutative rings (see `algebraic_geometry.structure_sheaf`). It is a fundamental building block in algebraic geometry. -/ @[nolint has_inhabited_instance] def prime_spectrum := {I : ideal R // I.is_prime} variable {R} namespace prime_spectrum /-- A method to view a point in the prime spectrum of a commutative ring as an ideal of that ring. -/ abbreviation as_ideal (x : prime_spectrum R) : ideal R := x.val instance is_prime (x : prime_spectrum R) : x.as_ideal.is_prime := x.2 /-- The prime spectrum of the zero ring is empty. -/ lemma punit (x : prime_spectrum punit) : false := x.1.ne_top_iff_one.1 x.2.1 $ subsingleton.elim (0 : punit) 1 ▸ x.1.zero_mem @[ext] lemma ext {x y : prime_spectrum R} : x = y ↔ x.as_ideal = y.as_ideal := subtype.ext_iff_val /-- The zero locus of a set `s` of elements of a commutative ring `R` is the set of all prime ideals of the ring that contain the set `s`. An element `f` of `R` can be thought of as a dependent function on the prime spectrum of `R`. At a point `x` (a prime ideal) the function (i.e., element) `f` takes values in the quotient ring `R` modulo the prime ideal `x`. In this manner, `zero_locus s` is exactly the subset of `prime_spectrum R` where all "functions" in `s` vanish simultaneously. -/ def zero_locus (s : set R) : set (prime_spectrum R) := {x | s ⊆ x.as_ideal} @[simp] lemma mem_zero_locus (x : prime_spectrum R) (s : set R) : x ∈ zero_locus s ↔ s ⊆ x.as_ideal := iff.rfl @[simp] lemma zero_locus_span (s : set R) : zero_locus (ideal.span s : set R) = zero_locus s := by { ext x, exact (submodule.gi R R).gc s x.as_ideal } /-- The vanishing ideal of a set `t` of points of the prime spectrum of a commutative ring `R` is the intersection of all the prime ideals in the set `t`. An element `f` of `R` can be thought of as a dependent function on the prime spectrum of `R`. At a point `x` (a prime ideal) the function (i.e., element) `f` takes values in the quotient ring `R` modulo the prime ideal `x`. In this manner, `vanishing_ideal t` is exactly the ideal of `R` consisting of all "functions" that vanish on all of `t`. -/ def vanishing_ideal (t : set (prime_spectrum R)) : ideal R := ⨅ (x : prime_spectrum R) (h : x ∈ t), x.as_ideal lemma coe_vanishing_ideal (t : set (prime_spectrum R)) : (vanishing_ideal t : set R) = {f : R | ∀ x : prime_spectrum R, x ∈ t → f ∈ x.as_ideal} := begin ext f, rw [vanishing_ideal, submodule.mem_coe, submodule.mem_infi], apply forall_congr, intro x, rw [submodule.mem_infi], end lemma mem_vanishing_ideal (t : set (prime_spectrum R)) (f : R) : f ∈ vanishing_ideal t ↔ ∀ x : prime_spectrum R, x ∈ t → f ∈ x.as_ideal := by rw [← submodule.mem_coe, coe_vanishing_ideal, set.mem_set_of_eq] lemma subset_zero_locus_iff_le_vanishing_ideal (t : set (prime_spectrum R)) (I : ideal R) : t ⊆ zero_locus I ↔ I ≤ vanishing_ideal t := begin split; intro h, { intros f hf, rw [mem_vanishing_ideal], intros x hx, have hxI := h hx, rw mem_zero_locus at hxI, exact hxI hf }, { intros x hx, rw mem_zero_locus, refine le_trans h _, intros f hf, rw [mem_vanishing_ideal] at hf, exact hf x hx } end section gc variable (R) /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc : @galois_connection (ideal R) (order_dual (set (prime_spectrum R))) _ _ (λ I, zero_locus I) (λ t, vanishing_ideal t) := λ I t, subset_zero_locus_iff_le_vanishing_ideal t I /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc_set : @galois_connection (set R) (order_dual (set (prime_spectrum R))) _ _ (λ s, zero_locus s) (λ t, vanishing_ideal t) := have ideal_gc : galois_connection (ideal.span) coe := (submodule.gi R R).gc, by simpa [zero_locus_span, function.comp] using galois_connection.compose _ _ _ _ ideal_gc (gc R) lemma subset_zero_locus_iff_subset_vanishing_ideal (t : set (prime_spectrum R)) (s : set R) : t ⊆ zero_locus s ↔ s ⊆ vanishing_ideal t := (gc_set R) s t end gc -- TODO: we actually get the radical ideal, -- but I think that isn't in mathlib yet. lemma subset_vanishing_ideal_zero_locus (s : set R) : s ⊆ vanishing_ideal (zero_locus s) := (gc_set R).le_u_l s lemma le_vanishing_ideal_zero_locus (I : ideal R) : I ≤ vanishing_ideal (zero_locus I) := (gc R).le_u_l I lemma subset_zero_locus_vanishing_ideal (t : set (prime_spectrum R)) : t ⊆ zero_locus (vanishing_ideal t) := (gc R).l_u_le t lemma zero_locus_bot : zero_locus ((⊥ : ideal R) : set R) = set.univ := (gc R).l_bot @[simp] lemma zero_locus_singleton_zero : zero_locus (0 : set R) = set.univ := zero_locus_bot @[simp] lemma zero_locus_empty : zero_locus (∅ : set R) = set.univ := (gc_set R).l_bot @[simp] lemma vanishing_ideal_univ : vanishing_ideal (∅ : set (prime_spectrum R)) = ⊤ := by simpa using (gc R).u_top lemma zero_locus_empty_of_one_mem {s : set R} (h : (1:R) ∈ s) : zero_locus s = ∅ := begin rw set.eq_empty_iff_forall_not_mem, intros x hx, rw mem_zero_locus at hx, have x_prime : x.as_ideal.is_prime := by apply_instance, have eq_top : x.as_ideal = ⊤, { rw ideal.eq_top_iff_one, exact hx h }, apply x_prime.1 eq_top, end lemma zero_locus_empty_iff_eq_top {I : ideal R} : zero_locus (I : set R) = ∅ ↔ I = ⊤ := begin split, { contrapose!, intro h, apply set.ne_empty_iff_nonempty.mpr, rcases ideal.exists_le_maximal I h with ⟨M, hM, hIM⟩, exact ⟨⟨M, hM.is_prime⟩, hIM⟩ }, { rintro rfl, apply zero_locus_empty_of_one_mem, trivial } end @[simp] lemma zero_locus_univ : zero_locus (set.univ : set R) = ∅ := zero_locus_empty_of_one_mem (set.mem_univ 1) lemma zero_locus_sup (I J : ideal R) : zero_locus ((I ⊔ J : ideal R) : set R) = zero_locus I ∩ zero_locus J := (gc R).l_sup lemma zero_locus_union (s s' : set R) : zero_locus (s ∪ s') = zero_locus s ∩ zero_locus s' := (gc_set R).l_sup lemma vanishing_ideal_union (t t' : set (prime_spectrum R)) : vanishing_ideal (t ∪ t') = vanishing_ideal t ⊓ vanishing_ideal t' := (gc R).u_inf lemma zero_locus_supr {ι : Sort*} (I : ι → ideal R) : zero_locus ((⨆ i, I i : ideal R) : set R) = (⋂ i, zero_locus (I i)) := (gc R).l_supr lemma zero_locus_Union {ι : Sort*} (s : ι → set R) : zero_locus (⋃ i, s i) = (⋂ i, zero_locus (s i)) := (gc_set R).l_supr lemma vanishing_ideal_Union {ι : Sort*} (t : ι → set (prime_spectrum R)) : vanishing_ideal (⋃ i, t i) = (⨅ i, vanishing_ideal (t i)) := (gc R).u_infi lemma zero_locus_inf (I J : ideal R) : zero_locus ((I ⊓ J : ideal R) : set R) = zero_locus I ∪ zero_locus J := begin ext x, split, { rintro h, rw set.mem_union, simp only [mem_zero_locus] at h ⊢, -- TODO: The rest of this proof should be factored out. rw or_iff_not_imp_right, intros hs r hr, rw set.not_subset at hs, rcases hs with ⟨s, hs1, hs2⟩, apply (ideal.is_prime.mem_or_mem (by apply_instance) _).resolve_left hs2, apply h, split, { exact ideal.mul_mem_left _ hr }, { exact ideal.mul_mem_right _ hs1 } }, { rintro (h|h), all_goals { rw mem_zero_locus at h ⊢, refine set.subset.trans _ h, intros r hr, cases hr, assumption } } end lemma union_zero_locus (s s' : set R) : zero_locus s ∪ zero_locus s' = zero_locus ((ideal.span s) ⊓ (ideal.span s') : ideal R) := by { rw zero_locus_inf, simp } lemma sup_vanishing_ideal_le (t t' : set (prime_spectrum R)) : vanishing_ideal t ⊔ vanishing_ideal t' ≤ vanishing_ideal (t ∩ t') := begin intros r, rw [submodule.mem_sup, mem_vanishing_ideal], rintro ⟨f, hf, g, hg, rfl⟩ x ⟨hxt, hxt'⟩, rw mem_vanishing_ideal at hf hg, apply submodule.add_mem; solve_by_elim end /-- The Zariski topology on the prime spectrum of a commutative ring is defined via the closed sets of the topology: they are exactly those sets that are the zero locus of a subset of the ring. -/ instance zariski_topology : topological_space (prime_spectrum R) := topological_space.of_closed (set.range prime_spectrum.zero_locus) (⟨set.univ, by simp⟩) begin intros Zs h, rw set.sInter_eq_Inter, let f : Zs → set R := λ i, classical.some (h i.2), have hf : ∀ i : Zs, ↑i = zero_locus (f i) := λ i, (classical.some_spec (h i.2)).symm, simp only [hf], exact ⟨_, zero_locus_Union _⟩ end (by { rintro _ _ ⟨s, rfl⟩ ⟨t, rfl⟩, exact ⟨_, (union_zero_locus s t).symm⟩ }) lemma is_open_iff (U : set (prime_spectrum R)) : is_open U ↔ ∃ s, Uᶜ = zero_locus s := by simp only [@eq_comm _ Uᶜ]; refl lemma is_closed_iff_zero_locus (Z : set (prime_spectrum R)) : is_closed Z ↔ ∃ s, Z = zero_locus s := by rw [is_closed, is_open_iff, set.compl_compl] lemma is_closed_zero_locus (s : set R) : is_closed (zero_locus s) := by { rw [is_closed_iff_zero_locus], exact ⟨s, rfl⟩ } section comap variables {S : Type v} [comm_ring S] {S' : Type*} [comm_ring S'] /-- The function between prime spectra of commutative rings induced by a ring homomorphism. This function is continuous. -/ def comap (f : R →+* S) : prime_spectrum S → prime_spectrum R := λ y, ⟨ideal.comap f y.as_ideal, by exact ideal.is_prime.comap _⟩ variables (f : R →+* S) @[simp] lemma comap_as_ideal (y : prime_spectrum S) : (comap f y).as_ideal = ideal.comap f y.as_ideal := rfl @[simp] lemma comap_id : comap (ring_hom.id R) = id := funext $ λ x, ext.mpr $ by { rw [comap_as_ideal], apply ideal.ext, intros r, simp } @[simp] lemma comap_comp (f : R →+* S) (g : S →+* S') : comap (g.comp f) = comap f ∘ comap g := funext $ λ x, ext.mpr $ by { simp, refl } @[simp] lemma preimage_comap_zero_locus (s : set R) : (comap f) ⁻¹' (zero_locus s) = zero_locus (f '' s) := begin ext x, simp only [mem_zero_locus, set.mem_preimage, comap_as_ideal, set.image_subset_iff], refl end lemma comap_continuous (f : R →+* S) : continuous (comap f) := begin rw continuous_iff_is_closed, simp only [is_closed_iff_zero_locus], rintro _ ⟨s, rfl⟩, exact ⟨_, preimage_comap_zero_locus f s⟩ end end comap lemma zero_locus_vanishing_ideal_eq_closure (t : set (prime_spectrum R)) : zero_locus (vanishing_ideal t : set R) = closure t := begin apply set.subset.antisymm, { rintro x hx t' ⟨ht', ht⟩, obtain ⟨fs, rfl⟩ : ∃ s, t' = zero_locus s, by rwa [is_closed_iff_zero_locus] at ht', rw [subset_zero_locus_iff_subset_vanishing_ideal] at ht, calc fs ⊆ vanishing_ideal t : ht ... ⊆ x.as_ideal : hx }, { rw (is_closed_zero_locus _).closure_subset_iff, exact subset_zero_locus_vanishing_ideal t } end /-- The prime spectrum of a commutative ring is a compact topological space. -/ instance : compact_space (prime_spectrum R) := begin apply compact_space_of_finite_subfamily_closed, intros ι Z hZc hZ, let I : ι → ideal R := λ i, vanishing_ideal (Z i), have hI : ∀ i, Z i = zero_locus (I i), { intro i, rw [zero_locus_vanishing_ideal_eq_closure, is_closed.closure_eq], exact hZc i }, have one_mem : (1:R) ∈ ⨆ (i : ι), I i, { rw [← ideal.eq_top_iff_one, ← zero_locus_empty_iff_eq_top, zero_locus_supr], simpa only [hI] using hZ }, obtain ⟨s, hs⟩ : ∃ s : finset ι, (1:R) ∈ ⨆ i ∈ s, I i := submodule.exists_finset_of_mem_supr I one_mem, show ∃ t : finset ι, (⋂ i ∈ t, Z i) = ∅, use s, rw [← ideal.eq_top_iff_one, ←zero_locus_empty_iff_eq_top] at hs, simpa only [zero_locus_supr, hI] using hs end section basic_open /-- `basic_open r` is the open subset containing all prime ideals not containing `r`. -/ def basic_open (r : R) : set (prime_spectrum R) := { x | r ∉ x.as_ideal } lemma basic_open_open {r : R} : is_open (basic_open r) := ⟨{r}, set.ext $ λ x, set.singleton_subset_iff.trans $ not_not.symm⟩ end basic_open end prime_spectrum
22eb2b537aaa9c9bb369032f8d9da935cfd39927
f68ef9a599ec5575db7b285d4960e63c5d464ccc
/Exercises/Lista 4/fol.lean
7361d8467325aa4baa684ddefcf42ba43e1d0644
[]
no_license
lucasmoschen/discrete-mathematics
a38d5970cc571b0b9d202bf6a43efeb8ed6f66e3
0f1945cc5eb094814c926cd6ae4a8b4c5c579a1e
refs/heads/master
1,677,111,757,003
1,611,500,097,000
1,611,500,097,000
205,903,359
1
0
null
null
null
null
UTF-8
Lean
false
false
1,440
lean
/- Alunos: - Lucas Emanuel - Lucas Moschen Assume that we have a language with the following parameters: ∀, intended to mean “for all things”; N, intended to mean “is a number”; I , intended to mean “is interesting”; ; 'lt', intended to mean “is less than”; and 'z', a constant symbol intended to denote zero. Translate into this language the English sentences listed below. If the English sentence is ambiguous, you will need more than one translation. (a) Zero is less than any number. (b) If any number is interesting, then zero is interesting. (c) No number is less than zero. (d) Any uninteresting number with the property that all smaller numbers are interesting certainly is interesting. (e) There is no number such that all numbers are less than it. (f) There is no number such that no number is less than it. Complete o exercício criando as demais variáveis hb...hf. Se a frase for ambigua, use hX1, hX2... -/ constant U : Type constant N : U → Prop constant z : U constant lt : U → U → Prop constant I : U → Prop variable ha : (∀x : U , N(x)) → lt z x variable hb1 : (∀ x : U, N(x) ∧ I(x)) → I(z) variable hb2 : (∃ x : U, N(x) ∧ I(x)) → I(z) variable hc : ¬ (∃ x : U, N(x) → lt x z) variable hd : ∀ x : U, ¬ I(x) ∧ (∀ y : U, lt y x → I(y)) → I(x) variable he : ¬ (∃ x : U, (∀ y : U, lt y x)) variable hf : ¬ (∃ x : U, ¬ (∃ y, lt y x))
8e1ea72dd344040964448e083980897f2dc8ee92
1437b3495ef9020d5413178aa33c0a625f15f15f
/logic/basic.lean
e27b072759f19c1169d27d39a361227904b2d568
[ "Apache-2.0" ]
permissive
jean002/mathlib
c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30
dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd
refs/heads/master
1,587,027,806,375
1,547,306,358,000
1,547,306,358,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,664
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 Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". Note: in the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ import data.prod tactic.cache /- miscellany TODO: move elsewhere -/ section miscellany variables {α : Type*} {β : Type*} @[reducible] def hidden {a : α} := a def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance : decidable_eq empty := λa, a.elim @[priority 0] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) /- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl @[simp] theorem coe_fn_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl @[simp] theorem coe_fn_coe_base {α β} [has_coe α β] [has_coe_to_fun β] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl @[simp] theorem coe_sort_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl @[simp] theorem coe_sort_coe_base {α β} [has_coe α β] [has_coe_to_sort β] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- `pempty` is the universe-polymorphic analogue of `empty`. -/ @[derive decidable_eq] inductive {u} pempty : Sort u def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl end miscellany /- propositional connectives -/ @[simp] theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /- implies -/ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β} (h : α) (h₂ : β) : α := h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm @[simp] theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /- not -/ theorem not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {α} : ¬(α → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem by_contradiction {p} [decidable p] : (¬p → false) → p := decidable.by_contradiction @[simp] theorem not_not [decidable a] : ¬¬a ↔ a := iff.intro by_contradiction not_not_intro theorem of_not_not [decidable a] : ¬¬a → a := by_contradiction theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a := by_contradiction (not_not_of_not_imp h) theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := by_contradiction $ hb ∘ h theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.imp_symm, not.imp_symm⟩ theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /- and -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp [and.left_comm, and.comm] theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ /- or -/ theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans or_iff_not_imp_left theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩ /- distributivity -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) /- iff -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨not_or_of_imp, or.neg_resolve_left⟩ theorem imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha @[simp] theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm theorem not_iff [decidable a] [decidable b] : ¬ (a ↔ b) ↔ (¬ a ↔ b) := by split; intro h; [split, skip]; intro h'; [by_contradiction,intro,skip]; try { refine h _; simp [*] }; rw [h',not_iff_self] at h; exact h theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm theorem iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } @[simp] theorem not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /- de morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, not_not] theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← not_and_distrib, not_not] end propositional /- equality -/ section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq := have p = q, from propext ⟨λ _, hq, λ _, hp⟩, by subst q; refl theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α} (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ lemma heq_of_eq_mp : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h end equality /- quantifiers -/ section quantifiers variables {α : Sort*} {p q : α → Prop} {b : Prop} def Exists.imp := @exists_imp_exists theorem forall_swap {α β} {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨function.swap, function.swap⟩ theorem exists_swap {α β} {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) theorem not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := by haveI := decidable_of_iff (¬ ∃ x, p x) not_exists; exact not_iff_comm.1 not_exists @[simp] theorem not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [inhabited α] : (α → b) ↔ b := ⟨λ h, h (arbitrary α), λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [inhabited α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, λ h, ⟨arbitrary α, h⟩⟩ theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq_left @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h @[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ @[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ @[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h @[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst end quantifiers /- classical versions -/ namespace classical variables {α : Sort*} {p : α → Prop} local attribute [instance] prop_decidable protected theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall protected theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := forall_or_distrib_left theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 theorem or_not {p : Prop} : p ∨ ¬ p := by_cases or.inl or.inr protected theorem or_iff_not_imp_left {p q : Prop} : p ∨ q ↔ (¬ p → q) := or_iff_not_imp_left protected theorem or_iff_not_imp_right {p q : Prop} : q ∨ p ↔ (¬ p → q) := or_iff_not_imp_right protected lemma not_not {p : Prop} : ¬¬p ↔ p := not_not /- use shortened names to avoid conflict when classical namespace is open -/ noncomputable theorem dec (p : Prop) : decidable p := by apply_instance noncomputable theorem dec_pred (p : α → Prop) : decidable_pred p := by apply_instance noncomputable theorem dec_rel (p : α → α → Prop) : decidable_rel p := by apply_instance noncomputable theorem dec_eq (α : Sort*) : decidable_eq α := by apply_instance @[elab_as_eliminator] noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C := if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0 lemma some_spec2 {α : Type*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ end classical @[elab_as_eliminator] noncomputable def {u} exists.classical_rec_on {α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C := H (classical.some h) (classical.some_spec h) /- bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) (_ : q x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical section nonempty universes u v w variables {α : Type u} {β : Type v} {γ : α → Type w} attribute [simp] nonempty_of_inhabited lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α := iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩) @[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p := iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩) lemma not_nonempty_iff_imp_false {p : Prop} : ¬ nonempty α ↔ α → false := ⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩ @[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) := iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩) @[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end) @[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end) @[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_empty : ¬ nonempty empty := assume ⟨h⟩, h.elim @[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) := iff.intro (assume h a, h _) (assume h ⟨a⟩, h _) @[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) := iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩) lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} : nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) := iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩) -- inhabited_of_nonempty already exists, in core/init/classical.lean, but the -- assumption is not [...], which makes it unsuitable for some applications noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α := ⟨classical.choice h⟩ end nonempty
37610f69676d44a923ae5e52c2c88dd473c0ff47
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/option/instances_auto.lean
ff91b97d1c702e6aa9f28405ace96aa25d0d2801
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,056
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.option.basic import Mathlib.Lean3Lib.init.meta.tactic import Mathlib.Lean3Lib.init.control.lawful universes u_1 u namespace Mathlib protected instance option.is_lawful_monad : is_lawful_monad Option := is_lawful_monad.mk (fun (α β : Type u_1) (x : α) (f : α → Option β) => rfl) fun (α β γ : Type u_1) (x : Option α) (f : α → Option β) (g : β → Option γ) => Option.rec rfl (fun (x : α) => rfl) x theorem option.eq_of_eq_some {α : Type u} {x : Option α} {y : Option α} : (∀ (z : α), x = some z ↔ y = some z) → x = y := sorry theorem option.eq_some_of_is_some {α : Type u} {o : Option α} (h : ↥(option.is_some o)) : o = some (option.get h) := sorry theorem option.eq_none_of_is_none {α : Type u} {o : Option α} : ↥(option.is_none o) → o = none := sorry end Mathlib
892e2389a1134707cdedbc84194794427bddd182
649957717d58c43b5d8d200da34bf374293fe739
/src/data/dfinsupp.lean
68db7e8ecc990880cad5ba03a8a57d68f460da20
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
32,395
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau Dependent functions with finite support (see `data/finsupp.lean`). -/ import data.finset data.set.finite algebra.big_operators algebra.module algebra.pi_instances universes u u₁ u₂ v v₁ v₂ v₃ w x y l variables (ι : Type u) (β : ι → Type v) def decidable_zero_symm {γ : Type w} [has_zero γ] [decidable_pred (eq (0 : γ))] : decidable_pred (λ x, x = (0:γ)) := λ x, decidable_of_iff (0 = x) eq_comm local attribute [instance] decidable_zero_symm namespace dfinsupp variable [Π i, has_zero (β i)] structure pre : Type (max u v) := (to_fun : Π i, β i) (pre_support : multiset ι) (zero : ∀ i, i ∈ pre_support ∨ to_fun i = 0) instance : setoid (pre ι β) := { r := λ x y, ∀ i, x.to_fun i = y.to_fun i, iseqv := ⟨λ f i, rfl, λ f g H i, (H i).symm, λ f g h H1 H2 i, (H1 i).trans (H2 i)⟩ } end dfinsupp variable {ι} @[reducible] def dfinsupp [Π i, has_zero (β i)] : Type* := quotient (dfinsupp.setoid ι β) variable {β} notation `Π₀` binders `, ` r:(scoped f, dfinsupp f) := r infix ` →ₚ `:25 := dfinsupp namespace dfinsupp section basic variables [Π i, has_zero (β i)] variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] instance : has_coe_to_fun (Π₀ i, β i) := ⟨λ _, Π i, β i, λ f, quotient.lift_on f pre.to_fun $ λ _ _, funext⟩ instance : has_zero (Π₀ i, β i) := ⟨⟦⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟧⟩ instance : inhabited (Π₀ i, β i) := ⟨0⟩ @[simp] lemma zero_apply {i : ι} : (0 : Π₀ i, β i) i = 0 := rfl @[extensionality] lemma ext {f g : Π₀ i, β i} (H : ∀ i, f i = g i) : f = g := quotient.induction_on₂ f g (λ _ _ H, quotient.sound H) H /-- The composition of `f : β₁ → β₂` and `g : Π₀ i, β₁ i` is `map_range f hf g : Π₀ i, β₂ i`, well defined when `f 0 = 0`. -/ def map_range (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (g : Π₀ i, β₁ i) : Π₀ i, β₂ i := quotient.lift_on g (λ x, ⟦(⟨λ i, f i (x.1 i), x.2, λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, hf]⟩ : pre ι β₂)⟧) $ λ x y H, quotient.sound $ λ i, by simp only [H i] @[simp] lemma map_range_apply {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {i : ι} : map_range f hf g i = f i (g i) := quotient.induction_on g $ λ x, rfl def zip_with (f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0) (g₁ : Π₀ i, β₁ i) (g₂ : Π₀ i, β₂ i) : (Π₀ i, β i) := begin refine quotient.lift_on₂ g₁ g₂ (λ x y, ⟦(⟨λ i, f i (x.1 i) (y.1 i), x.2 + y.2, λ i, _⟩ : pre ι β)⟧) _, { cases x.3 i with h1 h1, { left, rw multiset.mem_add, left, exact h1 }, cases y.3 i with h2 h2, { left, rw multiset.mem_add, right, exact h2 }, right, rw [h1, h2, hf] }, exact λ x₁ x₂ y₁ y₂ H1 H2, quotient.sound $ λ i, by simp only [H1 i, H2 i] end @[simp] lemma zip_with_apply {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} {i : ι} : zip_with f hf g₁ g₂ i = f i (g₁ i) (g₂ i) := quotient.induction_on₂ g₁ g₂ $ λ _ _, rfl end basic section algebra instance [Π i, add_monoid (β i)] : has_add (Π₀ i, β i) := ⟨zip_with (λ _, (+)) (λ _, add_zero 0)⟩ @[simp] lemma add_apply [Π i, add_monoid (β i)] {g₁ g₂ : Π₀ i, β i} {i : ι} : (g₁ + g₂) i = g₁ i + g₂ i := zip_with_apply instance [Π i, add_monoid (β i)] : add_monoid (Π₀ i, β i) := { add_monoid . zero := 0, add := (+), add_assoc := λ f g h, ext $ λ i, by simp only [add_apply, add_assoc], zero_add := λ f, ext $ λ i, by simp only [add_apply, zero_apply, zero_add], add_zero := λ f, ext $ λ i, by simp only [add_apply, zero_apply, add_zero] } instance [Π i, add_monoid (β i)] {i : ι} : is_add_monoid_hom (λ g : Π₀ i : ι, β i, g i) := { map_add := λ _ _, add_apply, map_zero := zero_apply } instance [Π i, add_group (β i)] : has_neg (Π₀ i, β i) := ⟨λ f, f.map_range (λ _, has_neg.neg) (λ _, neg_zero)⟩ instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (Π₀ i, β i) := { add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm], .. dfinsupp.add_monoid } @[simp] lemma neg_apply [Π i, add_group (β i)] {g : Π₀ i, β i} {i : ι} : (- g) i = - g i := map_range_apply instance [Π i, add_group (β i)] : add_group (Π₀ i, β i) := { add_left_neg := λ f, ext $ λ i, by simp only [add_apply, neg_apply, zero_apply, add_left_neg], .. dfinsupp.add_monoid, .. (infer_instance : has_neg (Π₀ i, β i)) } @[simp] lemma sub_apply [Π i, add_group (β i)] {g₁ g₂ : Π₀ i, β i} {i : ι} : (g₁ - g₂) i = g₁ i - g₂ i := by rw [sub_eq_add_neg]; simp instance [Π i, add_comm_group (β i)] : add_comm_group (Π₀ i, β i) := { add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm], ..dfinsupp.add_group } def to_has_scalar {γ : Type w} [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)] : has_scalar γ (Π₀ i, β i) := ⟨λc v, v.map_range (λ _, (•) c) (λ _, smul_zero _)⟩ local attribute [instance] to_has_scalar @[simp] lemma smul_apply {γ : Type w} [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)] {i : ι} {b : γ} {v : Π₀ i, β i} : (b • v) i = b • (v i) := map_range_apply def to_module {γ : Type w} [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)] : module γ (Π₀ i, β i) := module.of_core { smul_add := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, smul_add], add_smul := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, add_smul], one_smul := λ x, ext $ λ i, by simp only [smul_apply, one_smul], mul_smul := λ r s x, ext $ λ i, by simp only [smul_apply, smul_smul], .. (infer_instance : has_scalar γ (Π₀ i, β i)) } end algebra section filter_and_subtype_domain /-- `filter p f` is the function which is `f i` if `p i` is true and 0 otherwise. -/ def filter [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (f : Π₀ i, β i) : Π₀ i, β i := quotient.lift_on f (λ x, ⟦(⟨λ i, if p i then x.1 i else 0, x.2, λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H, quotient.sound $ λ i, by simp only [H i] @[simp] lemma filter_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {i : ι} {f : Π₀ i, β i} : f.filter p i = if p i then f i else 0 := quotient.induction_on f $ λ x, rfl @[simp] lemma filter_apply_pos [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {f : Π₀ i, β i} {i : ι} (h : p i) : f.filter p i = f i := by simp only [filter_apply, if_pos h] @[simp] lemma filter_apply_neg [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {f : Π₀ i, β i} {i : ι} (h : ¬ p i) : f.filter p i = 0 := by simp only [filter_apply, if_neg h] lemma filter_pos_add_filter_neg [Π i, add_monoid (β i)] {f : Π₀ i, β i} {p : ι → Prop} [decidable_pred p] : f.filter p + f.filter (λi, ¬ p i) = f := ext $ λ i, by simp only [add_apply, filter_apply]; split_ifs; simp only [add_zero, zero_add] /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (f : Π₀ i, β i) : Π₀ i : subtype p, β i.1 := begin fapply quotient.lift_on f, { intro x, refine ⟦⟨λ i, x.1 i.1, (x.2.filter p).attach.map $ λ j, ⟨j.1, (multiset.mem_filter.1 j.2).2⟩, _⟩⟧, refine λ i, or.cases_on (x.3 i.1) (λ H, _) or.inr, left, rw multiset.mem_map, refine ⟨⟨i.1, multiset.mem_filter.2 ⟨H, i.2⟩⟩, _, subtype.eta _ _⟩, apply multiset.mem_attach }, intros x y H, exact quotient.sound (λ i, H i.1) end @[simp] lemma subtype_domain_zero [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] : subtype_domain p (0 : Π₀ i, β i) = 0 := rfl @[simp] lemma subtype_domain_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {i : subtype p} {v : Π₀ i, β i} : (subtype_domain p v) i = v (i.val) := quotient.induction_on v $ λ x, rfl @[simp] lemma subtype_domain_add [Π i, add_monoid (β i)] {p : ι → Prop} [decidable_pred p] {v v' : Π₀ i, β i} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ i, by simp only [add_apply, subtype_domain_apply] instance subtype_domain.is_add_monoid_hom [Π i, add_monoid (β i)] {p : ι → Prop} [decidable_pred p] : is_add_monoid_hom (subtype_domain p : (Π₀ i : ι, β i) → Π₀ i : subtype p, β i) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma subtype_domain_neg [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v : Π₀ i, β i} : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ i, by simp only [neg_apply, subtype_domain_apply] @[simp] lemma subtype_domain_sub [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v v' : Π₀ i, β i} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ i, by simp only [sub_apply, subtype_domain_apply] end filter_and_subtype_domain variable [decidable_eq ι] section basic variable [Π i, has_zero (β i)] lemma finite_supp (f : Π₀ i, β i) : set.finite {i | f i ≠ 0} := quotient.induction_on f $ λ x, set.finite_subset (finset.finite_to_set x.2.to_finset) $ λ i H, multiset.mem_to_finset.2 $ (x.3 i).resolve_right H def mk (s : finset ι) (x : Π i : (↑s : set ι), β i.1) : Π₀ i, β i := ⟦⟨λ i, if H : i ∈ s then x ⟨i, H⟩ else 0, s.1, λ i, if H : i ∈ s then or.inl H else or.inr $ dif_neg H⟩⟧ @[simp] lemma mk_apply {s : finset ι} {x : Π i : (↑s : set ι), β i.1} {i : ι} : (mk s x : Π i, β i) i = if H : i ∈ s then x ⟨i, H⟩ else 0 := rfl theorem mk_inj (s : finset ι) : function.injective (@mk ι β _ _ s) := begin intros x y H, ext i, have h1 : (mk s x : Π i, β i) i = (mk s y : Π i, β i) i, {rw H}, cases i with i hi, change i ∈ s at hi, dsimp only [mk_apply, subtype.coe_mk] at h1, simpa only [dif_pos hi] using h1 end def single (i : ι) (b : β i) : Π₀ i, β i := mk (finset.singleton i) $ λ j, eq.rec_on (finset.mem_singleton.1 j.2).symm b @[simp] lemma single_apply {i i' b} : (single i b : Π₀ i, β i) i' = (if h : i = i' then eq.rec_on h b else 0) := begin dsimp only [single], by_cases h : i = i', { have h1 : i' ∈ finset.singleton i, { simp only [h, finset.mem_singleton] }, simp only [mk_apply, dif_pos h, dif_pos h1] }, { have h1 : i' ∉ finset.singleton i, { simp only [ne.symm h, finset.mem_singleton, not_false_iff] }, simp only [mk_apply, dif_neg h, dif_neg h1] } end @[simp] lemma single_zero {i} : (single i 0 : Π₀ i, β i) = 0 := quotient.sound $ λ j, if H : j ∈ finset.singleton i then by dsimp only; rw [dif_pos H]; cases finset.mem_singleton.1 H; refl else dif_neg H @[simp] lemma single_eq_same {i b} : (single i b : Π₀ i, β i) i = b := by simp only [single_apply, dif_pos rfl] @[simp] lemma single_eq_of_ne {i i' b} (h : i ≠ i') : (single i b : Π₀ i, β i) i' = 0 := by simp only [single_apply, dif_neg h] def erase (i : ι) (f : Π₀ i, β i) : Π₀ i, β i := quotient.lift_on f (λ x, ⟦(⟨λ j, if j = i then 0 else x.1 j, x.2, λ j, or.cases_on (x.3 j) or.inl $ λ H, or.inr $ by simp only [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H, quotient.sound $ λ j, if h : j = i then by simp only [if_pos h] else by simp only [if_neg h, H j] @[simp] lemma erase_apply {i j : ι} {f : Π₀ i, β i} : (f.erase i) j = if j = i then 0 else f j := quotient.induction_on f $ λ x, rfl @[simp] lemma erase_same {i : ι} {f : Π₀ i, β i} : (f.erase i) i = 0 := by simp @[simp] lemma erase_ne {i i' : ι} {f : Π₀ i, β i} (h : i' ≠ i) : (f.erase i) i' = f i' := by simp [h] end basic section add_monoid variable [Π i, add_monoid (β i)] @[simp] lemma single_add {i : ι} {b₁ b₂ : β i} : single i (b₁ + b₂) = single i b₁ + single i b₂ := ext $ assume i', begin by_cases h : i = i', { subst h, simp only [add_apply, single_eq_same] }, { simp only [add_apply, single_eq_of_ne h, zero_add] } end lemma single_add_erase {i : ι} {f : Π₀ i, β i} : single i (f i) + f.erase i = f := ext $ λ i', if h : i = i' then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, add_zero] else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), zero_add] lemma erase_add_single {i : ι} {f : Π₀ i, β i} : f.erase i + single i (f i) = f := ext $ λ i', if h : i = i' then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, zero_add] else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), add_zero] protected theorem induction {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i) (h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (single i b + f)) : p f := begin refine quotient.induction_on f (λ x, _), cases x with f s H, revert f H, apply multiset.induction_on s, { intros f H, convert h0, ext i, exact (H i).resolve_left id }, intros i s ih f H, by_cases H1 : i ∈ s, { have H2 : ∀ j, j ∈ s ∨ f j = 0, { intro j, cases H j with H2 H2, { cases multiset.mem_cons.1 H2 with H3 H3, { left, rw H3, exact H1 }, { left, exact H3 } }, right, exact H2 }, have H3 : (⟦{to_fun := f, pre_support := i :: s, zero := H}⟧ : Π₀ i, β i) = ⟦{to_fun := f, pre_support := s, zero := H2}⟧, { exact quotient.sound (λ i, rfl) }, rw H3, apply ih }, have H2 : p (erase i ⟦{to_fun := f, pre_support := i :: s, zero := H}⟧), { dsimp only [erase, quotient.lift_on_beta], have H2 : ∀ j, j ∈ s ∨ ite (j = i) 0 (f j) = 0, { intro j, cases H j with H2 H2, { cases multiset.mem_cons.1 H2 with H3 H3, { right, exact if_pos H3 }, { left, exact H3 } }, right, split_ifs; [refl, exact H2] }, have H3 : (⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j), pre_support := i :: s, zero := _}⟧ : Π₀ i, β i) = ⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j), pre_support := s, zero := H2}⟧ := quotient.sound (λ i, rfl), rw H3, apply ih }, have H3 : single i _ + _ = (⟦{to_fun := f, pre_support := i :: s, zero := H}⟧ : Π₀ i, β i) := single_add_erase, rw ← H3, change p (single i (f i) + _), cases classical.em (f i = 0) with h h, { rw [h, single_zero, zero_add], exact H2 }, refine ha _ _ _ _ h H2, rw erase_same end lemma induction₂ {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i) (h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (f + single i b)) : p f := dfinsupp.induction f h0 $ λ i b f h1 h2 h3, have h4 : f + single i b = single i b + f, { ext j, by_cases H : i = j, { subst H, simp [h1] }, { simp [H] } }, eq.rec_on h4 $ ha i b f h1 h2 h3 end add_monoid @[simp] lemma mk_add [Π i, add_monoid (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} : mk s (x + y) = mk s x + mk s y := ext $ λ i, by simp only [add_apply, mk_apply]; split_ifs; [refl, rw zero_add] @[simp] lemma mk_zero [Π i, has_zero (β i)] {s : finset ι} : mk s (0 : Π i : (↑s : set ι), β i.1) = 0 := ext $ λ i, by simp only [mk_apply]; split_ifs; refl @[simp] lemma mk_neg [Π i, add_group (β i)] {s : finset ι} {x : Π i : (↑s : set ι), β i.1} : mk s (-x) = -mk s x := ext $ λ i, by simp only [neg_apply, mk_apply]; split_ifs; [refl, rw neg_zero] @[simp] lemma mk_sub [Π i, add_group (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} : mk s (x - y) = mk s x - mk s y := ext $ λ i, by simp only [sub_apply, mk_apply]; split_ifs; [refl, rw sub_zero] instance [Π i, add_group (β i)] {s : finset ι} : is_add_group_hom (@mk ι β _ _ s) := ⟨λ _ _, mk_add⟩ section local attribute [instance] to_module variables (γ : Type w) [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)] include γ @[simp] lemma mk_smul {s : finset ι} {c : γ} (x : Π i : (↑s : set ι), β i.1) : mk s (c • x) = c • mk s x := ext $ λ i, by simp only [smul_apply, mk_apply]; split_ifs; [refl, rw smul_zero] @[simp] lemma single_smul {i : ι} {c : γ} {x : β i} : single i (c • x) = c • single i x := ext $ λ i, by simp only [smul_apply, single_apply]; split_ifs; [cases h, rw smul_zero]; refl variable β def lmk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →ₗ[γ] Π₀ i, β i := ⟨mk s, λ _ _, mk_add, λ c x, by rw [mk_smul γ x]⟩ def lsingle (i) : β i →ₗ[γ] Π₀ i, β i := ⟨single i, λ _ _, single_add, λ _ _, single_smul _⟩ variable {β} @[simp] lemma lmk_apply {s : finset ι} {x} : lmk β γ s x = mk s x := rfl @[simp] lemma lsingle_apply {i : ι} {x : β i} : lsingle β γ i x = single i x := rfl end section support_basic variables [Π i, has_zero (β i)] [Π i, decidable_pred (eq (0 : β i))] def support (f : Π₀ i, β i) : finset ι := quotient.lift_on f (λ x, x.2.to_finset.filter $ λ i, x.1 i ≠ 0) $ begin intros x y Hxy, ext i, split, { intro H, rcases finset.mem_filter.1 H with ⟨h1, h2⟩, rw Hxy i at h2, exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (y.3 i).resolve_right h2, h2⟩ }, { intro H, rcases finset.mem_filter.1 H with ⟨h1, h2⟩, rw ← Hxy i at h2, exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (x.3 i).resolve_right h2, h2⟩ }, end @[simp] theorem support_mk_subset {s : finset ι} {x : Π i : (↑s : set ι), β i.1} : (mk s x).support ⊆ s := λ i H, multiset.mem_to_finset.1 (finset.mem_filter.1 H).1 @[simp] theorem mem_support_to_fun (f : Π₀ i, β i) (i) : i ∈ f.support ↔ f i ≠ 0 := begin refine quotient.induction_on f (λ x, _), dsimp only [support, quotient.lift_on_beta], rw [finset.mem_filter, multiset.mem_to_finset], exact and_iff_right_of_imp (x.3 i).resolve_right end theorem eq_mk_support (f : Π₀ i, β i) : f = mk f.support (λ i, f i.1) := by ext i; by_cases h : f i = 0; try {simp at h}; simp [h] @[simp] lemma support_zero : (0 : Π₀ i, β i).support = ∅ := rfl @[simp] lemma mem_support_iff (f : Π₀ i, β i) : ∀i:ι, i ∈ f.support ↔ f i ≠ 0 := f.mem_support_to_fun @[simp] lemma support_eq_empty {f : Π₀ i, β i} : f.support = ∅ ↔ f = 0 := ⟨λ H, ext $ by simpa [finset.ext] using H, by simp {contextual:=tt}⟩ instance decidable_zero : decidable_pred (eq (0 : Π₀ i, β i)) := λ f, decidable_of_iff _ $ support_eq_empty.trans eq_comm lemma support_subset_iff {s : set ι} {f : Π₀ i, β i} : ↑f.support ⊆ s ↔ (∀i∉s, f i = 0) := by simp [set.subset_def]; exact forall_congr (assume i, @not_imp_comm _ _ (classical.dec _) (classical.dec _)) lemma support_single_ne_zero {i : ι} {b : β i} (hb : b ≠ 0) : (single i b).support = {i} := begin ext j, by_cases h : i = j, { subst h, simp [hb] }, simp [ne.symm h, h] end lemma support_single_subset {i : ι} {b : β i} : (single i b).support ⊆ {i} := support_mk_subset section map_range_and_zip_with variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] variables [Π i, decidable_pred (eq (0 : β₁ i))] [Π i, decidable_pred (eq (0 : β₂ i))] lemma map_range_def {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} : map_range f hf g = mk g.support (λ i, f i.1 (g i.1)) := begin ext i, by_cases h : g i = 0, { simp [h, hf] }, { simp at h, simp [h, hf] } end lemma support_map_range {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} : (map_range f hf g).support ⊆ g.support := by simp [map_range_def] @[simp] lemma map_range_single {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {i : ι} {b : β₁ i} : map_range f hf (single i b) = single i (f i b) := dfinsupp.ext $ λ i', by by_cases i = i'; [{subst i', simp}, simp [h, hf]] lemma zip_with_def {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} : zip_with f hf g₁ g₂ = mk (g₁.support ∪ g₂.support) (λ i, f i.1 (g₁ i.1) (g₂ i.1)) := begin ext i, by_cases h1 : g₁ i = 0; by_cases h2 : g₂ i = 0; try {simp at h1 h2}; simp [h1, h2, hf] end lemma support_zip_with {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := by simp [zip_with_def] end map_range_and_zip_with lemma erase_def (i : ι) (f : Π₀ i, β i) : f.erase i = mk (f.support.erase i) (λ j, f j.1) := begin ext j, by_cases h1 : j = i; by_cases h2 : f j = 0; try {simp at h2}; simp [h1, h2] end @[simp] lemma support_erase (i : ι) (f : Π₀ i, β i) : (f.erase i).support = f.support.erase i := begin ext j, by_cases h1 : j = i; by_cases h2 : f j = 0; try {simp at h2}; simp [h1, h2] end section filter_and_subtype_domain variables {p : ι → Prop} [decidable_pred p] lemma filter_def (f : Π₀ i, β i) : f.filter p = mk (f.support.filter p) (λ i, f i.1) := by ext i; by_cases h1 : p i; by_cases h2 : f i = 0; try {simp at h2}; simp [h1, h2] @[simp] lemma support_filter (f : Π₀ i, β i) : (f.filter p).support = f.support.filter p := by ext i; by_cases h : p i; simp [h] lemma subtype_domain_def (f : Π₀ i, β i) : f.subtype_domain p = mk (f.support.subtype p) (λ i, f i.1) := by ext i; cases i with i hi; by_cases h1 : p i; by_cases h2 : f i = 0; try {simp at h2}; dsimp; simp [h1, h2] @[simp] lemma support_subtype_domain {f : Π₀ i, β i} : (subtype_domain p f).support = f.support.subtype p := by ext i; cases i with i hi; by_cases h1 : p i; by_cases h2 : f i = 0; try {simp at h2}; dsimp; simp [h1, h2] end filter_and_subtype_domain end support_basic lemma support_add [Π i, add_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] {g₁ g₂ : Π₀ i, β i} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with @[simp] lemma support_neg [Π i, add_group (β i)] [Π i, decidable_pred (eq (0 : β i))] {f : Π₀ i, β i} : support (-f) = support f := by ext i; simp local attribute [instance] dfinsupp.to_module lemma support_smul {γ : Type w} [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)] [Π (i : ι), decidable_pred (eq (0 : β i))] {b : γ} {v : Π₀ i, β i} : (b • v).support ⊆ v.support := λ x, by simp [dfinsupp.mem_support_iff, not_imp_not] {contextual := tt} instance [decidable_eq ι] [Π i, has_zero (β i)] [Π i, decidable_eq (β i)] : decidable_eq (Π₀ i, β i) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀i∈f.support, f i = g i)) ⟨assume ⟨h₁, h₂⟩, ext $ assume i, if h : i ∈ f.support then h₂ i h else have hf : f i = 0, by rwa [f.mem_support_iff, not_not] at h, have hg : g i = 0, by rwa [h₁, g.mem_support_iff, not_not] at h, by rw [hf, hg], by intro h; subst h; simp⟩ section prod_and_sum variables {γ : Type w} -- [to_additive dfinsupp.sum] for dfinsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g i (f i)` over the support of `f`. -/ def sum [Π i, has_zero (β i)] [Π i, decidable_pred (eq (0 : β i))] [add_comm_monoid γ] (f : Π₀ i, β i) (g : Π i, β i → γ) : γ := f.support.sum (λi, g i (f i)) /-- `prod f g` is the product of `g i (f i)` over the support of `f`. -/ @[to_additive dfinsupp.sum] def prod [Π i, has_zero (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] (f : Π₀ i, β i) (g : Π i, β i → γ) : γ := f.support.prod (λi, g i (f i)) attribute [to_additive dfinsupp.sum.equations._eqn_1] dfinsupp.prod.equations._eqn_1 @[to_additive dfinsupp.sum_map_range_index] lemma prod_map_range_index {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] [Π i, decidable_pred (eq (0 : β₁ i))] [Π i, decidable_pred (eq (0 : β₂ i))] [comm_monoid γ] {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {h : Π i, β₂ i → γ} (h0 : ∀i, h i 0 = 1) : (map_range f hf g).prod h = g.prod (λi b, h i (f i b)) := begin rw [map_range_def], refine (finset.prod_subset support_mk_subset _).trans _, { intros i h1 h2, dsimp, simp [h1] at h2, dsimp at h2, simp [h1, h2, h0] }, { refine finset.prod_congr rfl _, intros i h1, simp [h1] } end @[to_additive dfinsupp.sum_zero_index] lemma prod_zero_index [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] {h : Π i, β i → γ} : (0 : Π₀ i, β i).prod h = 1 := rfl @[to_additive dfinsupp.sum_single_index] lemma prod_single_index [Π i, has_zero (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] {i : ι} {b : β i} {h : Π i, β i → γ} (h_zero : h i 0 = 1) : (single i b).prod h = h i b := begin by_cases h : b = 0, { simp [h, prod_zero_index, h_zero], refl }, { simp [dfinsupp.prod, support_single_ne_zero h] } end @[to_additive dfinsupp.sum_neg_index] lemma prod_neg_index [Π i, add_group (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] {g : Π₀ i, β i} {h : Π i, β i → γ} (h0 : ∀i, h i 0 = 1) : (-g).prod h = g.prod (λi b, h i (- b)) := prod_map_range_index h0 @[simp] lemma sum_apply {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i, decidable_pred (eq (0 : β₁ i))] [Π i, add_comm_monoid (β i)] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {i₂ : ι} : (f.sum g) i₂ = f.sum (λi₁ b, g i₁ b i₂) := (finset.sum_hom (λf : Π₀ i, β i, f i₂)).symm lemma support_sum {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i, decidable_pred (eq (0 : β₁ i))] [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} : (f.sum g).support ⊆ f.support.bind (λi, (g i (f i)).support) := have ∀i₁ : ι, f.sum (λ (i : ι₁) (b : β₁ i), (g i b) i₁) ≠ 0 → (∃ (i : ι₁), f i ≠ 0 ∧ ¬ (g i (f i)) i₁ = 0), from assume i₁ h, let ⟨i, hi, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨i, (f.mem_support_iff i).mp hi, ne⟩, by simpa [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply] using this @[simp] lemma sum_zero [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [add_comm_monoid γ] {f : Π₀ i, β i} : f.sum (λi b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [add_comm_monoid γ] {f : Π₀ i, β i} {h₁ h₂ : Π i, β i → γ} : f.sum (λi b, h₁ i b + h₂ i b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [add_comm_group γ] {f : Π₀ i, β i} {h : Π i, β i → γ} : f.sum (λi b, - h i b) = - f.sum h := finset.sum_hom (@has_neg.neg γ _) @[to_additive dfinsupp.sum_add_index] lemma prod_add_index [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] {f g : Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : (f.support ∪ g.support).prod (λi, h i (f i)) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, have g_eq : (f.support ∪ g.support).prod (λi, h i (g i)) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, calc (f + g).support.prod (λi, h i ((f + g) i)) = (f.support ∪ g.support).prod (λi, h i ((f + g) i)) : finset.prod_subset support_add $ by simp [mem_support_iff, h_zero] {contextual := tt} ... = (f.support ∪ g.support).prod (λi, h i (f i)) * (f.support ∪ g.support).prod (λi, h i (g i)) : by simp [h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index [Π i, add_comm_group (β i)] [Π i, decidable_pred (eq (0 : β i))] [add_comm_group γ] {f g : Π₀ i, β i} {h : Π i, β i → γ} (h_sub : ∀i b₁ b₂, h i (b₁ - b₂) = h i b₁ - h i b₂) : (f - g).sum h = f.sum h - g.sum h := have h_zero : ∀i, h i 0 = 0, from assume i, have h i (0 - 0) = h i 0 - h i 0, from h_sub i 0 0, by simpa using this, have h_neg : ∀i b, h i (- b) = - h i b, from assume i b, have h i (0 - b) = h i 0 - h i b, from h_sub i 0 b, by simpa [h_zero] using this, have h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ + h i b₂, from assume i b₁ b₂, have h i (b₁ - (- b₂)) = h i b₁ - h i (- b₂), from h_sub i b₁ (-b₂), by simpa [h_neg] using this, by simp [@sum_add_index ι β _ γ _ _ _ f (-g) h h_zero h_add]; simp [@sum_neg_index ι β _ γ _ _ _ g h h_zero, h_neg]; simp [@sum_neg ι β _ γ _ _ _ g h] @[to_additive dfinsupp.sum_finset_sum_index] lemma prod_finset_sum_index {γ : Type w} {α : Type x} [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] [decidable_eq α] {s : finset α} {g : α → Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : s.prod (λi, (g i).prod h) = (s.sum g).prod h := finset.induction_on s (by simp [prod_zero_index]) (by simp [prod_add_index, h_zero, h_add] {contextual := tt}) @[to_additive dfinsupp.sum_sum_index] lemma prod_sum_index {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i, decidable_pred (eq (0 : β₁ i))] [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : (f.sum g).prod h = f.prod (λi b, (g i b).prod h) := (prod_finset_sum_index h_zero h_add).symm @[simp] lemma sum_single [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] {f : Π₀ i, β i} : f.sum single = f := begin apply dfinsupp.induction f, {rw [sum_zero_index]}, intros i b f H hb ih, rw [sum_add_index, ih, sum_single_index], all_goals { intros, simp } end @[to_additive dfinsupp.sum_subtype_domain_index] lemma prod_subtype_domain_index [Π i, has_zero (β i)] [Π i, decidable_pred (eq (0 : β i))] [comm_monoid γ] {v : Π₀ i, β i} {p : ι → Prop} [decidable_pred p] {h : Π i, β i → γ} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λi b, h i.1 b) = v.prod h := finset.prod_bij (λp _, p.val) (by simp) (by simp) (assume ⟨a₀, ha₀⟩ ⟨a₁, ha₁⟩, by simp) (λ i hi, ⟨⟨i, hp i hi⟩, by simpa using hi, rfl⟩) lemma subtype_domain_sum [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] {s : finset γ} {h : γ → Π₀ i, β i} {p : ι → Prop} [decidable_pred p] : (s.sum h).subtype_domain p = s.sum (λc, (h c).subtype_domain p) := eq.symm (finset.sum_hom _) lemma subtype_domain_finsupp_sum {δ : γ → Type x} [decidable_eq γ] [Π c, has_zero (δ c)] [Π c, decidable_pred (eq (0 : δ c))] [Π i, add_comm_monoid (β i)] [Π i, decidable_pred (eq (0 : β i))] {p : ι → Prop} [decidable_pred p] {s : Π₀ c, δ c} {h : Π c, δ c → Π₀ i, β i} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum end prod_and_sum end dfinsupp
9a0c39980a09ef38967d2475ed6d47b6e821b80e
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/ring_theory/jacobson_ideal.lean
c538b17ea6eed59093de46ccc2eead490d69d067
[ "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
14,881
lean
/- Copyright (c) 2020 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Devon Tuma -/ import ring_theory.ideal.operations import ring_theory.polynomial.basic /-! # Jacobson radical The Jacobson radical of a ring `R` is defined to be the intersection of all maximal ideals of `R`. This is similar to how the nilradical is equal to the intersection of all prime ideals of `R`. We can extend the idea of the nilradical to ideals of `R`, by letting the radical of an ideal `I` be the intersection of prime ideals containing `I`. Under this extension, the original nilradical is the radical of the zero ideal `⊥`. Here we define the Jacobson radical of an ideal `I` in a similar way, as the intersection of maximal ideals containing `I`. ## Main definitions Let `R` be a commutative ring, and `I` be an ideal of `R` * `jacobson I` is the jacobson radical, i.e. the infimum of all maximal ideals containing I. * `is_local I` is the proposition that the jacobson radical of `I` is itself a maximal ideal ## Main statements * `mem_jacobson_iff` gives a characterization of members of the jacobson of I * `is_local_of_is_maximal_radical`: if the radical of I is maximal then so is the jacobson radical ## Tags Jacobson, Jacobson radical, Local Ideal -/ universes u v namespace ideal variables {R : Type u} [comm_ring R] {I : ideal R} variables {S : Type v} [comm_ring S] section jacobson /-- The Jacobson radical of `I` is the infimum of all maximal ideals containing `I`. -/ def jacobson (I : ideal R) : ideal R := Inf {J : ideal R | I ≤ J ∧ is_maximal J} lemma le_jacobson : I ≤ jacobson I := λ x hx, mem_Inf.mpr (λ J hJ, hJ.left hx) @[simp] lemma jacobson_idem : jacobson (jacobson I) = jacobson I := le_antisymm (Inf_le_Inf (λ J hJ, ⟨Inf_le hJ, hJ.2⟩)) le_jacobson lemma radical_le_jacobson : radical I ≤ jacobson I := le_Inf (λ J hJ, (radical_eq_Inf I).symm ▸ Inf_le ⟨hJ.left, is_maximal.is_prime hJ.right⟩) lemma eq_radical_of_eq_jacobson : jacobson I = I → radical I = I := λ h, le_antisymm (le_trans radical_le_jacobson (le_of_eq h)) le_radical @[simp] lemma jacobson_top : jacobson (⊤ : ideal R) = ⊤ := eq_top_iff.2 le_jacobson @[simp] theorem jacobson_eq_top_iff : jacobson I = ⊤ ↔ I = ⊤ := ⟨λ H, classical.by_contradiction $ λ hi, let ⟨M, hm, him⟩ := exists_le_maximal I hi in lt_top_iff_ne_top.1 (lt_of_le_of_lt (show jacobson I ≤ M, from Inf_le ⟨him, hm⟩) $ lt_top_iff_ne_top.2 hm.ne_top) H, λ H, eq_top_iff.2 $ le_Inf $ λ J ⟨hij, hj⟩, H ▸ hij⟩ lemma jacobson_eq_bot : jacobson I = ⊥ → I = ⊥ := λ h, eq_bot_iff.mpr (h ▸ le_jacobson) lemma jacobson_eq_self_of_is_maximal [H : is_maximal I] : I.jacobson = I := le_antisymm (Inf_le ⟨le_of_eq rfl, H⟩) le_jacobson @[priority 100] instance jacobson.is_maximal [H : is_maximal I] : is_maximal (jacobson I) := ⟨⟨λ htop, H.1.1 (jacobson_eq_top_iff.1 htop), λ J hJ, H.1.2 _ (lt_of_le_of_lt le_jacobson hJ)⟩⟩ theorem mem_jacobson_iff {x : R} : x ∈ jacobson I ↔ ∀ y, ∃ z, x * y * z + z - 1 ∈ I := ⟨λ hx y, classical.by_cases (assume hxy : I ⊔ span {x * y + 1} = ⊤, let ⟨p, hpi, q, hq, hpq⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 hxy) in let ⟨r, hr⟩ := mem_span_singleton.1 hq in ⟨r, by rw [← one_mul r, ← mul_assoc, ← add_mul, mul_one, ← hr, ← hpq, ← neg_sub, add_sub_cancel]; exact I.neg_mem hpi⟩) (assume hxy : I ⊔ span {x * y + 1} ≠ ⊤, let ⟨M, hm1, hm2⟩ := exists_le_maximal _ hxy in suffices x ∉ M, from (this $ mem_Inf.1 hx ⟨le_trans le_sup_left hm2, hm1⟩).elim, λ hxm, hm1.1.1 $ (eq_top_iff_one _).2 $ add_sub_cancel' (x * y) 1 ▸ M.sub_mem (le_sup_right.trans hm2 $ mem_span_singleton.2 dvd_rfl) (M.mul_mem_right _ hxm)), λ hx, mem_Inf.2 $ λ M ⟨him, hm⟩, classical.by_contradiction $ λ hxm, let ⟨y, hy⟩ := hm.exists_inv hxm, ⟨z, hz⟩ := hx (-y) in hm.1.1 $ (eq_top_iff_one _).2 $ sub_sub_cancel (x * -y * z + z) 1 ▸ M.sub_mem (by { rw [← one_mul z, ← mul_assoc, ← add_mul, mul_one, mul_neg_eq_neg_mul_symm, neg_add_eq_sub, ← neg_sub, neg_mul_eq_neg_mul_symm, neg_mul_eq_mul_neg, mul_comm x y, mul_comm _ (- z)], rcases hy with ⟨i, hi, df⟩, rw [← (sub_eq_iff_eq_add.mpr df.symm), sub_sub, add_comm, ← sub_sub, sub_self, zero_sub], refine M.mul_mem_left (-z) ((neg_mem_iff _).mpr hi) }) (him hz)⟩ lemma exists_mul_sub_mem_of_sub_one_mem_jacobson {I : ideal R} (r : R) (h : r - 1 ∈ jacobson I) : ∃ s, r * s - 1 ∈ I := begin cases mem_jacobson_iff.1 h 1 with s hs, use s, simpa [sub_mul] using hs end lemma is_unit_of_sub_one_mem_jacobson_bot (r : R) (h : r - 1 ∈ jacobson (⊥ : ideal R)) : is_unit r := begin cases exists_mul_sub_mem_of_sub_one_mem_jacobson r h with s hs, rw [mem_bot, sub_eq_zero] at hs, exact is_unit_of_mul_eq_one _ _ hs end /-- An ideal equals its Jacobson radical iff it is the intersection of a set of maximal ideals. Allowing the set to include ⊤ is equivalent, and is included only to simplify some proofs. -/ theorem eq_jacobson_iff_Inf_maximal : I.jacobson = I ↔ ∃ M : set (ideal R), (∀ J ∈ M, is_maximal J ∨ J = ⊤) ∧ I = Inf M := begin use λ hI, ⟨{J : ideal R | I ≤ J ∧ J.is_maximal}, ⟨λ _ hJ, or.inl hJ.right, hI.symm⟩⟩, rintros ⟨M, hM, hInf⟩, refine le_antisymm (λ x hx, _) le_jacobson, rw [hInf, mem_Inf], intros I hI, cases hM I hI with is_max is_top, { exact (mem_Inf.1 hx) ⟨le_Inf_iff.1 (le_of_eq hInf) I hI, is_max⟩ }, { exact is_top.symm ▸ submodule.mem_top } end theorem eq_jacobson_iff_Inf_maximal' : I.jacobson = I ↔ ∃ M : set (ideal R), (∀ (J ∈ M) (K : ideal R), J < K → K = ⊤) ∧ I = Inf M := eq_jacobson_iff_Inf_maximal.trans ⟨λ h, let ⟨M, hM⟩ := h in ⟨M, ⟨λ J hJ K hK, or.rec_on (hM.1 J hJ) (λ h, h.1.2 K hK) (λ h, eq_top_iff.2 (le_of_lt (h ▸ hK))), hM.2⟩⟩, λ h, let ⟨M, hM⟩ := h in ⟨M, ⟨λ J hJ, or.rec_on (classical.em (J = ⊤)) (λ h, or.inr h) (λ h, or.inl ⟨⟨h, hM.1 J hJ⟩⟩), hM.2⟩⟩⟩ /-- An ideal `I` equals its Jacobson radical if and only if every element outside `I` also lies outside of a maximal ideal containing `I`. -/ lemma eq_jacobson_iff_not_mem : I.jacobson = I ↔ ∀ x ∉ I, ∃ M : ideal R, (I ≤ M ∧ M.is_maximal) ∧ x ∉ M := begin split, { intros h x hx, erw [← h, mem_Inf] at hx, push_neg at hx, exact hx }, { refine λ h, le_antisymm (λ x hx, _) le_jacobson, contrapose hx, erw mem_Inf, push_neg, exact h x hx } end theorem map_jacobson_of_surjective {f : R →+* S} (hf : function.surjective f) : ring_hom.ker f ≤ I → map f (I.jacobson) = (map f I).jacobson := begin intro h, unfold ideal.jacobson, have : ∀ J ∈ {J : ideal R | I ≤ J ∧ J.is_maximal}, f.ker ≤ J := λ J hJ, le_trans h hJ.left, refine trans (map_Inf hf this) (le_antisymm _ _), { refine Inf_le_Inf (λ J hJ, ⟨comap f J, ⟨⟨le_comap_of_map_le hJ.1, _⟩, map_comap_of_surjective f hf J⟩⟩), haveI : J.is_maximal := hJ.right, exact comap_is_maximal_of_surjective f hf }, { refine Inf_le_Inf_of_subset_insert_top (λ j hj, hj.rec_on (λ J hJ, _)), rw ← hJ.2, cases map_eq_top_or_is_maximal_of_surjective f hf hJ.left.right with htop hmax, { exact htop.symm ▸ set.mem_insert ⊤ _ }, { exact set.mem_insert_of_mem ⊤ ⟨map_mono hJ.1.1, hmax⟩ } }, end lemma map_jacobson_of_bijective {f : R →+* S} (hf : function.bijective f) : map f (I.jacobson) = (map f I).jacobson := map_jacobson_of_surjective hf.right (le_trans (le_of_eq (f.injective_iff_ker_eq_bot.1 hf.left)) bot_le) lemma comap_jacobson {f : R →+* S} {K : ideal S} : comap f (K.jacobson) = Inf (comap f '' {J : ideal S | K ≤ J ∧ J.is_maximal}) := trans (comap_Inf' f _) (Inf_eq_infi).symm theorem comap_jacobson_of_surjective {f : R →+* S} (hf : function.surjective f) {K : ideal S} : comap f (K.jacobson) = (comap f K).jacobson := begin unfold ideal.jacobson, refine le_antisymm _ _, { refine le_trans (comap_mono (le_of_eq (trans top_inf_eq.symm Inf_insert.symm))) _, rw [comap_Inf', Inf_eq_infi], refine infi_le_infi_of_subset (λ J hJ, _), have : comap f (map f J) = J := trans (comap_map_of_surjective f hf J) (le_antisymm (sup_le_iff.2 ⟨le_of_eq rfl, le_trans (comap_mono bot_le) hJ.left⟩) le_sup_left), cases map_eq_top_or_is_maximal_of_surjective _ hf hJ.right with htop hmax, { refine ⟨⊤, ⟨set.mem_insert ⊤ _, htop ▸ this⟩⟩ }, { refine ⟨map f J, ⟨set.mem_insert_of_mem _ ⟨le_map_of_comap_le_of_surjective f hf hJ.1, hmax⟩, this⟩⟩ } }, { rw comap_Inf, refine le_infi_iff.2 (λ J, (le_infi_iff.2 (λ hJ, _))), haveI : J.is_maximal := hJ.right, refine Inf_le ⟨comap_mono hJ.left, comap_is_maximal_of_surjective _ hf⟩ } end lemma mem_jacobson_bot {x : R} : x ∈ jacobson (⊥ : ideal R) ↔ ∀ y, is_unit (x * y + 1) := ⟨λ hx y, let ⟨z, hz⟩ := (mem_jacobson_iff.1 hx) y in is_unit_iff_exists_inv.2 ⟨z, by rwa [add_mul, one_mul, ← sub_eq_zero]⟩, λ h, mem_jacobson_iff.mpr (λ y, (let ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (h y) in ⟨b, (submodule.mem_bot R).2 (hb ▸ (by ring))⟩))⟩ /-- An ideal `I` of `R` is equal to its Jacobson radical if and only if the Jacobson radical of the quotient ring `R/I` is the zero ideal -/ theorem jacobson_eq_iff_jacobson_quotient_eq_bot : I.jacobson = I ↔ jacobson (⊥ : ideal (I.quotient)) = ⊥ := begin have hf : function.surjective (quotient.mk I) := submodule.quotient.mk_surjective I, split, { intro h, replace h := congr_arg (map (quotient.mk I)) h, rw map_jacobson_of_surjective hf (le_of_eq mk_ker) at h, simpa using h }, { intro h, replace h := congr_arg (comap (quotient.mk I)) h, rw [comap_jacobson_of_surjective hf, ← (quotient.mk I).ker_eq_comap_bot] at h, simpa using h } end /-- The standard radical and Jacobson radical of an ideal `I` of `R` are equal if and only if the nilradical and Jacobson radical of the quotient ring `R/I` coincide -/ theorem radical_eq_jacobson_iff_radical_quotient_eq_jacobson_bot : I.radical = I.jacobson ↔ radical (⊥ : ideal (I.quotient)) = jacobson ⊥ := begin have hf : function.surjective (quotient.mk I) := submodule.quotient.mk_surjective I, split, { intro h, have := congr_arg (map (quotient.mk I)) h, rw [map_radical_of_surjective hf (le_of_eq mk_ker), map_jacobson_of_surjective hf (le_of_eq mk_ker)] at this, simpa using this }, { intro h, have := congr_arg (comap (quotient.mk I)) h, rw [comap_radical, comap_jacobson_of_surjective hf, ← (quotient.mk I).ker_eq_comap_bot] at this, simpa using this } end @[mono] lemma jacobson_mono {I J : ideal R} : I ≤ J → I.jacobson ≤ J.jacobson := begin intros h x hx, erw mem_Inf at ⊢ hx, exact λ K ⟨hK, hK_max⟩, hx ⟨trans h hK, hK_max⟩ end lemma jacobson_radical_eq_jacobson : I.radical.jacobson = I.jacobson := le_antisymm (le_trans (le_of_eq (congr_arg jacobson (radical_eq_Inf I))) (Inf_le_Inf (λ J hJ, ⟨Inf_le ⟨hJ.1, hJ.2.is_prime⟩, hJ.2⟩))) (jacobson_mono le_radical) end jacobson section polynomial open polynomial lemma jacobson_bot_polynomial_le_Inf_map_maximal : jacobson (⊥ : ideal (polynomial R)) ≤ Inf (map C '' {J : ideal R | J.is_maximal}) := begin refine le_Inf (λ J, exists_imp_distrib.2 (λ j hj, _)), haveI : j.is_maximal := hj.1, refine trans (jacobson_mono bot_le) (le_of_eq _ : J.jacobson ≤ J), suffices : (⊥ : ideal (polynomial j.quotient)).jacobson = ⊥, { rw [← hj.2, jacobson_eq_iff_jacobson_quotient_eq_bot], replace this := congr_arg (map (polynomial_quotient_equiv_quotient_polynomial j).to_ring_hom) this, rwa [map_jacobson_of_bijective _, map_bot] at this, exact (ring_equiv.bijective (polynomial_quotient_equiv_quotient_polynomial j)) }, refine eq_bot_iff.2 (λ f hf, _), simpa [(λ hX, by simpa using congr_arg (λ f, coeff f 1) hX : (X : polynomial j.quotient) ≠ 0)] using eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit ((mem_jacobson_bot.1 hf) X)), end lemma jacobson_bot_polynomial_of_jacobson_bot (h : jacobson (⊥ : ideal R) = ⊥) : jacobson (⊥ : ideal (polynomial R)) = ⊥ := begin refine eq_bot_iff.2 (le_trans jacobson_bot_polynomial_le_Inf_map_maximal _), refine (λ f hf, ((submodule.mem_bot _).2 (polynomial.ext (λ n, trans _ (coeff_zero n).symm)))), suffices : f.coeff n ∈ ideal.jacobson ⊥, by rwa [h, submodule.mem_bot] at this, exact mem_Inf.2 (λ j hj, (mem_map_C_iff.1 ((mem_Inf.1 hf) ⟨j, ⟨hj.2, rfl⟩⟩)) n), end end polynomial section is_local /-- An ideal `I` is local iff its Jacobson radical is maximal. -/ class is_local (I : ideal R) : Prop := (out : is_maximal (jacobson I)) theorem is_local_iff {I : ideal R} : is_local I ↔ is_maximal (jacobson I) := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem is_local_of_is_maximal_radical {I : ideal R} (hi : is_maximal (radical I)) : is_local I := ⟨have radical I = jacobson I, from le_antisymm (le_Inf $ λ M ⟨him, hm⟩, hm.is_prime.radical_le_iff.2 him) (Inf_le ⟨le_radical, hi⟩), show is_maximal (jacobson I), from this ▸ hi⟩ theorem is_local.le_jacobson {I J : ideal R} (hi : is_local I) (hij : I ≤ J) (hj : J ≠ ⊤) : J ≤ jacobson I := let ⟨M, hm, hjm⟩ := exists_le_maximal J hj in le_trans hjm $ le_of_eq $ eq.symm $ hi.1.eq_of_le hm.1.1 $ Inf_le ⟨le_trans hij hjm, hm⟩ theorem is_local.mem_jacobson_or_exists_inv {I : ideal R} (hi : is_local I) (x : R) : x ∈ jacobson I ∨ ∃ y, y * x - 1 ∈ I := classical.by_cases (assume h : I ⊔ span {x} = ⊤, let ⟨p, hpi, q, hq, hpq⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 h) in let ⟨r, hr⟩ := mem_span_singleton.1 hq in or.inr ⟨r, by rw [← hpq, mul_comm, ← hr, ← neg_sub, add_sub_cancel]; exact I.neg_mem hpi⟩) (assume h : I ⊔ span {x} ≠ ⊤, or.inl $ le_trans le_sup_right (hi.le_jacobson le_sup_left h) $ mem_span_singleton.2 $ dvd_refl x) end is_local theorem is_primary_of_is_maximal_radical {I : ideal R} (hi : is_maximal (radical I)) : is_primary I := have radical I = jacobson I, from le_antisymm (le_Inf $ λ M ⟨him, hm⟩, hm.is_prime.radical_le_iff.2 him) (Inf_le ⟨le_radical, hi⟩), ⟨ne_top_of_lt $ lt_of_le_of_lt le_radical (lt_top_iff_ne_top.2 hi.1.1), λ x y hxy, ((is_local_of_is_maximal_radical hi).mem_jacobson_or_exists_inv y).symm.imp (λ ⟨z, hz⟩, by rw [← mul_one x, ← sub_sub_cancel (z * y) 1, mul_sub, mul_left_comm]; exact I.sub_mem (I.mul_mem_left _ hxy) (I.mul_mem_left _ hz)) (this ▸ id)⟩ end ideal
c159c88d11354d3b3d7dba6d07a741aad05599cd
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/functor_category.lean
2cec3e18b0d3370ca93086fde6027a3eaddc4dd5
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
4,152
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.natural_transformation namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation open nat_trans category category_theory.functor variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] local attribute [simp] vcomp_app /-- `functor.category C D` gives the category structure on functors and natural transformations between categories `C` and `D`. Notice that if `C` and `D` are both small categories at the same universe level, this is another small category at that level. However if `C` and `D` are both large categories at the same universe level, this is a small category at the next higher level. -/ instance functor.category : category.{(max u₁ v₂)} (C ⥤ D) := { hom := λ F G, nat_trans F G, id := λ F, nat_trans.id F, comp := λ _ _ _ α β, vcomp α β } variables {C D} {E : Type u₃} [category.{v₃} E] variables {F G H I : C ⥤ D} namespace nat_trans @[simp] lemma vcomp_eq_comp (α : F ⟶ G) (β : G ⟶ H) : vcomp α β = α ≫ β := rfl lemma vcomp_app' (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = (α.app X) ≫ (β.app X) := rfl lemma congr_app {α β : F ⟶ G} (h : α = β) (X : C) : α.app X = β.app X := by rw h @[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟶ F).app X = 𝟙 (F.obj X) := rfl @[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = α.app X ≫ β.app X := rfl lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (X : C) {Y Z : D} (f : Y ⟶ Z) : ((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) := (T.app X).naturality f lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (Z : D) {X Y : C} (f : X ⟶ Y) : ((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) := congr_fun (congr_arg app (T.naturality f)) Z /-- `hcomp α β` is the horizontal composition of natural transformations. -/ def hcomp {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) : (F ⋙ H) ⟶ (G ⋙ I) := { app := λ X : C, (β.app (F.obj X)) ≫ (I.map (α.app X)), naturality' := λ X Y f, begin rw [functor.comp_map, functor.comp_map, ←assoc, naturality, assoc, ←map_comp I, naturality, map_comp, assoc] end } infix ` ◫ `:80 := hcomp @[simp] lemma hcomp_app {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) (X : C) : (α ◫ β).app X = (β.app (F.obj X)) ≫ (I.map (α.app X)) := rfl -- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we -- need to use associativity of functor composition. (It's true without the explicit associator, -- because functor composition is definitionally associative, but relying on the definitional equality -- causes bad problems with elaboration later.) lemma exchange {I J K : D ⥤ E} (α : F ⟶ G) (β : G ⟶ H) (γ : I ⟶ J) (δ : J ⟶ K) : (α ≫ β) ◫ (γ ≫ δ) = (α ◫ γ) ≫ (β ◫ δ) := by ext; simp end nat_trans open nat_trans namespace functor /-- Flip the arguments of a bifunctor. See also `currying.lean`. -/ protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) := { obj := λ k, { obj := λ j, (F.obj j).obj k, map := λ j j' f, (F.map f).app k, map_id' := λ X, begin rw category_theory.functor.map_id, refl end, map_comp' := λ X Y Z f g, by rw [map_comp, ←comp_app] }, map := λ c c' f, { app := λ j, (F.obj j).map f } }. @[simp] lemma flip_obj_obj (F : C ⥤ (D ⥤ E)) (c) (d) : (F.flip.obj d).obj c = (F.obj c).obj d := rfl @[simp] lemma flip_obj_map (F : C ⥤ (D ⥤ E)) {c c' : C} (f : c ⟶ c') (d : D) : (F.flip.obj d).map f = (F.map f).app d := rfl @[simp] lemma flip_map_app (F : C ⥤ (D ⥤ E)) {d d' : D} (f : d ⟶ d') (c : C) : (F.flip.map f).app c = (F.obj c).map f := rfl end functor end category_theory
137567860fdb374af21b67e989efbed66be5d278
df561f413cfe0a88b1056655515399c546ff32a5
/8-inequality-world/l7.lean
e15f2e5651405df77392d114c5be4f7b7d367034
[]
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
123
lean
lemma le_zero (a : mynat) (h : a ≤ 0) : a = 0 := begin cases h with b hd, symmetry at hd, exact add_right_eq_zero hd, end
0aa4aec02ac341f3acf8955662a49cfca8f677ea
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/mynat/fact.lean
a17d98a9a658c487356fd97be8df5abe2d8f25e8
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
2,217
lean
-- vim: ts=2 sw=0 sts=-1 et ai tw=70 import .dvd import .induction namespace hidden open mynat def fact: mynat → mynat | 0 := 1 | (succ n) := (fact n) * (succ n) variables {m n p k : mynat} @[simp] theorem fact_zero: fact 0 = 1 := rfl @[simp] theorem fact_succ: fact (succ n) = (fact n) * (succ n) := rfl theorem fact_nzero: fact m ≠ 0 := begin assume h, induction m with n hn, cases h, -- Magic? rw [fact_succ, mul_comm] at h, have := @mul_integral (succ n) (fact n), from hn (this succ_ne_zero h), end theorem fact_dvd_self {m : mynat}: m ≠ 0 → m ∣ fact m := begin assume hneq0, cases m, { simp at hneq0, contradiction, }, { existsi (fact m), refl, }, end theorem fact_dvd_succ (m : mynat): fact m ∣ fact (succ m) := begin existsi (succ m), rw mul_comm, refl, end theorem fact_dvd_le {m n : mynat}: m ≤ n → fact m ∣ fact n := begin assume hmlen, cases hmlen with k hk, revert n, induction k with k_n k_ih, { intro n, assume hmn, simp [hmn], }, { intro n, assume hnmsk, cases k_ih rfl with a ha, existsi a * succ (m + k_n), rw [hnmsk, add_succ, fact_succ, ha, mul_assoc, mul_assoc, mul_comm (fact m)], }, end theorem fact_dvd_nlt {m n : mynat}: m ≠ 0 → m ≤ n → m ∣ fact n := begin assume hmne0, induction n with k hk, { assume hmle0, simp at hmle0, have hmeq0 := le_zero hmle0, exfalso, contradiction, }, { assume hmlesucc, have hmself := fact_dvd_self hmne0, have hfmfsucc := fact_dvd_le hmlesucc, from dvd_trans hmself hfmfsucc, }, end theorem fact_ndvd_lt: ∀ k: mynat, k ≠ 1 → k ≤ m → ¬(k ∣ (fact m) + 1) := begin assume n hneq1 hleqm hdiv, cases hdiv with k hk, have : n ∣ 1, { apply dvd_remainder (fact m) 1 (k * n) n, { have hnne0 : n ≠ 0, { assume hn0, rw [hn0, mul_zero, add_comm] at hk, have : (1: mynat) = (0: mynat), { apply add_integral hk, }, cases this, }, from fact_dvd_nlt hnne0 hleqm, }, { rw mul_comm, apply dvd_mul k, refl, }, assumption, }, from hneq1 (dvd_one this), end end hidden
4c3067947774f2204281df22e5359bc95b59a40a
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/Tactic/Conv/Delta.lean
032618c08a92f2173f53a3feb90f8c965260750a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
535
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.Elab.Tactic.Delta import Lean.Elab.Tactic.Conv.Basic namespace Lean.Elab.Tactic.Conv open Meta @[builtinTactic Lean.Parser.Tactic.Conv.delta] def evalDelta : Tactic := fun stx => withMainContext do let declName ← resolveGlobalConstNoOverload stx[1] let lhsNew ← deltaExpand (← getLhs) (. == declName) changeLhs lhsNew end Lean.Elab.Tactic.Conv
7ed84eaf2bbecb16324111081a8c74263640057e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/vector_bundle/hom.lean
051ba15bccd8d7a640cb670b4966b93ca31f1d86
[ "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
16,086
lean
/- Copyright © 2022 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth, Floris van Doorn -/ import topology.vector_bundle.basic import analysis.normed_space.operator_norm /-! # The vector bundle of continuous (semi)linear maps We define the (topological) vector bundle of continuous (semi)linear maps between two vector bundles over the same base. Given bundles `E₁ E₂ : B → Type*`, normed spaces `F₁` and `F₂`, and a ring-homomorphism `σ` between their respective scalar fields, we define `bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x` to be a type synonym for `λ x, E₁ x →SL[σ] E₂ x`. If the `E₁` and `E₂` are vector bundles with model fibers `F₁` and `F₂`, then this will be a vector bundle with fiber `F₁ →SL[σ] F₂`. The topology is constructed from the trivializations for `E₁` and `E₂` and the norm-topology on the model fiber `F₁ →SL[𝕜] F₂` using the `vector_prebundle` construction. This is a bit awkward because it introduces a spurious (?) dependence on the normed space structure of the model fibre, rather than just its topological vector space structure; this might be fixable now that we have `continuous_linear_map.strong_topology`. Similar constructions should be possible (but are yet to be formalized) for tensor products of topological vector bundles, exterior algebras, and so on, where again the topology can be defined using a norm on the fiber model if this helps. ## Main Definitions * `bundle.continuous_linear_map.vector_bundle`: continuous semilinear maps between vector bundles form a vector bundle. -/ noncomputable theory open_locale bundle open bundle set continuous_linear_map section defs variables {𝕜₁ 𝕜₂ : Type*} [normed_field 𝕜₁] [normed_field 𝕜₂] variables (σ : 𝕜₁ →+* 𝕜₂) variables {B : Type*} variables (F₁ : Type*) (E₁ : B → Type*) [Π x, add_comm_monoid (E₁ x)] [Π x, module 𝕜₁ (E₁ x)] variables [Π x : B, topological_space (E₁ x)] variables (F₂ : Type*) (E₂ : B → Type*) [Π x, add_comm_monoid (E₂ x)] [Π x, module 𝕜₂ (E₂ x)] variables [Π x : B, topological_space (E₂ x)] include F₁ F₂ -- In this definition we require the scalar rings `𝕜₁` and `𝕜₂` to be normed fields, although -- something much weaker (maybe `comm_semiring`) would suffice mathematically -- this is because of -- a typeclass inference bug with pi-types: -- https://leanprover.zulipchat.com/#narrow/stream/116395-maths/topic/vector.20bundles.20--.20typeclass.20inference.20issue /-- The bundle of continuous `σ`-semilinear maps between the topological vector bundles `E₁` and `E₂`. This is a type synonym for `λ x, E₁ x →SL[σ] E₂ x`. We intentionally add `F₁` and `F₂` as arguments to this type, so that instances on this type (that depend on `F₁` and `F₂`) actually refer to `F₁` and `F₂`. -/ @[derive inhabited, nolint unused_arguments] def bundle.continuous_linear_map (x : B) : Type* := E₁ x →SL[σ] E₂ x instance bundle.continuous_linear_map.add_monoid_hom_class (x : B) : add_monoid_hom_class (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) (E₁ x) (E₂ x) := by delta_instance bundle.continuous_linear_map variables [Π x, has_continuous_add (E₂ x)] instance (x : B) : add_comm_monoid (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) := by delta_instance bundle.continuous_linear_map variables [∀ x, has_continuous_smul 𝕜₂ (E₂ x)] instance (x : B) : module 𝕜₂ (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) := by delta_instance bundle.continuous_linear_map end defs variables {𝕜₁ : Type*} [nontrivially_normed_field 𝕜₁] {𝕜₂ : Type*} [nontrivially_normed_field 𝕜₂] (σ : 𝕜₁ →+* 𝕜₂) [iσ : ring_hom_isometric σ] variables {B : Type*} [topological_space B] variables (F₁ : Type*) [normed_add_comm_group F₁] [normed_space 𝕜₁ F₁] (E₁ : B → Type*) [Π x, add_comm_monoid (E₁ x)] [Π x, module 𝕜₁ (E₁ x)] [topological_space (total_space E₁)] variables (F₂ : Type*) [normed_add_comm_group F₂][normed_space 𝕜₂ F₂] (E₂ : B → Type*) [Π x, add_comm_monoid (E₂ x)] [Π x, module 𝕜₂ (E₂ x)] [topological_space (total_space E₂)] variables {F₁ E₁ F₂ E₂} (e₁ e₁' : trivialization F₁ (π E₁)) (e₂ e₂' : trivialization F₂ (π E₂)) namespace pretrivialization include iσ /-- Assume `eᵢ` and `eᵢ'` are trivializations of the bundles `Eᵢ` over base `B` with fiber `Fᵢ` (`i ∈ {1,2}`), then `continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂'` is the coordinate change function between the two induced (pre)trivializations `pretrivialization.continuous_linear_map σ e₁ e₂` and `pretrivialization.continuous_linear_map σ e₁' e₂'` of `bundle.continuous_linear_map`. -/ def continuous_linear_map_coord_change [e₁.is_linear 𝕜₁] [e₁'.is_linear 𝕜₁] [e₂.is_linear 𝕜₂] [e₂'.is_linear 𝕜₂] (b : B) : (F₁ →SL[σ] F₂) →L[𝕜₂] F₁ →SL[σ] F₂ := ((e₁'.coord_changeL 𝕜₁ e₁ b).symm.arrow_congrSL (e₂.coord_changeL 𝕜₂ e₂' b) : (F₁ →SL[σ] F₂) ≃L[𝕜₂] F₁ →SL[σ] F₂) variables {σ e₁ e₁' e₂ e₂'} variables [Π x : B, topological_space (E₁ x)] [fiber_bundle F₁ E₁] variables [Π x : B, topological_space (E₂ x)] [fiber_bundle F₂ E₂] lemma continuous_on_continuous_linear_map_coord_change [vector_bundle 𝕜₁ F₁ E₁] [vector_bundle 𝕜₂ F₂ E₂] [mem_trivialization_atlas e₁] [mem_trivialization_atlas e₁'] [mem_trivialization_atlas e₂] [mem_trivialization_atlas e₂'] : continuous_on (continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂') ((e₁.base_set ∩ e₂.base_set) ∩ (e₁'.base_set ∩ e₂'.base_set)) := begin have h₁ := (compSL F₁ F₂ F₂ σ (ring_hom.id 𝕜₂)).continuous, have h₂ := (continuous_linear_map.flip (compSL F₁ F₁ F₂ (ring_hom.id 𝕜₁) σ)).continuous, have h₃ := (continuous_on_coord_change 𝕜₁ e₁' e₁), have h₄ := (continuous_on_coord_change 𝕜₂ e₂ e₂'), refine ((h₁.comp_continuous_on (h₄.mono _)).clm_comp (h₂.comp_continuous_on (h₃.mono _))).congr _, { mfld_set_tac }, { mfld_set_tac }, { intros b hb, ext L v, simp only [continuous_linear_map_coord_change, continuous_linear_equiv.coe_coe, continuous_linear_equiv.arrow_congrSL_apply, comp_apply, function.comp, compSL_apply, flip_apply, continuous_linear_equiv.symm_symm] }, end omit iσ variables (σ e₁ e₁' e₂ e₂') [e₁.is_linear 𝕜₁] [e₁'.is_linear 𝕜₁] [e₂.is_linear 𝕜₂] [e₂'.is_linear 𝕜₂] /-- Given trivializations `e₁`, `e₂` for vector bundles `E₁`, `E₂` over a base `B`, `pretrivialization.continuous_linear_map σ e₁ e₂` is the induced pretrivialization for the continuous `σ`-semilinear maps from `E₁` to `E₂`. That is, the map which will later become a trivialization, after the bundle of continuous semilinear maps is equipped with the right topological vector bundle structure. -/ def continuous_linear_map : pretrivialization (F₁ →SL[σ] F₂) (π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) := { to_fun := λ p, ⟨p.1, (e₂.continuous_linear_map_at 𝕜₂ p.1).comp $ p.2.comp $ e₁.symmL 𝕜₁ p.1⟩, inv_fun := λ p, ⟨p.1, (e₂.symmL 𝕜₂ p.1).comp $ p.2.comp $ e₁.continuous_linear_map_at 𝕜₁ p.1⟩, source := (bundle.total_space.proj) ⁻¹' (e₁.base_set ∩ e₂.base_set), target := (e₁.base_set ∩ e₂.base_set) ×ˢ set.univ, map_source' := λ ⟨x, L⟩ h, ⟨h, set.mem_univ _⟩, map_target' := λ ⟨x, f⟩ h, h.1, left_inv' := λ ⟨x, L⟩ ⟨h₁, h₂⟩, begin simp_rw [sigma.mk.inj_iff, eq_self_iff_true, heq_iff_eq, true_and], ext v, simp only [comp_apply, trivialization.symmL_continuous_linear_map_at, h₁, h₂] end, right_inv' := λ ⟨x, f⟩ ⟨⟨h₁, h₂⟩, _⟩, begin simp_rw [prod.mk.inj_iff, eq_self_iff_true, true_and], ext v, simp only [comp_apply, trivialization.continuous_linear_map_at_symmL, h₁, h₂] end, open_target := (e₁.open_base_set.inter e₂.open_base_set).prod is_open_univ, base_set := e₁.base_set ∩ e₂.base_set, open_base_set := e₁.open_base_set.inter e₂.open_base_set, source_eq := rfl, target_eq := rfl, proj_to_fun := λ ⟨x, f⟩ h, rfl } instance continuous_linear_map.is_linear [Π x, has_continuous_add (E₂ x)] [Π x, has_continuous_smul 𝕜₂ (E₂ x)] : (pretrivialization.continuous_linear_map σ e₁ e₂).is_linear 𝕜₂ := { linear := λ x h, { map_add := λ L L', show (e₂.continuous_linear_map_at 𝕜₂ x).comp ((L + L').comp (e₁.symmL 𝕜₁ x)) = _, begin simp_rw [add_comp, comp_add], refl end, map_smul := λ c L, show (e₂.continuous_linear_map_at 𝕜₂ x).comp ((c • L).comp (e₁.symmL 𝕜₁ x)) = _, begin simp_rw [smul_comp, comp_smulₛₗ, ring_hom.id_apply], refl end, } } lemma continuous_linear_map_apply (p : total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) : (continuous_linear_map σ e₁ e₂) p = ⟨p.1, (e₂.continuous_linear_map_at 𝕜₂ p.1).comp $ p.2.comp $ e₁.symmL 𝕜₁ p.1⟩ := rfl lemma continuous_linear_map_symm_apply (p : B × (F₁ →SL[σ] F₂)) : (continuous_linear_map σ e₁ e₂).to_local_equiv.symm p = ⟨p.1, (e₂.symmL 𝕜₂ p.1).comp $ p.2.comp $ e₁.continuous_linear_map_at 𝕜₁ p.1⟩ := rfl variables [Π x, has_continuous_add (E₂ x)] lemma continuous_linear_map_symm_apply' {b : B} (hb : b ∈ e₁.base_set ∩ e₂.base_set) (L : F₁ →SL[σ] F₂) : (continuous_linear_map σ e₁ e₂).symm b L = (e₂.symmL 𝕜₂ b).comp (L.comp $ e₁.continuous_linear_map_at 𝕜₁ b) := begin rw [symm_apply], refl, exact hb end lemma continuous_linear_map_coord_change_apply [ring_hom_isometric σ] (b : B) (hb : b ∈ (e₁.base_set ∩ e₂.base_set) ∩ (e₁'.base_set ∩ e₂'.base_set)) (L : F₁ →SL[σ] F₂) : continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂' b L = (continuous_linear_map σ e₁' e₂' (total_space_mk b ((continuous_linear_map σ e₁ e₂).symm b L))).2 := begin ext v, simp_rw [continuous_linear_map_coord_change, continuous_linear_equiv.coe_coe, continuous_linear_equiv.arrow_congrSL_apply, continuous_linear_map_apply, continuous_linear_map_symm_apply' σ e₁ e₂ hb.1, comp_apply, continuous_linear_equiv.coe_coe, continuous_linear_equiv.symm_symm, trivialization.continuous_linear_map_at_apply, trivialization.symmL_apply], dsimp only [total_space_mk], rw [e₂.coord_changeL_apply e₂', e₁'.coord_changeL_apply e₁, e₁.coe_linear_map_at_of_mem hb.1.1, e₂'.coe_linear_map_at_of_mem hb.2.2], exacts [⟨hb.2.1, hb.1.1⟩, ⟨hb.1.2, hb.2.2⟩] end end pretrivialization open pretrivialization variables (F₁ E₁ F₂ E₂) [ring_hom_isometric σ] variables [Π x : B, topological_space (E₁ x)] [fiber_bundle F₁ E₁] [vector_bundle 𝕜₁ F₁ E₁] variables [Π x : B, topological_space (E₂ x)] [fiber_bundle F₂ E₂] [vector_bundle 𝕜₂ F₂ E₂] variables [Π x, has_continuous_add (E₂ x)] [Π x, has_continuous_smul 𝕜₂ (E₂ x)] /-- The continuous `σ`-semilinear maps between two topological vector bundles form a `vector_prebundle` (this is an auxiliary construction for the `vector_bundle` instance, in which the pretrivializations are collated but no topology on the total space is yet provided). -/ def _root_.bundle.continuous_linear_map.vector_prebundle : vector_prebundle 𝕜₂ (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) := { pretrivialization_atlas := {e | ∃ (e₁ : trivialization F₁ (π E₁)) (e₂ : trivialization F₂ (π E₂)) [mem_trivialization_atlas e₁] [mem_trivialization_atlas e₂], by exactI e = pretrivialization.continuous_linear_map σ e₁ e₂}, pretrivialization_linear' := begin rintro _ ⟨e₁, he₁, e₂, he₂, rfl⟩, apply_instance end, pretrivialization_at := λ x, pretrivialization.continuous_linear_map σ (trivialization_at F₁ E₁ x) (trivialization_at F₂ E₂ x), mem_base_pretrivialization_at := λ x, ⟨mem_base_set_trivialization_at F₁ E₁ x, mem_base_set_trivialization_at F₂ E₂ x⟩, pretrivialization_mem_atlas := λ x, ⟨trivialization_at F₁ E₁ x, trivialization_at F₂ E₂ x, _, _, rfl⟩, exists_coord_change := by { rintro _ ⟨e₁, e₂, he₁, he₂, rfl⟩ _ ⟨e₁', e₂', he₁', he₂', rfl⟩, resetI, exact ⟨continuous_linear_map_coord_change σ e₁ e₁' e₂ e₂', continuous_on_continuous_linear_map_coord_change, continuous_linear_map_coord_change_apply σ e₁ e₁' e₂ e₂'⟩ } } /-- Topology on the continuous `σ`-semilinear_maps between the respective fibers at a point of two "normable" vector bundles over the same base. Here "normable" means that the bundles have fibers modelled on normed spaces `F₁`, `F₂` respectively. The topology we put on the continuous `σ`-semilinear_maps is the topology coming from the operator norm on maps from `F₁` to `F₂`. -/ instance (x : B) : topological_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂ x) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).fiber_topology x /-- Topology on the total space of the continuous `σ`-semilinear_maps between two "normable" vector bundles over the same base. -/ instance bundle.continuous_linear_map.topological_space_total_space : topological_space (total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).total_space_topology /-- The continuous `σ`-semilinear_maps between two vector bundles form a fiber bundle. -/ instance _root_.bundle.continuous_linear_map.fiber_bundle : fiber_bundle (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).to_fiber_bundle /-- The continuous `σ`-semilinear_maps between two vector bundles form a vector bundle. -/ instance _root_.bundle.continuous_linear_map.vector_bundle : vector_bundle 𝕜₂ (F₁ →SL[σ] F₂) (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂) := (bundle.continuous_linear_map.vector_prebundle σ F₁ E₁ F₂ E₂).to_vector_bundle variables (e₁ e₂) [he₁ : mem_trivialization_atlas e₁] [he₂ : mem_trivialization_atlas e₂] {F₁ E₁ F₂ E₂} include he₁ he₂ /-- Given trivializations `e₁`, `e₂` in the atlas for vector bundles `E₁`, `E₂` over a base `B`, the induced trivialization for the continuous `σ`-semilinear maps from `E₁` to `E₂`, whose base set is `e₁.base_set ∩ e₂.base_set`. -/ def trivialization.continuous_linear_map : trivialization (F₁ →SL[σ] F₂) (π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) := vector_prebundle.trivialization_of_mem_pretrivialization_atlas _ ⟨e₁, e₂, he₁, he₂, rfl⟩ instance _root_.bundle.continuous_linear_map.mem_trivialization_atlas : mem_trivialization_atlas (e₁.continuous_linear_map σ e₂ : trivialization (F₁ →SL[σ] F₂) (π (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂))) := { out := ⟨_, ⟨e₁, e₂, by apply_instance, by apply_instance, rfl⟩, rfl⟩ } variables {e₁ e₂} @[simp] lemma trivialization.base_set_continuous_linear_map : (e₁.continuous_linear_map σ e₂).base_set = e₁.base_set ∩ e₂.base_set := rfl lemma trivialization.continuous_linear_map_apply (p : total_space (bundle.continuous_linear_map σ F₁ E₁ F₂ E₂)) : e₁.continuous_linear_map σ e₂ p = ⟨p.1, (e₂.continuous_linear_map_at 𝕜₂ p.1).comp $ p.2.comp $ e₁.symmL 𝕜₁ p.1⟩ := rfl
a3458ac4cacb6df16b5c387895af50102e872066
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/torsion.lean
343ef3612fce0789b44c072074a1915d2e4b63db
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
14,891
lean
/- Copyright (c) 2022 Julian Berman. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Berman -/ import group_theory.exponent import group_theory.order_of_element import group_theory.p_group import group_theory.quotient_group import group_theory.submonoid.operations /-! # Torsion groups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines torsion groups, i.e. groups where all elements have finite order. ## Main definitions * `monoid.is_torsion` a predicate asserting `G` is torsion, i.e. that all elements are of finite order. * `comm_group.torsion G`, the torsion subgroup of an abelian group `G` * `comm_monoid.torsion G`, the above stated for commutative monoids * `monoid.is_torsion_free`, asserting no nontrivial elements have finite order in `G` * `add_monoid.is_torsion` and `add_monoid.is_torsion_free` the additive versions of the above ## Implementation All torsion monoids are really groups (which is proven here as `monoid.is_torsion.group`), but since the definition can be stated on monoids it is implemented on `monoid` to match other declarations in the group theory library. ## Tags periodic group, aperiodic group, torsion subgroup, torsion abelian group ## Future work * generalize to π-torsion(-free) groups for a set of primes π * free, free solvable and free abelian groups are torsion free * complete direct and free products of torsion free groups are torsion free * groups which are residually finite p-groups with respect to 2 distinct primes are torsion free -/ variables {G H : Type*} namespace monoid variables (G) [monoid G] /-- A predicate on a monoid saying that all elements are of finite order. -/ @[to_additive "A predicate on an additive monoid saying that all elements are of finite order."] def is_torsion := ∀ g : G, is_of_fin_order g /-- A monoid is not a torsion monoid if it has an element of infinite order. -/ @[simp, to_additive "An additive monoid is not a torsion monoid if it has an element of infinite order."] lemma not_is_torsion_iff : ¬ is_torsion G ↔ ∃ g : G, ¬is_of_fin_order g := by rw [is_torsion, not_forall] end monoid open monoid /-- Torsion monoids are really groups. -/ @[to_additive "Torsion additive monoids are really additive groups"] noncomputable def is_torsion.group [monoid G] (tG : is_torsion G) : group G := { inv := λ g, g ^ (order_of g - 1), mul_left_inv := λ g, begin erw [←pow_succ', tsub_add_cancel_of_le, pow_order_of_eq_one], exact order_of_pos' (tG g) end, ..‹monoid G› } section group variables [group G] {N : subgroup G} [group H] /-- Subgroups of torsion groups are torsion groups. -/ @[to_additive "Subgroups of additive torsion groups are additive torsion groups."] lemma is_torsion.subgroup (tG : is_torsion G) (H : subgroup G) : is_torsion H := λ h, (is_of_fin_order_iff_coe H.to_submonoid h).mpr $ tG h /-- The image of a surjective torsion group homomorphism is torsion. -/ @[to_additive add_is_torsion.of_surjective "The image of a surjective additive torsion group homomorphism is torsion."] lemma is_torsion.of_surjective {f : G →* H} (hf : function.surjective f) (tG : is_torsion G) : is_torsion H := λ h, begin obtain ⟨g, hg⟩ := hf h, rw ←hg, exact f.is_of_fin_order (tG g), end /-- Torsion groups are closed under extensions. -/ @[to_additive add_is_torsion.extension_closed "Additive torsion groups are closed under extensions."] lemma is_torsion.extension_closed {f : G →* H} (hN : N = f.ker) (tH : is_torsion H) (tN : is_torsion N) : is_torsion G := λ g, (is_of_fin_order_iff_pow_eq_one _).mpr $ begin obtain ⟨ngn, ngnpos, hngn⟩ := (is_of_fin_order_iff_pow_eq_one _).mp (tH $ f g), have hmem := f.mem_ker.mpr ((f.map_pow g ngn).trans hngn), lift g ^ ngn to N using hN.symm ▸ hmem with gn, obtain ⟨nn, nnpos, hnn⟩ := (is_of_fin_order_iff_pow_eq_one _).mp (tN gn), exact ⟨ngn * nn, mul_pos ngnpos nnpos, by rw [pow_mul, ←h, ←subgroup.coe_pow, hnn, subgroup.coe_one]⟩ end /-- The image of a quotient is torsion iff the group is torsion. -/ @[to_additive add_is_torsion.quotient_iff "The image of a quotient is additively torsion iff the group is torsion."] lemma is_torsion.quotient_iff {f : G →* H} (hf : function.surjective f) (hN : N = f.ker) (tN : is_torsion N) : is_torsion H ↔ is_torsion G := ⟨λ tH, is_torsion.extension_closed hN tH tN, λ tG, is_torsion.of_surjective hf tG⟩ /-- If a group exponent exists, the group is torsion. -/ @[to_additive exponent_exists.is_add_torsion "If a group exponent exists, the group is additively torsion."] lemma exponent_exists.is_torsion (h : exponent_exists G) : is_torsion G := λ g, begin obtain ⟨n, npos, hn⟩ := h, exact (is_of_fin_order_iff_pow_eq_one g).mpr ⟨n, npos, hn g⟩, end /-- The group exponent exists for any bounded torsion group. -/ @[to_additive is_add_torsion.exponent_exists "The group exponent exists for any bounded additive torsion group."] lemma is_torsion.exponent_exists (tG : is_torsion G) (bounded : (set.range (λ g : G, order_of g)).finite) : exponent_exists G := exponent_exists_iff_ne_zero.mpr $ (exponent_ne_zero_iff_range_order_of_finite (λ g, order_of_pos' (tG g))).mpr bounded /-- Finite groups are torsion groups. -/ @[to_additive is_add_torsion_of_finite "Finite additive groups are additive torsion groups."] lemma is_torsion_of_finite [finite G] : is_torsion G := exponent_exists.is_torsion $ exponent_exists_iff_ne_zero.mpr exponent_ne_zero_of_finite end group section module -- A (semi/)ring of scalars and a commutative monoid of elements variables (R M : Type*) [add_comm_monoid M] namespace add_monoid /-- A module whose scalars are additively torsion is additively torsion. -/ lemma is_torsion.module_of_torsion [semiring R] [module R M] (tR : is_torsion R) : is_torsion M := λ f, (is_of_fin_add_order_iff_nsmul_eq_zero _).mpr $ begin obtain ⟨n, npos, hn⟩ := (is_of_fin_add_order_iff_nsmul_eq_zero _).mp (tR 1), exact ⟨n, npos, by simp only [nsmul_eq_smul_cast R _ f, ←nsmul_one, hn, zero_smul]⟩, end /-- A module with a finite ring of scalars is additively torsion. -/ lemma is_torsion.module_of_finite [ring R] [finite R] [module R M] : is_torsion M := (is_add_torsion_of_finite : is_torsion R).module_of_torsion _ _ end add_monoid end module section comm_monoid variables (G) [comm_monoid G] namespace comm_monoid /-- The torsion submonoid of a commutative monoid. (Note that by `monoid.is_torsion.group` torsion monoids are truthfully groups.) -/ @[to_additive add_torsion "The torsion submonoid of an additive commutative monoid."] def torsion : submonoid G := { carrier := {x | is_of_fin_order x}, one_mem' := is_of_fin_order_one, mul_mem' := λ _ _ hx hy, hx.mul hy } variable {G} /-- Torsion submonoids are torsion. -/ @[to_additive "Additive torsion submonoids are additively torsion."] lemma torsion.is_torsion : is_torsion $ torsion G := λ ⟨_, n, npos, hn⟩, ⟨n, npos, subtype.ext $ by rw [mul_left_iterate, _root_.mul_one, submonoid.coe_pow, subtype.coe_mk, submonoid.coe_one, (is_periodic_pt_mul_iff_pow_eq_one _).mp hn]⟩ variables (G) (p : ℕ) [hp : fact p.prime] include hp /-- The `p`-primary component is the submonoid of elements with order prime-power of `p`. -/ @[to_additive "The `p`-primary component is the submonoid of elements with additive order prime-power of `p`.", simps] def primary_component : submonoid G := { carrier := {g | ∃ n : ℕ, order_of g = p ^ n}, one_mem' := ⟨0, by rw [pow_zero, order_of_one]⟩, mul_mem' := λ g₁ g₂ hg₁ hg₂, exists_order_of_eq_prime_pow_iff.mpr $ begin obtain ⟨m, hm⟩ := exists_order_of_eq_prime_pow_iff.mp hg₁, obtain ⟨n, hn⟩ := exists_order_of_eq_prime_pow_iff.mp hg₂, exact ⟨m + n, by rw [mul_pow, pow_add, pow_mul, hm, one_pow, monoid.one_mul, mul_comm, pow_mul, hn, one_pow]⟩, end } variables {G} {p} /-- Elements of the `p`-primary component have order `p^n` for some `n`. -/ @[to_additive "Elements of the `p`-primary component have additive order `p^n` for some `n`"] lemma primary_component.exists_order_of_eq_prime_pow (g : comm_monoid.primary_component G p) : ∃ n : ℕ, order_of g = p ^ n := by simpa [primary_component] using g.property /-- The `p`- and `q`-primary components are disjoint for `p ≠ q`. -/ @[to_additive "The `p`- and `q`-primary components are disjoint for `p ≠ q`."] lemma primary_component.disjoint {p' : ℕ} [hp' : fact p'.prime] (hne : p ≠ p') : disjoint (comm_monoid.primary_component G p) (comm_monoid.primary_component G p') := submonoid.disjoint_def.mpr $ begin rintro g ⟨(_|n), hn⟩ ⟨n', hn'⟩, { rwa [pow_zero, order_of_eq_one_iff] at hn }, { exact absurd (eq_of_prime_pow_eq hp.out.prime hp'.out.prime n.succ_pos (hn.symm.trans hn')) hne } end end comm_monoid open comm_monoid (torsion) namespace monoid.is_torsion variable {G} /-- The torsion submonoid of a torsion monoid is `⊤`. -/ @[simp, to_additive "The additive torsion submonoid of an additive torsion monoid is `⊤`."] lemma torsion_eq_top (tG : is_torsion G) : torsion G = ⊤ := by ext; tauto /-- A torsion monoid is isomorphic to its torsion submonoid. -/ @[to_additive "An additive torsion monoid is isomorphic to its torsion submonoid."] def torsion_mul_equiv (tG : is_torsion G) : torsion G ≃* G := (mul_equiv.submonoid_congr tG.torsion_eq_top).trans submonoid.top_equiv @[to_additive] lemma torsion_mul_equiv_apply (tG : is_torsion G) (a : torsion G) : tG.torsion_mul_equiv a = mul_equiv.submonoid_congr tG.torsion_eq_top a := rfl @[to_additive] lemma torsion_mul_equiv_symm_apply_coe (tG : is_torsion G) (a : G) : tG.torsion_mul_equiv.symm a = ⟨submonoid.top_equiv.symm a, tG _⟩ := rfl end monoid.is_torsion /-- Torsion submonoids of a torsion submonoid are isomorphic to the submonoid. -/ @[simp, to_additive add_comm_monoid.torsion.of_torsion "Additive torsion submonoids of an additive torsion submonoid are isomorphic to the submonoid."] def torsion.of_torsion : (torsion (torsion G)) ≃* (torsion G) := monoid.is_torsion.torsion_mul_equiv comm_monoid.torsion.is_torsion end comm_monoid section comm_group variables (G) [comm_group G] namespace comm_group /-- The torsion subgroup of an abelian group. -/ @[to_additive "The torsion subgroup of an additive abelian group."] def torsion : subgroup G := { comm_monoid.torsion G with inv_mem' := λ x, is_of_fin_order.inv } /-- The torsion submonoid of an abelian group equals the torsion subgroup as a submonoid. -/ @[to_additive add_torsion_eq_add_torsion_submonoid "The additive torsion submonoid of an abelian group equals the torsion subgroup as a submonoid."] lemma torsion_eq_torsion_submonoid : comm_monoid.torsion G = (torsion G).to_submonoid := rfl variables (p : ℕ) [hp : fact p.prime] include hp /-- The `p`-primary component is the subgroup of elements with order prime-power of `p`. -/ @[to_additive "The `p`-primary component is the subgroup of elements with additive order prime-power of `p`.", simps] def primary_component : subgroup G := { comm_monoid.primary_component G p with inv_mem' := λ g ⟨n, hn⟩, ⟨n, (order_of_inv g).trans hn⟩ } variables {G} {p} /-- The `p`-primary component is a `p` group. -/ lemma primary_component.is_p_group : is_p_group p $ primary_component G p := λ g, (propext exists_order_of_eq_prime_pow_iff.symm).mpr (comm_monoid.primary_component.exists_order_of_eq_prime_pow g) end comm_group end comm_group namespace monoid variables (G) [monoid G] /-- A predicate on a monoid saying that only 1 is of finite order. -/ @[to_additive "A predicate on an additive monoid saying that only 0 is of finite order."] def is_torsion_free := ∀ g : G, g ≠ 1 → ¬is_of_fin_order g /-- A nontrivial monoid is not torsion-free if any nontrivial element has finite order. -/ @[simp, to_additive "An additive monoid is not torsion free if any nontrivial element has finite order."] lemma not_is_torsion_free_iff : ¬ (is_torsion_free G) ↔ ∃ g : G, g ≠ 1 ∧ is_of_fin_order g := by simp_rw [is_torsion_free, ne.def, not_forall, not_not, exists_prop] end monoid section group open monoid variables [group G] /-- A nontrivial torsion group is not torsion-free. -/ @[to_additive add_monoid.is_torsion.not_torsion_free "A nontrivial additive torsion group is not torsion-free."] lemma is_torsion.not_torsion_free [hN : nontrivial G] : is_torsion G → ¬is_torsion_free G := λ tG, (not_is_torsion_free_iff _).mpr $ begin obtain ⟨x, hx⟩ := (nontrivial_iff_exists_ne (1 : G)).mp hN, exact ⟨x, hx, tG x⟩, end /-- A nontrivial torsion-free group is not torsion. -/ @[to_additive add_monoid.is_torsion_free.not_torsion "A nontrivial torsion-free additive group is not torsion."] lemma is_torsion_free.not_torsion [hN : nontrivial G] : is_torsion_free G → ¬is_torsion G := λ tfG, (not_is_torsion_iff _).mpr $ begin obtain ⟨x, hx⟩ := (nontrivial_iff_exists_ne (1 : G)).mp hN, exact ⟨x, (tfG x) hx⟩, end /-- Subgroups of torsion-free groups are torsion-free. -/ @[to_additive "Subgroups of additive torsion-free groups are additively torsion-free."] lemma is_torsion_free.subgroup (tG : is_torsion_free G) (H : subgroup G) : is_torsion_free H := λ h hne, (is_of_fin_order_iff_coe H.to_submonoid h).not.mpr $ tG h $ by norm_cast; simp [hne, not_false_iff] /-- Direct products of torsion free groups are torsion free. -/ @[to_additive add_monoid.is_torsion_free.prod "Direct products of additive torsion free groups are torsion free."] lemma is_torsion_free.prod {η : Type*} {Gs : η → Type*} [∀ i, group (Gs i)] (tfGs : ∀ i, is_torsion_free (Gs i)) : is_torsion_free $ Π i, Gs i := λ w hne h, hne $ funext $ λ i, not_not.mp $ mt (tfGs i (w i)) $ not_not.mpr $ h.apply i end group section comm_group open monoid (is_torsion_free) open comm_group (torsion) variables (G) [comm_group G] /-- Quotienting a group by its torsion subgroup yields a torsion free group. -/ @[to_additive add_is_torsion_free.quotient_torsion "Quotienting a group by its additive torsion subgroup yields an additive torsion free group."] lemma is_torsion_free.quotient_torsion : is_torsion_free $ G ⧸ torsion G := λ g hne hfin, hne $ begin induction g using quotient_group.induction_on', obtain ⟨m, mpos, hm⟩ := (is_of_fin_order_iff_pow_eq_one _).mp hfin, obtain ⟨n, npos, hn⟩ := (is_of_fin_order_iff_pow_eq_one _).mp ((quotient_group.eq_one_iff _).mp hm), exact (quotient_group.eq_one_iff g).mpr ((is_of_fin_order_iff_pow_eq_one _).mpr ⟨m * n, mul_pos mpos npos, (pow_mul g m n).symm ▸ hn⟩), end end comm_group
9e349ab0b0c51e6d9dc41ef27196e392f9a89d6a
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Elab/Binders.lean
6ef90dd4454f2c738e9102cf9003b39824781e63
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
21,572
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Elab.Term import Init.Lean.Elab.Quotation namespace Lean namespace Elab namespace Term /-- Given syntax of the forms a) (`:` term)? b) `:` term return `term` if it is present, or a hole if not. -/ private def expandBinderType (stx : Syntax) : Syntax := if stx.getNumArgs == 0 then mkHole stx else stx.getArg 1 /-- Given syntax of the form `ident <|> hole`, return `ident`. If `hole`, then we create a new anonymous name. -/ private def expandBinderIdent (stx : Syntax) : TermElabM Syntax := match_syntax stx with | `(_) => mkFreshAnonymousIdent stx | _ => pure stx /-- Given syntax of the form `(ident >> " : ")?`, return `ident`, or a new instance name. -/ private def expandOptIdent (stx : Syntax) : TermElabM Syntax := if stx.getNumArgs == 0 then do id ← mkFreshInstanceName; pure $ mkIdentFrom stx id else pure $ stx.getArg 0 structure BinderView := (id : Syntax) (type : Syntax) (bi : BinderInfo) partial def quoteAutoTactic : Syntax → TermElabM Syntax | stx@(Syntax.ident _ _ _ _) => throwError stx "invalic auto tactic, identifier is not allowed" | stx@(Syntax.node k args) => if Quotation.isAntiquot stx then throwError stx "invalic auto tactic, antiquotation is not allowed" else do empty ← `(Array.empty); args ← args.foldlM (fun args arg => if k == nullKind && Quotation.isAntiquotSplice arg then throwError arg "invalic auto tactic, antiquotation is not allowed" else do arg ← quoteAutoTactic arg; `(Array.push $args $arg)) empty; `(Syntax.node $(quote k) $args) | Syntax.atom info val => `(Syntax.atom none $(quote val)) | Syntax.missing => unreachable! def declareTacticSyntax (tactic : Syntax) : TermElabM Name := withFreshMacroScope $ do name ← MonadQuotation.addMacroScope `_auto; let type := Lean.mkConst `Lean.Syntax; tactic ← quoteAutoTactic tactic; val ← elabTerm tactic type; val ← instantiateMVars tactic val; trace `Elab.autoParam tactic $ fun _ => val; let decl := Declaration.defnDecl { name := name, lparams := [], type := type, value := val, hints := ReducibilityHints.opaque, isUnsafe := false }; addDecl tactic decl; compileDecl tactic decl; pure name /- Expand `optional (binderTactic <|> binderDefault)` def binderTactic := parser! " := " >> " by " >> tacticParser def binderDefault := parser! " := " >> termParser -/ private def expandBinderModifier (type : Syntax) (optBinderModifier : Syntax) : TermElabM Syntax := if optBinderModifier.isNone then pure type else let modifier := optBinderModifier.getArg 0; let kind := modifier.getKind; if kind == `Lean.Parser.Term.binderDefault then do let defaultVal := modifier.getArg 1; `(optParam $type $defaultVal) else if kind == `Lean.Parser.Term.binderTactic then do let tac := modifier.getArg 2; name ← declareTacticSyntax tac; `(autoParam $type $(mkTermIdFrom tac name)) else throwUnsupportedSyntax private def getBinderIds (ids : Syntax) : TermElabM (Array Syntax) := ids.getArgs.mapM $ fun id => let k := id.getKind; if k == identKind || k == `Lean.Parser.Term.hole then pure id else if k == `Lean.Parser.Term.id && id.getArgs.size == 2 && (id.getArg 1).isNone then -- The parser never generates this case, but it is convenient when writting macros. pure (id.getArg 0) else throwError id "identifier or `_` expected" private def matchBinder (stx : Syntax) : TermElabM (Array BinderView) := match stx with | Syntax.node k args => if k == `Lean.Parser.Term.simpleBinder then do -- binderIdent+ ids ← getBinderIds (args.get! 0); let type := mkHole stx; ids.mapM $ fun id => do id ← expandBinderIdent id; pure { id := id, type := type, bi := BinderInfo.default } else if k == `Lean.Parser.Term.explicitBinder then do -- `(` binderIdent+ binderType (binderDefault <|> binderTactic)? `)` ids ← getBinderIds (args.get! 1); let type := expandBinderType (args.get! 2); let optModifier := args.get! 3; type ← expandBinderModifier type optModifier; ids.mapM $ fun id => do id ← expandBinderIdent id; pure { id := id, type := type, bi := BinderInfo.default } else if k == `Lean.Parser.Term.implicitBinder then do -- `{` binderIdent+ binderType `}` ids ← getBinderIds (args.get! 1); let type := expandBinderType (args.get! 2); ids.mapM $ fun id => do id ← expandBinderIdent id; pure { id := id, type := type, bi := BinderInfo.implicit } else if k == `Lean.Parser.Term.instBinder then do -- `[` optIdent type `]` id ← expandOptIdent (args.get! 1); let type := args.get! 2; pure #[ { id := id, type := type, bi := BinderInfo.instImplicit } ] else throwUnsupportedSyntax | _ => throwUnsupportedSyntax private partial def elabBinderViews (binderViews : Array BinderView) : Nat → Array Expr → LocalContext → LocalInstances → TermElabM (Array Expr × LocalContext × LocalInstances) | i, fvars, lctx, localInsts => if h : i < binderViews.size then let binderView := binderViews.get ⟨i, h⟩; withLCtx lctx localInsts $ do type ← elabType binderView.type; fvarId ← mkFreshFVarId; let fvar := mkFVar fvarId; let fvars := fvars.push fvar; -- dbgTrace (toString binderView.id.getId ++ " : " ++ toString type); let lctx := lctx.mkLocalDecl fvarId binderView.id.getId type binderView.bi; className? ← isClass binderView.type type; match className? with | none => elabBinderViews (i+1) fvars lctx localInsts | some className => do resetSynthInstanceCache; let localInsts := localInsts.push { className := className, fvar := mkFVar fvarId }; elabBinderViews (i+1) fvars lctx localInsts else pure (fvars, lctx, localInsts) private partial def elabBindersAux (binders : Array Syntax) : Nat → Array Expr → LocalContext → LocalInstances → TermElabM (Array Expr × LocalContext × LocalInstances) | i, fvars, lctx, localInsts => if h : i < binders.size then do binderViews ← matchBinder (binders.get ⟨i, h⟩); (fvars, lctx, localInsts) ← elabBinderViews binderViews 0 fvars lctx localInsts; elabBindersAux (i+1) fvars lctx localInsts else pure (fvars, lctx, localInsts) /-- Elaborate the given binders (i.e., `Syntax` objects for `simpleBinder <|> bracktedBinder`), update the local context, set of local instances, reset instance chache (if needed), and then execute `x` with the updated context. -/ def elabBinders {α} (binders : Array Syntax) (x : Array Expr → TermElabM α) : TermElabM α := if binders.isEmpty then x #[] else do lctx ← getLCtx; localInsts ← getLocalInsts; (fvars, lctx, newLocalInsts) ← elabBindersAux binders 0 #[] lctx localInsts; resettingSynthInstanceCacheWhen (newLocalInsts.size > localInsts.size) $ withLCtx lctx newLocalInsts $ x fvars @[inline] def elabBinder {α} (binder : Syntax) (x : Expr → TermElabM α) : TermElabM α := elabBinders #[binder] (fun fvars => x (fvars.get! 0)) @[builtinTermElab «forall»] def elabForall : TermElab := fun stx _ => match_syntax stx with | `(forall $binders*, $term) => elabBinders binders $ fun xs => do e ← elabType term; mkForall stx xs e | _ => throwUnsupportedSyntax @[builtinTermElab arrow] def elabArrow : TermElab := adaptExpander $ fun stx => match_syntax stx with | `($dom:term -> $rng) => `(forall (a : $dom), $rng) | _ => throwUnsupportedSyntax @[builtinTermElab depArrow] def elabDepArrow : TermElab := fun stx _ => -- bracktedBinder `->` term let binder := stx.getArg 0; let term := stx.getArg 2; elabBinders #[binder] $ fun xs => do e ← elabType term; mkForall stx xs e /-- Main loop `getFunBinderIds?` -/ private partial def getFunBinderIdsAux? : Bool → Syntax → Array Syntax → TermElabM (Option (Array Syntax)) | idOnly, stx, acc => match_syntax stx with | `($f $a) => if idOnly then pure none else do (some acc) ← getFunBinderIdsAux? false f acc | pure none; getFunBinderIdsAux? true a acc | `(_) => do ident ← mkFreshAnonymousIdent stx; pure (some (acc.push ident)) | stx => match stx.isSimpleTermId? true with | some id => pure (some (acc.push id)) | _ => pure none /-- Auxiliary functions for converting `Term.app ... (Term.app id_1 id_2) ... id_n` into `#[id_1, ..., id_m]` It is used at `expandFunBinders`. -/ private def getFunBinderIds? (stx : Syntax) : TermElabM (Option (Array Syntax)) := getFunBinderIdsAux? false stx #[] /-- Main loop for `expandFunBinders`. -/ private partial def expandFunBindersAux (binders : Array Syntax) : Syntax → Nat → Array Syntax → TermElabM (Array Syntax × Syntax) | body, i, newBinders => if h : i < binders.size then let binder := binders.get ⟨i, h⟩; let processAsPattern : Unit → TermElabM (Array Syntax × Syntax) := fun _ => do { let pattern := binder; major ← mkFreshAnonymousIdent binder; (binders, newBody) ← expandFunBindersAux body (i+1) (newBinders.push $ mkExplicitBinder major (mkHole binder)); newBody ← `(match $major:ident with | $pattern => $newBody); pure (binders, newBody) }; match binder with | Syntax.node `Lean.Parser.Term.implicitBinder _ => expandFunBindersAux body (i+1) (newBinders.push binder) | Syntax.node `Lean.Parser.Term.instBinder _ => expandFunBindersAux body (i+1) (newBinders.push binder) | Syntax.node `Lean.Parser.Term.hole _ => do ident ← mkFreshAnonymousIdent binder; let type := binder; expandFunBindersAux body (i+1) (newBinders.push $ mkExplicitBinder ident type) | Syntax.node `Lean.Parser.Term.paren args => -- `(` (termParser >> parenSpecial)? `)` -- parenSpecial := (tupleTail <|> typeAscription)? let binderBody := binder.getArg 1; if binderBody.isNone then processAsPattern () else let idents := binderBody.getArg 0; let special := binderBody.getArg 1; if special.isNone then processAsPattern () else if (special.getArg 0).getKind != `Lean.Parser.Term.typeAscription then processAsPattern () else do -- typeAscription := `:` term let type := ((special.getArg 0).getArg 1); idents? ← getFunBinderIds? idents; match idents? with | some idents => expandFunBindersAux body (i+1) (newBinders ++ idents.map (fun ident => mkExplicitBinder ident type)) | none => processAsPattern () | binder => match binder.isTermId? true with | some (ident, extra) => do unless extra.isNone $ throwError binder "invalid binder, simple identifier expected"; let type := mkHole binder; expandFunBindersAux body (i+1) (newBinders.push $ mkExplicitBinder ident type) | none => processAsPattern () else pure (newBinders, body) /-- Auxiliary function for expanding `fun` notation binders. Recall that `fun` parser is defined as ``` def funBinder : Parser := implicitBinder <|> instBinder <|> termParser appPrec parser! unicodeSymbol "λ" "fun" >> many1 funBinder >> unicodeSymbol "⇒" "=>" >> termParser ``` to allow notation such as `fun (a, b) => a + b`, where `(a, b)` should be treated as a pattern. The result is a pair `(explicitBinders, newBody)`, where `explicitBinders` is syntax of the form ``` `(` ident `:` term `)` ``` which can be elaborated using `elabBinders`, and `newBody` is the updated `body` syntax. We update the `body` syntax when expanding the pattern notation. Example: `fun (a, b) => a + b` expands into `fun _a_1 => match _a_1 with | (a, b) => a + b`. See local function `processAsPattern` at `expandFunBindersAux`. -/ def expandFunBinders (binders : Array Syntax) (body : Syntax) : TermElabM (Array Syntax × Syntax) := expandFunBindersAux binders body 0 #[] namespace FunBinders structure State := (fvars : Array Expr := #[]) (lctx : LocalContext) (localInsts : LocalInstances) (expectedType? : Option Expr := none) (explicit : Bool := false) private def checkNoOptAutoParam (ref : Syntax) (type : Expr) : TermElabM Unit := do type ← instantiateMVars ref type; when type.isOptParam $ throwError ref "optParam is not allowed at 'fun/λ' binders"; when type.isAutoParam $ throwError ref "autoParam is not allowed at 'fun/λ' binders" private def propagateExpectedType (ref : Syntax) (fvar : Expr) (fvarType : Expr) (s : State) : TermElabM State := do match s.expectedType? with | none => pure s | some expectedType => do expectedType ← whnfForall ref expectedType; match expectedType with | Expr.forallE _ d b _ => do isDefEq ref fvarType d; checkNoOptAutoParam ref fvarType; let b := b.instantiate1 fvar; pure { expectedType? := some b, .. s } | _ => pure { expectedType? := none, .. s } private partial def elabFunBinderViews (binderViews : Array BinderView) : Nat → State → TermElabM State | i, s => if h : i < binderViews.size then let binderView := binderViews.get ⟨i, h⟩; withLCtx s.lctx s.localInsts $ do /- As soon as we find an explicit binder, we switch to `explict := true` mode. -/ let s := if binderView.bi.isExplicit then { explicit := true, .. s } else s; type ← elabType binderView.type; checkNoOptAutoParam binderView.type type; fvarId ← mkFreshFVarId; let fvar := mkFVar fvarId; let s := { fvars := s.fvars.push fvar, .. s }; let continue (s : State) : TermElabM State := do { className? ← isClass binderView.type type; match className? with | none => elabFunBinderViews (i+1) s | some className => do resetSynthInstanceCache; let localInsts := s.localInsts.push { className := className, fvar := mkFVar fvarId }; elabFunBinderViews (i+1) { localInsts := localInsts, .. s } }; if s.explicit then do -- dbgTrace (toString binderView.id.getId ++ " : " ++ toString type); /- We do **not** want to support default and auto arguments in lambda abstractions. Example: `fun (x : Nat := 10) => x+1`. We do not believe this is an useful feature, and it would complicate the logic here. -/ let lctx := s.lctx.mkLocalDecl fvarId binderView.id.getId type binderView.bi; s ← propagateExpectedType binderView.id fvar type s; continue { lctx := lctx, .. s } else do /- When `@` is not used, we use let-declarations to elaborate the implicit binders occurring in a prefix of lambda abstraction. For example, `fun {α} => b` is elaborated into `let α := ?m; b`. We do this because we can propagate the expected type more effectively. Recall that, a term `fun {α} => b` has to be elaborated as `(fun α => b) ?m` where `?m` is a fresh metavariable for the implicit argument `{α}`. `let α := ?m; b` is the same expression after beta-reduction, but we can elaborate `b` using the expected type for `fun {α} => b`. This design decision is also motivated by the implicit lambda feature. For example, suppose we have ``` def id : {α : Type} → α → α := fun {α} x => @id α x ``` When the elaborator reaches `fun {α} x => id x` the expected type is `{α : Type} → α → α`. Then, it introduces a new local variable `α_1` for the implict binder, and elaborates `fun {α} x => @id α x` with expected type `α_1 → α_1`. Then, the elaborator reaches this branch, and creates the let declaration `α : ?t_1 := ?mvar`, Note that `type` is the metavariable `?t_1` in this example since we de not specify any type at `{α}`. Then, it elaborates `fun x => @id α x` still using the expected type `α_1 → α_1`. When it reaches the binder `x`, it creates the variable `x : ?t_1`, and `propagateExpectedType` creates the unification problem `?t_1 =?= α_1` which is solved `?t_1 := α_1`. Then, when it elaborates `@id α x`, the unification constraint `α =?= α_1` is created. The unifier (aka `isDefEq`), zeta-reduce ‵α` and reduces the constraint to `?mvar =?= α_1`, which is solved `?mvar := α_1`, their type are also unified which produces the assignment `?t_1 := Type`. Thus, the resulting expression is: ``` def id : {α : Type} → α → α := fun {α_1} => let α : Type := α_1; fun (x : α_1) => @id α x ``` It is also matches the intuition that lambda binders {α} are useful for naming binders produced by the implicit lambda feature. -/ mvar ← mkFreshExprMVar binderView.id type; let lctx := s.lctx.mkLetDecl fvarId binderView.id.getId type mvar; continue { lctx := lctx, .. s } else pure s partial def elabFunBindersAux (binders : Array Syntax) : Nat → State → TermElabM State | i, s => if h : i < binders.size then do binderViews ← matchBinder (binders.get ⟨i, h⟩); s ← elabFunBinderViews binderViews 0 s; elabFunBindersAux (i+1) s else pure s end FunBinders def elabFunBinders {α} (binders : Array Syntax) (expectedType? : Option Expr) (explicit : Bool) (x : Array Expr → Option Expr → TermElabM α) : TermElabM α := if binders.isEmpty then x #[] expectedType? else do lctx ← getLCtx; localInsts ← getLocalInsts; s ← FunBinders.elabFunBindersAux binders 0 { lctx := lctx, localInsts := localInsts, expectedType? := expectedType?, explicit := explicit }; resettingSynthInstanceCacheWhen (s.localInsts.size > localInsts.size) $ withLCtx s.lctx s.localInsts $ x s.fvars s.expectedType? def elabFunCore (stx : Syntax) (expectedType? : Option Expr) (explicit : Bool) : TermElabM Expr := do -- `fun` term+ `=>` term let binders := (stx.getArg 1).getArgs; let body := stx.getArg 3; (binders, body) ← expandFunBinders binders body; elabFunBinders binders expectedType? explicit $ fun xs expectedType? => do { e ← elabTerm body expectedType?; mkLambda stx xs e } @[builtinTermElab «fun»] def elabFun : TermElab := fun stx expectedType? => elabFunCore stx expectedType? false /- Recall that ``` def typeSpec := parser! " : " >> termParser def optType : Parser := optional typeSpec ``` -/ def expandOptType (ref : Syntax) (optType : Syntax) : Syntax := if optType.isNone then mkHole ref else (optType.getArg 0).getArg 1 /- If `useLetExpr` is true, then a kernel let-expression `let x : type := val; body` is created. Otherwise, we create a term of the form `(fun (x : type) => body) val` -/ def elabLetDeclAux (ref : Syntax) (n : Name) (binders : Array Syntax) (typeStx : Syntax) (valStx : Syntax) (body : Syntax) (expectedType? : Option Expr) (useLetExpr : Bool) : TermElabM Expr := do (type, val) ← elabBinders binders $ fun xs => do { type ← elabType typeStx; val ← elabTerm valStx type; val ← ensureHasType valStx type val; type ← mkForall ref xs type; val ← mkLambda ref xs val; pure (type, val) }; trace `Elab.let.decl ref $ fun _ => n ++ " : " ++ type ++ " := " ++ val; if useLetExpr then withLetDecl ref n type val $ fun x => do body ← elabTerm body expectedType?; body ← instantiateMVars ref body; mkLet ref x body else do f ← withLocalDecl ref n BinderInfo.default type $ fun x => do { body ← elabTerm body expectedType?; body ← instantiateMVars ref body; mkLambda ref #[x] body }; pure $ mkApp f val @[builtinTermElab «let»] def elabLetDecl : TermElab := fun stx expectedType? => match_syntax stx with | `(let $id:ident $args* := $val; $body) => elabLetDeclAux stx id.getId args (mkHole stx) val body expectedType? true | `(let $id:ident $args* : $type := $val; $body) => elabLetDeclAux stx id.getId args type val body expectedType? true | `(let $pat:term := $val; $body) => do stxNew ← `(let x := $val; match x with $pat => $body); withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? | `(let $pat:term : $type := $val; $body) => do stxNew ← `(let x : $type := $val; match x with $pat => $body); withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? | _ => throwUnsupportedSyntax @[builtinTermElab «let!»] def elabLetBangDecl : TermElab := fun stx expectedType? => match_syntax stx with | `(let! $id:ident $args* := $val; $body) => elabLetDeclAux stx id.getId args (mkHole stx) val body expectedType? false | `(let! $id:ident $args* : $type := $val; $body) => elabLetDeclAux stx id.getId args type val body expectedType? false | `(let! $pat:term := $val; $body) => do stxNew ← `(let! x := $val; match x with $pat => $body); withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? | `(let! $pat:term : $type := $val; $body) => do stxNew ← `(let! x : $type := $val; match x with $pat => $body); withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? | _ => throwUnsupportedSyntax @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Elab.let; pure () end Term end Elab end Lean
893ec8d6bdb57aee89200f187a7c84ee87a78d07
4dbc106f944ae08d9082a937156fe53f8241336c
/src/data/list/dict.lean
c98183e73238dccf45071f044ccedd0cc1b6dc9e
[]
no_license
spl/lean-finmap
feff7ee53811b172531f84b20c02e50c787fe6fc
936d9caeb27631e3c6cf20e972de4837c9fe98fa
refs/heads/master
1,584,501,090,642
1,537,511,660,000
1,537,515,626,000
134,227,269
0
0
null
null
null
null
UTF-8
Lean
false
false
31,155
lean
import data.list.perm import data.sigma.on_fst local attribute [simp] not_or_distrib and.assoc local attribute [-simp] sigma.forall namespace list section αβ variables {α : Type*} {β : α → Type*} /-- Keys: the list of keys from a list of dependent key-value pairs -/ def keys : list (sigma β) → list α := map sigma.fst section keys variables {a : α} {s hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem keys_nil : @keys α β [] = [] := rfl @[simp] theorem keys_cons : (hd :: tl).keys = hd.1 :: tl.keys := rfl @[simp] theorem keys_singleton : [s].keys = [s.1] := rfl @[simp] theorem keys_append : (l₁ ++ l₂).keys = l₁.keys ++ l₂.keys := by simp [keys] @[simp] theorem keys_iff_ne_key_of_mem : (∀ (s : sigma β), s ∈ l → a ≠ s.1) ↔ a ∉ l.keys := by induction l; simp * theorem mem_of_ne_key_of_mem_cons (h : hd.1 ≠ s.1) : s ∈ hd :: tl → s ∈ tl := by cases s; cases hd; simp [ne.symm h] theorem mem_keys_of_mem : s ∈ l → s.1 ∈ l.keys := mem_map_of_mem sigma.fst theorem exists_mem_of_mem_keys (h : a ∈ l.keys) : ∃ (b : β a), sigma.mk a b ∈ l := let ⟨⟨a', b'⟩, m, e⟩ := exists_of_mem_map h in eq.rec_on e (exists.intro b' m) theorem mem_keys : a ∈ l.keys ↔ ∃ (b : β a), sigma.mk a b ∈ l := ⟨exists_mem_of_mem_keys, λ ⟨b, h⟩, mem_keys_of_mem h⟩ end keys /-- No duplicate keys in a list of dependent key-value pairs. -/ def nodupkeys : list (sigma β) → Prop := pairwise (sigma.fst_rel (≠)) section nodupkeys variables {s t hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem nodupkeys_nil : @nodupkeys α β [] := pairwise.nil _ @[simp] theorem nodupkeys_cons : (hd :: tl).nodupkeys ↔ hd.1 ∉ tl.keys ∧ tl.nodupkeys := by simp [nodupkeys, sigma.fst_rel] theorem nodupkeys_cons_of_nodupkeys (h : hd.1 ∉ tl.keys) (t : nodupkeys tl) : nodupkeys (hd :: tl) := nodupkeys_cons.mpr ⟨h, t⟩ theorem nodupkeys_singleton (s : sigma β) : nodupkeys [s] := nodupkeys_cons_of_nodupkeys (not_mem_nil s.1) nodupkeys_nil theorem nodup_of_nodupkeys : l.nodupkeys → l.nodup := pairwise.imp $ λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (h : a₁ ≠ a₂), by simp [h] @[simp] theorem nodupkeys_iff : l.keys.nodup ↔ l.nodupkeys := pairwise_map sigma.fst theorem perm_nodupkeys (p : l₁ ~ l₂) : l₁.nodupkeys ↔ l₂.nodupkeys := perm_pairwise (@sigma.fst_rel.symm α β (≠) (@ne.symm α)) p @[simp] theorem nodupkeys_cons_of_not_mem_keys (h : hd.1 ∉ tl.keys) : (hd :: tl).nodupkeys ↔ tl.nodupkeys := begin induction tl, case list.nil { simp }, case list.cons : hd₁ tl ih { simp at h, simp [perm_nodupkeys (perm.swap hd₁ hd tl), ne.symm h.1, ih h.2] } end variables {ls : list (list (sigma β))} theorem nodupkeys_join : (join ls).nodupkeys ↔ (∀ {l : list (sigma β)}, l ∈ ls → l.nodupkeys) ∧ pairwise disjoint (ls.map keys) := have ∀ (l₁ l₂ : list (sigma β)), (∀ (s ∈ l₁) (t ∈ l₂), sigma.fst_rel ne s t) ↔ disjoint l₁.keys l₂.keys := λ l₁ l₂, have h₁ : (∀ (s : sigma β), s ∈ l₁ → s.1 ∉ l₂.keys) → disjoint l₁.keys l₂.keys := λ f a mkas mkat, let ⟨b, mabs⟩ := exists_mem_of_mem_keys mkas in absurd mkat $ f ⟨a, b⟩ mabs, have h₂ : disjoint l₁.keys l₂.keys → ∀ (s : sigma β), s ∈ l₁ → s.1 ∉ l₂.keys := λ dj s mss mkat, absurd mkat $ dj $ mem_keys_of_mem mss, ⟨by simpa using h₁, by simpa using h₂⟩, pairwise_join.trans $ and_congr iff.rfl $ (pairwise.iff this).trans (pairwise_map _).symm theorem nodup_enum_map_fst (l : list α) : (l.enum.map prod.fst).nodup := by simp [list.nodup_range] theorem perm_keys_of_perm (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : l₁.keys ~ l₂.keys := begin induction p, case list.perm.nil { refl }, case list.perm.skip : hd tl₁ tl₂ p ih { simp at nd₁ nd₂, simp [perm.skip hd.1 (ih nd₁.2 nd₂.2)] }, case list.perm.swap : s₁ s₂ l { simp [perm.swap s₁.1 s₂.1 (keys l)] }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ nd₁ nd₃ { have nd₂ : l₂.nodupkeys := (perm_nodupkeys p₁₂).mp nd₁, exact perm.trans (ih₁₂ nd₁ nd₂) (ih₂₃ nd₂ nd₃) } end -- Is this useful? theorem nodupkeys_functional (d : l.nodupkeys) (ms : s ∈ l) (mt : t ∈ l) (h : s.1 = t.1) : (eq.rec_on h s.2 : β t.1) = t.2 := begin induction d, case pairwise.nil { cases ms }, case pairwise.cons : _ _ r _ ih { simp at ms mt, cases ms; cases mt, { subst ms, subst mt }, { induction ms, exact absurd h (r _ mt) }, { induction mt, exact absurd h (ne.symm (r _ ms)) }, { exact ih ms mt } }, end -- Is this useful? theorem eq_of_nodupkeys_of_eq_fst (d : l.nodupkeys) (ms : s ∈ l) (mt : t ∈ l) (h : s.1 = t.1) : s = t := sigma.eq h $ nodupkeys_functional d ms mt h end nodupkeys section decidable_eq_α variables [decidable_eq α] /-- Key-based single-value lookup in a list of dependent key-value pairs. The result is the first key-matching value found, if one exists. -/ def klookup (a : α) : list (sigma β) → option (β a) | [] := none | (hd :: tl) := if h : hd.1 = a then some (h.rec_on hd.2) else klookup tl section klookup variables {a : α} {s hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem klookup_nil : @klookup _ β _ a [] = none := rfl @[simp] theorem klookup_cons_eq (h : hd.1 = a) : klookup a (hd :: tl) = some (h.rec_on hd.2) := by simp [klookup, h] @[simp] theorem klookup_cons_ne (h : hd.1 ≠ a) : klookup a (hd :: tl) = klookup a tl := by simp [klookup, h] @[simp] theorem klookup_eq (a : α) : ∀ (l : list (sigma β)), klookup a l = none ∨ ∃ (b : β a), b ∈ l.klookup a | [] := or.inl rfl | (hd :: tl) := if h₁ : hd.1 = a then or.inr ⟨h₁.rec_on hd.2, klookup_cons_eq h₁⟩ else match klookup_eq tl with | or.inl h₂ := or.inl $ (klookup_cons_ne h₁).trans h₂ | or.inr ⟨b, h₂⟩ := or.inr ⟨b, (klookup_cons_ne h₁).trans h₂⟩ end theorem klookup_is_some : (l.klookup a).is_some ↔ ∃ (b : β a), b ∈ l.klookup a := by simp [option.is_some_iff_exists] theorem klookup_not_mem_keys : a ∉ l.keys ↔ klookup a l = none := by induction l with hd _ ih; [simp, {by_cases h : hd.1 = a; [simp [h], simp [h, ne.symm h, ih]]}] @[simp] theorem mem_klookup_of_nodupkeys (nd : l.nodupkeys) : s.2 ∈ l.klookup s.1 ↔ s ∈ l := begin induction l generalizing s, case list.nil { simp }, case list.cons : hd tl ih { simp at nd, by_cases h : hd.1 = s.1, { rw klookup_cons_eq h, cases s with a₁ b₁, cases hd with a₂ b₂, dsimp at h, induction h, split, { simp {contextual := tt} }, { intro h, simp at h, cases h with h h, { simp [h] }, { exact absurd (mem_keys_of_mem h) nd.1 } } }, { rw [klookup_cons_ne h, mem_cons_iff], split, { exact or.inr ∘ (ih nd.2).mp }, { intro p, cases p with p p, { induction p, exact false.elim (ne.irrefl h) }, { exact (ih nd.2).mpr p } } } } end theorem perm_klookup (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : l₁.klookup a = l₂.klookup a := begin induction p, case list.perm.nil { refl }, case list.perm.skip : hd tl₁ tl₂ p ih nd₁ nd₂ { by_cases h : hd.1 = a, { simp [h] }, { simp at nd₁ nd₂, simp [h, ih nd₁.2 nd₂.2] } }, case list.perm.swap : s₁ s₂ l nd₂₁ nd₁₂ { simp at nd₂₁ nd₁₂, by_cases h₂ : s₂.1 = a, { induction h₂, simp [nd₁₂.1] }, { by_cases h₁ : s₁.1 = a; simp [h₂, h₁] } }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ nd₁ nd₃ { have nd₂ : l₂.nodupkeys := (perm_nodupkeys p₁₂).mp nd₁, exact eq.trans (ih₁₂ nd₁ nd₂) (ih₂₃ nd₂ nd₃) } end end klookup /-- Key-based multiple-value lookup in a list of dependent key-value pairs. The result is a list of all key-matching values. -/ def klookup_all (a : α) : list (sigma β) → list (β a) | [] := [] | (hd :: tl) := let tl' := klookup_all tl in if h : hd.1 = a then h.rec_on hd.2 :: tl' else tl' section klookup_all variables {a : α} {hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem klookup_all_nil : @klookup_all _ β _ a [] = [] := rfl @[simp] theorem klookup_all_cons_eq (h : hd.1 = a) : (hd :: tl).klookup_all a = h.rec_on hd.2 :: tl.klookup_all a := by simp [klookup_all, h] @[simp] theorem klookup_all_cons_ne (h : hd.1 ≠ a) : (hd :: tl).klookup_all a = tl.klookup_all a := by simp [klookup_all, h] theorem klookup_all_head [inhabited (β a)] : (l.klookup_all a).head = (l.klookup a).iget := by induction l with hd; [refl, {by_cases hd.1 = a; simp *}] theorem perm_klookup_all (p : l₁ ~ l₂) : l₁.klookup_all a ~ l₂.klookup_all a := begin induction p, case list.perm.nil { refl }, case list.perm.skip : hd tl₁ tl₂ p ih { by_cases h : hd.1 = a; simp [h, ih, perm.skip] }, case list.perm.swap : s₁ s₂ l { by_cases h₁ : s₁.1 = a; by_cases h₂ : s₂.1 = a; simp [h₁, h₂, perm.swap] }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ { exact perm.trans ih₁₂ ih₂₃ } end end klookup_all /-- Key-based single-pair erasure in a list of dependent key-value pairs. The result is the list minus the first key-matching pair, if one exists. -/ def kerase (a : α) : list (sigma β) → list (sigma β) | [] := [] | (hd :: tl) := if hd.1 = a then tl else hd :: kerase tl section kerase variables {a a₁ a₂ : α} {s hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem kerase_nil : @kerase _ β _ a [] = [] := rfl @[simp] theorem kerase_cons_eq (h : hd.1 = a) : (hd :: tl).kerase a = tl := by simp [kerase, h] @[simp] theorem kerase_cons_ne (h : hd.1 ≠ a) : (hd :: tl).kerase a = hd :: tl.kerase a := by simp [kerase, h] theorem kerase_cons (a : α) (hd : sigma β) (tl : list (sigma β)) : hd.1 = a ∧ (hd :: tl).kerase a = tl ∨ hd.1 ≠ a ∧ (hd :: tl).kerase a = hd :: tl.kerase a := by by_cases h : hd.1 = a; simp [h] @[simp] theorem mem_kerase_nil : s ∈ @kerase _ β _ a [] ↔ false := by simp @[simp] theorem kerase_of_not_mem_keys (h : a ∉ l.keys) : l.kerase a = l := by induction l with _ _ ih; [refl, {simp at h, simp [h.1, ne.symm h.1, ih h.2]}] theorem exists_kerase_eq (h : a ∈ l.keys) : ∃ (b : β a) (l₁ l₂ : list (sigma β)), a ∉ l₁.keys ∧ l = l₁ ++ ⟨a, b⟩ :: l₂ ∧ l.kerase a = l₁ ++ l₂ := begin induction l, case list.nil { cases h }, case list.cons : hd tl ih { by_cases e : hd.1 = a, { induction e, exact ⟨hd.2, [], tl, by simp, by cases hd; refl, by simp⟩ }, { simp at h, cases h, case or.inl : h { exact absurd h (ne.symm e) }, case or.inr : h { rcases ih h with ⟨b, tl₁, tl₂, h₁, h₂, h₃⟩, exact ⟨b, hd :: tl₁, tl₂, not_mem_cons_of_ne_of_not_mem (ne.symm e) h₁, by rw h₂; refl, by simp [e, h₃]⟩ } } } end theorem kerase_sublist (a : α) (l : list (sigma β)) : l.kerase a <+ l := if h : a ∈ l.keys then match l, l.kerase a, exists_kerase_eq h with | _, _, ⟨_, _, _, _, rfl, rfl⟩ := by simp end else by simp [h] theorem kerase_subset (a : α) (l : list (sigma β)) : l.kerase a ⊆ l := subset_of_sublist (kerase_sublist a l) theorem kerase_sublist_kerase (a : α) : ∀ {l₁ l₂ : list (sigma β)}, l₁ <+ l₂ → l₁.kerase a <+ l₂.kerase a | _ _ sublist.slnil := sublist.slnil | _ _ (sublist.cons l₁ l₂ hd sl) := if h : hd.1 = a then by rw [kerase_cons_eq h]; exact (kerase_sublist _ _).trans sl else by rw kerase_cons_ne h; exact (kerase_sublist_kerase sl).cons _ _ _ | _ _ (sublist.cons2 l₁ l₂ hd sl) := if h : hd.1 = a then by repeat {rw kerase_cons_eq h}; exact sl else by repeat {rw kerase_cons_ne h}; exact (kerase_sublist_kerase sl).cons2 _ _ _ theorem mem_of_mem_kerase : s ∈ l.kerase a → s ∈ l := @kerase_subset _ _ _ _ _ _ @[simp] theorem mem_kerase_of_ne (h : s.1 ≠ a) : s ∈ l.kerase a ↔ s ∈ l := iff.intro mem_of_mem_kerase $ λ p, if q : a ∈ l.keys then match l, l.kerase a, exists_kerase_eq q, p with | _, _, ⟨_, _, _, _, rfl, rfl⟩, p := by clear _match; cases s; simpa [h] using p end else by simp [q, p] theorem kerase_subset_keys (a : α) (l : list (sigma β)) : (l.kerase a).keys ⊆ l.keys := subset_of_sublist (map_sublist_map _ (kerase_sublist a l)) theorem mem_keys_of_mem_keys_kerase : a₁ ∈ (l.kerase a₂).keys → a₁ ∈ l.keys := @kerase_subset_keys _ _ _ _ _ _ @[simp] theorem mem_keys_kerase_of_ne (h : a₂ ≠ a₁) : a₁ ∈ (l.kerase a₂).keys ↔ a₁ ∈ l.keys := iff.intro mem_keys_of_mem_keys_kerase $ λ p, if q : a₂ ∈ l.keys then match l, l.kerase a₂, exists_kerase_eq q, p with | _, _, ⟨_, _, _, _, rfl, rfl⟩, p := by simpa [ne.symm h] using p end else by simp [q, p] @[simp] theorem nodupkeys_kerase (a : α) : l.nodupkeys → (l.kerase a).nodupkeys := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { intro nd, simp at nd, by_cases h : hd.1 = a, { simp [h, nd.2] }, { rw [kerase_cons_ne h, nodupkeys_cons], exact ⟨mt (mem_keys_kerase_of_ne (ne.symm h)).mp nd.1, ih nd.2⟩ } } end @[simp] theorem not_mem_keys_kerase_self (nd : l.nodupkeys) : a ∉ (l.kerase a).keys := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { simp at nd, by_cases h : hd.1 = a, { induction h, simp [nd.1] }, { simp [h, ne.symm h, ih nd.2] } } end theorem kerase_append_left : ∀ {l₁ l₂ : list (sigma β)}, a ∈ l₁.keys → (l₁ ++ l₂).kerase a = l₁.kerase a ++ l₂ | [] _ h := by cases h | (hd :: tl₁) l₂ h₁ := if h₂ : hd.1 = a then by simp [h₂] else by simp at h₁; cases h₁; [exact absurd h₁ (ne.symm h₂), simp [h₂, kerase_append_left h₁]] theorem kerase_append_right : ∀ {l₁ l₂ : list (sigma β)}, a ∉ l₁.keys → (l₁ ++ l₂).kerase a = l₁ ++ l₂.kerase a | [] _ h := rfl | (_ :: tl₁) l₂ h := by simp at h; simp [ne.symm h.1, kerase_append_right h.2] theorem kerase_comm (a₁ a₂ : α) (l : list (sigma β)) : (l.kerase a₁).kerase a₂ = (l.kerase a₂).kerase a₁ := if h : a₂ = a₁ then by simp [h] else if ha₁ : a₁ ∈ l.keys then if ha₂ : a₂ ∈ l.keys then match l, l.kerase a₁, exists_kerase_eq ha₁, ha₂ with | _, _, ⟨b₁, l₁, l₂, a₁_nin_l₁, rfl, rfl⟩, a₂_in_l₁_app_l₂ := if h' : a₂ ∈ l₁.keys then by simp [kerase_append_left h', kerase_append_right (mt (mem_keys_kerase_of_ne h).mp a₁_nin_l₁)] else by simp [kerase_append_right h', kerase_append_right a₁_nin_l₁, @kerase_cons_ne _ _ _ a₂ ⟨a₁, b₁⟩ _ (ne.symm h)] end else by simp [ha₂, mt mem_keys_of_mem_keys_kerase ha₂] else by simp [ha₁, mt mem_keys_of_mem_keys_kerase ha₁] @[simp] theorem klookup_kerase (nd : l.nodupkeys) : (l.kerase a).klookup a = none := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { simp at nd, by_cases h₁ : hd.1 = a, { by_cases h₂ : a ∈ tl.keys, { induction h₁, exact absurd h₂ nd.1 }, { simp [h₁, klookup_not_mem_keys.mp h₂] } }, { simp [h₁, ih nd.2] } } end theorem ne_of_nodupkeys_of_mem_kerase : l.nodupkeys → s ∈ l.kerase a → a ≠ s.1 := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { intros nd h, simp at nd, rcases kerase_cons a hd tl with ⟨he, p⟩ | ⟨hn, p⟩, { induction he, simp [p] at h, exact ne.symm (ne_of_mem_of_not_mem (mem_keys_of_mem h) nd.1) }, { simp [hn] at h, cases h with h h, { induction h, exact ne.symm hn }, { exact ih nd.2 h } } } end theorem nodupkeys_kerase_eq_filter (a : α) (nd : l.nodupkeys) : l.kerase a = filter (λ s, s.1 ≠ a) l := begin induction nd, case pairwise.nil { refl }, case pairwise.cons : s l n p ih { by_cases h : s.1 = a, { have : filter (λ (t : sigma β), t.1 ≠ a) l = l := filter_eq_self.mpr (λ t th, h ▸ ne.symm (n t th)), simp [h, kerase, filter, this] }, { simp [h, ih] } } end @[simp] theorem mem_kerase_of_nodupkeys (nd : l.nodupkeys) : s ∈ l.kerase a ↔ s.1 ≠ a ∧ s ∈ l := by rw nodupkeys_kerase_eq_filter a nd; simp [and_comm] theorem perm_kerase (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : l₁.kerase a ~ l₂.kerase a := begin induction p, case list.perm.nil { refl }, case list.perm.skip : hd tl₁ tl₂ p ih { simp at nd₁ nd₂, by_cases h : hd.1 = a; simp [p, h, ih nd₁.2 nd₂.2, perm.skip] }, case list.perm.swap : s₁ s₂ l nd₂₁ nd₁₂ { simp at nd₁₂, by_cases h₂ : s₂.1 = a, { induction h₂, simp [nd₁₂.1] }, { by_cases h₁ : s₁.1 = a; simp [h₂, h₁, perm.swap] } }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ nd₁ nd₃ { have nd₂ : l₂.nodupkeys := (perm_nodupkeys p₁₂).mp nd₁, exact perm.trans (ih₁₂ nd₁ nd₂) (ih₂₃ nd₂ nd₃) } end end kerase /-- `cons` with `kerase` of the first `s`-key-matching pair -/ def kinsert (s : sigma β) (l : list (sigma β)) : list (sigma β) := s :: l.kerase s.1 section kinsert variables {a : α} {s t hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem kinsert_eq_cons_kerase : tl.kinsert hd = hd :: tl.kerase hd.1 := rfl @[simp] theorem mem_kinsert : s ∈ kinsert t l ↔ s = t ∨ s ∈ l.kerase t.1 := by simp [kinsert] @[simp] theorem mem_keys_kinsert : a ∈ (l.kinsert s).keys ↔ s.1 = a ∨ a ∈ l.keys := by by_cases h : s.1 = a; [simp [h], simp [h, ne.symm h]] @[simp] theorem nodupkeys_kinsert (s : sigma β) (nd : l.nodupkeys) : (l.kinsert s).nodupkeys := (nodupkeys_cons_of_not_mem_keys (not_mem_keys_kerase_self nd)).mpr $ nodupkeys_kerase _ nd theorem perm_kinsert (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : l₁.kinsert s ~ l₂.kinsert s := perm.skip s $ perm_kerase nd₁ nd₂ p end kinsert /-- Key-based single-pair replacement in a list of dependent key-value pairs. The result is the list with the first key-matching pair, if it exists, replaced by the given pair. -/ def kreplace (s : sigma β) : list (sigma β) → list (sigma β) | [] := [] | (hd :: tl) := if h : hd.1 = s.1 then s :: tl else hd :: kreplace tl section kreplace variables {a : α} {s t hd : sigma β} {l l₁ l₂ tl : list (sigma β)} @[simp] theorem kreplace_nil : kreplace s [] = [] := rfl @[simp] theorem kreplace_cons_eq (h : hd.1 = s.1) : (hd :: tl).kreplace s = s :: tl := by simp [kreplace, h] @[simp] theorem kreplace_cons_ne (h : hd.1 ≠ s.1) : (hd :: tl).kreplace s = hd :: tl.kreplace s := by simp [kreplace, h] theorem kreplace_cons (s hd : sigma β) (tl : list (sigma β)) : hd.1 = s.1 ∧ (hd :: tl).kreplace s = s :: tl ∨ hd.1 ≠ s.1 ∧ (hd :: tl).kreplace s = hd :: tl.kreplace s := by by_cases h : hd.1 = s.1; simp [h] theorem mem_of_mem_kreplace_ne (h : t.1 ≠ s.1) : s ∈ l.kreplace t → s ∈ l := begin induction l generalizing s t, case list.nil { simp }, case list.cons : hd tl ih { by_cases p : hd.1 = t.1, { rw kreplace_cons_eq p, exact mem_cons_of_mem hd ∘ mem_of_ne_key_of_mem_cons h }, { rw [kreplace_cons_ne p, mem_cons_iff, mem_cons_iff], exact or.imp_right (ih h) } } end theorem mem_keys_of_mem_keys_kreplace_ne (h₁ : a ≠ s.1) (h₂ : a ∈ (l.kreplace s).keys) : a ∈ l.keys := let ⟨b, h₃⟩ := exists_mem_of_mem_keys h₂ in @mem_keys_of_mem _ _ ⟨a, b⟩ _ (mem_of_mem_kreplace_ne (ne.symm h₁) h₃) @[simp] theorem nodupkeys_kreplace (s : sigma β) : l.nodupkeys → (l.kreplace s).nodupkeys := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { intro nd, simp at nd, by_cases p : hd.1 = s.1, { rw p at nd, simp [p, nd.1, nd.2] }, { simp [p, nd.1, ih nd.2, mt (mem_keys_of_mem_keys_kreplace_ne p)] } } end theorem perm_kreplace (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : l₁.kreplace s ~ l₂.kreplace s := begin induction p, case list.perm.nil { refl }, case list.perm.skip : hd tl₁ tl₂ p ih { simp at nd₁ nd₂, by_cases h : hd.1 = s.1; simp [p, h, ih nd₁.2 nd₂.2, perm.skip] }, case list.perm.swap : s₁ s₂ l nd₂₁ nd₁₂ { simp at nd₂₁ nd₁₂, by_cases h₂ : s₂.1 = s.1, { rw kreplace_cons_eq h₂, by_cases h₁ : s₁.1 = s.1, { rw kreplace_cons_eq h₁, exact absurd (h₁.trans h₂.symm) nd₁₂.1 }, { simp [h₁, h₂, perm.swap] } }, { by_cases h₁ : s₁.1 = s.1; simp [h₁, h₂, perm.swap] } }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ nd₁ nd₃ { have nd₂ : l₂.nodupkeys := (perm_nodupkeys p₁₂).mp nd₁, exact perm.trans (ih₁₂ nd₁ nd₂) (ih₂₃ nd₂ nd₃) } end end kreplace /-- Left-biased key-based union of lists of dependent key-value pairs. The result of `l₁.kunion l₂` is constructed from `l₁` with `l₂` appended such that the first pair matching each key in `l₁` is erased from `l₂`. Note that the result can still have duplicates if duplicates exist in either argument. -/ def kunion : list (sigma β) → list (sigma β) → list (sigma β) | [] l := l | (hd :: tl) l := hd :: kunion tl (kerase hd.1 l) section kunion variables {a : α} {s hd : sigma β} {l l₁ l₂ l₃ l₄ tl : list (sigma β)} @[simp] theorem nil_kunion (l : list (sigma β)) : [].kunion l = l := rfl @[simp] theorem kunion_nil : ∀ (l : list (sigma β)), l.kunion [] = l | [] := rfl | (_ :: tl) := by rw [kunion, kerase_nil, kunion_nil tl] @[simp] theorem kunion_cons : (hd :: tl).kunion l = hd :: tl.kunion (l.kerase hd.1) := rfl @[simp] theorem kerase_kunion : ∀ {l₁ : list (sigma β)} (l₂ : list (sigma β)), (l₁.kerase a).kunion (l₂.kerase a) = (l₁.kunion l₂).kerase a | [] _ := rfl | (hd :: _) l := by by_cases h : hd.1 = a; simp [h, kerase_comm a hd.1 l, kerase_kunion] @[simp] theorem map_kunion {γ : Type*} (f : sigma β → γ) (dk : disjoint l₁.keys l₂.keys) : (l₁.kunion l₂).map f = l₁.map f ++ l₂.map f := by induction l₁ with _ _ ih; [refl, {simp at dk, simp [dk.1, ih dk.2.symm]}] theorem keys_kunion (dk : disjoint l₁.keys l₂.keys) : (l₁.kunion l₂).keys = l₁.keys ++ l₂.keys := by simp [keys, dk] @[simp] theorem kinsert_kunion : (l₁.kinsert s).kunion l₂ = (l₁.kunion l₂).kinsert s := by simp @[simp] theorem kunion_assoc : (l₁.kunion l₂).kunion l₃ = l₁.kunion (l₂.kunion l₃) := by induction l₁ generalizing l₂ l₃; simp * theorem mem_of_mem_kunion : s ∈ l₁.kunion l₂ → s ∈ l₁ ∨ s ∈ l₂ := begin induction l₁ generalizing l₂, case list.nil { simp }, case list.cons : hd tl ih { intro h, simp at h, cases h, case or.inl : h { simp [h] }, case or.inr : h { cases ih h, case or.inl : h { simp [h] }, case or.inr : h { simp [mem_of_mem_kerase h] } } } end theorem mem_kunion_left (l₂ : list (sigma β)) (h : s ∈ l₁) : s ∈ l₁.kunion l₂ := by induction l₁ generalizing l₂; simp at h; cases h; simp * theorem mem_kunion_right (h₁ : s.1 ∉ l₁.keys) (h₂ : s ∈ l₂) : s ∈ l₁.kunion l₂ := by induction l₁ generalizing l₂; simp at h₁; cases h₁; simp * theorem mem_kunion_middle (dk : disjoint (l₁.kunion l₂).keys l₃.keys) (h : s ∈ l₁.kunion l₃) : s ∈ (l₁.kunion l₂).kunion l₃ := match mem_of_mem_kunion h with | or.inl h := mem_kunion_left _ (mem_kunion_left _ h) | or.inr h := mem_kunion_right (disjoint_right.mp dk (mem_keys_of_mem h)) h end theorem mem_kunion_of_disjoint_keys (dk : disjoint l₁.keys l₂.keys) (h : s ∈ l₁ ∨ s ∈ l₂) : s ∈ l₁.kunion l₂ := begin cases h with h h, { exact mem_kunion_left _ h }, { by_cases p : s.1 ∈ l₁.keys, { exact absurd h (mt mem_keys_of_mem (dk p)) }, { exact mem_kunion_right p h } } end @[simp] theorem mem_kunion_iff (dk : disjoint l₁.keys l₂.keys) : s ∈ l₁.kunion l₂ ↔ s ∈ l₁ ∨ s ∈ l₂ := ⟨mem_of_mem_kunion, mem_kunion_of_disjoint_keys dk⟩ @[simp] theorem mem_keys_kunion : a ∈ (l₁.kunion l₂).keys ↔ a ∈ l₁.keys ∨ a ∈ l₂.keys := by induction l₁ with hd _ ih generalizing l₂; [simp, {by_cases h : hd.1 = a; [simp [h], simp [h, ne.symm h, ih]]}] theorem nodupkeys_kunion (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) : (l₁.kunion l₂).nodupkeys := by induction l₁ generalizing l₂; simp at nd₁; simp * theorem perm_kunion_left (l : list (sigma β)) (p : l₁ ~ l₂) : l₁.kunion l ~ l₂.kunion l := begin induction p generalizing l, case list.perm.nil { refl }, case list.perm.skip : hd tl₁ tl₂ p ih { simp [ih (kerase hd.1 l), perm.skip] }, case list.perm.swap : s₁ s₂ l { simp [kerase_comm, perm.swap] }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ { exact perm.trans (ih₁₂ l) (ih₂₃ l) } end theorem perm_kunion_right : ∀ (l : list (sigma β)) {l₁ l₂ : list (sigma β)}, l₁.nodupkeys → l₂.nodupkeys → l₁ ~ l₂ → l.kunion l₁ ~ l.kunion l₂ | [] _ _ _ _ p := p | (hd :: tl) l₁ l₂ nd₁ nd₂ p := by simp [perm.skip hd (perm_kunion_right tl (nodupkeys_kerase hd.1 nd₁) (nodupkeys_kerase hd.1 nd₂) (perm_kerase nd₁ nd₂ p))] theorem perm_kunion (nd₂ : l₂.nodupkeys) (nd₄ : l₄.nodupkeys) (p₁₃ : l₁ ~ l₃) (p₂₄ : l₂ ~ l₄) : l₁.kunion l₂ ~ l₃.kunion l₄ := perm.trans (perm_kunion_left l₂ p₁₃) (perm_kunion_right l₃ nd₂ nd₄ p₂₄) end kunion end decidable_eq_α end αβ section α₁α₂α₃β₁β₂β₃ universes u v variables {α₁ α₂ α₃ : Type u} {β₁ : α₁ → Type v} {β₂ : α₂ → Type v} {β₃ : α₃ → Type v} section keys variables {s : sigma β₁} {l : list (sigma β₁)} {f : sigma β₁ → sigma β₂} theorem mem_keys_map_of_mem (f : sigma β₁ → sigma β₂) (ms : s ∈ l) : (f s).1 ∈ (l.map f).keys := mem_keys_of_mem (mem_map_of_mem f ms) theorem mem_keys_map (ff : sigma.fst_functional f) (h : s.1 ∈ l.keys) : (f s).1 ∈ (l.map f).keys := let ⟨_, m, e⟩ := exists_of_mem_map h in ff e ▸ mem_keys_map_of_mem f m theorem mem_keys_of_mem_keys_map (fi : sigma.fst_injective f) (h : (f s).1 ∈ (l.map f).keys) : s.1 ∈ l.keys := have h : (sigma.fst ∘ f) s ∈ map (sigma.fst ∘ f) l, by simpa [keys] using h, let ⟨_, m, e⟩ := exists_of_mem_map h in fi e ▸ mem_keys_of_mem m -- Is this useful? theorem mem_keys_of_mem_map (fi : sigma.fst_injective f) (h : f s ∈ l.map f) : s.1 ∈ l.keys := let ⟨_, m, e⟩ := exists_of_mem_map h in fi (sigma.eq_fst e) ▸ mem_keys_of_mem m @[simp] theorem mem_keys_map_iff (ff : sigma.fst_functional f) (fi : sigma.fst_injective f) : (f s).1 ∈ (l.map f).keys ↔ s.1 ∈ l.keys := ⟨mem_keys_of_mem_keys_map fi, mem_keys_map ff⟩ end keys section nodupkeys variables {s t : sigma β₁} {l : list (sigma β₁)} {f : sigma β₁ → sigma β₂} -- Is this useful? theorem nodupkeys_injective (fi : sigma.fst_injective f) (d : l.nodupkeys) (ms : s ∈ l) (mt : t ∈ l) (h : f s = f t) : s = t := eq_of_nodupkeys_of_eq_fst d ms mt $ fi $ sigma.eq_fst h theorem nodupkeys_of_nodupkeys_map (ff : sigma.fst_functional f) : nodupkeys (map f l) → nodupkeys l := pairwise_of_pairwise_map f $ λ s t, mt (@ff s t) theorem nodupkeys_map (fi : sigma.fst_injective f) : l.nodupkeys → (l.map f).nodupkeys := pairwise_map_of_pairwise f (λ s t (h : s ∈ l ∧ t ∈ l ∧ s.1 ≠ t.1), mt (@fi s t) h.2.2) ∘ pairwise.and_mem.mp theorem nodupkeys_map_iff (ff : sigma.fst_functional f) (fi : sigma.fst_injective f) : (l.map f).nodupkeys ↔ l.nodupkeys := ⟨nodupkeys_of_nodupkeys_map ff, nodupkeys_map fi⟩ -- Is this useful? theorem mem_map_of_mem_of_mem_keys_map (fi : sigma.fst_injective f) (d : l.nodupkeys) (ms : s ∈ l) (mfs : (f s).1 ∈ (l.map f).keys) : f s ∈ l.map f := begin simp [keys] at mfs, rcases mfs with ⟨a, b, mab, ef⟩, cases s with sa sb, have ea : a = sa := fi ef, subst ea, have eb : b = sb := nodupkeys_functional d mab ms rfl, subst eb, exact mem_map_of_mem f mab, end end nodupkeys section map_disjoint variables {l₁ l₂ : list (sigma β₁)} {f : sigma β₁ → sigma β₂} theorem map_disjoint_keys_of_disjoint_keys (fi : sigma.fst_injective f) (dk : disjoint l₁.keys l₂.keys) : disjoint (l₁.map f).keys (l₂.map f).keys := λ a h₁ h₂, have h₁ : a ∈ map (sigma.fst ∘ f) l₁, by simpa [keys] using h₁, let ⟨s, m, e⟩ := exists_of_mem_map h₁ in have e : (f s).1 = a := e, dk (mem_keys_of_mem m) (mem_keys_of_mem_keys_map fi (e.symm ▸ h₂)) theorem disjoint_keys_of_map_disjoint_keys (ff : sigma.fst_functional f) (dk : disjoint (l₁.map f).keys (l₂.map f).keys) : disjoint l₁.keys l₂.keys := λ a h₁ h₂, let ⟨b₁, h₁⟩ := exists_mem_of_mem_keys h₁ in dk (mem_keys_map_of_mem f h₁) (mem_keys_map ff h₂) @[simp] theorem map_disjoint_keys (ff : sigma.fst_functional f) (fi : sigma.fst_injective f) : disjoint (l₁.map f).keys (l₂.map f).keys ↔ disjoint l₁.keys l₂.keys := ⟨disjoint_keys_of_map_disjoint_keys ff, map_disjoint_keys_of_disjoint_keys fi⟩ end map_disjoint section decidable_eq_α₁_α₂ variables [decidable_eq α₁] [decidable_eq α₂] section map variables {s : sigma β₁} {l : list (sigma β₁)} {f : sigma β₁ → sigma β₂} @[simp] theorem map_kerase (ff : sigma.fst_functional f) (fi : sigma.fst_injective f) : (l.kerase s.1).map f = (l.map f).kerase (f s).1 := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { by_cases h : (f hd).1 = (f s).1, { simp [h, fi h] }, { simp [h, mt (@ff _ _) h, ih] } } end @[simp] theorem map_kinsert (ff : sigma.fst_functional f) (fi : sigma.fst_injective f) : (l.kinsert s).map f = (l.map f).kinsert (f s) := by simp [ff, fi] end map end decidable_eq_α₁_α₂ end α₁α₂α₃β₁β₂β₃ section αβ₁β₂ universes u v variables {α : Type u} {β₁ β₂ : α → Type v} section nodupkeys variables {l : list (sigma β₁)} theorem nodupkeys_map_id_iff (f : ∀ a, β₁ a → β₂ a) : (l.map (sigma.map id f)).nodupkeys ↔ l.nodupkeys := nodupkeys_map_iff (sigma.map_id_fst_functional f) (sigma.map_id_fst_injective f) end nodupkeys end αβ₁β₂ end list
44bb65a416c2b485b760f693df24882d0924b937
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Control/Alternative.lean
6b5754dfb89273884e796dbfb3de4fa0d775e286
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
1,291
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Core import Init.Control.Applicative universes u v class Alternative (f : Type u → Type v) extends Applicative f : Type (max (u+1) v) := (failure : ∀ {α : Type u}, f α) (orelse : ∀ {α : Type u}, f α → f α → f α) instance alternativeHasOrelse (f : Type u → Type v) (α : Type u) [Alternative f] : HasOrelse (f α) := ⟨Alternative.orelse⟩ section variables {f : Type u → Type v} [Alternative f] {α : Type u} @[inline] def failure : f α := Alternative.failure f @[inline] def guard {f : Type → Type v} [Alternative f] (p : Prop) [Decidable p] : f Unit := if p then pure () else failure @[inline] def assert {f : Type → Type v} [Alternative f] (p : Prop) [Decidable p] : f (Inhabited p) := if h : p then pure ⟨h⟩ else failure /- Later we define a coercion from Bool to Prop, but this version will still be useful. Given (t : tactic Bool), we can write t >>= guardb -/ @[inline] def guardb {f : Type → Type v} [Alternative f] : Bool → f Unit | true => pure () | false => failure @[inline] def optional (x : f α) : f (Option α) := some <$> x <|> pure none end
b0ee3fe15522f3821ad714485140e5a3c20e5050
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/char/lemmas_auto.lean
2d99e9c7f175677ab7e8d0ed73435c32d7315dd0
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,145
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.default import Mathlib.Lean3Lib.init.logic import Mathlib.Lean3Lib.init.data.nat.lemmas import Mathlib.Lean3Lib.init.data.char.basic namespace Mathlib namespace char theorem val_of_nat_eq_of_is_valid {n : ℕ} : is_valid_char n → val (of_nat n) = n := sorry theorem val_of_nat_eq_of_not_is_valid {n : ℕ} : ¬is_valid_char n → val (of_nat n) = 0 := sorry theorem of_nat_eq_of_not_is_valid {n : ℕ} : ¬is_valid_char n → of_nat n = of_nat 0 := fun (h : ¬is_valid_char n) => eq_of_veq (eq.mpr (id (Eq._oldrec (Eq.refl (val (of_nat n) = val (of_nat 0))) (val_of_nat_eq_of_not_is_valid h))) (Eq.refl 0)) theorem of_nat_ne_of_ne {n₁ : ℕ} {n₂ : ℕ} (h₁ : n₁ ≠ n₂) (h₂ : is_valid_char n₁) (h₃ : is_valid_char n₂) : of_nat n₁ ≠ of_nat n₂ := ne_of_vne (eq.mpr (id (Eq._oldrec (Eq.refl (val (of_nat n₁) ≠ val (of_nat n₂))) (val_of_nat_eq_of_is_valid h₂))) (eq.mpr (id (Eq._oldrec (Eq.refl (n₁ ≠ val (of_nat n₂))) (val_of_nat_eq_of_is_valid h₃))) h₁)) end Mathlib
07a4bd26aa4da5d90723325b6a2346ec02a1a2f5
cabd1ea95170493667c024ef2045eb86d981b133
/src/super/cdcl_solver.lean
76a062c5427b14fdb3611a05d3862350563d64a6
[]
no_license
semorrison/super
31db4b5aa5ef4c2313dc5803b8c79a95f809815b
0c6c03ba9c7470f801eb4d055294f424ff090308
refs/heads/master
1,630,272,140,541
1,511,054,739,000
1,511,054,756,000
114,317,807
0
0
null
1,513,304,776,000
1,513,304,775,000
null
UTF-8
Lean
false
false
14,255
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause open tactic expr monad super native namespace cdcl @[reducible] meta def prop_var := expr @[reducible] meta def proof_term := expr @[reducible] meta def proof_hyp := expr meta inductive trail_elem | dec : prop_var → bool → proof_hyp → trail_elem | propg : prop_var → bool → proof_term → proof_hyp → trail_elem | dbl_neg_propg : prop_var → bool → proof_term → proof_hyp → trail_elem namespace trail_elem meta def var : trail_elem → prop_var | (dec v _ _) := v | (propg v _ _ _) := v | (dbl_neg_propg v _ _ _) := v meta def phase : trail_elem → bool | (dec _ ph _) := ph | (propg _ ph _ _) := ph | (dbl_neg_propg _ ph _ _) := ph meta def hyp : trail_elem → proof_hyp | (dec _ _ h) := h | (propg _ _ _ h) := h | (dbl_neg_propg _ _ _ h) := h meta def is_decision : trail_elem → bool | (dec _ _ _) := tt | (propg _ _ _ _) := ff | (dbl_neg_propg _ _ _ _) := ff end trail_elem meta structure var_state := (phase : bool) (assigned : option proof_hyp) meta structure learned_clause := (c : clause) (actual_proof : proof_term) meta inductive prop_lit | neg : prop_var → prop_lit | pos : prop_var → prop_lit namespace prop_lit meta instance prop_lit.has_lt : has_lt prop_lit := ⟨λl₁ l₂, match l₁, l₂ with | pos _, neg _ := true | neg _, pos _ := false | pos v₁, pos v₂ := v₁ < v₂ | neg v₁, neg v₂ := v₁ < v₂ end⟩ meta instance prop_lit.decidable_lt : decidable_rel ((<) : prop_lit → prop_lit → Prop) := λl₁ l₂, match l₁, l₂ with | pos _, neg _ := is_true trivial | neg _, pos _ := is_false not_false | pos v₁, pos v₂ := expr.decidable_rel v₁ v₂ | neg v₁, neg v₂ := expr.decidable_rel v₁ v₂ end meta def of_cls_lit : clause.literal → prop_lit | (clause.literal.left v) := neg v | (clause.literal.right v) := pos v meta def of_var_and_phase (v : prop_var) : bool → prop_lit | tt := pos v | ff := neg v end prop_lit meta def watch_map := rb_map name (ℕ × ℕ × clause) meta structure state := (trail : list trail_elem) (vars : rb_map prop_var var_state) (unassigned : rb_map prop_var prop_var) (clauses : list clause) (learned : list learned_clause) (watches : rb_map prop_lit watch_map) (conflict : option proof_term) (unitp_queue : list prop_var) (local_false : expr) namespace state meta def initial (local_false : expr) : state := { trail := [], vars := rb_map.mk _ _, unassigned := rb_map.mk _ _, clauses := [], learned := [], watches := rb_map.mk _ _, conflict := none, unitp_queue := [], local_false := local_false } meta def watches_for (st : state) (pl : prop_lit) : watch_map := (st.watches.find pl).get_or_else (rb_map.mk _ _) end state meta def solver := state_t state tactic meta instance : monad solver := state_t.monad _ _ meta instance : has_monad_lift tactic solver := monad.monad_transformer_lift (state_t state) tactic meta instance (α : Type) : has_coe (tactic α) (solver α) := ⟨monad.monad_lift⟩ meta def fail {A B} [has_to_format B] (b : B) : solver A := @tactic.fail A B _ b meta def get_local_false : solver expr := do st ← state_t.read, return st.local_false meta def mk_var_core (v : prop_var) (ph : bool) : solver unit := do state_t.modify $ λst, match st.vars.find v with | (some _) := st | none := { st with vars := st.vars.insert v ⟨ph, none⟩, unassigned := st.unassigned.insert v v } end meta def mk_var (v : prop_var) : solver unit := mk_var_core v ff meta def set_conflict (proof : proof_term) : solver unit := state_t.modify $ λst, { st with conflict := some proof } meta def has_conflict : solver bool := do st ← state_t.read, return st.conflict.is_some meta def push_trail (elem : trail_elem) : solver unit := do st ← state_t.read, match st.vars.find elem.var with | none := fail $ "unknown variable: " ++ elem.var.to_string | some ⟨_, some _⟩ := fail $ "adding already assigned variable to trail: " ++ elem.var.to_string | some ⟨_, none⟩ := state_t.write { st with vars := st.vars.insert elem.var ⟨elem.phase, some elem.hyp⟩, unassigned := st.unassigned.erase elem.var, trail := elem :: st.trail, unitp_queue := elem.var :: st.unitp_queue } end meta def pop_trail_core : solver (option trail_elem) := do st ← state_t.read, match st.trail with | elem :: rest := do state_t.write { st with trail := rest, vars := st.vars.insert elem.var ⟨elem.phase, none⟩, unassigned := st.unassigned.insert elem.var elem.var, unitp_queue := [] }, return $ some elem | [] := return none end meta def is_decision_level_zero : solver bool := do st ← state_t.read, return $ st.trail.for_all $ λelem, ¬elem.is_decision meta def revert_to_decision_level_zero : unit → solver unit | () := do is_dl0 ← is_decision_level_zero, if is_dl0 then return () else do pop_trail_core, revert_to_decision_level_zero () meta def formula_of_lit (local_false : expr) (v : prop_var) (ph : bool) := if ph then v else imp v local_false meta def lookup_var (v : prop_var) : solver (option var_state) := do st ← state_t.read, return $ st.vars.find v meta def add_propagation (v : prop_var) (ph : bool) (just : proof_term) (just_is_dn : bool) : solver unit := do v_st ← lookup_var v, local_false ← get_local_false, match v_st with | none := fail $ "propagating unknown variable: " ++ v.to_string | some ⟨assg_ph, some proof⟩ := if ph = assg_ph then return () else if assg_ph ∧ ¬just_is_dn then set_conflict (app just proof) else set_conflict (app proof just) | some ⟨_, none⟩ := do hyp_name ← mk_fresh_name, hyp ← return $ local_const hyp_name hyp_name binder_info.default (formula_of_lit local_false v ph), if just_is_dn then do push_trail $ trail_elem.dbl_neg_propg v ph just hyp else do push_trail $ trail_elem.propg v ph just hyp end meta def add_decision (v : prop_var) (ph : bool) : solver unit := do hyp_name ← mk_fresh_name, local_false ← get_local_false, hyp ← return $ local_const hyp_name hyp_name binder_info.default (formula_of_lit local_false v ph), push_trail $ trail_elem.dec v ph hyp meta def lookup_lit (l : clause.literal) : solver (option (bool × proof_hyp)) := do var_st_opt ← lookup_var l.formula, match var_st_opt with | none := return none | some ⟨ph, none⟩ := return none | some ⟨ph, some proof⟩ := return $ some (if l.is_neg then bnot ph else ph, proof) end meta def lit_is_false (l : clause.literal) : solver bool := do s ← lookup_lit l, return $ match s with | some (ff, _) := tt | _ := ff end meta def lit_is_not_false (l : clause.literal) : solver bool := do isf ← lit_is_false l, return $ bnot isf meta def cls_is_false (c : clause) : solver bool := lift list.band $ mapm lit_is_false c.get_lits private meta def unit_propg_cls' : clause → solver (option prop_var) | c := if c.num_lits = 0 then return (some c.proof) else let hd := c.get_lit 0 in do lit_st ← lookup_lit hd, match lit_st with | some (ff, isf_prf) := unit_propg_cls' (c.inst isf_prf) | _ := return none end meta def unit_propg_cls : clause → solver unit | c := do has_confl ← has_conflict, if has_confl then return () else if c.num_lits = 0 then do set_conflict c.proof else let hd := c.get_lit 0 in do lit_st ← lookup_lit hd, match lit_st with | some (ff, isf_prf) := unit_propg_cls (c.inst isf_prf) | some (tt, _) := return () | none := do fls_prf_opt ← unit_propg_cls' (c.inst (expr.mk_var 0)), match fls_prf_opt with | some fls_prf := do fls_prf' ← return $ lam `H binder_info.default c.type.binding_domain fls_prf, if hd.is_neg then add_propagation hd.formula ff fls_prf' ff else add_propagation hd.formula tt fls_prf' tt | none := return () end end private meta def modify_watches_for (pl : prop_lit) (f : watch_map → watch_map) : solver unit := state_t.modify $ λst, { st with watches := st.watches.insert pl $ f $ st.watches_for pl } private meta def add_watch (n : name) (c : clause) (i j : ℕ) : solver unit := let l := c.get_lit i, pl := prop_lit.of_cls_lit l in modify_watches_for pl $ λw, w.insert n (i,j,c) private meta def remove_watch (n : name) (c : clause) (i : ℕ) : solver unit := let l := c.get_lit i, pl := prop_lit.of_cls_lit l in modify_watches_for pl $ λw, w.erase n private meta def set_watches (n : name) (c : clause) : solver unit := if c.num_lits = 0 then set_conflict c.proof else if c.num_lits = 1 then unit_propg_cls c else do not_false_lits ← filter (λi, lit_is_not_false (c.get_lit i)) (list.range c.num_lits), match not_false_lits with | [] := do add_watch n c 0 1, add_watch n c 1 0, unit_propg_cls c | [i] := let j := if i = 0 then 1 else 0 in do add_watch n c i j, add_watch n c j i, unit_propg_cls c | (i::j::_) := do add_watch n c i j, add_watch n c j i end meta def update_watches (n : name) (c : clause) (i₁ i₂ : ℕ) : solver unit := do remove_watch n c i₁, remove_watch n c i₂, set_watches n c meta def mk_clause (c : clause) : solver unit := do c : clause ← c.distinct, c.get_lits.mmap' (λl, mk_var l.formula), revert_to_decision_level_zero (), state_t.modify $ λst, { st with clauses := c :: st.clauses }, c_name ← mk_fresh_name, set_watches c_name c meta def unit_propg_var (v : prop_var) : solver unit := do st ← state_t.read, if st.conflict.is_some then return () else match st.vars.find v with | some ⟨ph, none⟩ := fail $ "propagating unassigned variable: " ++ v.to_string | none := fail $ "unknown variable: " ++ v.to_string | some ⟨ph, some _⟩ := let watches := st.watches_for $ prop_lit.of_var_and_phase v (bnot ph) in watches.to_list.mmap' $ λw, update_watches w.1 w.2.2.2 w.2.1 w.2.2.1 end meta def analyze_conflict' (local_false : expr) : proof_term → list trail_elem → clause | proof (trail_elem.dec v ph hyp :: es) := let abs_prf := abstract_local proof hyp.local_uniq_name in if has_var abs_prf then clause.close_const (analyze_conflict' proof es) hyp else analyze_conflict' proof es | proof (trail_elem.propg v ph l_prf hyp :: es) := let abs_prf := abstract_local proof hyp.local_uniq_name in if has_var abs_prf then analyze_conflict' (app (lam hyp.local_pp_name binder_info.default (formula_of_lit local_false v ph) abs_prf) l_prf) es else analyze_conflict' proof es | proof (trail_elem.dbl_neg_propg v ph l_prf hyp :: es) := let abs_prf := abstract_local proof hyp.local_uniq_name in if has_var abs_prf then analyze_conflict' (app l_prf (lambdas [hyp] proof)) es else analyze_conflict' proof es | proof [] := ⟨0, 0, proof, local_false, local_false⟩ meta def analyze_conflict (proof : proof_term) : solver clause := do st ← state_t.read, return $ analyze_conflict' st.local_false proof st.trail meta def add_learned (c : clause) : solver unit := do prf_abbrev_name ← mk_fresh_name, c' ← return { c with proof := local_const prf_abbrev_name prf_abbrev_name binder_info.default c.type }, state_t.modify $ λst, { st with learned := ⟨c', c.proof⟩ :: st.learned }, c_name ← mk_fresh_name, set_watches c_name c' meta def backtrack_with : clause → solver unit | conflict_clause := do isf ← cls_is_false conflict_clause, if ¬isf then state_t.modify (λst, { st with conflict := none }) else do removed_elem ← pop_trail_core, if removed_elem.is_some then backtrack_with conflict_clause else return () meta def replace_learned_clauses' : proof_term → list learned_clause → proof_term | proof [] := proof | proof (⟨c, actual_proof⟩ :: lcs) := let abs_prf := abstract_local proof c.proof.local_uniq_name in if has_var abs_prf then replace_learned_clauses' (elet c.proof.local_pp_name c.type actual_proof abs_prf) lcs else replace_learned_clauses' proof lcs meta def replace_learned_clauses (proof : proof_term) : solver proof_term := do st ← state_t.read, return $ replace_learned_clauses' proof st.learned meta inductive result | unsat : proof_term → result | sat : rb_map prop_var bool → result variable theory_solver : solver (option proof_term) meta def unit_propg : unit → solver unit | () := do st ← state_t.read, if st.conflict.is_some then return () else match st.unitp_queue with | [] := return () | (v::vs) := do state_t.write { st with unitp_queue := vs }, unit_propg_var v, unit_propg () end private meta def run' : unit → solver result | () := do unit_propg (), st ← state_t.read, match st.conflict with | some conflict := do conflict_clause ← analyze_conflict conflict, if conflict_clause.num_lits = 0 then do proof ← replace_learned_clauses conflict_clause.proof, return (result.unsat proof) else do backtrack_with conflict_clause, add_learned conflict_clause, run' () | none := match st.unassigned.min with | none := do theory_conflict ← theory_solver, match theory_conflict with | some conflict := do set_conflict conflict, run' () | none := return $ result.sat (st.vars.map (λvar_st, var_st.phase)) end | some unassigned := match st.vars.find unassigned with | some ⟨ph, none⟩ := do add_decision unassigned ph, run' () | _ := fail $ "unassigned variable is assigned: " ++ unassigned.to_string end end end meta def run : solver result := run' theory_solver () meta def solve (local_false : expr) (clauses : list clause) : tactic result := do res ← (do clauses.mmap' mk_clause, run theory_solver) (state.initial local_false), return res.1 meta def theory_solver_of_tactic (th_solver : tactic unit) : cdcl.solver (option cdcl.proof_term) := do s ← state_t.read, ↑do hyps ← return $ s.trail.map (λe, e.hyp), subgoal ← mk_meta_var s.local_false, goals ← get_goals, set_goals [subgoal], hvs ← hyps.mmap' (λhyp, assertv hyp.local_pp_name hyp.local_type hyp), solved ← (do th_solver, done, return tt) <|> return ff, set_goals goals, if solved then do proof ← instantiate_mvars subgoal, proof' ← whnf proof, -- gets rid of the unnecessary asserts return $ some proof' else return none end cdcl