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
249ddb8397b0eb9171c7d0a0db519caba7bbcc66
7bf54883c04ff2856c37f76a79599ceb380c1996
/non-mathlib/cubic_alternate.lean
bc0ecdbf860c043e11281eef9f443fa22ad67910
[]
no_license
anonymousLeanDocsHosting/lean-polynomials
4094466bf19acc0d1a47b4cabb60d8c21ddb2b00
361ef4cb7b68ef47d43b85cfa2d13f2ea0a47613
refs/heads/main
1,691,112,899,935
1,631,819,522,000
1,631,819,522,000
405,448,439
0
2
null
null
null
null
UTF-8
Lean
false
false
28,212
lean
import field_definition import field_results import numbers import roots import quadratic lemma multiply_out_cubed (f : Type) [myfld f] (x a : f) : (cubed f (x .+ a)) = ((cubed f x) .+ (((three f) .* ((square f x) .* a)) .+ (((three f) .* ((x .* a) .* a)) .+ (cubed f a)))) := begin unfold cubed, unfold three, unfold square, repeat {rw distrib_simp f _ _ _}, repeat {rw distrib_simp_alt f _ _ _}, rw one_mul_simp f _, rw myfld.mul_assoc _ _ _, rw myfld.mul_comm a x, rw myfld.mul_assoc a x a, rw myfld.mul_comm a x, rw myfld.mul_assoc _ _ _, rw myfld.mul_assoc a x x, rw myfld.mul_comm a x, rw <- myfld.mul_assoc x a x, rw myfld.mul_comm a x, repeat {rw myfld.mul_assoc _ _ _}, repeat {rw <- myfld.add_assoc _ _ _}, rw myfld.add_assoc ((x .* a) .* a) ((x .* x) .* a) _, rw myfld.add_comm ((x .* a) .* a) ((x .* x) .* a), repeat {rw <- myfld.add_assoc _ _ _}, rw one_mul_simp f _, end def depressed_cubic_subst (f : Type) [myfld f] (c d x : f) : f := (cubed f x) .+ ((c .* x) .+ d) lemma depressed_cubic_solution_two_vars (f : Type) [myfld f] (x c d s t : f) : (x = myfld.add s (myfld.negate t)) /\ c = (myfld.mul (three f) (myfld.mul s t)) /\ d = (myfld.add (cubed f s) (myfld.negate (cubed f t))) -> (myfld.add (cubed f x) (myfld.mul c x) = d) := begin intros h, cases h with hx h, cases h with ha hb, rw hx, rw ha, rw hb, rw multiply_out_cubed f s (myfld.negate t), rw distrib_simp_alt f _ _ _, repeat {rw <- mul_negate_alt f _ _}, repeat {rw mul_negate f _ _}, repeat {rw <- mul_negate_alt f _ _}, rw double_negative f _, have tmp : (cubed f (myfld.negate t)) = myfld.negate (cubed f t), unfold cubed, repeat {rw mul_negate_alt_simp f _ _}, repeat {rw mul_negate f _ _}, rw double_negative f _, rw mul_negate_alt_simp f _ _, rw tmp, clear tmp, rw <- myfld.mul_assoc _ (s .* t) s, rw myfld.mul_comm (s .* t) s, unfold square, repeat {rw myfld.mul_assoc}, rw myfld.add_assoc _ (three f) .* s .* s .* t _, rw myfld.add_comm _ (three f) .* s .* s .* t, repeat {rw <- myfld.add_assoc}, rw myfld.add_assoc _ (.- ((three f) .* s .* s .* t)) _, rw myfld.add_comm _ (.- ((three f) .* s .* s .* t)), repeat {rw myfld.add_assoc}, rw myfld.add_negate, rw simp_zero, rw myfld.add_comm _ (.- ((three f) .* s .* t .* t)), rw myfld.add_comm _ (three f) .* s .* t .* t, repeat {rw myfld.add_assoc}, rw myfld.add_comm (.- ((three f) .* s .* t .* t)) _, rw myfld.add_negate, rw simp_zero, end lemma depressed_cubic_simultaneous_solution (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d s t : f) (c_ne_zero : c ≠ myfld.zero) (s_ne_zero : s ≠ myfld.zero): (s = fld_with_cube_root.cubrt (quadratic_formula f myfld.one (.- d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one)) -> (t = myfld.mul c (myfld.reciprocal ((three f) .* s) (mul_nonzero f (three f) s fld_not_char_three.not_char_three s_ne_zero))) -> ((c = myfld.mul (three f) (myfld.mul s t)) /\ (d = myfld.add (cubed f s) (myfld.negate (cubed f t)))) := begin intros hs ht, have hs_tmp : (cubed f s) = (quadratic_formula f myfld.one (.- d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) _)) myfld.zero_distinct_one), rw hs, rw cubrt_cubed, clear hs, rename hs_tmp hs, split, rw ht, rw myfld.mul_assoc, rw myfld.mul_comm c _, rw myfld.mul_assoc, rw myfld.mul_reciprocal, rw one_mul_simp, have ht_tmp : (cubed f t) = (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)) .* (myfld.reciprocal (cubed f s) (cube_nonzero f s s_ne_zero)), rw ht, rw cube_of_product, rw cube_of_reciprocal, rw reciprocal_rewrite f _ _ (cube_of_product f _ _), rw split_reciprocal f, rw reciprocal_rewrite f _ _ (three_cubed f) _, rw only_one_reciprocal f (twenty_seven f) _ (twenty_seven_ne_zero f), rw only_one_reciprocal f (cubed f s) _ (cube_nonzero f s s_ne_zero), rw myfld.mul_assoc, clear ht, rename ht_tmp ht, rw ht, rw mul_both_sides f _ _ (cubed f s) (cube_nonzero f s s_ne_zero), rw carry_term_across_alt, rw distrib_simp, symmetry, rw <- mul_negate f d (cubed f s), rw mul_negate, repeat {rw <- myfld.mul_assoc}, rw myfld.mul_comm (myfld.reciprocal _ _) (cubed f _), rw myfld.mul_reciprocal, rw simp_mul_one, rw <- myfld.add_assoc, rw myfld.add_comm _ (.-d .* _), have tmp : quadratic_subst f (cubed f s) myfld.one (.- d) (.- ((cubed f c) .* (myfld.reciprocal (twenty_seven f) _))) = myfld.zero, rw hs, rw only_one_reciprocal f (twenty_seven f) _ (twenty_seven_ne_zero f), rw mul_negate, exact quadratic_formula_works f myfld.one (.- d) (.- ((cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)))) myfld.zero_distinct_one, unfold quadratic_subst at tmp, rw one_mul_simp at tmp, exact tmp, end lemma cardano_den_ne_zero (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) : (quadratic_formula f myfld.one (d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one ≠ myfld.zero) := begin unfold quadratic_formula, apply mul_nonzero, intros h, rw carry_term_across at h, rw simp_zero at h, have tmp : ∀ (a b : f), (.- a = .- b) <-> (a = b), intros a b, split, intros h, have tmp_tmp : .- .- a = .- .- b, rw h, repeat {rw double_negative at tmp_tmp}, exact tmp_tmp, intros h, rw h, rw tmp at h, clear tmp, have h_tmp : d .* d = sqroot (d .* d .+ .- ((four f) .* (myfld.one .* ( .- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)))))) .* sqroot (d .* d .+ .- ((four f) .* (myfld.one .* ( .- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)))))), rw <- h, clear h, rename h_tmp h, rw fld_with_sqrt.sqrt_mul_sqrt at h, have tmp : ∀ (a b : f), (a = a .+ b) <-> (b = myfld.zero), intros a b, split, intros h, have tmp_tmp : (.- a) .+ a = (.- a) .+ (a .+ b), rw <- h, rw myfld.add_assoc at tmp_tmp, rw myfld.add_comm (.- a) a at tmp_tmp, rw myfld.add_negate a at tmp_tmp, rw simp_zero at tmp_tmp, symmetry, exact tmp_tmp, intros h, rw h, rw zero_simp, rw tmp at h, clear tmp, rw one_mul_simp at h, repeat {rw mul_negate at h}, rw mul_negate_alt_simp at h, rw double_negative at h, exact (mul_nonzero f (four f) (_ .* _) (four_ne_zero f) (mul_nonzero f _ _ (cube_nonzero f c c_ne_zero) (reciprocal_ne_zero f _ _))) h, exact reciprocal_ne_zero f _ _, end def cardano_formula_new (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) (cubrt_func : f -> f) (cubrt_func_nonzero : ∀ (x : f), x ≠ myfld.zero -> (cubrt_func x ≠ myfld.zero)) (cubrt_func_correct : ∀ (x : f), (cubed f (cubrt_func x)) = x) : f := (fld_with_cube_root.cubrt (quadratic_formula f myfld.one d (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one)) .+ .- (myfld.mul c (myfld.reciprocal ((three f) .* (fld_with_cube_root.cubrt (quadratic_formula f myfld.one d (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one))) (mul_nonzero f (three f) (fld_with_cube_root.cubrt (quadratic_formula f myfld.one d (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one)) fld_not_char_three.not_char_three (cubrt_nonzero f (quadratic_formula f myfld.one (d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one) (cardano_den_ne_zero f c d c_ne_zero))))) def cardano_formula_a_new (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) : f := cardano_formula_new f c d c_ne_zero fld_with_cube_root.cubrt (cubrt_nonzero f) (cubrt_cubed f) def cardano_formula_b_new (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) : f := cardano_formula_new f c d c_ne_zero (alt_cubrt_a f) (alt_cubrt_a_nonzero f) (alt_cubrt_a_correct f) def cardano_formula_c_new (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) : f := cardano_formula_new f c d c_ne_zero (alt_cubrt_b f) (alt_cubrt_b_nonzero f) (alt_cubrt_b_correct f) lemma cardano_works (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) (cubrt_func : f -> f) (cubrt_func_nonzero : ∀ (x : f), x ≠ myfld.zero -> (cubrt_func x ≠ myfld.zero)) (cubrt_func_correct : ∀ (x : f), (cubed f (cubrt_func x)) = x) : depressed_cubic_subst f c d (cardano_formula_new f c d c_ne_zero cubrt_func cubrt_func_nonzero cubrt_func_correct) = myfld.zero := begin have clear_term : ∀ (a b x : f), a = b -> (x .+ a) = (x .+ b), intros a b x h, rw h, unfold depressed_cubic_subst, rw myfld.add_assoc, rw carry_term_across, rw simp_zero, apply depressed_cubic_solution_two_vars f (cardano_formula_new f c d c_ne_zero cubrt_func cubrt_func_nonzero cubrt_func_correct) c (.- d) (fld_with_cube_root.cubrt (quadratic_formula f myfld.one (d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one)) ((myfld.mul c (myfld.reciprocal ((three f) .* (fld_with_cube_root.cubrt (quadratic_formula f myfld.one (d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one))) (mul_nonzero f (three f) (fld_with_cube_root.cubrt (quadratic_formula f myfld.one (d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one)) fld_not_char_three.not_char_three (cubrt_nonzero f (quadratic_formula f myfld.one (d) (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one) (cardano_den_ne_zero f c d c_ne_zero)))))), split, unfold cardano_formula_new, apply depressed_cubic_simultaneous_solution, exact c_ne_zero, apply cubrt_eq, rw double_negative, refl, end def cubic_subst (f : Type) [myfld f] (a b c d x : f) : f := (a .* (cubed f x)) .+ ((b .* (square f x)) .+ ((c .* x) .+ d)) lemma divide_cubic_through (f : Type) [myfld f] (a b c d x : f) (a_ne_zero : a ≠ myfld.zero) : (cubic_subst f a b c d x) = myfld.zero <-> (cubic_subst f myfld.one (b .* (myfld.reciprocal a a_ne_zero)) (c .* (myfld.reciprocal a a_ne_zero)) (d .* (myfld.reciprocal a a_ne_zero)) x) = myfld.zero := begin unfold cubic_subst, split, intros h, have h1 : (((a .* (cubed f x)) .+ ((b .* (square f x)) .+ ((c .* x) .+ d))) .* (myfld.reciprocal a a_ne_zero)) = myfld.zero, rw h, rw mul_zero, clear h, rename h1 h, repeat {rw distrib_simp f _ _ _ at h}, repeat {rw <- myfld.mul_assoc _ _ (myfld.reciprocal a a_ne_zero) at h}, repeat {rw myfld.mul_comm _ (myfld.reciprocal a a_ne_zero) at h}, rw myfld.mul_assoc a (myfld.reciprocal a a_ne_zero) _ at h, rw myfld.mul_reciprocal a a_ne_zero at h, rw myfld.mul_comm d (myfld.reciprocal a a_ne_zero), repeat {rw myfld.mul_assoc _ _ _ at h}, repeat {rw myfld.mul_assoc _ _ _}, exact h, intros h, have h1 : (((myfld.one .* (cubed f x)) .+ (((b .* (myfld.reciprocal a a_ne_zero)) .* (square f x)) .+ (((c .* (myfld.reciprocal a a_ne_zero)) .* x) .+ (d .* (myfld.reciprocal a a_ne_zero))))) .* a) = myfld.zero, rw h, rw mul_zero, clear h, rename h1 h, repeat {rw distrib_simp f _ _ _ at h}, rw <- myfld.mul_assoc _ (myfld.reciprocal a a_ne_zero) a at h, rw myfld.mul_comm (myfld.reciprocal a a_ne_zero) a at h, rw myfld.mul_reciprocal a a_ne_zero at h, rw simp_mul_one at h, repeat {rw <- myfld.mul_assoc _ _ a at h}, repeat {rw myfld.mul_comm _ a at h}, repeat {rw myfld.mul_assoc _ a _ at h}, repeat {rw <- myfld.mul_assoc _ _ a at h}, repeat {rw myfld.mul_comm (myfld.reciprocal a a_ne_zero) a at h}, rw myfld.mul_reciprocal a a_ne_zero at h, repeat {rw simp_mul_one at h}, rw one_mul_simp at h, exact h, end def third (f : Type) [myfld f] [fld_not_char_three f] : f := (myfld.reciprocal (three f) fld_not_char_three.not_char_three) def twenty_seventh (f : Type) [myfld f] [fld_not_char_three f] : f := (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)) /- This looks weird and pointless but it simplifies a proof later.-/ lemma depressed_cubic_equal_split (f : Type) [myfld f] (c1 d1 c2 d2 x : f) : (c1 = c2) /\ (d1 = d2) -> (depressed_cubic_subst f c1 d1 x) = (depressed_cubic_subst f c2 d2 x) := begin intros h, cases h with h1 h2, rw h1, rw h2, end /- This is used in the next proof (reduce_cubic_to_depressed) . It could have been done with a "have", but the next proof is long enough that my computer was struggling with it, so it seemed worth moving this bit out.-/ lemma helper_bcubed (f : Type) [myfld f] [fld_not_char_three f] (b : f) : (b .* ((third f) .* (b .* ((third f) .* b)))) = ((three f) .* ((myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)) .* ((b .* b) .* b))) := begin unfold third, unfold twenty_seven, unfold nine, rw split_reciprocal f _ _ _, rw <- myfld.mul_assoc (myfld.reciprocal (three f) _), rw myfld.mul_assoc (three f) (myfld.reciprocal (three f) _) _, rw myfld.mul_reciprocal (three f) _, rw one_mul_simp f _, repeat {rw myfld.mul_comm b (_ .* _) }, repeat {rw myfld.mul_assoc _ _ _}, rw mul_two_reciprocals f _ _ _ _, end /- Ditto.-/ lemma helper_twothirds (f : Type) [myfld f] [fld_not_char_three f] : ((.- (third f)) .+ myfld.one) = (third f) .+ (third f) := begin have h : myfld.one = (third f) .* (three f) , unfold third, rw myfld.mul_comm, symmetry, exact myfld.mul_reciprocal (three f) _, rw h, clear h, have h : (.- (third f)) = (third f) .* (.- myfld.one) , rw <- mul_negate_alt f _ _, rw simp_mul_one f _, rw h, clear h, rw <- distrib_simp_alt f _ _ _, unfold third, unfold three, rw myfld.add_assoc (.- myfld.one) myfld.one _, rw myfld.add_comm (.- myfld.one) myfld.one, rw myfld.add_negate myfld.one, rw simp_zero f _, rw distrib_simp_alt f _ _ _, rw simp_mul_one f _, end lemma reduce_cubic_to_depressed (f : Type) [myfld f] [fld_not_char_three f] (b c d x y : f) : x = y .+ (.- (b .* (third f))) -> (cubic_subst f myfld.one b c d x) = (depressed_cubic_subst f (((square f b) .* (.- (third f))) .+ c) ((twenty_seventh f) .* ((((two f) .* (cubed f b)) .+ (.- ((nine f) .* (b .* c)))) .+ ((twenty_seven f) .* d))) y) := begin intros h, rw h, clear h, unfold cubic_subst, rw multiply_out_cubed f y _, rw multiply_out_squared f _ _, rw one_mul_simp f _, repeat {rw distrib_simp_alt f _ _ _}, rw mul_negate f _ _, repeat {rw mul_negate_alt_simp f _ _}, repeat {rw mul_negate f _ _}, rw mul_negate_alt_simp f _ _, rw double_negative f _, /- First job is to cancel the terms that have 3 * (1/3) in them.-/ have third_three : (three f) .* (third f) = myfld.one, unfold third, rw myfld.mul_reciprocal (three f) _, rw myfld.mul_comm b (third f), rw myfld.mul_assoc _ (third f) _, rw myfld.mul_comm _ (third f), rw <- myfld.mul_assoc (third f) _ _, rw myfld.mul_assoc y (third f) b, rw myfld.mul_comm y (third f), rw <- myfld.mul_assoc (third f) y b, rw <- myfld.mul_assoc (third f) (y .* b) _, repeat {rw myfld.mul_assoc (three f) (third f) _}, rw third_three, clear third_three, repeat {rw one_mul_simp f _}, /- Now we have a term for by^2 and -by^2, so these cancel.-/ unfold square, rw myfld.mul_comm b (y .* y), rw myfld.add_comm (.- ((y .* y) .* b)) _, repeat {rw <- myfld.add_assoc}, rw myfld.add_assoc (.- ((y .* y) .* b)) ((y .* y) .* b) _, rw myfld.add_comm (.- ((y .* y) .* b)) ((y .* y) .* b), rw myfld.add_negate ((y .* y) .* b), rw simp_zero, /- This ultimately needs to be made equivalent to the depressed cubic formula, so we gather all the terms that are multiplied by y together.-/ rw myfld.mul_comm _ y, repeat {rw myfld.mul_assoc _ y _, rw myfld.mul_comm _ y}, rw <- myfld.mul_assoc y (two f) _, rw myfld.mul_assoc _ y _, rw myfld.mul_comm b y, repeat {rw <- myfld.mul_assoc y b _}, rw mul_negate_alt f y _, rw myfld.mul_comm c y, rw myfld.add_assoc (cubed f (.- ((third f) .* b))) (y .* _) _, rw myfld.add_comm _ (y .* (.- (b .* ((two f) .* ((third f) .* b))))), rw <- myfld.add_assoc (y .* (.- (b .* ((two f) .* ((third f) .* b))))) _ _, rw myfld.add_assoc (y .* _) (y .* _), rw <- distrib_simp_alt f y _ _, rw myfld.add_assoc _ (y .* c) _, rw myfld.add_comm _ (y .* c), rw <- myfld.add_assoc (y .* c) _ _, rw myfld.add_assoc _ (y .* c) _, rw myfld.add_comm _ (y .* c), rw <- myfld.add_assoc (y .* c) _ _, rw myfld.add_assoc _ (y .* c) _, rw <- distrib_simp_alt f y _ c, /- We can now fold this back into the depressed cubic formula, which will make the remaining algebra slightly easier to follow.-/ rw myfld.mul_comm y _, /- The following line is ugly, but it's just a copy-paste of the algebra in the sidebar.-/ have tmp : (cubed f y) .+ (((((b .* ((third f) .* b)) .+ (.- (b .* ((two f) .* ((third f) .* b))))) .+ c) .* y) .+ ((cubed f (.- ((third f) .* b))) .+ ((b .* ((.- ((third f) .* b)) .* (.- ((third f) .* b)))) .+ ((.- (c .* ((third f) .* b))) .+ d)))) = depressed_cubic_subst f (((b .* ((third f) .* b)) .+ (.- (b .* ((two f) .* ((third f) .* b))))) .+ c) ((cubed f (.- ((third f) .* b))) .+ ((b .* ((.- ((third f) .* b)) .* (.- ((third f) .* b)))) .+ ((.- (c .* ((third f) .* b))) .+ d))) y, unfold depressed_cubic_subst, rw tmp, clear tmp, /- This enables us to break down into two goals (one for the y term and one for the constant.) -/ apply depressed_cubic_equal_split f _ _ _ _ y, split, /- (b^2) /3 - 2 (b/^2) 3 = - (b^2) /3, which solves the y term goal.-/ rw myfld.mul_comm _ b, rw myfld.mul_comm _ (b .* _), rw myfld.mul_assoc _ b _, rw myfld.mul_comm (two f) b, rw <- myfld.mul_assoc b (two f) _, rw myfld.mul_comm (b .* (third f)) b, repeat {rw myfld.mul_assoc b b _}, rw mul_negate_alt f (b .* b) _, rw <- distrib_simp_alt f (b .* b) _ _, rw myfld.mul_comm (two f) (third f), have tmp : (third f) = ((third f) .* myfld.one), exact myfld.mul_one (third f), rewrite [tmp] {occs := occurrences.pos [1]}, clear tmp, rw mul_negate_alt f (third f) (two f), rw <- distrib_simp_alt f (third f) _ _, have tmp : myfld.one .+ (.- (two f)) = .- (myfld.one), unfold two, rw add_negate f myfld.one myfld.one, rw myfld.add_assoc myfld.one (.- myfld.one) (.- myfld.one), rw myfld.add_negate myfld.one, rw <- zero_add f (.- myfld.one), rw tmp, clear tmp, rw <- mul_negate_alt f (third f) myfld.one, rw simp_mul_one f _, rw <- mul_negate_alt f (b .* b) (third f), /- The constant goal is a bit more complicated, but all it really involves is collecting together the b^3 terms and reducing the coefficient of each term to a single power of three.-/ unfold cubed, repeat {rw mul_negate f _ _}, repeat {rw mul_negate_alt_simp f _ _}, repeat {rw double_negative f _}, rw myfld.mul_assoc (twenty_seventh f) (twenty_seven f) _, rw myfld.mul_comm (twenty_seventh f) (twenty_seven f), unfold twenty_seventh, rw myfld.mul_reciprocal (twenty_seven f) _, rw one_mul_simp f _, rw myfld.mul_assoc (myfld.reciprocal (twenty_seven f) _) (nine f), rw only_one_reciprocal f (twenty_seven f) _ (twenty_seven_ne_zero f), have tmp : ((myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f)) .* (nine f)) = (myfld.reciprocal (three f) fld_not_char_three.not_char_three), unfold twenty_seven, unfold nine, rw split_reciprocal f _ _ _, rw split_reciprocal f _ _ _, repeat {rw <- myfld.mul_assoc _ _ _}, rw myfld.mul_assoc (myfld.reciprocal _ _) (three f) _, rw myfld.mul_comm (myfld.reciprocal (three f) _) (three f), rw myfld.mul_reciprocal (three f) _, rw one_mul_simp f _, rw myfld.mul_comm (myfld.reciprocal (three f) _) (three f), rw myfld.mul_reciprocal (three f) _, rw simp_mul_one f _, rw myfld.mul_assoc (myfld.reciprocal (twenty_seven f) _) (nine f) _, rw tmp, clear tmp, rw myfld.mul_assoc _ (two f) _, rw myfld.mul_comm _ (two f), rw <- myfld.mul_assoc (two f) _ _, unfold two, rw distrib_simp f myfld.one myfld.one _, rw one_mul_simp f _, rw myfld.add_assoc _ (b .* ((third f) .* (b .* ((third f) .* b)))) _, rw helper_bcubed f b, have tmp : ((three f) .* ((myfld.reciprocal (twenty_seven f) _) .* ((b .* b) .* b))) = (myfld.one .* ((three f) .* ((myfld.reciprocal (twenty_seven f) _) .* ((b .* b) .* b)))), rw one_mul_simp f _, rewrite [tmp] {occs := occurrences.pos [2]}, clear tmp, rw <- mul_negate f (third f) _, rw <- distrib_simp f _ _ _, rw helper_twothirds f, rw distrib_simp f (third f) (third f) _, rw myfld.mul_assoc (third f) (three f) _, have tmp : (third f) .* (three f) = myfld.one, unfold third, rw myfld.mul_comm _ _, rw myfld.mul_reciprocal (three f) _, rw tmp, rw one_mul_simp f _, clear tmp, rw myfld.mul_assoc c (third f) b, rw myfld.mul_comm c (third f), rw <- myfld.mul_assoc (third f) c b, rw myfld.mul_comm c b, rw myfld.mul_assoc b b b, unfold third, end lemma int_quantity_ne_zero_simp (f : Type) [myfld f] [fld_not_char_three f] (a1 b c : f) (a_ne_zero : a1 ≠ myfld.zero) : ((three f) .* (a1 .* c)) .+ (.- (square f b)) ≠ myfld.zero -> (((square f (b .* (myfld.reciprocal a1 a_ne_zero))) .* (.- (third f))) .+ (c .* (myfld.reciprocal a1 a_ne_zero))) ≠ myfld.zero := begin intros h1 h2, unfold square at h2, rw myfld.mul_comm _ (.- _) at h2, repeat {rw myfld.mul_assoc _ _ (myfld.reciprocal a1 a_ne_zero) at h2}, rw <- distrib_simp f _ _ _ at h2, rw <- mul_zero_by_reciprocal f a1 _ _ at h2, rw mul_both_sides f _ _ (three f) at h2, rw mul_zero at h2, rw distrib_simp at h2, repeat {rw mul_negate at h2}, rw myfld.mul_comm (_ .* _) (three f) at h2, rw myfld.mul_assoc (three f) (third f) _ at h2, unfold third at h2, rw myfld.mul_reciprocal (three f) _ at h2, rw one_mul_simp at h2, rw mul_both_sides f _ _ a1 at h2, rw distrib_simp at h2, rw mul_zero at h2, rw mul_negate at h2, rw myfld.mul_comm _ b at h2, repeat {rw <- myfld.mul_assoc at h2}, rw myfld.mul_comm _ a1 at h2, rw myfld.mul_reciprocal a1 _ at h2, rw simp_mul_one at h2, unfold square at h1, rw myfld.mul_comm c _ at h2, rw myfld.mul_assoc at h1, rw myfld.add_comm at h1, cc, cc, exact fld_not_char_three.not_char_three, end def cubic_formula_a (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (a b c d : f) (a_ne_zero : a ≠ myfld.zero) (int_quantity_ne_zero : ((three f) .* (a .* c)) .+ (.- (square f b)) ≠ myfld.zero) : f := ((cardano_formula_a_new f (((square f (b .* (myfld.reciprocal a a_ne_zero))) .* (.- (third f))) .+ (c .* (myfld.reciprocal a a_ne_zero))) ((twenty_seventh f) .* ((((two f) .* (cubed f (b .* (myfld.reciprocal a a_ne_zero)))) .+ (.- ((nine f) .* ((b .* (myfld.reciprocal a a_ne_zero)) .* (c .* (myfld.reciprocal a a_ne_zero)))))) .+ ((twenty_seven f) .* (d .* (myfld.reciprocal a a_ne_zero))))) (int_quantity_ne_zero_simp f a b c a_ne_zero int_quantity_ne_zero)) .+ (.- ((b .* (myfld.reciprocal a a_ne_zero)) .* (third f)))) lemma cubic_formula_a_correct (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (a b c d : f) (a_ne_zero : a ≠ myfld.zero) (int_quantity_ne_zero : ((three f) .* (a .* c)) .+ (.- (square f b)) ≠ myfld.zero) : cubic_subst f a b c d (cubic_formula_a f a b c d a_ne_zero int_quantity_ne_zero) = myfld.zero := begin rw divide_cubic_through f a b c d _ a_ne_zero, have tmp : (cubic_formula_a f a b c d a_ne_zero int_quantity_ne_zero) = (((cubic_formula_a f a b c d a_ne_zero int_quantity_ne_zero) .+ ((b .* (myfld.reciprocal a a_ne_zero)) .* (third f))) .+ (.- ((b .* (myfld.reciprocal a a_ne_zero)) .* (third f)))), simp, rw reduce_cubic_to_depressed f (b .* (myfld.reciprocal a a_ne_zero)) (c .* (myfld.reciprocal a a_ne_zero)) (d .* (myfld.reciprocal a a_ne_zero)) (cubic_formula_a f a b c d a_ne_zero int_quantity_ne_zero) ((cubic_formula_a f a b c d a_ne_zero int_quantity_ne_zero) .+ ((b .* (myfld.reciprocal a a_ne_zero)) .* (third f))) tmp, clear tmp, unfold cubic_formula_a, unfold cardano_formula_a_new, rw <- myfld.add_assoc (cardano_formula_new f _ _ _ _ _ _) (.- _) _, rw myfld.add_comm (.- _) _, rw myfld.add_negate _, rw zero_simp f _, exact cardano_works f _ _ _ _ _ _, end /- I was hoping to be able to prove the formulae formations equivalent, but that's unfortunately impossible. We end up needing to prove that sqrt (x)/2 = sqrt(x/4). This is obviously true for any sensible sqrt function, but our minimalist specification of square roots means it isn't necessarily true of any valid implementation.-/ /- def cardano_formula_old (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) (cubrt_func : f -> f) (cubrt_func_nonzero : ∀ (x : f), x ≠ myfld.zero -> (cubrt_func x ≠ myfld.zero)) (cubrt_func_correct : ∀ (x : f), (cubed f (cubrt_func x)) = x) : f := ((cubrt_func ((cardano_intermediate_val f c d) .+ (cardano_other_int_val f d))) .+ (.- (c .* (myfld.reciprocal ((cubrt_func ((cardano_intermediate_val f c d) .+ (cardano_other_int_val f d))) .* (three f)) (cardano_denominator_not_zero f c d c_ne_zero cubrt_func cubrt_func_nonzero))))) lemma cardano_versions_equiv (f : Type) [myfld f] [fld_with_sqrt f] [fld_with_cube_root f] [fld_not_char_two f] [fld_not_char_three f] (c d : f) (c_ne_zero : c ≠ myfld.zero) (cubrt_func : f -> f) (cubrt_func_nonzero : ∀ (x : f), x ≠ myfld.zero -> (cubrt_func x ≠ myfld.zero)) (cubrt_func_correct : ∀ (x : f), (cubed f (cubrt_func x)) = x) : (cardano_formula_new f c d c_ne_zero cubrt_func cubrt_func_nonzero cubrt_func_correct) = (cardano_formula_old f c d c_ne_zero cubrt_func cubrt_func_nonzero cubrt_func_correct) := begin have clear_term : ∀ (a b x : f), a = b -> (x .+ a) = (x .+ b), intros a b x h, rw h, unfold cardano_formula_new, unfold cardano_formula_old, have reduce_1 : ((quadratic_formula f myfld.one d (.- (cubed f c) .* (myfld.reciprocal (twenty_seven f) (twenty_seven_ne_zero f))) myfld.zero_distinct_one)) = (cardano_intermediate_val f c d) .+ (cardano_other_int_val f d), unfold quadratic_formula, unfold cardano_intermediate_val, unfold cardano_other_int_val, rw one_mul_simp, rw reciprocal_rewrite f _ _ (simp_mul_one f _), rw distrib_simp, repeat {rw mul_negate}, repeat {rw mul_negate_alt_simp}, rw double_negative, rw myfld.add_comm _ (.- (d .* (myfld.reciprocal (two f) fld_not_char_two.not_char_two))), rw only_one_reciprocal f (two f) _ (fld_not_char_two.not_char_two), apply clear_term, end -/
201acd46ca3cc385d978c848ed088641d8cb80f3
6b02ce66658141f3e0aa3dfa88cd30bbbb24d17b
/src/Init/Data/Array/Basic.lean
a24ee3b74d20935828e4cefef3f3a337ab81ef06
[ "Apache-2.0" ]
permissive
pbrinkmeier/lean4
d31991fd64095e64490cb7157bcc6803f9c48af4
32fd82efc2eaf1232299e930ec16624b370eac39
refs/heads/master
1,681,364,001,662
1,618,425,427,000
1,618,425,427,000
358,314,562
0
0
Apache-2.0
1,618,504,558,000
1,618,501,999,000
null
UTF-8
Lean
false
false
27,800
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Nat.Basic import Init.Data.Fin.Basic import Init.Data.UInt import Init.Data.Repr import Init.Data.ToString.Basic import Init.Util universes u v w namespace Array variable {α : Type u} @[extern "lean_mk_array"] def mkArray {α : Type u} (n : Nat) (v : α) : Array α := { data := List.replicate n v } @[simp] theorem size_mkArray (n : Nat) (v : α) : (mkArray n v).size = n := List.length_replicate .. instance : EmptyCollection (Array α) := ⟨Array.empty⟩ instance : Inhabited (Array α) where default := Array.empty def isEmpty (a : Array α) : Bool := a.size = 0 def singleton (v : α) : Array α := mkArray 1 v /- Low-level version of `fget` which is as fast as a C array read. `Fin` values are represented as tag pointers in the Lean runtime. Thus, `fget` may be slightly slower than `uget`. -/ @[extern "lean_array_uget"] def uget (a : @& Array α) (i : USize) (h : i.toNat < a.size) : α := a.get ⟨i.toNat, h⟩ def back [Inhabited α] (a : Array α) : α := a.get! (a.size - 1) def get? (a : Array α) (i : Nat) : Option α := if h : i < a.size then some (a.get ⟨i, h⟩) else none def back? (a : Array α) : Option α := a.get? (a.size - 1) -- auxiliary declaration used in the equation compiler when pattern matching array literals. abbrev getLit {α : Type u} {n : Nat} (a : Array α) (i : Nat) (h₁ : a.size = n) (h₂ : i < n) : α := a.get ⟨i, h₁.symm ▸ h₂⟩ @[simp] theorem size_set (a : Array α) (i : Fin a.size) (v : α) : (set a i v).size = a.size := List.length_set .. @[simp] theorem size_push (a : Array α) (v : α) : (push a v).size = a.size + 1 := List.length_concat .. /- Low-level version of `fset` which is as fast as a C array fset. `Fin` values are represented as tag pointers in the Lean runtime. Thus, `fset` may be slightly slower than `uset`. -/ @[extern "lean_array_uset"] def uset (a : Array α) (i : USize) (v : α) (h : i.toNat < a.size) : Array α := a.set ⟨i.toNat, h⟩ v @[extern "lean_array_fswap"] def swap (a : Array α) (i j : @& Fin a.size) : Array α := let v₁ := a.get i let v₂ := a.get j let a' := a.set i v₂ a'.set (size_set a i v₂ ▸ j) v₁ @[extern "lean_array_swap"] def swap! (a : Array α) (i j : @& Nat) : Array α := if h₁ : i < a.size then if h₂ : j < a.size then swap a ⟨i, h₁⟩ ⟨j, h₂⟩ else panic! "index out of bounds" else panic! "index out of bounds" @[inline] def swapAt (a : Array α) (i : Fin a.size) (v : α) : α × Array α := let e := a.get i let a := a.set i v (e, a) @[inline] def swapAt! (a : Array α) (i : Nat) (v : α) : α × Array α := if h : i < a.size then swapAt a ⟨i, h⟩ v else have Inhabited α from ⟨v⟩ panic! ("index " ++ toString i ++ " out of bounds") @[extern "lean_array_pop"] def pop (a : Array α) : Array α := { data := a.data.dropLast } def shrink (a : Array α) (n : Nat) : Array α := let rec loop | 0, a => a | n+1, a => loop n a.pop loop (a.size - n) a @[inline] def modifyM [Monad m] [Inhabited α] (a : Array α) (i : Nat) (f : α → m α) : m (Array α) := do if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩ let v := a.get idx let a' := a.set idx arbitrary let v ← f v pure <| a'.set (size_set a .. ▸ idx) v else pure a @[inline] def modify [Inhabited α] (a : Array α) (i : Nat) (f : α → α) : Array α := Id.run <| a.modifyM i f @[inline] def modifyOp [Inhabited α] (self : Array α) (idx : Nat) (f : α → α) : Array α := self.modify idx f /- We claim this unsafe implementation is correct because an array cannot have more than `usizeSz` elements in our runtime. This kind of low level trick can be removed with a little bit of compiler support. For example, if the compiler simplifies `as.size < usizeSz` to true. -/ @[inline] unsafe def forInUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (b : β) (f : α → β → m (ForInStep β)) : m β := let sz := USize.ofNat as.size let rec @[specialize] loop (i : USize) (b : β) : m β := do if i < sz then let a := as.uget i lcProof match (← f a b) with | ForInStep.done b => pure b | ForInStep.yield b => loop (i+1) b else pure b loop 0 b -- Move? private theorem zeroLtOfLt : {a b : Nat} → a < b → 0 < b | 0, _, h => h | a+1, b, h => have a < b from Nat.ltTrans (Nat.ltSuccSelf _) h zeroLtOfLt this /- Reference implementation for `forIn` -/ @[implementedBy Array.forInUnsafe] protected def forIn {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (b : β) (f : α → β → m (ForInStep β)) : m β := let rec loop (i : Nat) (h : i ≤ as.size) (b : β) : m β := do match i, h with | 0, _ => pure b | i+1, h => have h' : i < as.size from Nat.ltOfLtOfLe (Nat.ltSuccSelf i) h have as.size - 1 < as.size from Nat.subLt (zeroLtOfLt h') (by decide) have as.size - 1 - i < as.size from Nat.ltOfLeOfLt (Nat.subLe (as.size - 1) i) this match (← f (as.get ⟨as.size - 1 - i, this⟩) b) with | ForInStep.done b => pure b | ForInStep.yield b => loop i (Nat.leOfLt h') b loop as.size (Nat.leRefl _) b instance : ForIn m (Array α) α where forIn := Array.forIn /- See comment at forInUnsafe -/ @[inline] unsafe def foldlMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Array α) (start := 0) (stop := as.size) : m β := let rec @[specialize] fold (i : USize) (stop : USize) (b : β) : m β := do if i == stop then pure b else fold (i+1) stop (← f b (as.uget i lcProof)) if start < stop then if stop ≤ as.size then fold (USize.ofNat start) (USize.ofNat stop) init else pure init else pure init /- Reference implementation for `foldlM` -/ @[implementedBy foldlMUnsafe] def foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Array α) (start := 0) (stop := as.size) : m β := let fold (stop : Nat) (h : stop ≤ as.size) := let rec loop (i : Nat) (j : Nat) (b : β) : m β := do if hlt : j < stop then match i with | 0 => pure b | i'+1 => loop i' (j+1) (← f b (as.get ⟨j, Nat.ltOfLtOfLe hlt h⟩)) else pure b loop (stop - start) start init if h : stop ≤ as.size then fold stop h else fold as.size (Nat.leRefl _) /- See comment at forInUnsafe -/ @[inline] unsafe def foldrMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Array α) (start := as.size) (stop := 0) : m β := let rec @[specialize] fold (i : USize) (stop : USize) (b : β) : m β := do if i == stop then pure b else fold (i-1) stop (← f (as.uget (i-1) lcProof) b) if start ≤ as.size then if stop < start then fold (USize.ofNat start) (USize.ofNat stop) init else pure init else if stop < as.size then fold (USize.ofNat as.size) (USize.ofNat stop) init else pure init /- Reference implementation for `foldrM` -/ @[implementedBy foldrMUnsafe] def foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Array α) (start := as.size) (stop := 0) : m β := let rec fold (i : Nat) (h : i ≤ as.size) (b : β) : m β := do if i == stop then pure b else match i, h with | 0, _ => pure b | i+1, h => have i < as.size from Nat.ltOfLtOfLe (Nat.ltSuccSelf _) h fold i (Nat.leOfLt this) (← f (as.get ⟨i, this⟩) b) if h : start ≤ as.size then if stop < start then fold start h init else pure init else if stop < as.size then fold as.size (Nat.leRefl _) init else pure init /- See comment at forInUnsafe -/ @[inline] unsafe def mapMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → m β) (as : Array α) : m (Array β) := let sz := USize.ofNat as.size let rec @[specialize] map (i : USize) (r : Array NonScalar) : m (Array PNonScalar.{v}) := do if i < sz then let v := r.uget i lcProof let r := r.uset i arbitrary lcProof let vNew ← f (unsafeCast v) map (i+1) (r.uset i (unsafeCast vNew) lcProof) else pure (unsafeCast r) unsafeCast <| map 0 (unsafeCast as) /- Reference implementation for `mapM` -/ @[implementedBy mapMUnsafe] def mapM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → m β) (as : Array α) : m (Array β) := as.foldlM (fun bs a => do let b ← f a; pure (bs.push b)) (mkEmpty as.size) @[inline] def mapIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : Fin as.size → α → m β) : m (Array β) := let rec @[specialize] map (i : Nat) (j : Nat) (inv : i + j = as.size) (bs : Array β) : m (Array β) := do match i, inv with | 0, _ => pure bs | i+1, inv => have j < as.size by rw [← inv, Nat.add_assoc, Nat.add_comm 1 j, Nat.add_left_comm]; apply Nat.leAddRight let idx : Fin as.size := ⟨j, this⟩ have i + (j + 1) = as.size by rw [← inv, Nat.add_comm j 1, Nat.add_assoc] map i (j+1) this (bs.push (← f idx (as.get idx))) map as.size 0 rfl (mkEmpty as.size) @[inline] def findSomeM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : α → m (Option β)) : m (Option β) := do for a in as do match (← f a) with | some b => return b | _ => pure ⟨⟩ return none @[inline] def findM? {α : Type} {m : Type → Type} [Monad m] (as : Array α) (p : α → m Bool) : m (Option α) := do for a in as do if (← p a) then return a return none @[inline] def findIdxM? [Monad m] (as : Array α) (p : α → m Bool) : m (Option Nat) := do let mut i := 0 for a in as do if (← p a) then return some i i := i + 1 return none @[inline] unsafe def anyMUnsafe {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m Bool := let rec @[specialize] any (i : USize) (stop : USize) : m Bool := do if i == stop then pure false else if (← p (as.uget i lcProof)) then pure true else any (i+1) stop if start < stop then if stop ≤ as.size then any (USize.ofNat start) (USize.ofNat stop) else pure false else pure false @[implementedBy anyMUnsafe] def anyM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m Bool := let any (stop : Nat) (h : stop ≤ as.size) := let rec loop (i : Nat) (j : Nat) : m Bool := do if hlt : j < stop then match i with | 0 => pure false | i'+1 => if (← p (as.get ⟨j, Nat.ltOfLtOfLe hlt h⟩)) then pure true else loop i' (j+1) else pure false loop (stop - start) start if h : stop ≤ as.size then any stop h else any as.size (Nat.leRefl _) @[inline] def allM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m Bool := return !(← as.anyM fun v => return !(← p v)) @[inline] def findSomeRevM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : α → m (Option β)) : m (Option β) := let rec @[specialize] find : (i : Nat) → i ≤ as.size → m (Option β) | 0, h => pure none | i+1, h => do have i < as.size from Nat.ltOfLtOfLe (Nat.ltSuccSelf _) h let r ← f (as.get ⟨i, this⟩) match r with | some v => pure r | none => have i ≤ as.size from Nat.leOfLt this find i this find as.size (Nat.leRefl _) @[inline] def findRevM? {α : Type} {m : Type → Type w} [Monad m] (as : Array α) (p : α → m Bool) : m (Option α) := as.findSomeRevM? fun a => return if (← p a) then some a else none @[inline] def forM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Array α) (start := 0) (stop := as.size) : m PUnit := as.foldlM (fun _ => f) ⟨⟩ start stop @[inline] def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Array α) (start := as.size) (stop := 0) : m PUnit := as.foldrM (fun a _ => f a) ⟨⟩ start stop @[inline] def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Array α) (start := 0) (stop := as.size) : β := Id.run <| as.foldlM f init start stop @[inline] def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Array α) (start := as.size) (stop := 0) : β := Id.run <| as.foldrM f init start stop @[inline] def map {α : Type u} {β : Type v} (f : α → β) (as : Array α) : Array β := Id.run <| as.mapM f @[inline] def mapIdx {α : Type u} {β : Type v} (as : Array α) (f : Fin as.size → α → β) : Array β := Id.run <| as.mapIdxM f @[inline] def find? {α : Type} (as : Array α) (p : α → Bool) : Option α := Id.run <| as.findM? p @[inline] def findSome? {α : Type u} {β : Type v} (as : Array α) (f : α → Option β) : Option β := Id.run <| as.findSomeM? f @[inline] def findSome! {α : Type u} {β : Type v} [Inhabited β] (a : Array α) (f : α → Option β) : β := match findSome? a f with | some b => b | none => panic! "failed to find element" @[inline] def findSomeRev? {α : Type u} {β : Type v} (as : Array α) (f : α → Option β) : Option β := Id.run <| as.findSomeRevM? f @[inline] def findRev? {α : Type} (as : Array α) (p : α → Bool) : Option α := Id.run <| as.findRevM? p @[inline] def findIdx? {α : Type u} (as : Array α) (p : α → Bool) : Option Nat := let rec loop (i : Nat) (j : Nat) (inv : i + j = as.size) : Option Nat := if hlt : j < as.size then match i, inv with | 0, inv => by apply False.elim rw [Nat.zero_add] at inv rw [inv] at hlt exact absurd hlt (Nat.ltIrrefl _) | i+1, inv => if p (as.get ⟨j, hlt⟩) then some j else have i + (j+1) = as.size by rw [← inv, Nat.add_comm j 1, Nat.add_assoc] loop i (j+1) this else none loop as.size 0 rfl def getIdx? [BEq α] (a : Array α) (v : α) : Option Nat := a.findIdx? fun a => a == v @[inline] def any (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool := Id.run <| as.anyM p start stop @[inline] def all (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool := Id.run <| as.allM p start stop def contains [BEq α] (as : Array α) (a : α) : Bool := as.any fun b => a == b def elem [BEq α] (a : α) (as : Array α) : Bool := as.contains a -- TODO(Leo): justify termination using wf-rec, and use `swap` partial def reverse (as : Array α) : Array α := let n := as.size let mid := n / 2 let rec rev (as : Array α) (i : Nat) := if i < mid then rev (as.swap! i (n - i - 1)) (i+1) else as rev as 0 @[inline] def getEvenElems (as : Array α) : Array α := (·.2) <| as.foldl (init := (true, Array.empty)) fun (even, r) a => if even then (false, r.push a) else (true, r) @[export lean_array_to_list] def toList (as : Array α) : List α := as.foldr List.cons [] instance {α : Type u} [Repr α] : Repr (Array α) where reprPrec a _ := if a.size == 0 then "#[]" else Std.Format.bracketFill "#[" (@Std.Format.joinSep _ ⟨repr⟩ (toList a) ("," ++ Std.Format.line)) "]" instance [ToString α] : ToString (Array α) where toString a := "#" ++ toString a.toList protected def append (as : Array α) (bs : Array α) : Array α := bs.foldl (init := as) fun r v => r.push v instance : Append (Array α) := ⟨Array.append⟩ protected def appendList (as : Array α) (bs : List α) : Array α := bs.foldl (init := as) fun r v => r.push v instance : HAppend (Array α) (List α) (Array α) := ⟨Array.appendList⟩ @[inline] def concatMapM [Monad m] (f : α → m (Array β)) (as : Array α) : m (Array β) := as.foldlM (init := empty) fun bs a => do return bs ++ (← f a) @[inline] def concatMap (f : α → Array β) (as : Array α) : Array β := as.foldl (init := empty) fun bs a => bs ++ f a end Array @[inlineIfReduce] def List.toArrayAux : List α → Array α → Array α | [], r => r | a::as, r => toArrayAux as (r.push a) @[inlineIfReduce] def List.redLength : List α → Nat | [] => 0 | _::as => as.redLength + 1 @[inline, matchPattern, export lean_list_to_array] def List.toArray (as : List α) : Array α := as.toArrayAux (Array.mkEmpty as.redLength) export Array (mkArray) syntax "#[" sepBy(term, ", ") "]" : term macro_rules | `(#[ $elems,* ]) => `(List.toArray [ $elems,* ]) namespace Array -- TODO(Leo): cleanup @[specialize] partial def isEqvAux (a b : Array α) (hsz : a.size = b.size) (p : α → α → Bool) (i : Nat) : Bool := if h : i < a.size then let aidx : Fin a.size := ⟨i, h⟩; let bidx : Fin b.size := ⟨i, hsz ▸ h⟩; match p (a.get aidx) (b.get bidx) with | true => isEqvAux a b hsz p (i+1) | false => false else true @[inline] def isEqv (a b : Array α) (p : α → α → Bool) : Bool := if h : a.size = b.size then isEqvAux a b h p 0 else false instance [BEq α] : BEq (Array α) := ⟨fun a b => isEqv a b BEq.beq⟩ @[inline] def filter (p : α → Bool) (as : Array α) (start := 0) (stop := as.size) : Array α := as.foldl (init := #[]) (start := start) (stop := stop) fun r a => if p a then r.push a else r @[inline] def filterM [Monad m] (p : α → m Bool) (as : Array α) (start := 0) (stop := as.size) : m (Array α) := as.foldlM (init := #[]) (start := start) (stop := stop) fun r a => do if (← p a) then r.push a else r @[specialize] def filterMapM [Monad m] (f : α → m (Option β)) (as : Array α) (start := 0) (stop := as.size) : m (Array β) := as.foldlM (init := #[]) (start := start) (stop := stop) fun bs a => do match (← f a) with | some b => pure (bs.push b) | none => pure bs @[inline] def filterMap (f : α → Option β) (as : Array α) (start := 0) (stop := as.size) : Array β := Id.run <| as.filterMapM f (start := start) (stop := stop) @[specialize] def getMax? (as : Array α) (lt : α → α → Bool) : Option α := if h : 0 < as.size then let a0 := as.get ⟨0, h⟩ some <| as.foldl (init := a0) (start := 1) fun best a => if lt best a then a else best else none @[inline] def partition (p : α → Bool) (as : Array α) : Array α × Array α := do let mut bs := #[] let mut cs := #[] for a in as do if p a then bs := bs.push a else cs := cs.push a return (bs, cs) theorem ext (a b : Array α) (h₁ : a.size = b.size) (h₂ : (i : Nat) → (hi₁ : i < a.size) → (hi₂ : i < b.size) → a.get ⟨i, hi₁⟩ = b.get ⟨i, hi₂⟩) : a = b := by let rec extAux (a b : List α) (h₁ : a.length = b.length) (h₂ : (i : Nat) → (hi₁ : i < a.length) → (hi₂ : i < b.length) → a.get i hi₁ = b.get i hi₂) : a = b := by induction a generalizing b with | nil => cases b with | nil => rfl | cons b bs => rw [List.length_cons] at h₁; injection h₁ | cons a as ih => cases b with | nil => rw [List.length_cons] at h₁; injection h₁ | cons b bs => have hz₁ : 0 < (a::as).length by rw [List.length_cons]; apply Nat.zeroLtSucc have hz₂ : 0 < (b::bs).length by rw [List.length_cons]; apply Nat.zeroLtSucc have headEq : a = b from h₂ 0 hz₁ hz₂ have h₁' : as.length = bs.length by rw [List.length_cons, List.length_cons] at h₁; injection h₁; assumption have h₂' : (i : Nat) → (hi₁ : i < as.length) → (hi₂ : i < bs.length) → as.get i hi₁ = bs.get i hi₂ by intro i hi₁ hi₂ have hi₁' : i+1 < (a::as).length by rw [List.length_cons]; apply Nat.succ_lt_succ; assumption have hi₂' : i+1 < (b::bs).length by rw [List.length_cons]; apply Nat.succ_lt_succ; assumption have (a::as).get (i+1) hi₁' = (b::bs).get (i+1) hi₂' from h₂ (i+1) hi₁' hi₂' apply this have tailEq : as = bs from ih bs h₁' h₂' rw [headEq, tailEq] cases a; cases b apply congrArg apply extAux assumption assumption theorem extLit {n : Nat} (a b : Array α) (hsz₁ : a.size = n) (hsz₂ : b.size = n) (h : (i : Nat) → (hi : i < n) → a.getLit i hsz₁ hi = b.getLit i hsz₂ hi) : a = b := Array.ext a b (hsz₁.trans hsz₂.symm) fun i hi₁ hi₂ => h i (hsz₁ ▸ hi₁) end Array -- CLEANUP the following code namespace Array partial def indexOfAux [BEq α] (a : Array α) (v : α) : Nat → Option (Fin a.size) | i => if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩; if a.get idx == v then some idx else indexOfAux a v (i+1) else none def indexOf? [BEq α] (a : Array α) (v : α) : Option (Fin a.size) := indexOfAux a v 0 partial def eraseIdxAux : Nat → Array α → Array α | i, a => if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩; let idx1 : Fin a.size := ⟨i - 1, by exact Nat.ltOfLeOfLt (Nat.predLe i) h⟩; eraseIdxAux (i+1) (a.swap idx idx1) else a.pop def feraseIdx (a : Array α) (i : Fin a.size) : Array α := eraseIdxAux (i.val + 1) a def eraseIdx (a : Array α) (i : Nat) : Array α := if i < a.size then eraseIdxAux (i+1) a else a @[simp] theorem size_swap (a : Array α) (i j : Fin a.size) : (a.swap i j).size = a.size := by show ((a.set i (a.get j)).set (size_set a i _ ▸ j) (a.get i)).size = a.size rw [size_set, size_set] @[simp] theorem size_pop (a : Array α) : a.pop.size = a.size - 1 := List.length_dropLast .. section /- Instance for justifying `partial` declaration. We should be able to delete it as soon as we restore support for well-founded recursion. -/ instance eraseIdxSzAuxInstance (a : Array α) : Inhabited { r : Array α // r.size = a.size - 1 } where default := ⟨a.pop, size_pop a⟩ partial def eraseIdxSzAux (a : Array α) : ∀ (i : Nat) (r : Array α), r.size = a.size → { r : Array α // r.size = a.size - 1 } | i, r, heq => if h : i < r.size then let idx : Fin r.size := ⟨i, h⟩; let idx1 : Fin r.size := ⟨i - 1, by exact Nat.ltOfLeOfLt (Nat.predLe i) h⟩; eraseIdxSzAux a (i+1) (r.swap idx idx1) ((size_swap r idx idx1).trans heq) else ⟨r.pop, (size_pop r).trans (heq ▸ rfl)⟩ end def eraseIdx' (a : Array α) (i : Fin a.size) : { r : Array α // r.size = a.size - 1 } := eraseIdxSzAux a (i.val + 1) a rfl def erase [BEq α] (as : Array α) (a : α) : Array α := match as.indexOf? a with | none => as | some i => as.feraseIdx i partial def insertAtAux (i : Nat) : Array α → Nat → Array α | as, j => if i == j then as else let as := as.swap! (j-1) j; insertAtAux i as (j-1) /-- Insert element `a` at position `i`. Pre: `i < as.size` -/ def insertAt (as : Array α) (i : Nat) (a : α) : Array α := if i > as.size then panic! "invalid index" else let as := as.push a; as.insertAtAux i as.size def toListLitAux (a : Array α) (n : Nat) (hsz : a.size = n) : ∀ (i : Nat), i ≤ a.size → List α → List α | 0, hi, acc => acc | (i+1), hi, acc => toListLitAux a n hsz i (Nat.leOfSuccLe hi) (a.getLit i hsz (Nat.ltOfLtOfEq (Nat.ltOfLtOfLe (Nat.ltSuccSelf i) hi) hsz) :: acc) def toArrayLit (a : Array α) (n : Nat) (hsz : a.size = n) : Array α := List.toArray <| toListLitAux a n hsz n (hsz ▸ Nat.leRefl _) [] theorem toArrayLitEq (a : Array α) (n : Nat) (hsz : a.size = n) : a = toArrayLit a n hsz := -- TODO: this is painful to prove without proper automation sorry /- First, we need to prove ∀ i j acc, i ≤ a.size → (toListLitAux a n hsz (i+1) hi acc).index j = if j < i then a.getLit j hsz _ else acc.index (j - i) by induction Base case is trivial (j : Nat) (acc : List α) (hi : 0 ≤ a.size) |- (toListLitAux a n hsz 0 hi acc).index j = if j < 0 then a.getLit j hsz _ else acc.index (j - 0) ... |- acc.index j = acc.index j Induction (j : Nat) (acc : List α) (hi : i+1 ≤ a.size) |- (toListLitAux a n hsz (i+1) hi acc).index j = if j < i + 1 then a.getLit j hsz _ else acc.index (j - (i + 1)) ... |- (toListLitAux a n hsz i hi' (a.getLit i hsz _ :: acc)).index j = if j < i + 1 then a.getLit j hsz _ else acc.index (j - (i + 1)) * by def ... |- if j < i then a.getLit j hsz _ else (a.getLit i hsz _ :: acc).index (j-i) * by induction hypothesis = if j < i + 1 then a.getLit j hsz _ else acc.index (j - (i + 1)) If j < i, then both are a.getLit j hsz _ If j = i, then lhs reduces else-branch to (a.getLit i hsz _) and rhs is then-brachn (a.getLit i hsz _) If j >= i + 1, we use - j - i >= 1 > 0 - (a::as).index k = as.index (k-1) If k > 0 - j - (i + 1) = (j - i) - 1 Then lhs = (a.getLit i hsz _ :: acc).index (j-i) = acc.index (j-i-1) = acc.index (j-(i+1)) = rhs With this proof, we have ∀ j, j < n → (toListLitAux a n hsz n _ []).index j = a.getLit j hsz _ We also need - (toListLitAux a n hsz n _ []).length = n - j < n -> (List.toArray as).getLit j _ _ = as.index j Then using Array.extLit, we have that a = List.toArray <| toListLitAux a n hsz n _ [] -/ partial def isPrefixOfAux [BEq α] (as bs : Array α) (hle : as.size ≤ bs.size) : Nat → Bool | i => if h : i < as.size then let a := as.get ⟨i, h⟩; let b := bs.get ⟨i, Nat.ltOfLtOfLe h hle⟩; if a == b then isPrefixOfAux as bs hle (i+1) else false else true /- Return true iff `as` is a prefix of `bs` -/ def isPrefixOf [BEq α] (as bs : Array α) : Bool := if h : as.size ≤ bs.size then isPrefixOfAux as bs h 0 else false private def allDiffAuxAux [BEq α] (as : Array α) (a : α) : forall (i : Nat), i < as.size → Bool | 0, h => true | i+1, h => have i < as.size from Nat.ltTrans (Nat.ltSuccSelf _) h; a != as.get ⟨i, this⟩ && allDiffAuxAux as a i this private partial def allDiffAux [BEq α] (as : Array α) : Nat → Bool | i => if h : i < as.size then allDiffAuxAux as (as.get ⟨i, h⟩) i h && allDiffAux as (i+1) else true def allDiff [BEq α] (as : Array α) : Bool := allDiffAux as 0 @[specialize] partial def zipWithAux (f : α → β → γ) (as : Array α) (bs : Array β) : Nat → Array γ → Array γ | i, cs => if h : i < as.size then let a := as.get ⟨i, h⟩; if h : i < bs.size then let b := bs.get ⟨i, h⟩; zipWithAux f as bs (i+1) <| cs.push <| f a b else cs else cs @[inline] def zipWith (as : Array α) (bs : Array β) (f : α → β → γ) : Array γ := zipWithAux f as bs 0 #[] def zip (as : Array α) (bs : Array β) : Array (α × β) := zipWith as bs Prod.mk def split (as : Array α) (p : α → Bool) : Array α × Array α := as.foldl (init := (#[], #[])) fun (as, bs) a => if p a then (as.push a, bs) else (as, bs.push a) end Array
445fcf68971a1af04f8c42891001ea0ee2c588fa
ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb
/ijcar16/examples/cc15.lean
c0364212bc9329b531165c1efc2f799330bec282
[ "MIT" ]
permissive
leanprover/leanprover.github.io
cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a
071a20d2e059a2c3733e004c681d3949cac3c07a
refs/heads/master
1,692,621,047,417
1,691,396,994,000
1,691,396,994,000
19,366,263
18
27
MIT
1,693,989,071,000
1,399,006,345,000
Lean
UTF-8
Lean
false
false
1,100
lean
/- Example/test file for the congruence closure procedure described in the paper: "Congruence Closure for Intensional Type Theory" Daniel Selsam and Leonardo de Moura The tactic `by blast` has been configured in this file to use just the congruence closure procedure using the command set_option blast.strategy "cc" -/ set_option blast.strategy "cc" axiom vector.{l} : Type.{l} → nat → Type.{l} axiom app : Π {A : Type} {n m : nat}, vector A m → vector A n → vector A (m+n) example (n1 n2 n3 : nat) (v1 w1 : vector nat n1) (w1' : vector nat n3) (v2 w2 : vector nat n2) : n1 = n3 → v1 = w1 → w1 == w1' → v2 = w2 → app v1 v2 == app w1' w2 := by blast example (n1 n2 n3 : nat) (v1 w1 : vector nat n1) (w1' : vector nat n3) (v2 w2 : vector nat n2) : n1 == n3 → v1 = w1 → w1 == w1' → v2 == w2 → app v1 v2 == app w1' w2 := by blast example (n1 n2 n3 : nat) (v1 w1 v : vector nat n1) (w1' : vector nat n3) (v2 w2 w : vector nat n2) : n1 == n3 → v1 = w1 → w1 == w1' → v2 == w2 → app w1' w2 == app v w → app v1 v2 = app v w := by blast
e16ff86a74c642220a807e8213d3d10706b3deae
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/algebra/polynomial.lean
cc3aaef487808ba39662c7258261603dc3f1196f
[ "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
8,495
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 -/ import analysis.normed_space.basic import data.polynomial.algebra_map import data.polynomial.inductions import ring_theory.polynomial.vieta import field_theory.splitting_field /-! # Polynomials and limits In this file we prove the following lemmas. * `polynomial.continuous_eval₂: `polynomial.eval₂` defines a continuous function. * `polynomial.continuous_aeval: `polynomial.aeval` defines a continuous function; we also prove convenience lemmas `polynomial.continuous_at_aeval`, `polynomial.continuous_within_at_aeval`, `polynomial.continuous_on_aeval`. * `polynomial.continuous`: `polynomial.eval` defines a continuous functions; we also prove convenience lemmas `polynomial.continuous_at`, `polynomial.continuous_within_at`, `polynomial.continuous_on`. * `polynomial.tendsto_norm_at_top`: `λ x, ∥polynomial.eval (z x) p∥` tends to infinity provided that `λ x, ∥z x∥` tends to infinity and `0 < degree p`; * `polynomial.tendsto_abv_eval₂_at_top`, `polynomial.tendsto_abv_at_top`, `polynomial.tendsto_abv_aeval_at_top`: a few versions of the previous statement for `is_absolute_value abv` instead of norm. ## Tags polynomial, continuity -/ open is_absolute_value filter namespace polynomial open_locale polynomial section topological_semiring variables {R S : Type*} [semiring R] [topological_space R] [topological_semiring R] (p : R[X]) @[continuity] protected lemma continuous_eval₂ [semiring S] (p : S[X]) (f : S →+* R) : continuous (λ x, p.eval₂ f x) := begin dsimp only [eval₂_eq_sum, finsupp.sum], exact continuous_finset_sum _ (λ c hc, continuous_const.mul (continuous_pow _)) end @[continuity] protected lemma continuous : continuous (λ x, p.eval x) := p.continuous_eval₂ _ protected lemma continuous_at {a : R} : continuous_at (λ x, p.eval x) a := p.continuous.continuous_at protected lemma continuous_within_at {s a} : continuous_within_at (λ x, p.eval x) s a := p.continuous.continuous_within_at protected lemma continuous_on {s} : continuous_on (λ x, p.eval x) s := p.continuous.continuous_on end topological_semiring section topological_algebra variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] [topological_space A] [topological_semiring A] (p : R[X]) @[continuity] protected lemma continuous_aeval : continuous (λ x : A, aeval x p) := p.continuous_eval₂ _ protected lemma continuous_at_aeval {a : A} : continuous_at (λ x : A, aeval x p) a := p.continuous_aeval.continuous_at protected lemma continuous_within_at_aeval {s a} : continuous_within_at (λ x : A, aeval x p) s a := p.continuous_aeval.continuous_within_at protected lemma continuous_on_aeval {s} : continuous_on (λ x : A, aeval x p) s := p.continuous_aeval.continuous_on end topological_algebra lemma tendsto_abv_eval₂_at_top {R S k α : Type*} [semiring R] [ring S] [linear_ordered_field k] (f : R →+* S) (abv : S → k) [is_absolute_value abv] (p : R[X]) (hd : 0 < degree p) (hf : f p.leading_coeff ≠ 0) {l : filter α} {z : α → S} (hz : tendsto (abv ∘ z) l at_top) : tendsto (λ x, abv (p.eval₂ f (z x))) l at_top := begin revert hf, refine degree_pos_induction_on p hd _ _ _; clear hd p, { rintros c - hc, rw [leading_coeff_mul_X, leading_coeff_C] at hc, simpa [abv_mul abv] using hz.const_mul_at_top ((abv_pos abv).2 hc) }, { intros p hpd ihp hf, rw [leading_coeff_mul_X] at hf, simpa [abv_mul abv] using (ihp hf).at_top_mul_at_top hz }, { intros p a hd ihp hf, rw [add_comm, leading_coeff_add_of_degree_lt (degree_C_le.trans_lt hd)] at hf, refine tendsto_at_top_of_add_const_right (abv (-f a)) _, refine tendsto_at_top_mono (λ _, abv_add abv _ _) _, simpa using ihp hf } end lemma tendsto_abv_at_top {R k α : Type*} [ring R] [linear_ordered_field k] (abv : R → k) [is_absolute_value abv] (p : R[X]) (h : 0 < degree p) {l : filter α} {z : α → R} (hz : tendsto (abv ∘ z) l at_top) : tendsto (λ x, abv (p.eval (z x))) l at_top := tendsto_abv_eval₂_at_top _ _ _ h (mt leading_coeff_eq_zero.1 $ ne_zero_of_degree_gt h) hz lemma tendsto_abv_aeval_at_top {R A k α : Type*} [comm_semiring R] [ring A] [algebra R A] [linear_ordered_field k] (abv : A → k) [is_absolute_value abv] (p : R[X]) (hd : 0 < degree p) (h₀ : algebra_map R A p.leading_coeff ≠ 0) {l : filter α} {z : α → A} (hz : tendsto (abv ∘ z) l at_top) : tendsto (λ x, abv (aeval (z x) p)) l at_top := tendsto_abv_eval₂_at_top _ abv p hd h₀ hz variables {α R : Type*} [normed_ring R] [is_absolute_value (norm : R → ℝ)] lemma tendsto_norm_at_top (p : R[X]) (h : 0 < degree p) {l : filter α} {z : α → R} (hz : tendsto (λ x, ∥z x∥) l at_top) : tendsto (λ x, ∥p.eval (z x)∥) l at_top := p.tendsto_abv_at_top norm h hz lemma exists_forall_norm_le [proper_space R] (p : R[X]) : ∃ x, ∀ y, ∥p.eval x∥ ≤ ∥p.eval y∥ := if hp0 : 0 < degree p then p.continuous.norm.exists_forall_le $ p.tendsto_norm_at_top hp0 tendsto_norm_cocompact_at_top else ⟨p.coeff 0, by rw [eq_C_of_degree_le_zero (le_of_not_gt hp0)]; simp⟩ section roots open_locale polynomial open_locale nnreal variables {F K : Type*} [field F] [normed_field K] open multiset lemma coeff_le_of_roots_le {p : F[X]} {f : F →+* K} {B : ℝ} (i : ℕ) (h1 : p.monic) (h2 : splits f p) (h3 : ∀ z ∈ (map f p).roots, ∥z∥ ≤ B) : ∥ (map f p).coeff i ∥ ≤ B^(p.nat_degree - i) * p.nat_degree.choose i := begin have hcd : card (map f p).roots = p.nat_degree := (nat_degree_eq_card_roots h2).symm, by_cases hB : 0 ≤ B, { by_cases hi : i ≤ p.nat_degree, { rw [eq_prod_roots_of_splits h2, monic.def.mp h1, ring_hom.map_one, ring_hom.map_one, one_mul], rw prod_X_sub_C_coeff, swap, rwa hcd, rw [norm_mul, (by norm_num : ∥(-1 : K) ^ (card (map f p).roots - i)∥= 1), one_mul], apply le_trans (le_sum_of_subadditive norm _ _ _ ), rotate, exact norm_zero, exact norm_add_le, rw multiset.map_map, suffices : ∀ r ∈ multiset.map (norm_hom ∘ prod) (powerset_len (card (map f p).roots - i) (map f p).roots), r ≤ B^(p.nat_degree - i), { convert sum_le_sum_map _ this, simp only [hi, hcd, multiset.map_const, card_map, card_powerset_len, nat.choose_symm, sum_repeat, nsmul_eq_mul, mul_comm], }, { intros r hr, obtain ⟨t, ht⟩ := multiset.mem_map.mp hr, have hbounds : ∀ x ∈ (multiset.map norm_hom t), 0 ≤ x ∧ x ≤ B, { intros x hx, obtain ⟨z, hz⟩ := multiset.mem_map.mp hx, rw ←hz.right, exact ⟨norm_nonneg z, h3 z (mem_of_le (mem_powerset_len.mp ht.left).left hz.left)⟩, }, lift B to ℝ≥0 using hB, lift (multiset.map norm_hom t) to (multiset ℝ≥0) using (λ x hx, (hbounds x hx).left) with normt hn, rw (by rw_mod_cast [←ht.right, function.comp_apply, ←prod_hom t norm_hom, ←hn] : r = normt.prod), convert multiset.prod_le_pow_card normt _ _, { rw (_ : card normt = card (multiset.map coe normt)), rwa [hn, ←hcd, card_map, (mem_powerset_len.mp ht.left).right.symm], rwa card_map, }, { intros x hx, have xmem : (x : ℝ) ∈ multiset.map coe normt := mem_map_of_mem _ hx, exact (hbounds x xmem).right, }}}, { push_neg at hi, rw [nat.choose_eq_zero_of_lt hi, coeff_eq_zero_of_nat_degree_lt, norm_zero], rw_mod_cast mul_zero, { rwa monic.nat_degree_map h1, apply_instance, }}}, { push_neg at hB, have noroots : (map f p).roots = 0, { contrapose! hB, obtain ⟨z, hz⟩ := exists_mem_of_ne_zero hB, exact le_trans (norm_nonneg z) (h3 z hz), }, suffices : p.nat_degree = 0, { by_cases hi : i = 0, { rw [this, hi, (monic.nat_degree_eq_zero_iff_eq_one h1).mp this], simp only [polynomial.map_one, coeff_one_zero, norm_one, pow_zero, nat.choose_self, nat.cast_one, mul_one], }, { replace hi := zero_lt_iff.mpr hi, rw ←this at hi, rw [nat.choose_eq_zero_of_lt hi, coeff_eq_zero_of_nat_degree_lt, norm_zero], rw_mod_cast mul_zero, { rwa monic.nat_degree_map h1, apply_instance, }}}, rw [←hcd, noroots, card_zero], }, end end roots end polynomial
b62b7b8f5981633799bf4882e019dfb06ad83f33
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/meta/tactic.lean
2a6fcd669a0782d33b4a2526225e25a9dd77d1d4
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
24,461
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.trace init.function init.option init.monad init.alternative init.nat_div import init.meta.exceptional init.meta.format init.meta.environment init.meta.pexpr meta_constant tactic_state : Type namespace tactic_state meta_constant env : tactic_state → environment meta_constant to_format : tactic_state → format /- Format expression with respect to the main goal in the tactic state. If the tactic state does not contain any goals, then format expression using an empty local context. -/ meta_constant format_expr : tactic_state → expr → format meta_constant get_options : tactic_state → options meta_constant set_options : tactic_state → options → tactic_state end tactic_state attribute [instance] meta_definition tactic_state.has_to_format : has_to_format tactic_state := has_to_format.mk tactic_state.to_format inductive tactic_result (A : Type) | success : A → tactic_state → tactic_result | exception : (unit → format) → tactic_state → tactic_result open tactic_result section variables {A : Type} variables [has_to_string A] meta_definition tactic_result_to_string : tactic_result A → string | (success a s) := to_string a | (exception .A e s) := "Exception: " ++ to_string (e ()) attribute [instance] meta_definition tactic_result_has_to_string : has_to_string (tactic_result A) := has_to_string.mk tactic_result_to_string end attribute [reducible] meta_definition tactic (A : Type) := tactic_state → tactic_result A section variables {A B : Type} attribute [inline] meta_definition tactic_fmap (f : A → B) (t : tactic A) : tactic B := λ s, tactic_result.cases_on (t s) (λ a s', success (f a) s') (λ e s', exception B e s') attribute [inline] meta_definition tactic_bind (t₁ : tactic A) (t₂ : A → tactic B) : tactic B := λ s, tactic_result.cases_on (t₁ s) (λ a s', t₂ a s') (λ e s', exception B e s') attribute [inline] meta_definition tactic_return (a : A) : tactic A := λ s, success a s meta_definition tactic_orelse {A : Type} (t₁ t₂ : tactic A) : tactic A := λ s, tactic_result.cases_on (t₁ s) success (λ e₁ s', tactic_result.cases_on (t₂ s) success (exception A)) end attribute [instance] meta_definition tactic_is_monad : monad tactic := monad.mk @tactic_fmap @tactic_return @tactic_bind meta_definition tactic.fail {A B : Type} [has_to_format B] (msg : B) : tactic A := λ s, exception A (λ u, to_fmt msg) s meta_definition tactic.failed {A : Type} : tactic A := tactic.fail "failed" attribute [instance] meta_definition tactic_is_alternative : alternative tactic := alternative.mk @tactic_fmap (λ A a s, success a s) (@fapp _ _) @tactic.failed @tactic_orelse namespace tactic variables {A : Type} meta_definition try (t : tactic A) : tactic unit := λ s, tactic_result.cases_on (t s) (λ a, success ()) (λ e s', success () s) meta_definition skip : tactic unit := success () open list meta_definition foreach : list A → (A → tactic unit) → tactic unit | [] fn := skip | (e::es) fn := do fn e, foreach es fn open nat /- (repeat_at_most n t): repeat the given tactic at most n times or until t fails -/ meta_definition repeat_at_most : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := (do t, repeat_at_most n t) <|> skip /- (repeat_exactly n t) : execute t n times -/ meta_definition repeat_exactly : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := do t, repeat_exactly n t meta_definition repeat : tactic unit → tactic unit := repeat_at_most 100000 meta_definition returnex (e : exceptional A) : tactic A := λ s, match e with | (exceptional.success a) := tactic_result.success a s | (exceptional.exception .A f) := tactic_result.exception A (λ u, f options.mk) s -- TODO(Leo): extract options from environment end meta_definition returnopt (e : option A) : tactic A := λ s, match e with | (some a) := tactic_result.success a s | none := tactic_result.exception A (λ u, to_fmt "failed") s end /- Decorate t's exceptions with msg -/ meta_definition decorate_ex (msg : format) (t : tactic A) : tactic A := λ s, tactic_result.cases_on (t s) success (λ e, exception A (λ u, msg ++ format.nest 2 (format.line ++ e u))) attribute [inline] meta_definition write (s' : tactic_state) : tactic unit := λ s, success () s' attribute [inline] meta_definition read : tactic tactic_state := λ s, success s s end tactic meta_definition tactic_format_expr (e : expr) : tactic format := do s ← tactic.read, return (tactic_state.format_expr s e) structure [class] has_to_tactic_format (A : Type) := (to_tactic_format : A → tactic format) attribute [instance] meta_definition expr_has_to_tactic_format : has_to_tactic_format expr := has_to_tactic_format.mk tactic_format_expr meta_definition tactic.pp {A : Type} [has_to_tactic_format A] : A → tactic format := has_to_tactic_format.to_tactic_format open tactic format meta_definition list_to_tactic_format_aux {A : Type} [has_to_tactic_format A] : bool → list A → tactic format | b [] := return $ to_fmt "" | b (x::xs) := do f₁ ← pp x, f₂ ← list_to_tactic_format_aux ff xs, return $ (if b = ff then to_fmt "," ++ line else nil) ++ f₁ ++ f₂ meta_definition list_to_tactic_format {A : Type} [has_to_tactic_format A] : list A → tactic format | [] := return $ to_fmt "[]" | (x::xs) := do f ← list_to_tactic_format_aux tt (x::xs), return $ to_fmt "[" ++ group (nest 1 f) ++ to_fmt "]" attribute [instance] meta_definition list_has_to_tactic_format {A : Type} [has_to_tactic_format A] : has_to_tactic_format (list A) := has_to_tactic_format.mk list_to_tactic_format attribute [instance] meta_definition has_to_format_to_has_to_tactic_format (A : Type) [has_to_format A] : has_to_tactic_format A := has_to_tactic_format.mk ((λ x, return x) ∘ to_fmt) namespace tactic open tactic_state meta_definition get_env : tactic environment := do s ← read, return $ env s meta_definition get_decl (n : name) : tactic declaration := do s ← read, returnex $ environment.get (env s) n meta_definition trace {A : Type} [has_to_tactic_format A] (a : A) : tactic unit := do fmt ← pp a, return $ _root_.trace_fmt fmt (λ u, ()) meta_definition trace_state : tactic unit := do s ← read, trace $ to_fmt s inductive transparency | all | semireducible | reducible | none export transparency (reducible semireducible) /- Return the partial term/proof constructed so far. Note that the resultant expression may contain variables that are not declarate in the current main goal. -/ meta_constant result : tactic expr /- Display the partial term/proof constructed so far. This tactic is *not* equivalent to do { r ← result, s ← read, return (format_expr s r) } because this one will format the result with respect to the current goal, and trace_result will do it with respect to the initial goal. -/ meta_constant format_result : tactic format /- Return target type of the main goal. Fail if tactic_state does not have any goal left. -/ meta_constant target : tactic expr meta_constant intro_core : name → tactic expr meta_constant intron : nat → tactic unit meta_constant rename : name → name → tactic unit /- Clear the given local constant. The tactic fails if the given expression is not a local constant. -/ meta_constant clear : expr → tactic unit meta_constant revert_lst : list expr → tactic nat meta_constant whnf_core : transparency → expr → tactic expr meta_constant eta_expand : expr → tactic expr meta_constant unify_core : transparency → expr → expr → tactic unit /- is_def_eq_core is similar to unify_core, but it treats metavariables as constants. -/ meta_constant is_def_eq_core : transparency → expr → expr → tactic unit /- Infer the type of the given expression. Remark: transparency does not affect type inference -/ meta_constant infer_type : expr → tactic expr meta_constant get_local : name → tactic expr /- Return the hypothesis in the main goal. Fail if tactic_state does not have any goal left. -/ meta_constant local_context : tactic (list expr) meta_constant get_unused_name : name → option nat → tactic name /- Helper tactic for creating simple applications where some arguments are inferred using type inference. Example, given rel.{l_1 l_2} : Pi (A : Type.{l_1}) (B : A -> Type.{l_2}), (Pi x : A, B x) -> (Pi x : A, B x) -> , Prop nat : Type 1 real : Type 1 vec.{l} : Pi (A : Type l) (n : nat), Type.{l1} f g : Pi (n : nat), vec real n then mk_app_core semireducible "rel" [f, g] returns the application rel.{1 2} nat (fun n : nat, vec real n) f g -/ meta_constant mk_app_core : transparency → name → list expr → tactic expr /- Similar to mk_app, but allows to specify which arguments are explicit/implicit. Example, given a b : nat then mk_mapp_core semireducible "ite" [some (a > b), none, none, some a, some b] returns the application @ite.{1} (a > b) (nat.decidable_gt a b) nat a b -/ meta_constant mk_mapp_core : transparency → name → list (option expr) → tactic expr /- Given a local constant t, if t has type (lhs = rhs) apply susbstitution. Otherwise, try to find a local constant that has type of the form (t = t') or (t' = t). The tactic fails if the given expression is not a local constant. -/ meta_constant subst : expr → tactic unit meta_constant exact : expr → tactic unit /- Elaborate the given quoted expression with respect to the current main goal. If the boolean argument is tt, then metavariables are tolerated and become new goals. -/ meta_constant to_expr_core : bool → pexpr → tactic expr /- Return true if the given expression is a type class. -/ meta_constant is_class : expr → tactic bool /- Try to create an instance of the given type class. -/ meta_constant mk_instance : expr → tactic expr /- Change the target of the main goal. The input expression must be definitionally equal to the current target. -/ meta_constant change : expr → tactic unit /- (assert H T), adds a new goal for T, and the hypothesis (H : T) in the current goal. -/ meta_constant assert : name → expr → tactic unit /- (assertv H T P), adds the hypothesis (H : T) in the current goal if P has type T. -/ meta_constant assertv : name → expr → expr → tactic unit /- (define H T), adds a new goal for T, and the hypothesis (H : T := ?M) in the current goal. -/ meta_constant define : name → expr → tactic unit /- (definev H T P), adds the hypothesis (H : T := P) in the current goal if P has type T. -/ meta_constant definev : name → expr → expr → tactic unit /- rotate goals to the left -/ meta_constant rotate_left : nat → tactic unit meta_constant get_goals : tactic (list expr) meta_constant set_goals : list expr → tactic unit /- (apply_core t all insts e), apply the expression e to the main goal, the unification is performed using the given transparency mode. If all is tt, then all unassigned meta-variables are added as new goals. If insts is tt, then use type class resolution to instantiate unassigned meta-variables. -/ meta_constant apply_core : transparency → bool → bool → expr → tactic unit /- Create a fresh meta universe variable. -/ meta_constant mk_meta_univ : tactic level /- Create a fresh meta-variable with the given type. The scope of the new meta-variable is the local context of the main goal. -/ meta_constant mk_meta_var : expr → tactic expr /- Return the value assigned to the given universe meta-variable. Fail if argument is not an universe meta-variable or if it is not assigned. -/ meta_constant get_univ_assignment : level → tactic level /- Return the value assigned to the given meta-variable. Fail if argument is not a meta-variable or if it is not assigned. -/ meta_constant get_assignment : expr → tactic expr meta_constant mk_fresh_name : tactic name /- Return a hash code for expr that ignores inst_implicit arguments, and proofs. -/ meta_constant abstract_hash : expr → tactic nat /- Return the "weight" of the given expr while ignoring inst_implicit arguments, and proofs. -/ meta_constant abstract_weight : expr → tactic nat meta_constant abstract_eq : expr → expr → tactic bool /- (induction_core m H rec ns) induction on H using recursor rec, names for the new hypotheses are retrieved from ns. If ns does not have sufficient names, then use the internal binder names in the recursor. -/ meta_constant induction_core : transparency → expr → name → list name → tactic unit /- (cases_core m H ns) apply cases_on recursor, names for the new hypotheses are retrieved from ns. H must be a local constant -/ meta_constant cases_core : transparency → expr → list name → tactic unit /- (generalize_core m e n) -/ meta_constant generalize_core : transparency → expr → name → tactic unit /- instantiate assigned metavariables in the given expression -/ meta_constant instantiate_mvars : expr → tactic expr /- Add the given declaration to the environment -/ meta_constant add_decl : declaration → tactic unit /- (set_basic_attribute_core attr_name c_name prio) set attribute attr_name for constant c_name with the given priority. If the priority is none, then use default -/ meta_constant set_basic_attribute_core : name → name → option nat → tactic unit /- (unset_attribute attr_name c_name) -/ meta_constant unset_attribute : name → name → tactic unit meta_definition set_basic_attribute : name → name → tactic unit := λ a n, set_basic_attribute_core a n none open list nat /- Add (H : T := pr) to the current goal -/ meta_definition note (n : name) (pr : expr) : tactic unit := do t ← infer_type pr, definev n t pr meta_definition whnf : expr → tactic expr := whnf_core semireducible meta_definition whnf_target : tactic unit := target >>= whnf >>= change meta_definition intro (n : name) : tactic expr := do t ← target, if expr.is_pi t = tt ∨ expr.is_let t = tt then intro_core n else whnf_target >> intro_core n meta_definition intro1 : tactic expr := intro `_ /- Remark: the unit argument is a trick to allow us to write a recursive definition. Lean3 only allows recursive functions when "equations" are used. -/ meta_definition intros_core : unit → tactic (list expr) | u := do t ← target, match t with | (expr.pi n bi d b) := do H ← intro1, Hs ← intros_core u, return (H :: Hs) | (expr.elet n t v b) := do H ← intro1, Hs ← intros_core u, return (H :: Hs) | e := return [] end meta_definition intros : tactic (list expr) := intros_core () meta_definition intro_lst : list name → tactic (list expr) | [] := return [] | (n::ns) := do H ← intro n, Hs ← intro_lst ns, return (H :: Hs) meta_definition mk_app : name → list expr → tactic expr := mk_app_core semireducible meta_definition mk_mapp : name → list (option expr) → tactic expr := mk_mapp_core semireducible meta_definition to_expr : pexpr → tactic expr := to_expr_core ff meta_definition revert (l : expr) : tactic nat := revert_lst [l] meta_definition clear_lst : list name → tactic unit | [] := skip | (n::ns) := do H ← get_local n, clear H, clear_lst ns meta_definition unify : expr → expr → tactic unit := unify_core semireducible meta_definition is_def_eq : expr → expr → tactic unit := is_def_eq_core semireducible meta_definition match_not (e : expr) : tactic expr := match (expr.is_not e) with | (some a) := return a | none := fail "expression is not a negation" end meta_definition match_eq (e : expr) : tactic (expr × expr) := match (expr.is_eq e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an equality" end meta_definition match_ne (e : expr) : tactic (expr × expr) := match (expr.is_ne e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not a disequality" end meta_definition match_heq (e : expr) : tactic (expr × expr × expr × expr) := do match (expr.is_heq e) with | (some (A, lhs, B, rhs)) := return (A, lhs, B, rhs) | none := fail "expression is not a heterogeneous equality" end meta_definition match_refl_app (e : expr) : tactic (name × expr × expr) := do env ← get_env, match (environment.is_refl_app env e) with | (some (R, lhs, rhs)) := return (R, lhs, rhs) | none := fail "expression is not an application of a reflexive relation" end meta_definition get_local_type (n : name) : tactic expr := get_local n >>= infer_type meta_definition trace_result : tactic unit := format_result >>= trace /- (find_same_type t es) tries to find in es an expression with type definitionally equal to t -/ meta_definition find_same_type : expr → list expr → tactic expr | e [] := failed | e (H :: Hs) := do t ← infer_type H, (unify e t >> return H) <|> find_same_type e Hs meta_definition assumption : tactic unit := do { ctx ← local_context, t ← target, H ← find_same_type t ctx, exact H } <|> fail "assumption tactic failed" /- Swap first two goals, do nothing if tactic state does not have at least two goals. -/ meta_definition swap : tactic unit := do gs ← get_goals, match gs with | (g₁ :: g₂ :: rs) := set_goals (g₂ :: g₁ :: rs) | e := skip end /- Return the number of goals that need to be solved -/ meta_definition num_goals : tactic nat := do gs ← get_goals, return (length gs) /- We have to provide the instance argument `[has_mod nat]` because mod for nat was not defined yet -/ meta_definition rotate_right (n : nat) [has_mod nat] : tactic unit := do ng ← num_goals, if ng = 0 then skip else rotate_left (ng - n % ng) meta_definition rotate : nat → tactic unit := rotate_left /- first [t_1, ..., t_n] applies the first tactic that doesn't fail. The tactic fails if all t_i's fail. -/ meta_definition first {A : Type} : list (tactic A) → tactic A | [] := fail "first tactic failed, no more alternatives" | (t::ts) := t <|> first ts /- Applies the given tactic to the main goal and fails if it is not solved. -/ meta_definition solve1 (tac : tactic unit) : tactic unit := do gs ← get_goals, match gs with | [] := fail "focus tactic failed, there isn't any goal left to focus" | (g::rs) := do set_goals [g], tac, gs' ← get_goals, match gs' with | [] := set_goals rs | gs := fail "focus tactic failed, focused goal has not been solved" end end /- solve [t_1, ... t_n] applies the first tactic that solves the main goal. -/ meta_definition solve (ts : list (tactic unit)) : tactic unit := first $ map solve1 ts private meta_definition focus_aux : list (tactic unit) → list expr → list expr → tactic unit | [] gs rs := set_goals $ gs ++ rs | (t::ts) (g::gs) rs := do set_goals [g], t, rs' ← get_goals, focus_aux ts gs (rs ++ rs') | (t::ts) [] rs := fail "focus tactic failed, insufficient number of goals" /- focus [t_1, ..., t_n] applies t_i to the i-th goal. Fails if there are less tha n goals. -/ meta_definition focus (ts : list (tactic unit)) : tactic unit := do gs ← get_goals, focus_aux ts gs [] private meta_definition all_goals_core : tactic unit → list expr → list expr → tactic unit | tac [] ac := set_goals ac | tac (g :: gs) ac := do set_goals [g], tac, new_gs ← get_goals, all_goals_core tac gs (ac ++ new_gs) /- Apply the given tactic to all goals. -/ meta_definition all_goals (tac : tactic unit) : tactic unit := do gs ← get_goals, all_goals_core tac gs [] /- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta_definition seq (tac1 : tactic unit) (tac2 : tactic unit) : tactic unit := do g::gs ← get_goals | failed, set_goals [g], tac1, all_goals tac2, gs' ← get_goals, set_goals (gs' ++ gs) attribute [instance] meta_definition tactic_has_andthen : has_andthen (tactic unit) := has_andthen.mk seq /- Applies tac if c holds -/ meta_definition when (c : Prop) [decidable c] (tac : tactic unit) : tactic unit := if c then tac else skip meta_constant is_trace_enabled_for : name → bool /- Execute tac only if option trace.n is set to true. -/ meta_definition when_tracing (n : name) (tac : tactic unit) : tactic unit := when (is_trace_enabled_for n = tt) tac /- Fail if there are no remaining goals. -/ meta_definition fail_if_no_goals : tactic unit := do n ← num_goals, when (n = 0) (fail "tactic failed, there are no goals to be solved") /- Fail if there are unsolved goals. -/ meta_definition now : tactic unit := do n ← num_goals, when (n ≠ 0) (fail "now tactic failed, there are unsolved goals") meta_definition apply : expr → tactic unit := apply_core semireducible ff tt meta_definition fapply : expr → tactic unit := apply_core semireducible tt tt /- Try to solve the main goal using type class resolution. -/ meta_definition apply_instance : tactic unit := do tgt ← target, b ← is_class tgt, if b = tt then mk_instance tgt >>= exact else fail "apply_instance tactic fail, target is not a type class" /- Create a list of universe meta-variables of the given size. -/ meta_definition mk_num_meta_univs : nat → tactic (list level) | 0 := return [] | (succ n) := do l ← mk_meta_univ, ls ← mk_num_meta_univs n, return (l::ls) /- Return (expr.const c [l_1, ..., l_n]) where l_i's are fresh universe meta-variables. -/ meta_definition mk_const (c : name) : tactic expr := do env ← get_env, decl ← returnex (environment.get env c), num ← return (length (declaration.univ_params decl)), ls ← mk_num_meta_univs num, return (expr.const c ls) /- Create a fresh universe ?u, a metavariable (?T : Type.{?u}), and return metavariable (?M : ?T). This action can be used to create a meta-variable when we don't know its type at creation time -/ meta_definition mk_mvar : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), mk_meta_var t private meta_definition get_pi_arity_aux : expr → tactic nat | (expr.pi n bi d b) := do m ← mk_fresh_name, l ← return (expr.local_const m n bi d), new_b ← whnf (expr.instantiate_var b l), r ← get_pi_arity_aux new_b, return (r + 1) | e := return 0 /- Compute the arity of the given (Pi-)type -/ meta_definition get_pi_arity (type : expr) : tactic nat := whnf type >>= get_pi_arity_aux /- Compute the arity of the given function -/ meta_definition get_arity (fn : expr) : tactic nat := infer_type fn >>= get_pi_arity meta_definition triv : tactic unit := mk_const `trivial >>= exact meta_definition by_contradiction (H : name) : tactic expr := do tgt : expr ← target, (match_not tgt >> return ()) <|> (mk_mapp `decidable.by_contradiction [some tgt, none] >>= apply) <|> fail "tactic by_contradiction failed, target is not a negation nor a decidable proposition (remark: when 'local attribute classical.prop_decidable [instance]' is used all propositions are decidable)", intro H meta_definition cases (H : expr) : tactic unit := cases_core semireducible H [] meta_definition cases_using : expr → list name → tactic unit := cases_core semireducible meta_definition generalize : expr → name → tactic unit := generalize_core semireducible meta_definition generalizes : list expr → tactic unit | [] := skip | (e::es) := generalize e `x >> generalizes es meta_definition refine (e : pexpr) : tactic unit := do tgt : expr ← target, to_expr_core tt `((%%e : %%tgt)) >>= exact meta_definition expr_of_nat : nat → tactic expr | n := if n = 0 then to_expr `(0) else if n = 1 then to_expr `(1) else do r : expr ← expr_of_nat (n / 2), if n % 2 = 0 then to_expr `(bit0 %%r) else to_expr `(bit1 %%r) notation `command`:max := tactic unit end tactic
72b5a8defda61ebaf983c709ca77ccfd96a74846
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/group/arithmetic.lean
4445b7fd061a3a4d077657096ec7a92c35cffb9c
[ "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
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_countable $ λ 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_countable {m : measurable_space α} {E} [measurable_space E] [measurable_singleton_class E] [countable 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_countable $ λ 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_countable (λ 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_countable (λ 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
2db0b67955ff67757c5ea171347a78ec3afd3fd9
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/coeIssue3.lean
fea701918968306be790153836e6d5e2ffdc411e
[ "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
512
lean
structure Var : Type := (name : String) instance Var.nameCoe : Coe String Var := ⟨Var.mk⟩ structure A : Type := (u : Unit) structure B : Type := (u : Unit) def a : A := A.mk () def b : B := B.mk () def Foo.chalk : A → List Var → Unit := λ _ _ => () def Bar.chalk : B → Unit := λ _ => () instance listCoe {α β} [Coe α β] : Coe (List α) (List β) := ⟨fun as => as.map (fun a => coe a)⟩ new_frontend open Foo open Bar #check Foo.chalk a ["foo"] -- works #check chalk a ["foo"] -- works
ff4e5bea52ddbdf932d25ec65e24d89f358c0f3d
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/archive/imo/imo1962_q4.lean
c6f927203d76e6f1615656e7a34b95cb68bc3dec
[ "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
4,161
lean
/- Copyright (c) 2020 Kevin Lacker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Lacker, Heather Macbeth -/ import analysis.special_functions.trigonometric.complex /-! # IMO 1962 Q4 Solve the equation `cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 = 1`. Since Lean does not have a concept of "simplest form", we just express what is in fact the simplest form of the set of solutions, and then prove it equals the set of solutions. -/ open real open_locale real noncomputable theory def problem_equation (x : ℝ) : Prop := cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 = 1 def solution_set : set ℝ := { x : ℝ | ∃ k : ℤ, x = (2 * ↑k + 1) * π / 4 ∨ x = (2 * ↑k + 1) * π / 6 } /- The key to solving this problem simply is that we can rewrite the equation as a product of terms, shown in `alt_formula`, being equal to zero. -/ def alt_formula (x : ℝ) : ℝ := cos x * (cos x ^ 2 - 1/2) * cos (3 * x) lemma cos_sum_equiv {x : ℝ} : (cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 - 1) / 4 = alt_formula x := begin simp only [real.cos_two_mul, cos_three_mul, alt_formula], ring end lemma alt_equiv {x : ℝ} : problem_equation x ↔ alt_formula x = 0 := begin rw [ problem_equation, ← cos_sum_equiv, div_eq_zero_iff, sub_eq_zero], norm_num, end lemma finding_zeros {x : ℝ} : alt_formula x = 0 ↔ cos x ^ 2 = 1/2 ∨ cos (3 * x) = 0 := begin simp only [alt_formula, mul_assoc, mul_eq_zero, sub_eq_zero], split, { rintro (h1|h2), { right, rw [cos_three_mul, h1], ring }, { exact h2 } }, { exact or.inr } end /- Now we can solve for `x` using basic-ish trigonometry. -/ lemma solve_cos2_half {x : ℝ} : cos x ^ 2 = 1/2 ↔ ∃ k : ℤ, x = (2 * ↑k + 1) * π / 4 := begin rw cos_sq, simp only [add_right_eq_self, div_eq_zero_iff], norm_num, rw cos_eq_zero_iff, split; { rintro ⟨k, h⟩, use k, linarith }, end lemma solve_cos3x_0 {x : ℝ} : cos (3 * x) = 0 ↔ ∃ k : ℤ, x = (2 * ↑k + 1) * π / 6 := begin rw cos_eq_zero_iff, refine exists_congr (λ k, _), split; intro; linarith end /- The final theorem is now just gluing together our lemmas. -/ theorem imo1962_q4 {x : ℝ} : problem_equation x ↔ x ∈ solution_set := begin rw [alt_equiv, finding_zeros, solve_cos3x_0, solve_cos2_half], exact exists_or_distrib.symm end /- We now present a second solution. The key to this solution is that, when the identity is converted to an identity which is polynomial in `a` := `cos x`, it can be rewritten as a product of terms, `a ^ 2 * (2 * a ^ 2 - 1) * (4 * a ^ 2 - 3)`, being equal to zero. -/ /-- Someday, when there is a Grobner basis tactic, try to automate this proof. (A little tricky -- the ideals are not the same but their Jacobson radicals are.) -/ lemma formula {R : Type*} [comm_ring R] [integral_domain R] [char_zero R] (a : R) : a ^ 2 + (2 * a ^ 2 - 1) ^ 2 + (4 * a ^ 3 - 3 * a) ^ 2 = 1 ↔ (2 * a ^ 2 - 1) * (4 * a ^ 3 - 3 * a) = 0 := calc a ^ 2 + (2 * a ^ 2 - 1) ^ 2 + (4 * a ^ 3 - 3 * a) ^ 2 = 1 ↔ a ^ 2 + (2 * a ^ 2 - 1) ^ 2 + (4 * a ^ 3 - 3 * a) ^ 2 - 1 = 0 : by rw ← sub_eq_zero ... ↔ 2 * a ^ 2 * (2 * a ^ 2 - 1) * (4 * a ^ 2 - 3) = 0 : by { split; intros h; convert h; ring } ... ↔ a * (2 * a ^ 2 - 1) * (4 * a ^ 2 - 3) = 0 : by simp [(by norm_num : (2:R) ≠ 0)] ... ↔ (2 * a ^ 2 - 1) * (4 * a ^ 3 - 3 * a) = 0 : by { split; intros h; convert h using 1; ring } /- Again, we now can solve for `x` using basic-ish trigonometry. -/ lemma solve_cos2x_0 {x : ℝ} : cos (2 * x) = 0 ↔ ∃ k : ℤ, x = (2 * ↑k + 1) * π / 4 := begin rw cos_eq_zero_iff, refine exists_congr (λ k, _), split; intro; linarith end /- Again, the final theorem is now just gluing together our lemmas. -/ theorem imo1962_q4' {x : ℝ} : problem_equation x ↔ x ∈ solution_set := calc problem_equation x ↔ cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 = 1 : by refl ... ↔ cos (2 * x) = 0 ∨ cos (3 * x) = 0 : by simp [cos_two_mul, cos_three_mul, formula] ... ↔ x ∈ solution_set : by { rw [solve_cos2x_0, solve_cos3x_0, ← exists_or_distrib], refl }
d3dacfbfa88be9991a086d1fed57591a8aff789a
5ca7b1b12d14c4742e29366312ba2c2ef8201b21
/src/game/world10/level17.lean
19980aafe1f26acd3ac92411b86e7674bd53f55d
[ "Apache-2.0" ]
permissive
MatthiasHu/natural_number_game
2e464482ef3001863430b0336133b6697b275ba3
2d764f72669ae30861f6a1057fce0257f3e466c4
refs/heads/master
1,609,719,110,419
1,576,345,737,000
1,576,345,737,000
240,296,314
0
0
Apache-2.0
1,581,608,357,000
1,581,608,356,000
null
UTF-8
Lean
false
false
746
lean
--import mynat.lt -- definition of < import game.world10.level16 -- hide namespace mynat -- hide /- # Inequality world. ## Level 17: definition of `<` OK so we are going to *define* `a < b` by `a ≤ b ∧ ¬ (b ≤ a)`, and given `lt_aux_one a b` and `lt_aux_two a b` it should now just be a few lines to prove `a < b ↔ succ(a) ≤ b`. -/ definition lt (a b : mynat) := a ≤ b ∧ ¬ (b ≤ a) -- incantation so that we can use `<` notation: instance : has_lt mynat := ⟨lt⟩ /- Lemma : For all naturals $a$ and $b$, $$a<b\iff\operatorname{succ}(a)\le b.$$ -/ lemma lt_iff_succ_le (a b : mynat) : a < b ↔ succ a ≤ b := begin [nat_num_game] split, exact lt_aux_one a b, exact lt_aux_two a b, end end mynat -- hide
01abec88e2453330e50036d5c5e719b4379ecf5a
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed_space/bounded_linear_maps.lean
f105ef1de57dbfe89e24567f9dc38360bc81c0cf
[ "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
25,629
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import analysis.normed_space.multilinear import analysis.normed_space.units import analysis.asymptotics.asymptotics /-! # Bounded linear maps This file defines a class stating that a map between normed vector spaces is (bi)linear and continuous. Instead of asking for continuity, the definition takes the equivalent condition (because the space is normed) that `∥f x∥` is bounded by a multiple of `∥x∥`. Hence the "bounded" in the name refers to `∥f x∥/∥x∥` rather than `∥f x∥` itself. ## Main definitions * `is_bounded_linear_map`: Class stating that a map `f : E → F` is linear and has `∥f x∥` bounded by a multiple of `∥x∥`. * `is_bounded_bilinear_map`: Class stating that a map `f : E × F → G` is bilinear and continuous, but through the simpler to provide statement that `∥f (x, y)∥` is bounded by a multiple of `∥x∥ * ∥y∥` * `is_bounded_bilinear_map.linear_deriv`: Derivative of a continuous bilinear map as a linear map. * `is_bounded_bilinear_map.deriv`: Derivative of a continuous bilinear map as a continuous linear map. The proof that it is indeed the derivative is `is_bounded_bilinear_map.has_fderiv_at` in `analysis.calculus.fderiv`. ## Main theorems * `is_bounded_bilinear_map.continuous`: A bounded bilinear map is continuous. * `continuous_linear_equiv.is_open`: The continuous linear equivalences are an open subset of the set of continuous linear maps between a pair of Banach spaces. Placed in this file because its proof uses `is_bounded_bilinear_map.continuous`. ## Notes The main use of this file is `is_bounded_bilinear_map`. The file `analysis.normed_space.multilinear` already expounds the theory of multilinear maps, but the `2`-variables case is sufficiently simpler to currently deserve its own treatment. `is_bounded_linear_map` is effectively an unbundled version of `continuous_linear_map` (defined in `topology.algebra.module.basic`, theory over normed spaces developed in `analysis.normed_space.operator_norm`), albeit the name disparity. A bundled `continuous_linear_map` is to be preferred over a `is_bounded_linear_map` hypothesis. Historical artifact, really. -/ noncomputable theory open_locale classical big_operators topological_space open filter (tendsto) metric continuous_linear_map variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] /-- A function `f` satisfies `is_bounded_linear_map 𝕜 f` if it is linear and satisfies the inequality `∥f x∥ ≤ M * ∥x∥` for some positive constant `M`. -/ structure is_bounded_linear_map (𝕜 : Type*) [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E → F) extends is_linear_map 𝕜 f : Prop := (bound : ∃ M, 0 < M ∧ ∀ x : E, ∥f x∥ ≤ M * ∥x∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map 𝕜 f) (M : ℝ) (h : ∀ x : E, ∥f x∥ ≤ M * ∥x∥) : is_bounded_linear_map 𝕜 f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, λ x, (h x).trans $ mul_le_mul_of_nonneg_right (this.trans zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 f := { bound := f.bound, ..f.to_linear_map.is_linear } namespace is_bounded_linear_map /-- Construct a linear map from a function `f` satisfying `is_bounded_linear_map 𝕜 f`. -/ def to_linear_map (f : E → F) (h : is_bounded_linear_map 𝕜 f) : E →ₗ[𝕜] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map 𝕜 f) : E →L[𝕜] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in add_monoid_hom_class.continuous_of_bound (to_linear_map f hf) C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map 𝕜 (λ (x:E), (0:F)) := (0 : E →ₗ[𝕜] F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map 𝕜 (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map 𝕜 (λ x : E × F, x.1) := begin refine (linear_map.fst 𝕜 E F).is_linear.with_bound 1 (λ x, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map 𝕜 (λ x : E × F, x.2) := begin refine (linear_map.snd 𝕜 E F).is_linear.with_bound 1 (λ x, _), rw one_mul, exact le_max_right _ _ end variables {f g : E → F} lemma smul (c : 𝕜) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (c • f) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ λ x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : 𝕜) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ λ x, calc ∥f x + g x∥ ≤ Mf * ∥x∥ + Mg * ∥x∥ : norm_add_le_of_le (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e - g e) := by simpa [sub_eq_add_neg] using add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map 𝕜 g) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (g ∘ f) := (hg.to_continuous_linear_map.comp hf.to_continuous_linear_map).is_bounded_linear_map protected lemma tendsto (x : E) (hf : is_bounded_linear_map 𝕜 f) : tendsto f (𝓝 x) (𝓝 (f x)) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (λ e, norm_nonneg _) (λ e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices tendsto (λ (e : E), M * ∥e - x∥) (𝓝 x) (𝓝 (M * 0)), by simpa, tendsto_const_nhds.mul (tendsto_norm_sub_self _)) lemma continuous (hf : is_bounded_linear_map 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map 𝕜 f) : tendsto f (𝓝 0) (𝓝 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) : f =O[l] (λ x, x) := let ⟨M, hMp, hM⟩ := h.bound in is_O.of_bound _ (mem_of_superset univ_mem (λ x _, hM x)) theorem is_O_comp {E : Type*} {g : F → G} (hg : is_bounded_linear_map 𝕜 g) {f : E → F} (l : filter E) : (λ x', g (f x')) =O[l] f := (hg.is_O_id ⊤).comp_tendsto le_top theorem is_O_sub {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) (x : E) : (λ x', f (x' - x)) =O[l] (λ x', x' - x) := is_O_comp h l end end is_bounded_linear_map section variables {ι : Type*} [decidable_eq ι] [fintype ι] /-- Taking the cartesian product of two continuous multilinear maps is a bounded linear operation. -/ lemma is_bounded_linear_map_prod_multilinear {E : ι → Type*} [∀ i, normed_group (E i)] [∀ i, normed_space 𝕜 (E i)] : is_bounded_linear_map 𝕜 (λ p : (continuous_multilinear_map 𝕜 E F) × (continuous_multilinear_map 𝕜 E G), p.1.prod p.2) := { map_add := λ p₁ p₂, by { ext1 m, refl }, map_smul := λ c p, by { ext1 m, refl }, bound := ⟨1, zero_lt_one, λ p, begin rw one_mul, apply continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λ m, _), rw [continuous_multilinear_map.prod_apply, norm_prod_le_iff], split, { exact (p.1.le_op_norm m).trans (mul_le_mul_of_nonneg_right (norm_fst_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, { exact (p.2.le_op_norm m).trans (mul_le_mul_of_nonneg_right (norm_snd_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, end⟩ } /-- Given a fixed continuous linear map `g`, associating to a continuous multilinear map `f` the continuous multilinear map `f (g m₁, ..., g mₙ)` is a bounded linear operation. -/ lemma is_bounded_linear_map_continuous_multilinear_map_comp_linear (g : G →L[𝕜] E) : is_bounded_linear_map 𝕜 (λ f : continuous_multilinear_map 𝕜 (λ (i : ι), E) F, f.comp_continuous_linear_map (λ _, g)) := begin refine is_linear_map.with_bound ⟨λ f₁ f₂, by { ext m, refl }, λ c f, by { ext m, refl }⟩ (∥g∥ ^ (fintype.card ι)) (λ f, _), apply continuous_multilinear_map.op_norm_le_bound _ _ (λ m, _), { apply_rules [mul_nonneg, pow_nonneg, norm_nonneg] }, calc ∥f (g ∘ m)∥ ≤ ∥f∥ * ∏ i, ∥g (m i)∥ : f.le_op_norm _ ... ≤ ∥f∥ * ∏ i, (∥g∥ * ∥m i∥) : begin apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), exact finset.prod_le_prod (λ i hi, norm_nonneg _) (λ i hi, g.le_op_norm _) end ... = ∥g∥ ^ fintype.card ι * ∥f∥ * ∏ i, ∥m i∥ : by { simp [finset.prod_mul_distrib, finset.card_univ], ring } end end section bilinear_map namespace continuous_linear_map /-! We prove some computation rules for continuous (semi-)bilinear maps in their first argument. If `f` is a continuuous bilinear map, to use the corresponding rules for the second argument, use `(f _).map_add` and similar. We have to assume that `F` and `G` are normed spaces in this section, to use `continuous_linear_map.to_normed_group`, but we don't need to assume this for the first argument of `f`. -/ variables {R : Type*} variables {𝕜₂ 𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [nondiscrete_normed_field 𝕜₂] variables {M : Type*} [topological_space M] variables {σ₁₂ : 𝕜 →+* 𝕜₂} [ring_hom_isometric σ₁₂] variables {G' : Type*} [normed_group G'] [normed_space 𝕜₂ G'] [normed_space 𝕜' G'] variables [smul_comm_class 𝕜₂ 𝕜' G'] section semiring variables [semiring R] [add_comm_monoid M] [module R M] {ρ₁₂ : R →+* 𝕜'} lemma map_add₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (x x' : M) (y : F) : f (x + x') y = f x y + f x' y := by rw [f.map_add, add_apply] lemma map_zero₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (y : F) : f 0 y = 0 := by rw [f.map_zero, zero_apply] lemma map_smulₛₗ₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (c : R) (x : M) (y : F) : f (c • x) y = ρ₁₂ c • f x y := by rw [f.map_smulₛₗ, smul_apply] end semiring section ring variables [ring R] [add_comm_group M] [module R M] {ρ₁₂ : R →+* 𝕜'} lemma map_sub₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (x x' : M) (y : F) : f (x - x') y = f x y - f x' y := by rw [f.map_sub, sub_apply] lemma map_neg₂ (f : M →SL[ρ₁₂] F →SL[σ₁₂] G') (x : M) (y : F) : f (- x) y = - f x y := by rw [f.map_neg, neg_apply] end ring lemma map_smul₂ (f : E →L[𝕜] F →L[𝕜] G) (c : 𝕜) (x : E) (y : F) : f (c • x) y = c • f x y := by rw [f.map_smul, smul_apply] end continuous_linear_map variable (𝕜) /-- A map `f : E × F → G` satisfies `is_bounded_bilinear_map 𝕜 f` if it is bilinear and continuous. -/ structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀ (x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀ (c : 𝕜) (x : E) (y : F), f (c • x, y) = c • f (x, y)) (add_right : ∀ (x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀ (c : 𝕜) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃ C > 0, ∀ (x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {𝕜} variable {f : E × F → G} lemma continuous_linear_map.is_bounded_bilinear_map (f : E →L[𝕜] F →L[𝕜] G) : is_bounded_bilinear_map 𝕜 (λ x : E × F, f x.1 x.2) := { add_left := f.map_add₂, smul_left := f.map_smul₂, add_right := λ x, (f x).map_add, smul_right := λ c x, (f x).map_smul c, bound := ⟨max ∥f∥ 1, zero_lt_one.trans_le (le_max_right _ _), λ x y, (f.le_op_norm₂ x y).trans $ by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, le_max_left]⟩ } protected lemma is_bounded_bilinear_map.is_O (h : is_bounded_bilinear_map 𝕜 f) : f =O[⊤] (λ p : E × F, ∥p.1∥ * ∥p.2∥) := let ⟨C, Cpos, hC⟩ := h.bound in asymptotics.is_O.of_bound _ $ filter.eventually_of_forall $ λ ⟨x, y⟩, by simpa [mul_assoc] using hC x y lemma is_bounded_bilinear_map.is_O_comp {α : Type*} (H : is_bounded_bilinear_map 𝕜 f) {g : α → E} {h : α → F} {l : filter α} : (λ x, f (g x, h x)) =O[l] (λ x, ∥g x∥ * ∥h x∥) := H.is_O.comp_tendsto le_top protected lemma is_bounded_bilinear_map.is_O' (h : is_bounded_bilinear_map 𝕜 f) : f =O[⊤] (λ p : E × F, ∥p∥ * ∥p∥) := h.is_O.trans (asymptotics.is_O_fst_prod'.norm_norm.mul asymptotics.is_O_snd_prod'.norm_norm) lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map 𝕜 f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : 𝕜) • y, z) : by simp [sub_eq_add_neg] ... = f (x, z) + (-1 : 𝕜) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp [sub_eq_add_neg] lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map 𝕜 f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : 𝕜) • z) : by simp [sub_eq_add_neg] ... = f (x, y) + (-1 : 𝕜) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp [sub_eq_add_neg] /-- Useful to use together with `continuous.comp₂`. -/ lemma is_bounded_bilinear_map.continuous (h : is_bounded_bilinear_map 𝕜 f) : continuous f := begin have one_ne : (1:ℝ) ≠ 0 := by simp, obtain ⟨C, (Cpos : 0 < C), hC⟩ := h.bound, rw continuous_iff_continuous_at, intros x, have H : ∀ (a:E) (b:F), ∥f (a, b)∥ ≤ C * ∥∥a∥ * ∥b∥∥, { intros a b, simpa [mul_assoc] using hC a b }, have h₁ : (λ e : E × F, f (e.1 - x.1, e.2)) =o[𝓝 x] (λ e, (1:ℝ)), { refine (asymptotics.is_O_of_le' (𝓝 x) (λ e, H (e.1 - x.1) e.2)).trans_is_o _, rw asymptotics.is_o_const_iff one_ne, convert ((continuous_fst.sub continuous_const).norm.mul continuous_snd.norm).continuous_at, { simp }, apply_instance }, have h₂ : (λ e : E × F, f (x.1, e.2 - x.2)) =o[𝓝 x] (λ e, (1:ℝ)), { refine (asymptotics.is_O_of_le' (𝓝 x) (λ e, H x.1 (e.2 - x.2))).trans_is_o _, rw asymptotics.is_o_const_iff one_ne, convert (continuous_const.mul (continuous_snd.sub continuous_const).norm).continuous_at, { simp }, apply_instance }, have := h₁.add h₂, rw asymptotics.is_o_const_iff one_ne at this, change tendsto _ _ _, convert this.add_const (f x), { ext e, simp [h.map_sub_left, h.map_sub_right], }, { simp } end lemma is_bounded_bilinear_map.continuous_left (h : is_bounded_bilinear_map 𝕜 f) {e₂ : F} : continuous (λe₁, f (e₁, e₂)) := h.continuous.comp (continuous_id.prod_mk continuous_const) lemma is_bounded_bilinear_map.continuous_right (h : is_bounded_bilinear_map 𝕜 f) {e₁ : E} : continuous (λe₂, f (e₁, e₂)) := h.continuous.comp (continuous_const.prod_mk continuous_id) /-- Useful to use together with `continuous.comp₂`. -/ lemma continuous_linear_map.continuous₂ (f : E →L[𝕜] F →L[𝕜] G) : continuous (function.uncurry (λ x y, f x y)) := f.is_bounded_bilinear_map.continuous lemma is_bounded_bilinear_map.is_bounded_linear_map_left (h : is_bounded_bilinear_map 𝕜 f) (y : F) : is_bounded_linear_map 𝕜 (λ x, f (x, y)) := { map_add := λ x x', h.add_left _ _ _, map_smul := λ c x, h.smul_left _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥y∥ + 1), mul_pos C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ x, _⟩, have : ∥y∥ ≤ ∥y∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * ∥x∥ * (∥y∥ + 1) : by apply_rules [norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos, mul_nonneg] ... = C * (∥y∥ + 1) * ∥x∥ : by ring end } lemma is_bounded_bilinear_map.is_bounded_linear_map_right (h : is_bounded_bilinear_map 𝕜 f) (x : E) : is_bounded_linear_map 𝕜 (λ y, f (x, y)) := { map_add := λ y y', h.add_right _ _ _, map_smul := λ c y, h.smul_right _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥x∥ + 1), mul_pos C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ y, _⟩, have : ∥x∥ ≤ ∥x∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * (∥x∥ + 1) * ∥y∥ : by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos] end } lemma is_bounded_bilinear_map_smul {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] {E : Type*} [normed_group E] [normed_space 𝕜 E] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜' × E), p.1 • p.2) := (lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] E →L[𝕜] E).is_bounded_bilinear_map lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := by simp_rw ← smul_eq_mul; exact is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map 𝕜 (λ (p : (F →L[𝕜] G) × (E →L[𝕜] F)), p.1.comp p.2) := (compL 𝕜 E F G).is_bounded_bilinear_map lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : F →L[𝕜] G) : is_bounded_linear_map 𝕜 (λ (f : E →L[𝕜] F), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_right _ lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 (λ (g : F →L[𝕜] G), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_left _ lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map 𝕜 (λ p : (E →L[𝕜] F) × E, p.1 p.2) := (continuous_linear_map.flip (apply 𝕜 F : E →L[𝕜] (E →L[𝕜] F) →L[𝕜] F)).is_bounded_bilinear_map /-- The function `continuous_linear_map.smul_right`, associating to a continuous linear map `f : E → 𝕜` and a scalar `c : F` the tensor product `f ⊗ c` as a continuous linear map from `E` to `F`, is a bounded bilinear map. -/ lemma is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map 𝕜 (λ p, (continuous_linear_map.smul_right : (E →L[𝕜] 𝕜) → F → (E →L[𝕜] F)) p.1 p.2) := (smul_rightL 𝕜 E F).is_bounded_bilinear_map /-- The composition of a continuous linear map with a continuous multilinear map is a bounded bilinear operation. -/ lemma is_bounded_bilinear_map_comp_multilinear {ι : Type*} {E : ι → Type*} [decidable_eq ι] [fintype ι] [∀ i, normed_group (E i)] [∀ i, normed_space 𝕜 (E i)] : is_bounded_bilinear_map 𝕜 (λ p : (F →L[𝕜] G) × (continuous_multilinear_map 𝕜 E F), p.1.comp_continuous_multilinear_map p.2) := (comp_continuous_multilinear_mapL 𝕜 E F G).is_bounded_bilinear_map /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here as a linear map `E × F →ₗ[𝕜] G`, then `is_bounded_bilinear_map.deriv` strengthens it to a continuous linear map `E × F →L[𝕜] G`. ``. -/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : E × F →ₗ[𝕜] G := { to_fun := λ q, f (p.1, q.2) + f (q.1, p.2), map_add' := λ q₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right], abel end, map_smul' := λ c q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } /-- The derivative of a bounded bilinear map at a point `p : E × F`, as a continuous linear map from `E × F` to `G`. The statement that this is indeed the derivative of `f` is `is_bounded_bilinear_map.has_fderiv_at` in `analysis.calculus.fderiv`. -/ def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : E × F →L[𝕜] G := (h.linear_deriv p).mk_continuous_of_exists_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λ q, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map 𝕜 f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl variables (𝕜) /-- The function `lmul_left_right : 𝕜' × 𝕜' → (𝕜' →L[𝕜] 𝕜')` is a bounded bilinear map. -/ lemma continuous_linear_map.lmul_left_right_is_bounded_bilinear (𝕜' : Type*) [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : is_bounded_bilinear_map 𝕜 (λ p : 𝕜' × 𝕜', continuous_linear_map.lmul_left_right 𝕜 𝕜' p.1 p.2) := (continuous_linear_map.lmul_left_right 𝕜 𝕜').is_bounded_bilinear_map variables {𝕜} /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ p : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos : 0 < C, hC⟩, refine is_linear_map.with_bound ⟨λ p₁ p₂, _, λ c p, _⟩ (C + C) (λ p, _), { ext; simp [h.add_left, h.add_right]; abel }, { ext; simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg Cpos.le Cpos.le) (norm_nonneg _)) (λ q, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, Cpos.le, le_refl, le_max_left, le_max_right, mul_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map lemma continuous.clm_comp {X} [topological_space X] {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} (hg : continuous g) (hf : continuous f) : continuous (λ x, (g x).comp (f x)) := (compL 𝕜 E F G).continuous₂.comp₂ hg hf lemma continuous_on.clm_comp {X} [topological_space X] {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F} {s : set X} (hg : continuous_on g s) (hf : continuous_on f s) : continuous_on (λ x, (g x).comp (f x)) s := (compL 𝕜 E F G).continuous₂.comp_continuous_on (hg.prod hf) namespace continuous_linear_equiv open set /-! ### The set of continuous linear equivalences between two Banach spaces is open In this section we establish that the set of continuous linear equivalences between two Banach spaces is an open subset of the space of linear maps between them. -/ protected lemma is_open [complete_space E] : is_open (range (coe : (E ≃L[𝕜] F) → (E →L[𝕜] F))) := begin rw [is_open_iff_mem_nhds, forall_range_iff], refine λ e, is_open.mem_nhds _ (mem_range_self _), let O : (E →L[𝕜] F) → (E →L[𝕜] E) := λ f, (e.symm : F →L[𝕜] E).comp f, have h_O : continuous O := is_bounded_bilinear_map_comp.continuous_right, convert units.is_open.preimage h_O using 1, ext f', split, { rintros ⟨e', rfl⟩, exact ⟨(e'.trans e.symm).to_unit, rfl⟩ }, { rintros ⟨w, hw⟩, use (units_equiv 𝕜 E w).trans e, ext x, simp [coe_fn_coe_base' w, hw] } end protected lemma nhds [complete_space E] (e : E ≃L[𝕜] F) : (range (coe : (E ≃L[𝕜] F) → (E →L[𝕜] F))) ∈ 𝓝 (e : E →L[𝕜] F) := is_open.mem_nhds continuous_linear_equiv.is_open (by simp) end continuous_linear_equiv
daa9022fc984d7b8cef400ecd8d1326cee0832f6
7a468d7c7c0949ab8b191bb62ff6d4d2af9f3917
/test/test_tactics.lean
b365e4d06150f4319607007a14e6a29b03dae156
[ "Apache-2.0" ]
permissive
seanpm2001/LeanProver_SMT2_Interface
c15b2fba021c406d965655b182eef54a14121b82
7ff0ce248b68ea4db2a2d4966a97b5786da05ed7
refs/heads/master
1,688,599,220,366
1,547,825,187,000
1,547,825,187,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
190
lean
import init.meta.tactic universe u axiom should_of_failed (A : Sort u) : A meta def must_fail (tac : tactic unit) := do tactic.fail_if_success tac, tactic.refine ``(should_of_failed _)
b8a863b69bb875b2d9aaa29e23f3bdd99668979d
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/arith4.lean
93d3c315a128dfa19c8868a20be49a8f34db1dd7
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
107
lean
import specialfn. variable x : Real eval sin(x) eval cos(x) eval tan(x) eval cot(x) eval sec(x) eval csc(x)
e901d60a99eb45e49a2accd2cb1d23c81d9345b5
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/fp/basic.lean
be26faf149003c4fbaa77a02e7164362479d62f1
[ "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
6,346
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Implementation of floating-point numbers (experimental). -/ import data.rat data.semiquot def int.shift2 (a b : ℕ) : ℤ → ℕ × ℕ | (int.of_nat e) := (a.shiftl e, b) | -[1+ e] := (a, b.shiftl e.succ) namespace fp @[derive inhabited] inductive rmode | NE -- round to nearest even class float_cfg := (prec emax : ℕ) (prec_pos : 0 < prec) (prec_max : prec ≤ emax) variable [C : float_cfg] include C def prec := C.prec def emax := C.emax def emin : ℤ := 1 - C.emax def valid_finite (e : ℤ) (m : ℕ) : Prop := emin ≤ e + prec - 1 ∧ e + prec - 1 ≤ emax ∧ e = max (e + m.size - prec) emin instance dec_valid_finite (e m) : decidable (valid_finite e m) := by unfold valid_finite; apply_instance inductive float | inf : bool → float | nan : float | finite : bool → Π e m, valid_finite e m → float def float.is_finite : float → bool | (float.finite s e m f) := tt | _ := ff def to_rat : Π (f : float), f.is_finite → ℚ | (float.finite s e m f) _ := let (n, d) := int.shift2 m 1 e, r := rat.mk_nat n d in if s then -r else r theorem float.zero.valid : valid_finite emin 0 := ⟨begin rw add_sub_assoc, apply le_add_of_nonneg_right, apply sub_nonneg_of_le, apply int.coe_nat_le_coe_nat_of_le, exact C.prec_pos end, by simpa [add_comm, add_left_comm, sub_eq_add_neg, emin] using show (prec : ℤ) ≤ emax + float_cfg.emax, from le_trans (int.coe_nat_le.2 C.prec_max) (le_add_of_nonneg_left (int.coe_zero_le _)), by rw max_eq_right; simp [sub_eq_add_neg]⟩ def float.zero (s : bool) : float := float.finite s emin 0 float.zero.valid instance : inhabited float := ⟨float.zero tt⟩ protected def float.sign' : float → semiquot bool | (float.inf s) := pure s | float.nan := ⊤ | (float.finite s e m f) := pure s protected def float.sign : float → bool | (float.inf s) := s | float.nan := ff | (float.finite s e m f) := s protected def float.is_zero : float → bool | (float.finite s e 0 f) := tt | _ := ff protected def float.neg : float → float | (float.inf s) := float.inf (bnot s) | float.nan := float.nan | (float.finite s e m f) := float.finite (bnot s) e m f def div_nat_lt_two_pow (n d : ℕ) : ℤ → bool | (int.of_nat e) := n < d.shiftl e | -[1+ e] := n.shiftl e.succ < d -- TODO(Mario): Prove these and drop 'meta' meta def of_pos_rat_dn (n : ℕ+) (d : ℕ+) : float × bool := begin let e₁ : ℤ := n.1.size - d.1.size - prec, cases h₁ : int.shift2 d.1 n.1 (e₁ + prec) with d₁ n₁, let e₂ := if n₁ < d₁ then e₁ - 1 else e₁, let e₃ := max e₂ emin, cases h₂ : int.shift2 d.1 n.1 (e₃ + prec) with d₂ n₂, let r := rat.mk_nat n₂ d₂, let m := r.floor, refine (float.finite ff e₃ (int.to_nat m) _, r.denom = 1), { exact undefined } end meta def next_up_pos (e m) (v : valid_finite e m) : float := let m' := m.succ in if ss : m'.size = m.size then float.finite ff e m' (by unfold valid_finite at *; rw ss; exact v) else if h : e = emax then float.inf ff else float.finite ff e.succ (nat.div2 m') undefined meta def next_dn_pos (e m) (v : valid_finite e m) : float := match m with | 0 := next_up_pos _ _ float.zero.valid | nat.succ m' := if ss : m'.size = m.size then float.finite ff e m' (by unfold valid_finite at *; rw ss; exact v) else if h : e = emin then float.finite ff emin m' undefined else float.finite ff e.pred (bit1 m') undefined end meta def next_up : float → float | (float.finite ff e m f) := next_up_pos e m f | (float.finite tt e m f) := float.neg $ next_dn_pos e m f | f := f meta def next_dn : float → float | (float.finite ff e m f) := next_dn_pos e m f | (float.finite tt e m f) := float.neg $ next_up_pos e m f | f := f meta def of_rat_up : ℚ → float | ⟨0, _, _, _⟩ := float.zero ff | ⟨nat.succ n, d, h, _⟩ := let (f, exact) := of_pos_rat_dn n.succ_pnat ⟨d, h⟩ in if exact then f else next_up f | ⟨-[1+n], d, h, _⟩ := float.neg (of_pos_rat_dn n.succ_pnat ⟨d, h⟩).1 meta def of_rat_dn (r : ℚ) : float := float.neg $ of_rat_up (-r) meta def of_rat : rmode → ℚ → float | rmode.NE r := let low := of_rat_dn r, high := of_rat_up r in if hf : high.is_finite then if r = to_rat _ hf then high else if lf : low.is_finite then if r - to_rat _ lf > to_rat _ hf - r then high else if r - to_rat _ lf < to_rat _ hf - r then low else match low, lf with float.finite s e m f, _ := if 2 ∣ m then low else high end else float.inf tt else float.inf ff namespace float instance : has_neg float := ⟨float.neg⟩ meta def add (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf tt) (inf ff) := nan | (inf ff) (inf tt) := nan | (inf s₁) _ := inf s₁ | _ (inf s₂) := inf s₂ | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in of_rat mode (to_rat f₁ rfl + to_rat f₂ rfl) meta instance : has_add float := ⟨float.add rmode.NE⟩ meta def sub (mode : rmode) (f1 f2 : float) : float := add mode f1 (-f2) meta instance : has_sub float := ⟨float.sub rmode.NE⟩ meta def mul (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf s₁) f₂ := if f₂.is_zero then nan else inf (bxor s₁ f₂.sign) | f₁ (inf s₂) := if f₁.is_zero then nan else inf (bxor f₁.sign s₂) | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in of_rat mode (to_rat f₁ rfl * to_rat f₂ rfl) meta def div (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf s₁) (inf s₂) := nan | (inf s₁) f₂ := inf (bxor s₁ f₂.sign) | f₁ (inf s₂) := zero (bxor f₁.sign s₂) | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in if f₂.is_zero then inf (bxor s₁ s₂) else of_rat mode (to_rat f₁ rfl / to_rat f₂ rfl) end float end fp
8bee8ccdc9a6c919f7a11574d946bea084abda3e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/module/linear_pmap.lean
26777fabe0e9f2a47939ecc7a13b6c3ab49a4d6d
[ "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
6,494
lean
/- Copyright (c) 2022 Moritz Doll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Moritz Doll -/ import linear_algebra.linear_pmap import topology.algebra.module.basic /-! # Partially defined linear operators over topological vector spaces We define basic notions of partially defined linear operators, which we call unbounded operators for short. In this file we prove all elementary properties of unbounded operators that do not assume that the underlying spaces are normed. ## Main definitions * `linear_pmap.is_closed`: An unbounded operator is closed iff its graph is closed. * `linear_pmap.is_closable`: An unbounded operator is closable iff the closure of its graph is a graph. * `linear_pmap.closure`: For a closable unbounded operator `f : linear_pmap R E F` the closure is the smallest closed extension of `f`. If `f` is not closable, then `f.closure` is defined as `f`. * `linear_pmap.has_core`: a submodule contained in the domain is a core if restricting to the core does not lose information about the unbounded operator. ## Main statements * `linear_pmap.closable_iff_exists_closed_extension`: an unbounded operator is closable iff it has a closed extension. * `linear_pmap.closable.exists_unique`: there exists a unique closure * `linear_pmap.closure_has_core`: the domain of `f` is a core of its closure ## References * [J. Weidmann, *Linear Operators in Hilbert Spaces*][weidmann_linear] ## Tags Unbounded operators, closed operators -/ open_locale topological_space variables {R E F : Type*} variables [comm_ring R] [add_comm_group E] [add_comm_group F] variables [module R E] [module R F] variables [topological_space E] [topological_space F] namespace linear_pmap /-! ### Closed and closable operators -/ /-- An unbounded operator is closed iff its graph is closed. -/ def is_closed (f : E →ₗ.[R] F) : Prop := is_closed (f.graph : set (E × F)) variables [has_continuous_add E] [has_continuous_add F] variables [topological_space R] [has_continuous_smul R E] [has_continuous_smul R F] /-- An unbounded operator is closable iff the closure of its graph is a graph. -/ def is_closable (f : E →ₗ.[R] F) : Prop := ∃ (f' : linear_pmap R E F), f.graph.topological_closure = f'.graph /-- A closed operator is trivially closable. -/ lemma is_closed.is_closable {f : E →ₗ.[R] F} (hf : f.is_closed) : f.is_closable := ⟨f, hf.submodule_topological_closure_eq⟩ /-- If `g` has a closable extension `f`, then `g` itself is closable. -/ lemma is_closable.le_is_closable {f g : E →ₗ.[R] F} (hf : f.is_closable) (hfg : g ≤ f) : g.is_closable := begin cases hf with f' hf, have : g.graph.topological_closure ≤ f'.graph := by { rw ←hf, exact submodule.topological_closure_mono (le_graph_of_le hfg) }, refine ⟨g.graph.topological_closure.to_linear_pmap _, _⟩, { intros x hx hx', cases x, exact f'.graph_fst_eq_zero_snd (this hx) hx' }, rw [submodule.to_linear_pmap_graph_eq], end /-- The closure is unique. -/ lemma is_closable.exists_unique {f : E →ₗ.[R] F} (hf : f.is_closable) : ∃! (f' : E →ₗ.[R] F), f.graph.topological_closure = f'.graph := begin refine exists_unique_of_exists_of_unique hf (λ _ _ hy₁ hy₂, eq_of_eq_graph _), rw [←hy₁, ←hy₂], end open_locale classical /-- If `f` is closable, then `f.closure` is the closure. Otherwise it is defined as `f.closure = f`. -/ noncomputable def closure (f : E →ₗ.[R] F) : E →ₗ.[R] F := if hf : f.is_closable then hf.some else f lemma closure_def {f : E →ₗ.[R] F} (hf : f.is_closable) : f.closure = hf.some := by simp [closure, hf] lemma closure_def' {f : E →ₗ.[R] F} (hf : ¬f.is_closable) : f.closure = f := by simp [closure, hf] /-- The closure (as a submodule) of the graph is equal to the graph of the closure (as a `linear_pmap`). -/ lemma is_closable.graph_closure_eq_closure_graph {f : E →ₗ.[R] F} (hf : f.is_closable) : f.graph.topological_closure = f.closure.graph := begin rw closure_def hf, exact hf.some_spec, end /-- A `linear_pmap` is contained in its closure. -/ lemma le_closure (f : E →ₗ.[R] F) : f ≤ f.closure := begin by_cases hf : f.is_closable, { refine le_of_le_graph _, rw ←hf.graph_closure_eq_closure_graph, exact (graph f).submodule_topological_closure }, rw closure_def' hf, end lemma is_closable.closure_mono {f g : E →ₗ.[R] F} (hg : g.is_closable) (h : f ≤ g) : f.closure ≤ g.closure := begin refine le_of_le_graph _, rw ←(hg.le_is_closable h).graph_closure_eq_closure_graph, rw ←hg.graph_closure_eq_closure_graph, exact submodule.topological_closure_mono (le_graph_of_le h), end /-- If `f` is closable, then the closure is closed. -/ lemma is_closable.closure_is_closed {f : E →ₗ.[R] F} (hf : f.is_closable) : f.closure.is_closed := begin rw [is_closed, ←hf.graph_closure_eq_closure_graph], exact f.graph.is_closed_topological_closure, end /-- If `f` is closable, then the closure is closable. -/ lemma is_closable.closure_is_closable {f : E →ₗ.[R] F} (hf : f.is_closable) : f.closure.is_closable := hf.closure_is_closed.is_closable lemma is_closable_iff_exists_closed_extension {f : E →ₗ.[R] F} : f.is_closable ↔ ∃ (g : E →ₗ.[R] F) (hg : g.is_closed), f ≤ g := ⟨λ h, ⟨f.closure, h.closure_is_closed, f.le_closure⟩, λ ⟨_, hg, h⟩, hg.is_closable.le_is_closable h⟩ /-! ### The core of a linear operator -/ /-- A submodule `S` is a core of `f` if the closure of the restriction of `f` to `S` is again `f`.-/ structure has_core (f : E →ₗ.[R] F) (S : submodule R E) : Prop := (le_domain : S ≤ f.domain) (closure_eq : (f.dom_restrict S).closure = f) lemma has_core_def {f : E →ₗ.[R] F} {S : submodule R E} (h : f.has_core S) : (f.dom_restrict S).closure = f := h.2 /-- For every unbounded operator `f` the submodule `f.domain` is a core of its closure. Note that we don't require that `f` is closable, due to the definition of the closure. -/ lemma closure_has_core (f : E →ₗ.[R] F) : f.closure.has_core f.domain := begin refine ⟨f.le_closure.1, _⟩, congr, ext, { simp only [dom_restrict_domain, submodule.mem_inf, and_iff_left_iff_imp], intro hx, exact f.le_closure.1 hx }, intros x y hxy, let z : f.closure.domain := ⟨y.1, f.le_closure.1 y.2⟩, have hyz : (y : E) = z := by simp, rw f.le_closure.2 hyz, exact dom_restrict_apply (hxy.trans hyz), end end linear_pmap
813efbfaf3dadcd0d9e81ee8deb05f0fa83838f1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/option/defs.lean
535df54ee80b6e18ca200bb148a2b00bc4cc3162
[ "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
7,038
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ /-! # Extra definitions on `option` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines more operations involving `option α`. Lemmas about them are located in other files under `data.option.`. Other basic operations on `option` are defined in the core library. -/ namespace option variables {α : Type*} {β : Type*} attribute [inline] option.is_some option.is_none /-- An elimination principle for `option`. It is a nondependent version of `option.rec`. -/ @[simp] protected def elim (b : β) (f : α → β) : option α → β | (some a) := f a | none := b instance has_mem : has_mem α (option α) := ⟨λ a b, b = some a⟩ @[simp] theorem mem_def {a : α} {b : option α} : a ∈ b ↔ b = some a := iff.rfl lemma mem_iff {a : α} {b : option α} : a ∈ b ↔ b = a := iff.rfl theorem is_none_iff_eq_none {o : option α} : o.is_none = tt ↔ o = none := ⟨option.eq_none_of_is_none, λ e, e.symm ▸ rfl⟩ theorem some_inj {a b : α} : some a = some b ↔ a = b := by simp lemma mem_some_iff {α : Type*} {a b : α} : a ∈ some b ↔ b = a := by simp /-- `o = none` is decidable even if the wrapped type does not have decidable equality. This is not an instance because it is not definitionally equal to `option.decidable_eq`. Try to use `o.is_none` or `o.is_some` instead. -/ @[inline] def decidable_eq_none {o : option α} : decidable (o = none) := decidable_of_decidable_of_iff (bool.decidable_eq _ _) is_none_iff_eq_none instance decidable_forall_mem {p : α → Prop} [decidable_pred p] : ∀ o : option α, decidable (∀ a ∈ o, p a) | none := is_true (by simp [false_implies_iff]) | (some a) := if h : p a then is_true $ λ o e, some_inj.1 e ▸ h else is_false $ mt (λ H, H _ rfl) h instance decidable_exists_mem {p : α → Prop} [decidable_pred p] : ∀ o : option α, decidable (∃ a ∈ o, p a) | none := is_false (λ ⟨a, ⟨h, _⟩⟩, by cases h) | (some a) := if h : p a then is_true $ ⟨_, rfl, h⟩ else is_false $ λ ⟨_, ⟨rfl, hn⟩⟩, h hn /-- Inhabited `get` function. Returns `a` if the input is `some a`, otherwise returns `default`. -/ @[reducible] def iget [inhabited α] : option α → α | (some x) := x | none := default @[simp] theorem iget_some [inhabited α] {a : α} : (some a).iget = a := rfl /-- `guard p a` returns `some a` if `p a` holds, otherwise `none`. -/ def guard (p : α → Prop) [decidable_pred p] (a : α) : option α := if p a then some a else none /-- `filter p o` returns `some a` if `o` is `some a` and `p a` holds, otherwise `none`. -/ def filter (p : α → Prop) [decidable_pred p] (o : option α) : option α := o.bind (guard p) /-- Cast of `option` to `list `. Returns `[a]` if the input is `some a`, and `[]` if it is `none`. -/ def to_list : option α → list α | none := [] | (some a) := [a] @[simp] theorem mem_to_list {a : α} {o : option α} : a ∈ to_list o ↔ a ∈ o := by cases o; simp [to_list, eq_comm] /-- Two arguments failsafe function. Returns `f a b` if the inputs are `some a` and `some b`, and "does nothing" otherwise. -/ def lift_or_get (f : α → α → α) : option α → option α → option α | none none := none | (some a) none := some a -- get a | none (some b) := some b -- get b | (some a) (some b) := some (f a b) -- lift f instance lift_or_get_comm (f : α → α → α) [h : is_commutative α f] : is_commutative (option α) (lift_or_get f) := ⟨λ a b, by cases a; cases b; simp [lift_or_get, h.comm]⟩ instance lift_or_get_assoc (f : α → α → α) [h : is_associative α f] : is_associative (option α) (lift_or_get f) := ⟨λ a b c, by cases a; cases b; cases c; simp [lift_or_get, h.assoc]⟩ instance lift_or_get_idem (f : α → α → α) [h : is_idempotent α f] : is_idempotent (option α) (lift_or_get f) := ⟨λ a, by cases a; simp [lift_or_get, h.idempotent]⟩ instance lift_or_get_is_left_id (f : α → α → α) : is_left_id (option α) (lift_or_get f) none := ⟨λ a, by cases a; simp [lift_or_get]⟩ instance lift_or_get_is_right_id (f : α → α → α) : is_right_id (option α) (lift_or_get f) none := ⟨λ a, by cases a; simp [lift_or_get]⟩ /-- Lifts a relation `α → β → Prop` to a relation `option α → option β → Prop` by just adding `none ~ none`. -/ inductive rel (r : α → β → Prop) : option α → option β → Prop /-- If `a ~ b`, then `some a ~ some b` -/ | some {a b} : r a b → rel (some a) (some b) /-- `none ~ none` -/ | none : rel none none /-- Partial bind. If for some `x : option α`, `f : Π (a : α), a ∈ x → option β` is a partial function defined on `a : α` giving an `option β`, where `some a = x`, then `pbind x f h` is essentially the same as `bind x f` but is defined only when all `x = some a`, using the proof to apply `f`. -/ @[simp] def pbind : Π (x : option α), (Π (a : α), a ∈ x → option β) → option β | none _ := none | (some a) f := f a rfl /-- Partial map. If `f : Π a, p a → β` is a partial function defined on `a : α` satisfying `p`, then `pmap f x h` is essentially the same as `map f x` but is defined only when all members of `x` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {p : α → Prop} (f : Π (a : α), p a → β) : Π x : option α, (∀ a ∈ x, p a) → option β | none _ := none | (some a) H := some (f a (H a (mem_def.mpr rfl))) /-- Flatten an `option` of `option`, a specialization of `mjoin`. -/ @[simp] def join : option (option α) → option α := λ x, bind x id protected def {u v} traverse {F : Type u → Type v} [applicative F] {α β : Type*} (f : α → F β) : option α → F (option β) | none := pure none | (some x) := some <$> f x /- By analogy with `monad.sequence` in `init/category/combinators.lean`. -/ /-- If you maybe have a monadic computation in a `[monad m]` which produces a term of type `α`, then there is a naturally associated way to always perform a computation in `m` which maybe produces a result. -/ def {u v} maybe {m : Type u → Type v} [monad m] {α : Type u} : option (m α) → m (option α) | none := return none | (some fn) := some <$> fn /-- Map a monadic function `f : α → m β` over an `o : option α`, maybe producing a result. -/ def {u v w} mmap {m : Type u → Type v} [monad m] {α : Type w} {β : Type u} (f : α → m β) (o : option α) : m (option β) := (o.map f).maybe /-- A monadic analogue of `option.elim`. -/ def melim {α β : Type*} {m : Type* → Type*} [monad m] (y : m β) (z : α → m β) (x : m (option α)) : m β := x >>= option.elim y z /-- A monadic analogue of `option.get_or_else`. -/ def mget_or_else {α : Type*} {m : Type* → Type*} [monad m] (x : m (option α)) (y : m α) : m α := melim y pure x end option
a5e9d995882ef26be843bc64fcb00ac11fae2ee1
2cf781335f4a6706b7452ab07ce323201e2e101f
/lean/deps/typed_smt2/src/galois/smt2/test.lean
7ed7bccd1b1d97cce4d69e3f8e9d3d62bf7037a1
[ "Apache-2.0" ]
permissive
simonjwinwood/reopt-vcg
697cdd5e68366b5aa3298845eebc34fc97ccfbe2
6aca24e759bff4f2230bb58270bac6746c13665e
refs/heads/master
1,586,353,878,347
1,549,667,148,000
1,549,667,148,000
159,409,828
0
0
null
1,543,358,444,000
1,543,358,444,000
null
UTF-8
Lean
false
false
2,620
lean
import system.io import .file_writer import .semantics import .theories.bv open smt2 /- Generte a proble mtaht asserts a constant x is equal to 3. -/ def generate_problem {m} [is_generator m ] : m unit := do let x := symbol.of_string "x", declare_const x (BitVec 32), assert (all_equal [var x (BitVec 32), bv.decimal (bitvec.of_nat 32 3)]), check_sat /- This actually generates the SMTLIB file. -/ def main : io unit := do smt2.file_writer.run "bar.smt2" $ do generate_problem /- This will either report an error because the problem is not well-formed, or return the property implied by the program. -/ def mkprop : except string Prop := semantics.run_and_collect_unsat generate_problem /- This is a hack to show that mkprop can be evaluated into a simpler proof. -/ example : mkprop = except.ok (¬∃(x : bitvec 32), x = bitvec.of_nat 32 3) := begin simp only [ mkprop , semantics.run_and_collect_unsat , id , generate_problem , declare_const , is_generator.declare_const , semantics.context.initial , semantics.declare_fun , semantics.register_symbol , semantics.context.add_binding , smt2.assert, is_generator.assert, semantics.assert , all_equal , check_sat, is_generator.check_sat, semantics.check_sat , semantics.check_sat_assuming , semantics.quantify_bindings , semantics.quantify_binding , pure, has_bind.bind , except.return , except.bind , except.map_poly , put , pwhen , mk_rbmap, mk_rbtree , has_mem.mem, rbmap.mem , get , state_t.pure, modify, state_t.modify, state_t.bind, state_t.get, state_t.put , monad_state.lift , list.forall_prop , list.reverse_core , interpretation.bind , apply_chainable , all_band , smt2.var , interpretation.lookup_var , rbmap.insert, rbtree.insert , has_emptyc.emptyc , interpretation.empty , rbmap.find , rbmap.find_entry , rbnode.find , rbnode.insert , rbnode.ins , rbnode.get_color , rbnode.mk_insert_result , rbtree.find , cmp_using, rbmap_lt , symbol.lt , symbol.of_string , except.is_ok.value , string.to_char_buffer , buffer.append_string , buffer.nil , rbmap.to_value , bv.decimal , if_false , list.map , dif_pos , band_tt , and_true , not_exists , coe_sort , to_bool_iff , cast ], exact rfl, end
1fc87c843443cb605b77a7962dde10c64d31b857
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/algebra/category/constructions/cone.hlean
5cb0c872feb7c965cc4511f1c02902e6b7137d8b
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,557
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Cones -/ import ..nat_trans ..category open functor nat_trans eq equiv is_trunc is_equiv iso sigma sigma.ops pi namespace category structure cone_obj {I C : Precategory} (F : I ⇒ C) := (c : C) (η : constant_functor I c ⟹ F) local attribute cone_obj.c [coercion] variables {I C : Precategory} {F : I ⇒ C} {x y z : cone_obj F} structure cone_hom (x y : cone_obj F) := (f : x ⟶ y) (p : Πi, cone_obj.η y i ∘ f = cone_obj.η x i) local attribute cone_hom.f [coercion] definition cone_id [constructor] (x : cone_obj F) : cone_hom x x := cone_hom.mk id (λi, !id_right) definition cone_comp [constructor] (g : cone_hom y z) (f : cone_hom x y) : cone_hom x z := cone_hom.mk (cone_hom.f g ∘ cone_hom.f f) abstract λi, by rewrite [assoc, +cone_hom.p] end definition is_hprop_hom_eq [instance] [priority 1001] {ob : Type} [C : precategory ob] {x y : ob} (f g : x ⟶ y) : is_hprop (f = g) := _ definition cone_obj_eq (p : cone_obj.c x = cone_obj.c y) (q : Πi, cone_obj.η x i = cone_obj.η y i ∘ hom_of_eq p) : x = y := begin induction x, induction y, esimp at *, induction p, apply ap (cone_obj.mk c), apply nat_trans_eq, intro i, exact q i ⬝ !id_right end theorem c_cone_obj_eq (p : cone_obj.c x = cone_obj.c y) (q : Πi, cone_obj.η x i = cone_obj.η y i ∘ hom_of_eq p) : ap cone_obj.c (cone_obj_eq p q) = p := begin induction x, induction y, esimp at *, induction p, esimp [cone_obj_eq], rewrite [-ap_compose,↑function.compose,ap_constant] end theorem cone_hom_eq {f f' : cone_hom x y} (q : cone_hom.f f = cone_hom.f f') : f = f' := begin induction f, induction f', esimp at *, induction q, apply ap (cone_hom.mk f), apply @is_hprop.elim, apply pi.is_trunc_pi, intro x, apply is_trunc_eq, -- type class fails end variable (F) definition precategory_cone [instance] [constructor] : precategory (cone_obj F) := @precategory.mk _ cone_hom abstract begin intro x y, assert H : cone_hom x y ≃ Σ(f : x ⟶ y), Πi, cone_obj.η y i ∘ f = cone_obj.η x i, { fapply equiv.MK, { intro f, induction f, constructor, assumption}, { intro v, induction v, constructor, assumption}, { intro v, induction v, reflexivity}, { intro f, induction f, reflexivity}}, apply is_trunc.is_trunc_equiv_closed_rev, exact H, fapply sigma.is_trunc_sigma, intros, apply is_trunc_succ, apply pi.is_trunc_pi, intros, esimp, /-exact _,-/ -- type class inference fails here apply is_trunc_eq, end end (λx y z, cone_comp) cone_id abstract begin intros, apply cone_hom_eq, esimp, apply assoc end end abstract begin intros, apply cone_hom_eq, esimp, apply id_left end end abstract begin intros, apply cone_hom_eq, esimp, apply id_right end end definition cone [constructor] : Precategory := precategory.Mk (precategory_cone F) variable {F} definition cone_iso_pr1 (h : x ≅ y) : cone_obj.c x ≅ cone_obj.c y := iso.MK (cone_hom.f (to_hom h)) (cone_hom.f (to_inv h)) (ap cone_hom.f (to_left_inverse h)) (ap cone_hom.f (to_right_inverse h)) definition cone_iso.mk (f : cone_obj.c x ≅ cone_obj.c y) (p : Πi, cone_obj.η y i ∘ to_hom f = cone_obj.η x i) : x ≅ y := begin fapply iso.MK, { exact !cone_hom.mk p}, { fapply cone_hom.mk, { exact to_inv f}, { intro i, apply comp_inverse_eq_of_eq_comp, exact (p i)⁻¹}}, { apply cone_hom_eq, esimp, apply left_inverse}, { apply cone_hom_eq, esimp, apply right_inverse}, end variables (x y) definition cone_iso_equiv [constructor] : (x ≅ y) ≃ Σ(f : cone_obj.c x ≅ cone_obj.c y), Πi, cone_obj.η y i ∘ to_hom f = cone_obj.η x i := begin fapply equiv.MK, { intro h, exact ⟨cone_iso_pr1 h, cone_hom.p (to_hom h)⟩}, { intro v, exact cone_iso.mk v.1 v.2}, { intro v, induction v with f p, fapply sigma_eq: esimp, { apply iso_eq, reflexivity}, { apply is_hprop.elimo, apply is_trunc_pi, intro i, apply is_hprop_hom_eq}}, { intro h, esimp, apply iso_eq, apply cone_hom_eq, reflexivity}, end --set_option pp.implicit true definition cone_eq_equiv : (x = y) ≃ Σ(f : cone_obj.c x = cone_obj.c y), Πi, cone_obj.η y i ∘ hom_of_eq f = cone_obj.η x i := begin fapply equiv.MK, { intro r, fapply sigma.mk, exact ap cone_obj.c r, induction r, intro i, apply id_right}, { intro v, induction v with p q, induction x with c η, induction y with c' η', esimp at *, apply cone_obj_eq p, esimp, intro i, exact (q i)⁻¹}, { intro v, induction v with p q, induction x with c η, induction y with c' η', esimp at *, induction p, esimp, fapply sigma_eq: esimp, { apply c_cone_obj_eq}, { apply is_hprop.elimo, apply is_trunc_pi, intro i, apply is_hprop_hom_eq}}, { intro r, induction r, esimp, induction x, esimp, apply ap02, apply is_hprop.elim}, end section is_univalent definition is_univalent_cone {I : Precategory} {C : Category} (F : I ⇒ C) : is_univalent (cone F) := begin intro x y, fapply is_equiv_of_equiv_of_homotopy, { exact calc (x = y) ≃ (Σ(f : cone_obj.c x = cone_obj.c y), Πi, cone_obj.η y i ∘ hom_of_eq f = cone_obj.η x i) : cone_eq_equiv ... ≃ (Σ(f : cone_obj.c x ≅ cone_obj.c y), Πi, cone_obj.η y i ∘ to_hom f = cone_obj.η x i) : sigma_equiv_sigma !eq_equiv_iso (λa, !equiv.refl) ... ≃ (x ≅ y) : cone_iso_equiv }, { intro p, induction p, esimp [equiv.trans,equiv.symm], esimp [sigma_functor], apply iso_eq, reflexivity} end definition category_cone [instance] [constructor] {I : Precategory} {C : Category} (F : I ⇒ C) : category (cone_obj F) := category.mk _ (is_univalent_cone F) definition Category_cone [constructor] {I : Precategory} {C : Category} (F : I ⇒ C) : Category := Category.mk _ (category_cone F) end is_univalent end category
37c1ed92e87004295cfa7341cfe98e36f8c58115
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/complete_boolean_algebra.lean
ee0945a23edc803c4f975eda9479f3654a3027b7
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,862
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Theory of complete Boolean algebras. -/ import order.complete_lattice set_option old_structure_cmd true universes u v w variables {α : Type u} {β : Type v} {ι : Sort w} /-- A complete distributive lattice is a bit stronger than the name might suggest; perhaps completely distributive lattice is more descriptive, as this class includes a requirement that the lattice join distribute over *arbitrary* infima, and similarly for the dual. -/ class complete_distrib_lattice α extends complete_lattice α := (infi_sup_le_sup_Inf : ∀a s, (⨅ b ∈ s, a ⊔ b) ≤ a ⊔ Inf s) (inf_Sup_le_supr_inf : ∀a s, a ⊓ Sup s ≤ (⨆ b ∈ s, a ⊓ b)) section complete_distrib_lattice variables [complete_distrib_lattice α] {a b : α} {s t : set α} instance : complete_distrib_lattice (order_dual α) := { infi_sup_le_sup_Inf := complete_distrib_lattice.inf_Sup_le_supr_inf, inf_Sup_le_supr_inf := complete_distrib_lattice.infi_sup_le_sup_Inf, .. order_dual.complete_lattice α } theorem sup_Inf_eq : a ⊔ Inf s = (⨅ b ∈ s, a ⊔ b) := sup_Inf_le_infi_sup.antisymm (complete_distrib_lattice.infi_sup_le_sup_Inf _ _) theorem Inf_sup_eq : Inf s ⊔ b = (⨅ a ∈ s, a ⊔ b) := by simpa only [sup_comm] using @sup_Inf_eq α _ b s theorem inf_Sup_eq : a ⊓ Sup s = (⨆ b ∈ s, a ⊓ b) := (complete_distrib_lattice.inf_Sup_le_supr_inf _ _).antisymm supr_inf_le_inf_Sup theorem Sup_inf_eq : Sup s ⊓ b = (⨆ a ∈ s, a ⊓ b) := by simpa only [inf_comm] using @inf_Sup_eq α _ b s theorem supr_inf_eq (f : ι → α) (a : α) : (⨆ i, f i) ⊓ a = ⨆ i, f i ⊓ a := by rw [supr, Sup_inf_eq, supr_range] theorem inf_supr_eq (a : α) (f : ι → α) : a ⊓ (⨆ i, f i) = ⨆ i, a ⊓ f i := by simpa only [inf_comm] using supr_inf_eq f a theorem infi_sup_eq (f : ι → α) (a : α) : (⨅ i, f i) ⊔ a = ⨅ i, f i ⊔ a := @supr_inf_eq (order_dual α) _ _ _ _ theorem sup_infi_eq (a : α) (f : ι → α) : a ⊔ (⨅ i, f i) = ⨅ i, a ⊔ f i := @inf_supr_eq (order_dual α) _ _ _ _ instance pi.complete_distrib_lattice {ι : Type*} {π : ι → Type*} [∀ i, complete_distrib_lattice (π i)] : complete_distrib_lattice (Π i, π i) := { infi_sup_le_sup_Inf := λ a s i, by simp only [← sup_infi_eq, complete_lattice.Inf, Inf_apply, ←infi_subtype'', infi_apply, sup_apply], inf_Sup_le_supr_inf := λ a s i, by simp only [complete_lattice.Sup, Sup_apply, supr_apply, inf_apply, inf_supr_eq, ← supr_subtype''], .. pi.complete_lattice } theorem Inf_sup_Inf : Inf s ⊔ Inf t = (⨅p ∈ set.prod s t, (p : α × α).1 ⊔ p.2) := begin apply le_antisymm, { simp only [and_imp, prod.forall, le_infi_iff, set.mem_prod], intros a b ha hb, exact sup_le_sup (Inf_le ha) (Inf_le hb) }, { have : ∀ a ∈ s, (⨅p ∈ set.prod s t, (p : α × α).1 ⊔ p.2) ≤ a ⊔ Inf t, { assume a ha, have : (⨅p ∈ set.prod s t, ((p : α × α).1 : α) ⊔ p.2) ≤ (⨅p ∈ prod.mk a '' t, (p : α × α).1 ⊔ p.2), { apply infi_le_infi_of_subset, rintros ⟨x, y⟩, simp only [and_imp, set.mem_image, prod.mk.inj_iff, set.prod_mk_mem_set_prod_eq, exists_imp_distrib], assume x' x't ax x'y, rw [← x'y, ← ax], simp [ha, x't] }, rw [infi_image] at this, simp only at this, rwa ← sup_Inf_eq at this }, calc (⨅p ∈ set.prod s t, (p : α × α).1 ⊔ p.2) ≤ (⨅a∈s, a ⊔ Inf t) : by simp; exact this ... = Inf s ⊔ Inf t : Inf_sup_eq.symm } end theorem Sup_inf_Sup : Sup s ⊓ Sup t = (⨆p ∈ set.prod s t, (p : α × α).1 ⊓ p.2) := @Inf_sup_Inf (order_dual α) _ _ _ lemma supr_disjoint_iff {f : ι → α} : disjoint (⨆ i, f i) a ↔ ∀ i, disjoint (f i) a := by simp only [disjoint_iff, supr_inf_eq, supr_eq_bot] lemma disjoint_supr_iff {f : ι → α} : disjoint a (⨆ i, f i) ↔ ∀ i, disjoint a (f i) := by simpa only [disjoint.comm] using @supr_disjoint_iff _ _ _ a f end complete_distrib_lattice @[priority 100] -- see Note [lower instance priority] instance complete_distrib_lattice.bounded_distrib_lattice [d : complete_distrib_lattice α] : bounded_distrib_lattice α := { le_sup_inf := λ x y z, by rw [← Inf_pair, ← Inf_pair, sup_Inf_eq, ← Inf_image, set.image_pair], ..d } /-- A complete boolean algebra is a completely distributive boolean algebra. -/ class complete_boolean_algebra α extends boolean_algebra α, complete_distrib_lattice α instance pi.complete_boolean_algebra {ι : Type*} {π : ι → Type*} [∀ i, complete_boolean_algebra (π i)] : complete_boolean_algebra (Π i, π i) := { .. pi.boolean_algebra, .. pi.complete_distrib_lattice } instance : complete_boolean_algebra Prop := { infi_sup_le_sup_Inf := λ p s, iff.mp $ by simp only [forall_or_distrib_left, complete_lattice.Inf, infi_Prop_eq, sup_Prop_eq], inf_Sup_le_supr_inf := λ p s, iff.mp $ by simp only [complete_lattice.Sup, exists_and_distrib_left, inf_Prop_eq, supr_Prop_eq], .. boolean_algebra_Prop, .. complete_lattice_Prop } section complete_boolean_algebra variables [complete_boolean_algebra α] {a b : α} {s : set α} {f : ι → α} theorem compl_infi : (infi f)ᶜ = (⨆i, (f i)ᶜ) := le_antisymm (compl_le_of_compl_le $ le_infi $ assume i, compl_le_of_compl_le $ le_supr (compl ∘ f) i) (supr_le $ assume i, compl_le_compl $ infi_le _ _) theorem compl_supr : (supr f)ᶜ = (⨅i, (f i)ᶜ) := compl_injective (by simp [compl_infi]) theorem compl_Inf : (Inf s)ᶜ = (⨆i∈s, iᶜ) := by simp only [Inf_eq_infi, compl_infi] theorem compl_Sup : (Sup s)ᶜ = (⨅i∈s, iᶜ) := by simp only [Sup_eq_supr, compl_supr] end complete_boolean_algebra
667acf87542512ef1892cc8e9da39002e3dbc7a6
08a8ee10652ba4f8592710ceb654b37e951d9082
/src/hott/types/sigma.lean
d6c3c6b01bc270d6dc21577fc93fac3df2df61a6
[ "Apache-2.0" ]
permissive
felixwellen/hott3
e9f299c84d30a782a741c40d38741ec024d391fb
8ac87a2699ab94c23ea7984b4a5fbd5a7052575c
refs/heads/master
1,619,972,899,098
1,509,047,351,000
1,518,040,986,000
120,676,559
0
0
null
1,518,040,503,000
1,518,040,503,000
null
UTF-8
Lean
false
false
24,103
lean
/- Copyright (c) 2014-15 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about sigma-types (dependent sums) -/ import .prod universes u v w hott_theory namespace hott open hott.eq sigma hott.equiv hott.is_equiv function hott.is_trunc sum unit @[reducible, hott] def dpair {α β} := @sigma.mk α β namespace sigma variables {A : Type _} {A' : Type _} {B : A → Type _} {B' : A' → Type _} {C : Πa, B a → Type _} {D : Πa b, C a b → Type _} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a} @[hott] def destruct := @sigma.cases_on /- Paths in a sigma-type -/ @[hott] protected def eta : Π (u : Σa, B a), (⟨u.1 , u.2⟩: sigma _) = u | ⟨u₁, u₂⟩ := idp @[hott] def eta2 : Π (u : Σa b, C a b), (⟨u.1, u.2.1, u.2.2⟩: Σ _ _, _) = u | ⟨u₁, u₂, u₃⟩ := idp @[hott] def eta3 : Π (u : Σa b c, D a b c), (⟨u.1, u.2.1, u.2.2.1, u.2.2.2⟩: Σ _ _ _, _) = u | ⟨u₁, u₂, u₃, u₄⟩ := idp @[hott] def dpair_eq_dpair (p : a = a') (q : b =[p] b') : (⟨a, b⟩: Σ _, _) = ⟨a', b'⟩ := apd011 sigma.mk p q @[hott] def sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : u = v := by induction u; induction v; exact (dpair_eq_dpair p q) @[hott] def sigma_eq_right (q : b₁ = b₂) : (⟨a, b₁⟩: Σ _, _) = ⟨a, b₂⟩ := ap (dpair a) q @[hott] def eq_fst (p : u = v) : u.1 = v.1 := ap fst p postfix `..1`:(max+1) := eq_fst @[hott] def eq_snd (p : u = v) : u.2 =[p..1] v.2 := by induction p; exact idpo postfix `..2`:(max+1) := eq_snd @[hott] def dpair_sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : (⟨(sigma_eq p q)..1, (sigma_eq p q)..2⟩: Σ p, u.2 =[p] v.2) = ⟨p, q⟩ := by induction u; induction v;dsimp at *;induction q;refl @[hott] def sigma_eq_fst (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..1 = p := (dpair_sigma_eq p q)..1 @[hott] def sigma_eq_snd (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..2 =[sigma_eq_fst p q; λ p, u.2 =[p] v.2] q := (dpair_sigma_eq p q)..2 @[hott] def sigma_eq_eta (p : u = v) : sigma_eq (p..1) (p..2) = p := by induction p; induction u; reflexivity @[hott] def eq2_fst {p q : u = v} (r : p = q) : p..1 = q..1 := ap eq_fst r @[hott] def eq2_snd {p q : u = v} (r : p = q) : p..2 =[eq2_fst r; λ x, u.2 =[x] v.2] q..2 := by apply pathover_ap; apply (apd eq_snd r) @[hott] def tr_fst_sigma_eq {B' : A → Type _} (p : u.1 = v.1) (q : u.2 =[p] v.2) : transport (λx : sigma _, B' x.1) (sigma_eq p q) = transport B' p := by induction u; induction v; dsimp at *;induction q; reflexivity @[hott] protected def ap_fst (p : u = v) : ap (λx : sigma B, x.1) p = p..1 := idp /- the uncurried version of sigma_eq. We will prove that this is an equivalence -/ @[hott] def sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), u = v | ⟨pq₁, pq₂⟩ := sigma_eq pq₁ pq₂ @[hott] def dpair_sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), (⟨(sigma_eq_unc pq)..1, (sigma_eq_unc pq)..2⟩: Σ p, u.2 =[p] v.2) = pq | ⟨pq₁, pq₂⟩ := dpair_sigma_eq pq₁ pq₂ @[hott] def sigma_eq_fst_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..1 = pq.1 := (dpair_sigma_eq_unc pq)..1 @[hott] def sigma_eq_snd_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..2 =[sigma_eq_fst_unc pq; λ p, u.2 =[p] v.2] pq.2 := (dpair_sigma_eq_unc pq)..2 @[hott] def sigma_eq_eta_unc (p : u = v) : sigma_eq_unc ⟨p..1, p..2⟩ = p := sigma_eq_eta p @[hott] def tr_sigma_eq_fst_unc {B' : A → Type _} (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : transport (λx:sigma _, B' x.1) (@sigma_eq_unc A B u v pq) = transport B' pq.1 := by apply destruct pq; apply tr_fst_sigma_eq @[hott, instance] def is_equiv_sigma_eq (u v : Σa, B a) : is_equiv (@sigma_eq_unc A B u v) := adjointify sigma_eq_unc (λp, ⟨p..1, p..2⟩) sigma_eq_eta_unc dpair_sigma_eq_unc @[hott] def sigma_eq_equiv (u v : Σa, B a) : (u = v) ≃ (Σ(p : u.1 = v.1), u.2 =[p] v.2) := (equiv.mk sigma_eq_unc (by apply_instance))⁻¹ᵉ @[hott] def dpair_eq_dpair_con (p1 : a = a' ) (q1 : b =[p1] b' ) (p2 : a' = a'') (q2 : b' =[p2] b'') : dpair_eq_dpair (p1 ⬝ p2) (q1 ⬝o q2) = dpair_eq_dpair p1 q1 ⬝ dpair_eq_dpair p2 q2 := by induction q1; induction q2; reflexivity @[hott] def sigma_eq_con (p1 : u.1 = v.1) (q1 : u.2 =[p1] v.2) (p2 : v.1 = w.1) (q2 : v.2 =[p2] w.2) : sigma_eq (p1 ⬝ p2) (q1 ⬝o q2) = sigma_eq p1 q1 ⬝ sigma_eq p2 q2 := by induction u; induction v; induction w; apply dpair_eq_dpair_con @[hott] def dpair_eq_dpair_con_idp (p : a = a') (q : b =[p] b') : dpair_eq_dpair p q = dpair_eq_dpair p (pathover_tr _ _) ⬝ dpair_eq_dpair idp (pathover_idp_of_eq _ (tr_eq_of_pathover q)) := by induction q; reflexivity /- eq_fst commutes with the groupoid structure. -/ @[hott] def eq_fst_idp (u : Σa, B a) : (idpath u) ..1 = refl (u.1) := idp @[hott] def eq_fst_con (p : u = v) (q : v = w) : (p ⬝ q) ..1 = (p..1) ⬝ (q..1) := ap_con _ _ _ @[hott] def eq_fst_inv (p : u = v) : p⁻¹ ..1 = (p..1)⁻¹ := ap_inv _ _ /- Applying dpair to one argument is the same as dpair_eq_dpair with reflexivity in the first place. -/ @[hott] def ap_dpair (q : b₁ = b₂) : ap (sigma.mk a) q = dpair_eq_dpair idp (pathover_idp_of_eq _ q) := by induction q; reflexivity /- Dependent transport is the same as transport along a sigma_eq. -/ @[hott] def transportD_eq_transport (p : a = a') (c : C a b) : p ▸D c = transport (λu : sigma _, C (u.1) (u.2)) (dpair_eq_dpair p (pathover_tr _ _)) c := by induction p; reflexivity @[hott] def sigma_eq_eq_sigma_eq {p1 q1 : a = a'} {p2 : b =[p1] b'} {q2 : b =[q1] b'} (r : p1 = q1) (s : p2 =[r; λ p, b =[p] b'] q2) : @sigma_eq _ _ ⟨a,b⟩ ⟨a',b'⟩ p1 p2 = sigma_eq q1 q2 := by induction s; reflexivity /- A path between paths in a total space is commonly shown component wise. -/ @[hott] def sigma_eq2 {p q : u = v} (r : p..1 = q..1) (s : p..2 =[r; λ p, u.2 =[p] v.2] q..2) : p = q := begin induction p, induction u with u1 u2, transitivity sigma_eq q..1 q..2, apply sigma_eq_eq_sigma_eq r s, apply sigma_eq_eta, end @[hott] def sigma_eq2_unc {p q : u = v} (rs : Σ(r : p..1 = q..1), p..2 =[r; λ p, u.2 =[p] v.2] q..2) : p = q := by apply destruct rs; apply sigma_eq2 @[hott] def ap_dpair_eq_dpair (f : Πa, B a → A') (p : a = a') (q : b =[p] b') : @ap _ A' (@sigma.rec _ _ (λ _, A') f) _ _ (dpair_eq_dpair p q) = apd011 f p q := by induction q; reflexivity /- Transport -/ /- The concrete description of transport in sigmas (and also pis) is rather trickier than in the other types. In particular, these cannot be described just in terms of transport in simpler types; they require also the dependent transport [transportD]. In particular, this indicates why `transport` alone cannot be fully defined by induction on the structure of types, although Id-elim/transportD can be (cf. Observational Type _ Theory). A more thorough set of lemmas, along the lines of the present ones but dealing with Id-elim rather than just transport, might be nice to have eventually? -/ @[hott] def sigma_transport (p : a = a') (bc : Σ(b : B a), C a b) : p ▸ bc = ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; reflexivity /- The special case when the second variable doesn't depend on the first is simpler. -/ @[hott] def sigma_transport_nondep {B : Type _} {C : A → B → Type _} (p : a = a') (bc : Σ(b : B), C a b) : p ▸ bc = ⟨bc.1, transport (λ a, C a bc.1) p bc.2⟩ := by induction p; induction bc; reflexivity /- Or if the second variable contains a first component that doesn't depend on the first. -/ @[hott] def sigma_transport2_nondep {C : A → Type _} {D : Π a:A, B a → C a → Type _} (p : a = a') (bcd : Σ(b : B a) (c : C a), D a b c) : p ▸ bcd = ⟨p ▸ bcd.1, p ▸ bcd.2.1, p ▸D2 bcd.2.2⟩ := begin induction p, induction bcd with b cd, induction cd, reflexivity end /- Pathovers -/ @[hott] def etao (p : a = a') (bc : Σ(b : B a), C a b) : bc =[p; λ a, Σ b, C a b] ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; apply idpo -- TODO: interchange sigma_pathover and sigma_pathover' @[hott] def sigma_pathover (p : a = a') (u : Σ(b : B a), C a b) (v : Σ(b : B a'), C a' b) (r : u.1 =[p] v.1) (s : u.2 =[apd011 C p r; id] v.2) : u =[p; λ a, Σ b, C a b] v := begin induction u, induction v, dsimp at *, induction r, dsimp [apd011] at s, apply idp_rec_on s, apply idpo end @[hott] def sigma_pathover' (p : a = a') (u : Σ(b : B a), C a b) (v : Σ(b : B a'), C a' b) (r : u.1 =[p] v.1) (s : @pathover (sigma _) ⟨a,u.1⟩ (λ x, C x.1 x.2) u.2 ⟨a',v.1⟩ (sigma_eq p r) v.2) : u =[p; λ a, Σ b, C a b] v := begin induction u, induction v, dsimp at *, induction r, apply idp_rec_on s, apply idpo end @[hott] def sigma_pathover_nondep {B : Type _} {C : A → B → Type _} (p : a = a') (u : Σ(b : B), C a b) (v : Σ(b : B), C a' b) (r : u.1 = v.1) (s : @pathover (prod _ _) (a,u.1) (λx, C x.1 x.2) u.2 (a',v.1) (prod.prod_eq p r) v.2) : u =[p; λ a, Σ b, C a b] v := begin induction p, induction u, induction v, dsimp at *, induction r, apply idp_rec_on s, apply idpo end @[hott] def pathover_fst {A : Type _} {B : A → Type _} {C : Πa, B a → Type _} {a a' : A} {p : a = a'} {x : Σb, C a b} {x' : Σb', C a' b'} (q : x =[p; λ a, Σb, C a b] x') : x.1 =[p] x'.1 := begin induction q, constructor end @[hott] def sigma_pathover_equiv_of_is_prop {A : Type _} {B : A → Type _} (C : Πa, B a → Type _) {a a' : A} (p : a = a') (x : Σb, C a b) (x' : Σb', C a' b') [Πa b, is_prop (C a b)] : x =[p; λa, Σb, C a b] x' ≃ x.1 =[p] x'.1 := begin fapply equiv.MK, { exact pathover_fst }, { intro q, induction x with b c, induction x' with b' c', dsimp at q, induction q, apply pathover_idp_of_eq, exact sigma_eq idp (is_prop.elimo _ _ _) }, { intro q, induction x with b c, induction x' with b' c', dsimp at q, induction q, have: c = c', by apply is_prop.elim, induction this, dsimp, rwr is_prop_elimo_self, }, { intro q, induction q, induction x with b c, dsimp [pathover_fst], rwr is_prop_elimo_self } end /- TODO: * define the projections from the type u =[p] v * show that the uncurried version of sigma_pathover is an equivalence -/ /- Squares in a sigma type are characterized in cubical.squareover (to avoid circular imports) -/ /- Functorial action -/ variables (f : A → A') (g : Πa, B a → B' (f a)) @[hott] def sigma_functor (u : Σa, B a) : Σa', B' a' := ⟨f u.1, g u.1 u.2⟩ @[hott] def total {B' : A → Type _} (g : Πa, B a → B' a) : (Σa, B a) → (Σa, B' a) := sigma_functor id g /- Equivalences -/ @[hott] def is_equiv_sigma_functor [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : is_equiv (sigma_functor f g) := adjointify (sigma_functor f g) (sigma_functor f⁻¹ᶠ (λ(a' : A') (b' : B' a'), ((g (f⁻¹ᶠ a'))⁻¹ᶠ (transport B' (right_inv f a')⁻¹ b')))) begin abstract { intro u', induction u' with a' b', fapply sigma_eq, {apply right_inv f}, {dsimp [sigma_functor], rwr right_inv (g (f⁻¹ᶠ a')), apply tr_pathover} } end begin abstract { intro u, induction u with a b, fapply sigma_eq, {apply left_inv f}, {apply pathover_of_tr_eq, dsimp only [sigma_functor], rwr [adj f, ← fn_tr_eq_tr_fn (left_inv f a) (λ a, (g a)⁻¹ᶠ), tr_compose B', tr_inv_tr], dsimp, rwr left_inv } } end @[hott] def sigma_equiv_sigma_of_is_equiv [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : (Σa, B a) ≃ (Σa', B' a') := equiv.mk (sigma_functor f g) (is_equiv_sigma_functor _ _) @[hott] def sigma_equiv_sigma (Hf : A ≃ A') (Hg : Π a, B a ≃ B' (Hf a)) : (Σa, B a) ≃ (Σa', B' a') := sigma_equiv_sigma_of_is_equiv Hf (λ a, Hg a) @[hott] def sigma_equiv_sigma_right {B' : A → Type _} (Hg : Π a, B a ≃ B' a) : (Σa, B a) ≃ Σa, B' a := sigma_equiv_sigma equiv.rfl Hg variable (B) @[hott] def sigma_equiv_sigma_left (Hf : A ≃ A') : (Σa, B a) ≃ (Σa', B (Hf⁻¹ᶠ a')) := sigma_equiv_sigma Hf (λ a, equiv_ap B (right_inv Hf⁻¹ᶠ a)⁻¹ᵖ) @[hott] def sigma_equiv_sigma_left' (Hf : A' ≃ A) : (Σa, B (Hf a)) ≃ (Σa', B a') := sigma_equiv_sigma Hf (λa, erfl) variable {B} @[hott] def ap_sigma_functor_eq_dpair (p : a = a') (q : b =[p] b') : ap (sigma_functor f g) (@sigma_eq _ _ ⟨a,b⟩ ⟨a',b'⟩ p q) = sigma_eq (ap f p) (by exact pathover.rec_on q idpo) := by induction q; reflexivity @[hott] def sigma_ua {A B : Type _} (C : A ≃ B → Type _) : (Σ(p : A = B), C (equiv_of_eq p)) ≃ Σ(e : A ≃ B), C e := sigma_equiv_sigma_left' C (eq_equiv_equiv _ _) -- @[hott] def ap_sigma_functor_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) -- : ap (sigma_functor f g) (sigma_eq p q) = -- sigma_eq (ap f p) -- ((tr_compose B' f p (g u.1 u.2))⁻¹ ⬝ (fn_tr_eq_tr_fn p g u.2)⁻¹ ⬝ ap (g v.1) q) := -- by induction u; induction v; apply ap_sigma_functor_eq_dpair /- definition 3.11.9(i): Summing up a contractible family of types does nothing. -/ @[hott, instance] def is_equiv_fst (B : A → Type _) [H : Π a, is_contr (B a)] : is_equiv (@fst A B) := adjointify fst (λa, ⟨a, center _⟩) (λa, idp) (λu, sigma_eq idp (pathover_idp_of_eq _ (center_eq _))) @[hott] def sigma_equiv_of_is_contr_right (B : A → Type _) [H : Π a, is_contr (B a)] : (Σa, B a) ≃ A := equiv.mk fst (by apply_instance) /- definition 3.11.9(ii): Dually, summing up over a contractible type does nothing. -/ @[hott] def sigma_equiv_of_is_contr_left (B : A → Type _) [H : is_contr A] : (Σa, B a) ≃ B (center A) := equiv.MK (λu, (center_eq u.1)⁻¹ ▸ u.2) (λb, ⟨center _, b⟩) begin abstract { intro b, change _ = idpath (center A) ▸ b, apply ap (λx, x ▸ b), apply prop_eq_of_is_contr, } end begin abstract { exact λu, sigma_eq (center_eq _) (tr_pathover _ _) } end /- Associativity -/ --this proof is harder than in Coq because we don't have eta definitionally for sigma @[hott] def sigma_assoc_equiv (C : (Σa, B a) → Type _) : (Σa b, C ⟨a, b⟩) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩) (λuc, ⟨uc.1.1, uc.1.2, by rwr sigma.eta; exact uc.2⟩) begin abstract { intro uc, induction uc with u c, induction u, reflexivity } end begin abstract { intro av, induction av with a v, induction v, reflexivity } end) open prod @[hott] def assoc_equiv_prod (C : (A × A') → Type _) : (Σa a', C (a,a')) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨(av.1, av.2.1), av.2.2⟩) (λuc, ⟨(uc.1).1, (uc.1).2, by rwr prod.eta; exact uc.2⟩) (λ ⟨⟨a,b⟩,c⟩, idp) (λ ⟨a,⟨b,c⟩⟩, idp)) /- Symmetry -/ @[hott] def comm_equiv_unc (C : A × A' → Type _) : (Σa a', C (a, a')) ≃ (Σa' a, C (a, a')) := calc (Σa a', C (a, a')) ≃ Σu, C u : assoc_equiv_prod _ ... ≃ Σv, C (flip v) : sigma_equiv_sigma (prod.prod_comm_equiv _ _) (λ ⟨a,a'⟩, equiv.rfl) ... ≃ Σa' a, C (a, a') : by symmetry; exact assoc_equiv_prod (C ∘ prod.flip) @[hott] def sigma_comm_equiv (C : A → A' → Type _) : (Σa a', C a a') ≃ (Σa' a, C a a') := comm_equiv_unc (λu, C (fst u) (snd u)) @[hott] def equiv_prod (A B : Type _) : (Σ(a : A), B) ≃ A × B := equiv.mk _ (adjointify (λs, (s.1, s.2)) (λp, ⟨fst p, snd p⟩) (λ⟨a,b⟩, idp) (λ⟨a,b⟩, idp)) @[hott] def comm_equiv_nondep (A B : Type _) : (Σ(a : A), B) ≃ Σ(b : B), A := calc (Σ(a : A), B) ≃ A × B : by apply equiv_prod ... ≃ B × A : by apply prod.prod_comm_equiv ... ≃ Σ(b : B), A : by symmetry; apply equiv_prod @[hott] def sigma_assoc_comm_equiv {A : Type _} (B C : A → Type _) : (Σ(v : Σa, B a), C v.1) ≃ (Σ(u : Σa, C a), B u.1) := calc (Σ(v : Σa, B a), C v.1) ≃ (Σa (b : B a), C a) : by symmetry; apply sigma_assoc_equiv (C ∘ fst) ... ≃ (Σa (c : C a), B a) : by apply sigma_equiv_sigma_right; intro a; apply comm_equiv_nondep ... ≃ (Σ(u : Σa, C a), B u.1) : by apply sigma_assoc_equiv (B ∘ fst) /- Interaction with other type constructors -/ @[hott] def sigma_empty_left (B : empty → Type _) : (Σx, B x) ≃ empty := begin fapply equiv.MK, { intro v, induction v, cases v_fst}, { intro x, cases x}, { intro x, cases x}, { intro v, induction v, cases v_fst}, end @[hott] def sigma_empty_right (A : Type _) : (Σ(a : A), empty) ≃ empty := begin fapply equiv.MK, { intro v, induction v, cases v_snd}, { intro x, cases x}, { intro x, cases x}, { intro v, induction v, cases v_snd}, end @[hott] def sigma_unit_left (B : unit → Type _) : (Σx, B x) ≃ B star := sigma_equiv_of_is_contr_left _ @[hott] def sigma_unit_right (A : Type _) : (Σ(a : A), unit) ≃ A := sigma_equiv_of_is_contr_right _ @[hott] def sigma_sum_left (B : A ⊎ A' → Type _) : (Σp, B p) ≃ (Σa, B (inl a)) ⊎ (Σa, B (inr a)) := begin fapply equiv.MK, { intro v, induction v with p b, induction p, { apply inl, constructor, assumption }, { apply inr, constructor, assumption }}, { intro p, induction p with v v; induction v; constructor; assumption}, { intro p, induction p with v v; induction v; reflexivity}, { intro v, induction v with p b, induction p; reflexivity}, end @[hott] def sigma_sum_right (B C : A → Type _) : (Σa, B a ⊎ C a) ≃ (Σa, B a) ⊎ (Σa, C a) := begin fapply equiv.MK, { intro v, induction v with a p, induction p, { apply inl, constructor, assumption}, { apply inr, constructor, assumption}}, { intro p, induction p with v v, { induction v, constructor, apply inl, assumption }, { induction v, constructor, apply inr, assumption }}, { intro p, induction p with v v; induction v; reflexivity}, { intro v, induction v with a p, induction p; reflexivity}, end @[hott] def sigma_sigma_eq_right {A : Type _} (a : A) (P : Π(b : A), a = b → Type _) : (Σ(b : A) (p : a = b), P b p) ≃ P a idp := calc (Σ(b : A) (p : a = b), P b p) ≃ (Σ(v : Σ(b : A), a = b), P v.1 v.2) : by apply sigma_assoc_equiv (λ u, P u.fst u.snd) ... ≃ P a idp : by apply sigma_equiv_of_is_contr_left (λ v : Σ b, a=b, P v.fst v.snd) @[hott] def sigma_sigma_eq_left {A : Type _} (a : A) (P : Π(b : A), b = a → Type _) : (Σ(b : A) (p : b = a), P b p) ≃ P a idp := calc (Σ(b : A) (p : b = a), P b p) ≃ (Σ(v : Σ(b : A), b = a), P v.1 v.2) : by apply sigma_assoc_equiv (λ u : Σ b, b=a, P u.fst u.snd) ... ≃ P a idp : by apply sigma_equiv_of_is_contr_left (λ v : Σ b, b=a, P v.fst v.snd) /- ** Universal mapping properties -/ /- *** The positive universal property. -/ section @[hott, instance] def is_equiv_sigma_rec (C : (Σa, B a) → Type _) : is_equiv (sigma.rec : (Πa b, C ⟨a, b⟩) → Πab, C ab) := adjointify _ (λ g a b, g ⟨a, b⟩) (λ g, eq_of_homotopy (λ⟨a,b⟩, idp)) (λ f, refl f) @[hott] def equiv_sigma_rec (C : (Σa, B a) → Type _) : (Π(a : A) (b: B a), C ⟨a, b⟩) ≃ (Πxy, C xy) := equiv.mk sigma.rec (by apply_instance) /- *** The negative universal property. -/ @[hott] protected def coind_unc (fg : Σ(f : Πa, B a), Πa, C a (f a)) (a : A) : Σ(b : B a), C a b := ⟨fg.1 a, fg.2 a⟩ @[hott] protected def coind (f : Π a, B a) (g : Π a, C a (f a)) (a : A) : Σ(b : B a), C a b := sigma.coind_unc ⟨f, g⟩ a --is the instance below dangerous? --in Coq this can be done without function extensionality @[hott, instance] def is_equiv_coind (C : Πa, B a → Type _) : is_equiv (@sigma.coind_unc _ _ C) := adjointify _ (λ h, ⟨λa, (h a).1, λa, (h a).2⟩) (λ h, eq_of_homotopy (λu, sigma.eta _)) (λ⟨f,g⟩, idp) @[hott] def sigma_pi_equiv_pi_sigma : (Σ(f : Πa, B a), Πa, C a (f a)) ≃ (Πa, Σb, C a b) := equiv.mk sigma.coind_unc (by apply_instance) end /- Subtypes (sigma types whose second components are props) -/ @[hott] def subtype {A : Type _} (P : A → Type _) [H : Πa, is_prop (P a)] := Σ(a : A), P a notation [parsing_only] `{` binder `|` r:(scoped:1 P, subtype P) `}` := r /- To prove equality in a subtype, we only need equality of the first component. -/ @[hott] def subtype_eq [H : Πa, is_prop (B a)] {u v : {a | B a}} : u.1 = v.1 → u = v := sigma_eq_unc ∘ inv fst @[hott] def is_equiv_subtype_eq [H : Πa, is_prop (B a)] (u v : {a | B a}) : is_equiv (subtype_eq : u.1 = v.1 → u = v) := is_equiv_compose _ _ local attribute [instance] is_equiv_subtype_eq @[hott] def equiv_subtype [H : Πa, is_prop (B a)] (u v : {a | B a}) : (u.1 = v.1) ≃ (u = v) := equiv.mk subtype_eq (by apply_instance) @[hott] def subtype_eq_equiv [H : Πa, is_prop (B a)] (u v : {a | B a}) : (u = v) ≃ (u.1 = v.1) := (equiv_subtype u v)⁻¹ᵉ @[hott] def subtype_eq_inv {A : Type _} {B : A → Type _} [H : Πa, is_prop (B a)] (u v : Σa, B a) : u = v → u.1 = v.1 := subtype_eq⁻¹ᶠ @[hott] def is_equiv_subtype_eq_inv {A : Type _} {B : A → Type _} [H : Πa, is_prop (B a)] (u v : Σa, B a) : is_equiv (subtype_eq_inv u v) := by delta subtype_eq_inv; apply_instance /- truncatedness -/ @[hott] def is_trunc_sigma (B : A → Type _) (n : trunc_index) [HA : is_trunc n A] [HB : Πa, is_trunc n (B a)] : is_trunc n (Σa, B a) := begin revert A B HA HB, induction n with n IH, { intros A B HA HB, apply is_trunc_equiv_closed_rev -2 (sigma_equiv_of_is_contr_left B) (HB _) }, { intros A B HA HB, apply is_trunc_succ_intro, intros u v, apply is_trunc_equiv_closed_rev _ (sigma_eq_equiv _ _) (IH _); apply_instance } end @[hott] theorem is_trunc_subtype (B : A → Prop) (n : trunc_index) [HA : is_trunc (n.+1) A] : is_trunc (n.+1) (Σa, B a) := @is_trunc_sigma _ (λ a, B a) (n.+1) _ (λa, is_trunc_succ_of_is_prop _ _) /- if the total space is a mere proposition, you can equate two points in the base type by finding points in their fibers -/ @[hott] def eq_base_of_is_prop_sigma {A : Type _} (B : A → Type _) (H : is_prop (Σa, B a)) {a a' : A} (b : B a) (b' : B a') : a = a' := (@is_prop.elim (Σa, B a) _ ⟨a, b⟩ ⟨a', b'⟩)..1 end sigma attribute [instance] sigma.is_trunc_sigma attribute [instance] sigma.is_trunc_subtype namespace sigma /- pointed sigma type -/ open pointed @[hott, instance] def pointed_sigma {A : Type _} (P : A → Type _) [G : pointed A] [H : pointed (P pt)] : pointed (Σx, P x) := pointed.mk ⟨pt,pt⟩ @[hott] def psigma {A : Type*} (P : A → Type*) : Type* := pointed.mk' (Σa, P a) notation `Σ*` binders `, ` r:(scoped P, psigma P) := r @[hott] def pfst {A : Type*} {B : A → Type*} : (Σ*(x : A), B x) →* A := pmap.mk fst idp @[hott] def psnd {A : Type*} {B : A → Type*} (v : (Σ*(x : A), B x)) : B (pfst.to_fun v) := snd v @[hott] def ptsigma {n : ℕ₋₂} {A : n-Type*} (P : A → (n-Type*)) : n-Type* := ptrunctype.mk' n (Σa, P a) end sigma end hott
8dcb9581e54989cb81d8d99bc7319200cf2eb6b3
35960c5b117752aca7e3e7767c0b393e4dbd72a7
/src/typing/core.lean
8dc10d79b05cde7806b33de0309898b3eacc9964
[ "Apache-2.0" ]
permissive
spl/tts
461dc76b83df8db47e4660d0941dc97e6d4fd7d1
b65298fea68ce47c8ed3ba3dbce71c1a20dd3481
refs/heads/master
1,541,049,198,347
1,537,967,023,000
1,537,967,029,000
119,653,145
1
0
null
null
null
null
UTF-8
Lean
false
false
1,309
lean
import env exp namespace tts ------------------------------------------------------------------ variables {V : Type} [decidable_eq V] -- Type of variable names open occurs exp typ sch inductive typing : env V → exp V → typ V → Prop | var : Π {Γ : env V} {x : tagged V} {s : sch V} {ts : list (typ V)}, x :~ s ∈ Γ → s.arity = ts.length → (∀ (t : typ V), t ∈ ts → lc t) → lc s → typing Γ (var free x) (open_typs ts s) | app : Π {Γ : env V} {ef ea : exp V} {t₁ t₂ : typ V}, typing Γ ef (arr t₁ t₂) → typing Γ ea t₁ → typing Γ (app ef ea) t₂ | lam : Π {v} {Γ : env V} {eb : exp V} {t₁ t₂ : typ V} (L : finset (tagged V)), lc t₁ → (∀ {x : tagged V}, x ∉ L → typing (insert ⟨x, of_typ t₁⟩ Γ) (open_var x eb) t₂) → typing Γ (lam v eb) (arr t₁ t₂) | let_ : Π {v} {Γ : env V} {ed eb : exp V} (sd : sch V) {tb : typ V} (Ld Lb : finset (tagged V)), (∀ {xs : list (tagged V)}, xs.length = sd.arity → xs.nodup → (∀ x ∈ xs, x ∉ Ld) → typing Γ ed (open_vars xs sd)) → (∀ {x : tagged V}, x ∉ Lb → typing (insert ⟨x, sd⟩ Γ) (open_var x eb) tb) → typing Γ (let_ v ed eb) tb end /- namespace -/ tts --------------------------------------------------------
79c96d47be6d09a827c9740cb140bab62bcdb8e7
4fa161becb8ce7378a709f5992a594764699e268
/src/category_theory/limits/shapes/biproducts.lean
bf71a9f85d9c8fa881f71bbb8ab20f7659f5770c
[ "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
19,226
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.epi_mono import category_theory.limits.shapes.binary_products import category_theory.preadditive /-! # Biproducts and binary biproducts We introduce the notion of biproducts and binary biproducts. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) We model these here using a `bicone`, with a cone point `X`, and natural transformations `π` from the constant functor with value `X` to `F` and `ι` in the other direction. We implement `has_bilimit` as a `bicone`, equipped with the evidence `is_limit bicone.to_cone` and `is_colimit bicone.to_cocone`. In practice, of course, we are only interested in the special case of bilimits over `discrete J` for `[fintype J] [decidable_eq J]`, which corresponds to finite biproducts. TODO: We should provide a constructor that takes `has_limit F`, `has_colimit F`, and and iso `limit F ≅ colimit F`, and produces `has_bilimit F`. TODO: perhaps it makes sense to unify the treatment of zero objects with this a bit. In addition to biproducts and binary biproducts, we define the notion of preadditive binary biproduct, which is a preadditive version of binary biproducts. We show that a preadditive binary biproduct is a binary biproduct and construct preadditive binary biproducts both from binary products and from binary coproducts. TODO: the preadditive version of finite biproducts ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. -/ universes v u open category_theory open category_theory.functor namespace category_theory.limits variables {J : Type v} [small_category J] variables {C : Type u} [category.{v} C] /-- A `c : bicone F` is: * an object `c.X` and * a natural transformation `c.π : c.X ⟶ F` from the constant `c.X` functor to `F`. * a natural transformation `c.ι : F ⟶ c.X` from `F` to the constant `c.X` functor. -/ @[nolint has_inhabited_instance] structure bicone {J : Type v} [small_category J] (F : J ⥤ C) := (X : C) (π : (const J).obj X ⟶ F) (ι : F ⟶ (const J).obj X) variables {F : J ⥤ C} namespace bicone /-- Extract the cone from a bicone. -/ @[simps] def to_cone (B : bicone F) : cone F := { .. B } /-- Extract the cocone from a bicone. -/ @[simps] def to_cocone (B : bicone F) : cocone F := { .. B } end bicone /-- `has_bilimit F` represents a particular chosen bicone which is simultaneously a limit and a colimit of the diagram `F`. (This is only interesting when the source category is discrete.) -/ class has_bilimit (F : J ⥤ C) := (bicone : bicone F) (is_limit : is_limit bicone.to_cone) (is_colimit : is_colimit bicone.to_cocone) @[priority 100] instance has_limit_of_has_bilimit [has_bilimit F] : has_limit F := { cone := has_bilimit.bicone.to_cone, is_limit := has_bilimit.is_limit, } @[priority 100] instance has_colimit_of_has_bilimit [has_bilimit F] : has_colimit F := { cocone := has_bilimit.bicone.to_cocone, is_colimit := has_bilimit.is_colimit, } variables (J C) /-- `C` has bilimits of shape `J` if we have chosen a particular limit and a particular colimit, with the same cone points, of every functor `F : J ⥤ C`. (This is only interesting if `J` is discrete.) -/ class has_bilimits_of_shape := (has_bilimit : Π F : J ⥤ C, has_bilimit F) attribute [instance, priority 100] has_bilimits_of_shape.has_bilimit @[priority 100] instance [has_bilimits_of_shape J C] : has_limits_of_shape J C := { has_limit := λ F, by apply_instance } @[priority 100] instance [has_bilimits_of_shape J C] : has_colimits_of_shape J C := { has_colimit := λ F, by apply_instance } /-- `has_finite_biproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type with decidable equality. -/ class has_finite_biproducts := (has_bilimits_of_shape : Π (J : Type v) [fintype J] [decidable_eq J], has_bilimits_of_shape.{v} (discrete J) C) attribute [instance] has_finite_biproducts.has_bilimits_of_shape /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproduct_iso {J : Type v} (F : J → C) [has_bilimit (discrete.functor F)] : limits.pi_obj F ≅ limits.sigma_obj F := eq_to_iso rfl end category_theory.limits namespace category_theory.limits variables {J : Type v} variables {C : Type u} [category.{v} C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbreviation biproduct (f : J → C) [has_bilimit (discrete.functor f)] := limit (discrete.functor f) notation `⨁ ` f:20 := biproduct f /-- The projection onto a summand of a biproduct. -/ abbreviation biproduct.π (f : J → C) [has_bilimit (discrete.functor f)] (b : J) : ⨁ f ⟶ f b := limit.π (discrete.functor f) b /-- The inclusion into a summand of a biproduct. -/ abbreviation biproduct.ι (f : J → C) [has_bilimit (discrete.functor f)] (b : J) : f b ⟶ ⨁ f := colimit.ι (discrete.functor f) b /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbreviation biproduct.lift {f : J → C} [has_bilimit (discrete.functor f)] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ⨁ f := limit.lift _ (fan.mk p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbreviation biproduct.desc {f : J → C} [has_bilimit (discrete.functor f)] {P : C} (p : Π b, f b ⟶ P) : ⨁ f ⟶ P := colimit.desc _ (cofan.mk p) /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map betweeen the biproducts. -/ abbreviation biproduct.map [fintype J] [decidable_eq J] {f g : J → C} [has_finite_biproducts.{v} C] (p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := (@lim (discrete J) _ C _ _).map (discrete.nat_trans p) instance biproduct.ι_mono [decidable_eq J] (f : J → C) [has_bilimit (discrete.functor f)] (b : J) : split_mono (biproduct.ι f b) := { retraction := biproduct.desc $ λ b', if h : b' = b then eq_to_hom (congr_arg f h) else biproduct.ι f b' ≫ biproduct.π f b } instance biproduct.π_epi [decidable_eq J] (f : J → C) [has_bilimit (discrete.functor f)] (b : J) : split_epi (biproduct.π f b) := { section_ := biproduct.lift $ λ b', if h : b = b' then eq_to_hom (congr_arg f h) else biproduct.ι f b ≫ biproduct.π f b' } variables {C} /-- A binary bicone for a pair of objects `P Q : C` consists of the cone point `X`, maps from `X` to both `P` and `Q`, and maps from both `P` and `Q` to `X`. -/ @[nolint has_inhabited_instance] structure binary_bicone (P Q : C) := (X : C) (π₁ : X ⟶ P) (π₂ : X ⟶ Q) (ι₁ : P ⟶ X) (ι₂ : Q ⟶ X) namespace binary_bicone variables {P Q : C} /-- Extract the cone from a binary bicone. -/ @[simp] def to_cone (c : binary_bicone.{v} P Q) : cone (pair P Q) := binary_fan.mk c.π₁ c.π₂ /-- Extract the cocone from a binary bicone. -/ @[simp] def to_cocone (c : binary_bicone.{v} P Q) : cocone (pair P Q) := binary_cofan.mk c.ι₁ c.ι₂ end binary_bicone /-- `has_binary_biproduct P Q` represents a particular chosen bicone which is simultaneously a limit and a colimit of the diagram `pair P Q`. -/ class has_binary_biproduct (P Q : C) := (bicone : binary_bicone.{v} P Q) (is_limit : is_limit bicone.to_cone) (is_colimit : is_colimit bicone.to_cocone) section variable (C) /-- `has_binary_biproducts C` represents a particular chosen bicone which is simultaneously a limit and a colimit of the diagram `pair P Q`, for every `P Q : C`. -/ class has_binary_biproducts := (has_binary_biproduct : Π (P Q : C), has_binary_biproduct.{v} P Q) attribute [instance, priority 100] has_binary_biproducts.has_binary_biproduct end variables {P Q : C} instance has_binary_biproduct.has_limit_pair [has_binary_biproduct.{v} P Q] : has_limit (pair P Q) := { cone := has_binary_biproduct.bicone.to_cone, is_limit := has_binary_biproduct.is_limit.{v}, } instance has_binary_biproduct.has_colimit_pair [has_binary_biproduct.{v} P Q] : has_colimit (pair P Q) := { cocone := has_binary_biproduct.bicone.to_cocone, is_colimit := has_binary_biproduct.is_colimit.{v}, } @[priority 100] instance has_limits_of_shape_walking_pair [has_binary_biproducts.{v} C] : has_limits_of_shape.{v} (discrete walking_pair) C := { has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm } @[priority 100] instance has_colimits_of_shape_walking_pair [has_binary_biproducts.{v} C] : has_colimits_of_shape.{v} (discrete walking_pair) C := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) } @[priority 100] instance has_binary_products_of_has_binary_biproducts [has_binary_biproducts.{v} C] : has_binary_products.{v} C := ⟨by apply_instance⟩ @[priority 100] instance has_binary_coproducts_of_has_binary_biproducts [has_binary_biproducts.{v} C] : has_binary_coproducts.{v} C := ⟨by apply_instance⟩ /-- The isomorphism between the specified binary product and the specified binary coproduct for a pair for a binary biproduct. -/ def biprod_iso (X Y : C) [has_binary_biproduct.{v} X Y] : limits.prod X Y ≅ limits.coprod X Y := eq_to_iso rfl /-- The chosen biproduct of a pair of objects. -/ abbreviation biprod (X Y : C) [has_binary_biproduct.{v} X Y] := limit (pair X Y) notation X ` ⊞ `:20 Y:20 := biprod X Y /-- The projection onto the first summand of a binary biproduct. -/ abbreviation biprod.fst {X Y : C} [has_binary_biproduct.{v} X Y] : X ⊞ Y ⟶ X := limit.π (pair X Y) walking_pair.left /-- The projection onto the second summand of a binary biproduct. -/ abbreviation biprod.snd {X Y : C} [has_binary_biproduct.{v} X Y] : X ⊞ Y ⟶ Y := limit.π (pair X Y) walking_pair.right /-- The inclusion into the first summand of a binary biproduct. -/ abbreviation biprod.inl {X Y : C} [has_binary_biproduct.{v} X Y] : X ⟶ X ⊞ Y := colimit.ι (pair X Y) walking_pair.left /-- The inclusion into the second summand of a binary biproduct. -/ abbreviation biprod.inr {X Y : C} [has_binary_biproduct.{v} X Y] : Y ⟶ X ⊞ Y := colimit.ι (pair X Y) walking_pair.right /-- Given a pair of maps into the summands of a binary biproduct, we obtain a map into the binary biproduct. -/ abbreviation biprod.lift {W X Y : C} [has_binary_biproduct.{v} X Y] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⊞ Y := limit.lift _ (binary_fan.mk f g) /-- Given a pair of maps out of the summands of a binary biproduct, we obtain a map out of the binary biproduct. -/ abbreviation biprod.desc {W X Y : C} [has_binary_biproduct.{v} X Y] (f : X ⟶ W) (g : Y ⟶ W) : X ⊞ Y ⟶ W := colimit.desc _ (binary_cofan.mk f g) /-- Given a pair of maps between the summands of a pair of binary biproducts, we obtain a map between the binary biproducts. -/ abbreviation biprod.map {W X Y Z : C} [has_binary_biproducts.{v} C] (f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z := (@lim (discrete walking_pair) _ C _ _).map (@map_pair _ _ (pair W X) (pair Y Z) f g) instance biprod.inl_mono {X Y : C} [has_binary_biproduct.{v} X Y] : split_mono (biprod.inl : X ⟶ X ⊞ Y) := { retraction := biprod.desc (𝟙 X) (biprod.inr ≫ biprod.fst) } instance biprod.inr_mono {X Y : C} [has_binary_biproduct.{v} X Y] : split_mono (biprod.inr : Y ⟶ X ⊞ Y) := { retraction := biprod.desc (biprod.inl ≫ biprod.snd) (𝟙 Y)} instance biprod.fst_epi {X Y : C} [has_binary_biproduct.{v} X Y] : split_epi (biprod.fst : X ⊞ Y ⟶ X) := { section_ := biprod.lift (𝟙 X) (biprod.inl ≫ biprod.snd) } instance biprod.snd_epi {X Y : C} [has_binary_biproduct.{v} X Y] : split_epi (biprod.snd : X ⊞ Y ⟶ Y) := { section_ := biprod.lift (biprod.inr ≫ biprod.fst) (𝟙 Y) } @[ext] lemma biprod.hom_ext {X Y Z : C} [has_binary_biproduct.{v} X Y] (f g : Z ⟶ X ⊞ Y) (h₀ : f ≫ biprod.fst = g ≫ biprod.fst) (h₁ : f ≫ biprod.snd = g ≫ biprod.snd) : f = g := binary_fan.is_limit.hom_ext has_binary_biproduct.is_limit h₀ h₁ @[ext] lemma biprod.hom_ext' {X Y Z : C} [has_binary_biproduct.{v} X Y] (f g : X ⊞ Y ⟶ Z) (h₀ : biprod.inl ≫ f = biprod.inl ≫ g) (h₁ : biprod.inr ≫ f = biprod.inr ≫ g) : f = g := binary_cofan.is_colimit.hom_ext has_binary_biproduct.is_colimit h₀ h₁ -- TODO: -- If someone is interested, they could provide the constructions: -- has_binary_biproducts ↔ has_finite_biproducts section preadditive variables [preadditive.{v} C] open category_theory.preadditive /-- A preadditive binary biproduct is a bicone on two objects `X` and `Y` satisfying a set of five axioms expressing the properties of a biproduct in additive terms. The notion of preadditive binary biproduct is strictly stronger than the notion of binary biproduct (but it in any preadditive category, the existence of a binary biproduct implies the existence of a preadditive binary biproduct: a biproduct is, in particular, a product, and every product gives rise to a preadditive binary biproduct, see `has_preadditive_binary_biproduct.of_has_limit_pair`). -/ class has_preadditive_binary_biproduct (X Y : C) := (bicone : binary_bicone.{v} X Y) (ι₁_π₁' : bicone.ι₁ ≫ bicone.π₁ = 𝟙 X . obviously) (ι₂_π₂' : bicone.ι₂ ≫ bicone.π₂ = 𝟙 Y . obviously) (ι₂_π₁' : bicone.ι₂ ≫ bicone.π₁ = 0 . obviously) (ι₁_π₂' : bicone.ι₁ ≫ bicone.π₂ = 0 . obviously) (total' : bicone.π₁ ≫ bicone.ι₁ + bicone.π₂ ≫ bicone.ι₂ = 𝟙 bicone.X . obviously) restate_axiom has_preadditive_binary_biproduct.ι₁_π₁' restate_axiom has_preadditive_binary_biproduct.ι₂_π₂' restate_axiom has_preadditive_binary_biproduct.ι₂_π₁' restate_axiom has_preadditive_binary_biproduct.ι₁_π₂' restate_axiom has_preadditive_binary_biproduct.total' attribute [simp, reassoc] has_preadditive_binary_biproduct.ι₁_π₁ has_preadditive_binary_biproduct.ι₂_π₂ has_preadditive_binary_biproduct.ι₂_π₁ has_preadditive_binary_biproduct.ι₁_π₂ attribute [simp] has_preadditive_binary_biproduct.total section local attribute [tidy] tactic.case_bash /-- A preadditive binary biproduct is a binary biproduct. -/ @[priority 100] instance (X Y : C) [has_preadditive_binary_biproduct.{v} X Y] : has_binary_biproduct.{v} X Y := { bicone := has_preadditive_binary_biproduct.bicone, is_limit := { lift := λ s, binary_fan.fst s ≫ has_preadditive_binary_biproduct.bicone.ι₁ + binary_fan.snd s ≫ has_preadditive_binary_biproduct.bicone.ι₂, uniq' := λ s m h, by erw [←category.comp_id m, ←has_preadditive_binary_biproduct.total, comp_add, reassoc_of (h walking_pair.left), reassoc_of (h walking_pair.right)] }, is_colimit := { desc := λ s, has_preadditive_binary_biproduct.bicone.π₁ ≫ binary_cofan.inl s + has_preadditive_binary_biproduct.bicone.π₂ ≫ binary_cofan.inr s, uniq' := λ s m h, by erw [←category.id_comp m, ←has_preadditive_binary_biproduct.total, add_comp, category.assoc, category.assoc, h walking_pair.left, h walking_pair.right] } } end section variables (X Y : C) [has_preadditive_binary_biproduct.{v} X Y] @[simp, reassoc] lemma biprod.inl_fst : (biprod.inl : X ⟶ X ⊞ Y) ≫ biprod.fst = 𝟙 X := has_preadditive_binary_biproduct.ι₁_π₁ @[simp, reassoc] lemma biprod.inr_snd : (biprod.inr : Y ⟶ X ⊞ Y) ≫ biprod.snd = 𝟙 Y := has_preadditive_binary_biproduct.ι₂_π₂ @[simp, reassoc] lemma biprod.inr_fst : (biprod.inr : Y ⟶ X ⊞ Y) ≫ biprod.fst = 0 := has_preadditive_binary_biproduct.ι₂_π₁ @[simp, reassoc] lemma biprod.inl_snd : (biprod.inl : X ⟶ X ⊞ Y) ≫ biprod.snd = 0 := has_preadditive_binary_biproduct.ι₁_π₂ @[simp] lemma biprod.total : biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y) := has_preadditive_binary_biproduct.total lemma biprod.inl_add_inr {T : C} {f : T ⟶ X} {g : T ⟶ Y} : f ≫ biprod.inl + g ≫ biprod.inr = biprod.lift f g := rfl lemma biprod.fst_add_snd {T : C} {f : X ⟶ T} {g : Y ⟶ T} : biprod.fst ≫ f + biprod.snd ≫ g = biprod.desc f g := rfl @[simp, reassoc] lemma biprod.lift_desc {T U : C} {f : T ⟶ X} {g : T ⟶ Y} {h : X ⟶ U} {i : Y ⟶ U} : biprod.lift f g ≫ biprod.desc h i = f ≫ h + g ≫ i := by simp [←biprod.inl_add_inr, ←biprod.fst_add_snd] end section has_limit_pair /-- In a preadditive category, if the product of `X` and `Y` exists, then the preadditive binary biproduct of `X` and `Y` exists. -/ def has_preadditive_binary_biproduct.of_has_limit_pair (X Y : C) [has_limit.{v} (pair X Y)] : has_preadditive_binary_biproduct.{v} X Y := { bicone := { X := X ⨯ Y, π₁ := category_theory.limits.prod.fst, π₂ := category_theory.limits.prod.snd, ι₁ := prod.lift (𝟙 X) 0, ι₂ := prod.lift 0 (𝟙 Y) } } /-- In a preadditive category, if the coproduct of `X` and `Y` exists, then the preadditive binary biproduct of `X` and `Y` exists. -/ def has_preadditive_binary_biproduct.of_has_colimit_pair (X Y : C) [has_colimit.{v} (pair X Y)] : has_preadditive_binary_biproduct.{v} X Y := { bicone := { X := X ⨿ Y, π₁ := coprod.desc (𝟙 X) 0, π₂ := coprod.desc 0 (𝟙 Y), ι₁ := category_theory.limits.coprod.inl, ι₂ := category_theory.limits.coprod.inr } } end has_limit_pair section variable (C) /-- A preadditive category `has_preadditive_binary_biproducts` if the preadditive binary biproduct exists for every pair of objects. -/ class has_preadditive_binary_biproducts := (has_preadditive_binary_biproduct : Π (X Y : C), has_preadditive_binary_biproduct.{v} X Y) attribute [instance, priority 100] has_preadditive_binary_biproducts.has_preadditive_binary_biproduct @[priority 100] instance [has_preadditive_binary_biproducts.{v} C] : has_binary_biproducts.{v} C := ⟨λ X Y, by apply_instance⟩ /-- If a preadditive category has all binary products, then it has all preadditive binary biproducts. -/ -- This particularly dangerous as an instance, -- as we can deduce `has_binary_products` from `has_preadditive_binary_biproducts`. def has_preadditive_binary_biproducts_of_has_binary_products [has_binary_products.{v} C] : has_preadditive_binary_biproducts.{v} C := ⟨λ X Y, has_preadditive_binary_biproduct.of_has_limit_pair X Y⟩ /-- If a preadditive category has all binary coproducts, then it has all preadditive binary biproducts. -/ -- This particularly dangerous as an instance, -- as we can deduce `has_binary_products` from `has_preadditive_binary_biproducts`. def has_preadditive_binary_biproducts_of_has_binary_coproducts [has_binary_coproducts.{v} C] : has_preadditive_binary_biproducts.{v} C := ⟨λ X Y, has_preadditive_binary_biproduct.of_has_colimit_pair X Y⟩ end end preadditive end category_theory.limits
11954f2766439d8967e46cfa3b27464ba84b0724
26b8b0964ca8e1c2e203585ba5940f83fe05e48a
/test/rewrite_search.lean
5082a4183fab44ec5b17dd0f53acc468ff497fe7
[]
no_license
jcommelin/lean-tidy
ef3cd32a3804221d93f0dff9e180bb2c52f4b143
9cecf497e90db64b5ea140ad6ae1603976dcd402
refs/heads/master
1,585,129,919,276
1,533,512,680,000
1,533,512,680,000
143,677,361
0
0
null
1,616,803,481,000
1,533,530,576,000
Lean
UTF-8
Lean
false
false
4,013
lean
import tidy.rewrite_search namespace tidy.rewrite_search.testing axiom foo' : [6] = [7] axiom bar' : [[6],[6]] = [[5],[5]] example : [[7],[6]] = [[5],[5]] := begin success_if_fail { rewrite_search [] }, -- rw [←foo', bar'] rewrite_search [←foo', bar'], end @[search] private axiom foo : [0] = [1] @[search] private axiom bar1 : [1] = [2] @[search] private axiom bar2 : [3] = [2] @[search] private axiom bar3 : [3] = [4] private example (a : unit) : [[0],[0]] = [[4],[4]] := begin -- perform_nth_rewrite_lhs [foo] 0, -- perform_nth_rewrite_lhs [bar1] 0, -- perform_nth_rewrite_lhs [←bar2] 0, -- perform_nth_rewrite_lhs [foo] 0, -- perform_nth_rewrite_lhs [bar1] 0, -- perform_nth_rewrite_lhs [←bar2] 0, -- perform_nth_rewrite_rhs [←bar3] 0, -- perform_nth_rewrite_rhs [←bar3] 0, rewrite_search [foo, bar1, ← bar2, bar2, ← bar3], end private example : [[0],[0]] = [[4],[4]] := begin rewrite_search_using `search, end @[search] private axiom qux' : [[1], [2]] = [[6], [7]] @[search] private axiom qux'' : [6] = [7] private example : [[1], [1]] = [[7], [7]] := begin -- perform_nth_rewrite_lhs [bar1] 0, -- perform_nth_rewrite_lhs [qux'] 0, -- perform_nth_rewrite_lhs [qux''] 0, rewrite_search_using `search, end private example : [[0],[0]] = [[4],[4]] := begin -- perform_nth_rewrite_lhs [foo] 0, -- perform_nth_rewrite_lhs [bar1] 0, -- perform_nth_rewrite_lhs [←bar2] 0, -- perform_nth_rewrite_lhs [bar3] 0, -- perform_nth_rewrite_lhs [foo] 0, -- perform_nth_rewrite_lhs [bar1] 0, -- perform_nth_rewrite_lhs [←bar2] 0, -- perform_nth_rewrite_lhs [bar3] 0, rewrite_search_using `search {trace:=tt}, end private structure cat := (O : Type) (H : O → O → Type) (i : Π o : O, H o o) (c : Π {X Y Z : O} (f : H X Y) (g : H Y Z), H X Z) (li : Π {X Y : O} (f : H X Y), c (i X) f = f) (ri : Π {X Y : O} (f : H X Y), c f (i Y) = f) (a : Π {W X Y Z : O} (f : H W X) (g : H X Y) (h : H Y Z), c (c f g) h = c f (c g h)) attribute [search] cat.li cat.a private example (C : cat) (X Y Z : C.O) (f : C.H X Y) (g : C.H Y X) (w : C.c g f = C.i Y) (h k : C.H Y Z) (p : C.c f h = C.c f k) : h = k := begin -- rewrite_search_using `search {trace := tt, trace_rules:=tt}, -- not quite there, we haven't activated intense search perform_nth_rewrite [← @cat.li C Y Z h] 0, perform_nth_rewrite [← w] 0, perform_nth_rewrite [C.a] 0, perform_nth_rewrite [p] 0, perform_nth_rewrite [← C.a] 0, perform_nth_rewrite [w] 0, perform_nth_rewrite [@cat.li C Y Z k] 0, -- PROJECT automate this! -- rw [← C.li Y Z h], -- rw [← C.li Y Z k], -- rw [← w], -- rw [C.a], -- rw [C.a], -- rw [p], end end tidy.rewrite_search.testing namespace tidy.rewrite_search.examples constants f g : ℕ → ℕ → ℕ → ℕ @[search] axiom f_0_0 : ∀ a b c : ℕ, f a b c = f 0 b c @[search] axiom f_0_1 : ∀ a b c : ℕ, f a b c = f 1 b c @[search] axiom f_0_2 : ∀ a b c : ℕ, f a b c = f 2 b c @[search] axiom f_1_0 : ∀ a b c : ℕ, f a b c = f a 0 c @[search] axiom f_1_1 : ∀ a b c : ℕ, f a b c = f a 1 c @[search] axiom f_1_2 : ∀ a b c : ℕ, f a b c = f a 2 c @[search] axiom f_2_0 : ∀ a b c : ℕ, f a b c = f a b 0 @[search] axiom f_2_1 : ∀ a b c : ℕ, f a b c = f a b 1 @[search] axiom f_2_2 : ∀ a b c : ℕ, f a b c = f a b 2 @[search] axiom g_0_0 : ∀ a b c : ℕ, g a b c = g 0 b c @[search] axiom g_0_1 : ∀ a b c : ℕ, g a b c = g 1 b c @[search] axiom g_0_2 : ∀ a b c : ℕ, g a b c = g 2 b c @[search] axiom g_1_0 : ∀ a b c : ℕ, g a b c = g a 0 c @[search] axiom g_1_1 : ∀ a b c : ℕ, g a b c = g a 1 c @[search] axiom g_1_2 : ∀ a b c : ℕ, g a b c = g a 2 c @[search] axiom g_2_0 : ∀ a b c : ℕ, g a b c = g a b 0 @[search] axiom g_2_1 : ∀ a b c : ℕ, g a b c = g a b 1 @[search] axiom g_2_2 : ∀ a b c : ℕ, g a b c = g a b 2 @[search] axiom f_g : f 0 1 2 = g 2 0 1 lemma test : f 0 0 0 = g 0 0 0 := by rewrite_search_using `search {trace := tt} end tidy.rewrite_search.examples
513fbd7f5914c30775e2403e06ec000a532394a8
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/prime.lean
caae523b3054c5b37df651693b99cd1d56701ff0
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
2,697
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.associated import algebra.big_operators.basic /-! # Prime elements in rings This file contains lemmas about prime elements of commutative rings. -/ variables {R : Type*} [comm_cancel_monoid_with_zero R] open finset open_locale big_operators /-- If `x * y = a * ∏ i in s, p i` where `p i` is always prime, then `x` and `y` can both be written as a divisor of `a` multiplied by a product over a subset of `s` -/ lemma mul_eq_mul_prime_prod {α : Type*} [decidable_eq α] {x y a : R} {s : finset α} {p : α → R} (hp : ∀ i ∈ s, prime (p i)) (hx : x * y = a * ∏ i in s, p i) : ∃ (t u : finset α) (b c : R), t ∪ u = s ∧ disjoint t u ∧ a = b * c ∧ x = b * ∏ i in t, p i ∧ y = c * ∏ i in u, p i := begin induction s using finset.induction with i s his ih generalizing x y a, { exact ⟨∅, ∅, x, y, by simp [hx]⟩ }, { rw [prod_insert his, ← mul_assoc] at hx, have hpi : prime (p i), { exact hp i (mem_insert_self _ _) }, rcases ih (λ i hi, hp i (mem_insert_of_mem hi)) hx with ⟨t, u, b, c, htus, htu, hbc, rfl, rfl⟩, have hit : i ∉ t, from λ hit, his (htus ▸ mem_union_left _ hit), have hiu : i ∉ u, from λ hiu, his (htus ▸ mem_union_right _ hiu), obtain ⟨d, rfl⟩ | ⟨d, rfl⟩ : p i ∣ b ∨ p i ∣ c, from hpi.div_or_div ⟨a, by rw [← hbc, mul_comm]⟩, { rw [mul_assoc, mul_comm a, mul_right_inj' hpi.ne_zero] at hbc, exact ⟨insert i t, u, d, c, by rw [insert_union, htus], disjoint_insert_left.2 ⟨hiu, htu⟩, by simp [hbc, prod_insert hit, mul_assoc, mul_comm, mul_left_comm]⟩ }, { rw [← mul_assoc, mul_right_comm b, mul_left_inj' hpi.ne_zero] at hbc, exact ⟨t, insert i u, b, d, by rw [union_insert, htus], disjoint_insert_right.2 ⟨hit, htu⟩, by simp [← hbc, prod_insert hiu, mul_assoc, mul_comm, mul_left_comm]⟩ } } end /-- If ` x * y = a * p ^ n` where `p` is prime, then `x` and `y` can both be written as the product of a power of `p` and a divisor of `a`. -/ lemma mul_eq_mul_prime_pow {x y a p : R} {n : ℕ} (hp : prime p) (hx : x * y = a * p ^ n) : ∃ (i j : ℕ) (b c : R), i + j = n ∧ a = b * c ∧ x = b * p ^ i ∧ y = c * p ^ j := begin rcases mul_eq_mul_prime_prod (λ _ _, hp) (show x * y = a * (range n).prod (λ _, p), by simpa) with ⟨t, u, b, c, htus, htu, rfl, rfl, rfl⟩, exact ⟨t.card, u.card, b, c, by rw [← card_disjoint_union htu, htus, card_range], by simp⟩, end
79160b5059140508610bd7e265c87da0d6fdb883
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/TermSeq.lean
9c5a4d8b4b9eaacff30788c152699f7afbebe6bd
[ "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
351
lean
inductive TermSeq where | empty : TermSeq | cons : {α : Type} → (a : α) → (tail: TermSeq) → TermSeq namespace TermSeq def prodType : TermSeq → Type | empty => Unit | @cons α a tail => Prod α (prodType tail) def asProd : (ts: TermSeq) → prodType ts | empty => (() : Unit) | @cons α a tail => (a, asProd tail) end TermSeq
9d7b3222a8748ea891e9510c43f6f2d22ae3f8fe
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/list/lemmas.lean
144a707cf5c448b9bbdb53a68bc6baa89959a3ad
[ "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
2,148
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky, Yury Kudryashov -/ import data.set.function import data.list.basic /-! # Some lemmas about lists involving sets Split out from `data.list.basic` to reduce its dependencies. -/ open list variables {α β : Type*} namespace list lemma range_map (f : α → β) : set.range (map f) = {l | ∀ x ∈ l, x ∈ set.range f} := begin refine set.subset.antisymm (set.range_subset_iff.2 $ λ l, forall_mem_map_iff.2 $ λ y _, set.mem_range_self _) (λ l hl, _), induction l with a l ihl, { exact ⟨[], rfl⟩ }, rcases ihl (λ x hx, hl x $ subset_cons _ _ hx) with ⟨l, rfl⟩, rcases hl a (mem_cons_self _ _) with ⟨a, rfl⟩, exact ⟨a :: l, map_cons _ _ _⟩ end lemma range_map_coe (s : set α) : set.range (map (coe : s → α)) = {l | ∀ x ∈ l, x ∈ s} := by rw [range_map, subtype.range_coe] /-- If each element of a list can be lifted to some type, then the whole list can be lifted to this type. -/ instance can_lift (c) (p) [can_lift α β c p] : can_lift (list α) (list β) (list.map c) (λ l, ∀ x ∈ l, p x) := { prf := λ l H, begin rw [← set.mem_range, range_map], exact λ a ha, can_lift.prf a (H a ha), end} lemma inj_on_insert_nth_index_of_not_mem (l : list α) (x : α) (hx : x ∉ l) : set.inj_on (λ k, insert_nth k x l) {n | n ≤ l.length} := begin induction l with hd tl IH, { intros n hn m hm h, simp only [set.mem_singleton_iff, set.set_of_eq_eq_singleton, length, nonpos_iff_eq_zero] at hn hm, simp [hn, hm] }, { intros n hn m hm h, simp only [length, set.mem_set_of_eq] at hn hm, simp only [mem_cons_iff, not_or_distrib] at hx, cases n; cases m, { refl }, { simpa [hx.left] using h }, { simpa [ne.symm hx.left] using h }, { simp only [true_and, eq_self_iff_true, insert_nth_succ_cons] at h, rw nat.succ_inj', refine IH hx.right _ _ h, { simpa [nat.succ_le_succ_iff] using hn }, { simpa [nat.succ_le_succ_iff] using hm } } } end end list
fb53dccaef093e3e46229b33e2e29bf59b7a9407
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/ring_theory/power_basis.lean
53dadb23c1b45074857160b198671851789e7a01
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
14,596
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import field_theory.minpoly /-! # Power basis This file defines a structure `power_basis R S`, giving a basis of the `R`-algebra `S` as a finite list of powers `1, x, ..., x^n`. For example, if `x` is algebraic over a ring/field, adjoining `x` gives a `power_basis` structure generated by `x`. ## Definitions * `power_basis R A`: a structure containing an `x` and an `n` such that `1, x, ..., x^n` is a basis for the `R`-algebra `A` (viewed as an `R`-module). * `finrank (hf : f ≠ 0) : finite_dimensional.finrank K (adjoin_root f) = f.nat_degree`, the dimension of `adjoin_root f` equals the degree of `f` * `power_basis.lift (pb : power_basis R S)`: if `y : S'` satisfies the same equations as `pb.gen`, this is the map `S →ₐ[R] S'` sending `pb.gen` to `y` * `power_basis.equiv`: if two power bases satisfy the same equations, they are equivalent as algebras ## Implementation notes Throughout this file, `R`, `S`, ... are `comm_ring`s, `A`, `B`, ... are `integral_domain`s and `K`, `L`, ... are `field`s. `S` is an `R`-algebra, `B` is an `A`-algebra, `L` is a `K`-algebra. ## Tags power basis, powerbasis -/ open polynomial variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T] variables {A B : Type*} [integral_domain A] [integral_domain B] [algebra A B] variables {K L : Type*} [field K] [field L] [algebra K L] /-- `pb : power_basis R S` states that `1, pb.gen, ..., pb.gen ^ (pb.dim - 1)` is a basis for the `R`-algebra `S` (viewed as `R`-module). This is a structure, not a class, since the same algebra can have many power bases. For the common case where `S` is defined by adjoining an integral element to `R`, the canonical power basis is given by `{algebra,intermediate_field}.adjoin.power_basis`. -/ @[nolint has_inhabited_instance] structure power_basis (R S : Type*) [comm_ring R] [ring S] [algebra R S] := (gen : S) (dim : ℕ) (basis : basis (fin dim) R S) (basis_eq_pow : ∀ i, basis i = gen ^ (i : ℕ)) namespace power_basis @[simp] lemma coe_basis (pb : power_basis R S) : ⇑pb.basis = λ (i : fin pb.dim), pb.gen ^ (i : ℕ) := funext pb.basis_eq_pow /-- Cannot be an instance because `power_basis` cannot be a class. -/ lemma finite_dimensional [algebra K S] (pb : power_basis K S) : finite_dimensional K S := finite_dimensional.of_fintype_basis pb.basis lemma finrank [algebra K S] (pb : power_basis K S) : finite_dimensional.finrank K S = pb.dim := by rw [finite_dimensional.finrank_eq_card_basis pb.basis, fintype.card_fin] lemma mem_span_pow' {x y : S} {d : ℕ} : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : polynomial R, f.degree < d ∧ y = aeval x f := begin have : set.range (λ (i : fin d), x ^ (i : ℕ)) = (λ (i : ℕ), x ^ i) '' ↑(finset.range d), { ext n, simp_rw [set.mem_range, set.mem_image, finset.mem_coe, finset.mem_range], exact ⟨λ ⟨⟨i, hi⟩, hy⟩, ⟨i, hi, hy⟩, λ ⟨i, hi, hy⟩, ⟨⟨i, hi⟩, hy⟩⟩ }, simp only [this, finsupp.mem_span_iff_total, degree_lt_iff_coeff_zero, exists_iff_exists_finsupp, coeff, aeval, eval₂_ring_hom', eval₂_eq_sum, polynomial.sum, support, finsupp.mem_supported', finsupp.total, finsupp.sum, algebra.smul_def, eval₂_zero, exists_prop, linear_map.id_coe, eval₂_one, id.def, not_lt, finsupp.coe_lsum, linear_map.coe_smul_right, finset.mem_range, alg_hom.coe_mk, finset.mem_coe], simp_rw [@eq_comm _ y], exact iff.rfl end lemma mem_span_pow {x y : S} {d : ℕ} (hd : d ≠ 0) : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : polynomial R, f.nat_degree < d ∧ y = aeval x f := begin rw mem_span_pow', split; { rintros ⟨f, h, hy⟩, refine ⟨f, _, hy⟩, by_cases hf : f = 0, { simp only [hf, nat_degree_zero, degree_zero] at h ⊢, exact lt_of_le_of_ne (nat.zero_le d) hd.symm <|> exact with_bot.bot_lt_some d }, simpa only [degree_eq_nat_degree hf, with_bot.coe_lt_coe] using h }, end lemma dim_ne_zero [nontrivial S] (pb : power_basis R S) : pb.dim ≠ 0 := λ h, one_ne_zero $ show (1 : S) = 0, by { rw [← pb.basis.total_repr 1, finsupp.total_apply, finsupp.sum_fintype], { refine finset.sum_eq_zero (λ x hx, _), cases x with x x_lt, rw h at x_lt, cases x_lt }, { simp } } lemma dim_pos [nontrivial S] (pb : power_basis R S) : 0 < pb.dim := nat.pos_of_ne_zero pb.dim_ne_zero lemma exists_eq_aeval [nontrivial S] (pb : power_basis R S) (y : S) : ∃ f : polynomial R, f.nat_degree < pb.dim ∧ y = aeval pb.gen f := (mem_span_pow pb.dim_ne_zero).mp (by simpa using pb.basis.mem_span y) section minpoly open_locale big_operators variable [algebra A S] /-- `pb.minpoly_gen` is a minimal polynomial for `pb.gen`. If `A` is not a field, it might not necessarily be *the* minimal polynomial, however `nat_degree_minpoly` shows its degree is indeed minimal. -/ noncomputable def minpoly_gen (pb : power_basis A S) : polynomial A := X ^ pb.dim - ∑ (i : fin pb.dim), C (pb.basis.repr (pb.gen ^ pb.dim) i) * X ^ (i : ℕ) @[simp] lemma nat_degree_minpoly_gen (pb : power_basis A S) : nat_degree (minpoly_gen pb) = pb.dim := begin unfold minpoly_gen, apply nat_degree_eq_of_degree_eq_some, rw degree_sub_eq_left_of_degree_lt; rw degree_X_pow, apply degree_sum_fin_lt end lemma minpoly_gen_monic (pb : power_basis A S) : monic (minpoly_gen pb) := begin apply monic_sub_of_left (monic_pow (monic_X) _), rw degree_X_pow, exact degree_sum_fin_lt _ end @[simp] lemma aeval_minpoly_gen (pb : power_basis A S) : aeval pb.gen (minpoly_gen pb) = 0 := begin simp_rw [minpoly_gen, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_mul, alg_hom.map_pow, aeval_C, ← algebra.smul_def, aeval_X], refine sub_eq_zero.mpr ((pb.basis.total_repr (pb.gen ^ pb.dim)).symm.trans _), rw [finsupp.total_apply, finsupp.sum_fintype]; simp only [pb.coe_basis, zero_smul, eq_self_iff_true, implies_true_iff] end lemma is_integral_gen (pb : power_basis A S) : is_integral A pb.gen := ⟨minpoly_gen pb, minpoly_gen_monic pb, aeval_minpoly_gen pb⟩ lemma dim_le_nat_degree_of_root (h : power_basis A S) {p : polynomial A} (ne_zero : p ≠ 0) (root : aeval h.gen p = 0) : h.dim ≤ p.nat_degree := begin refine le_of_not_lt (λ hlt, ne_zero _), let p_coeff : fin (h.dim) → A := λ i, p.coeff i, suffices : ∀ i, p_coeff i = 0, { ext i, by_cases hi : i < h.dim, { exact this ⟨i, hi⟩ }, exact coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)) }, intro i, refine linear_independent_iff'.mp h.basis.linear_independent _ _ _ i (finset.mem_univ _), rw aeval_eq_sum_range' hlt at root, rw finset.sum_fin_eq_sum_range, convert root, ext i, split_ifs with hi, { simp_rw [coe_basis, p_coeff, fin.coe_mk] }, { rw [coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)), zero_smul] } end @[simp] lemma nat_degree_minpoly (pb : power_basis A S) : (minpoly A pb.gen).nat_degree = pb.dim := begin refine le_antisymm _ (dim_le_nat_degree_of_root pb (minpoly.ne_zero pb.is_integral_gen) (minpoly.aeval _ _)), rw ← nat_degree_minpoly_gen, apply nat_degree_le_of_degree_le, rw ← degree_eq_nat_degree (minpoly_gen_monic pb).ne_zero, exact minpoly.min _ _ (minpoly_gen_monic pb) (aeval_minpoly_gen pb) end end minpoly section equiv variables [algebra A S] {S' : Type*} [comm_ring S'] [algebra A S'] lemma nat_degree_lt_nat_degree {p q : polynomial R} (hp : p ≠ 0) (hpq : p.degree < q.degree) : p.nat_degree < q.nat_degree := begin by_cases hq : q = 0, { rw [hq, degree_zero] at hpq, have := not_lt_bot hpq, contradiction }, rwa [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe] at hpq end lemma constr_pow_aeval (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (f : polynomial A) : pb.basis.constr A (λ i, y ^ (i : ℕ)) (aeval pb.gen f) = aeval y f := begin rw [← aeval_mod_by_monic_eq_self_of_root (minpoly.monic pb.is_integral_gen) (minpoly.aeval _ _), ← @aeval_mod_by_monic_eq_self_of_root _ _ _ _ _ f _ (minpoly.monic pb.is_integral_gen) y hy], by_cases hf : f %ₘ minpoly A pb.gen = 0, { simp only [hf, alg_hom.map_zero, linear_map.map_zero] }, have : (f %ₘ minpoly A pb.gen).nat_degree < pb.dim, { rw ← pb.nat_degree_minpoly, apply nat_degree_lt_nat_degree hf, exact degree_mod_by_monic_lt _ (minpoly.monic pb.is_integral_gen) (minpoly.ne_zero pb.is_integral_gen) }, rw [aeval_eq_sum_range' this, aeval_eq_sum_range' this, linear_map.map_sum], refine finset.sum_congr rfl (λ i (hi : i ∈ finset.range pb.dim), _), rw finset.mem_range at hi, rw linear_map.map_smul, congr, rw [← fin.coe_mk hi, ← pb.basis_eq_pow ⟨i, hi⟩, basis.constr_basis] end lemma constr_pow_gen (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) : pb.basis.constr A (λ i, y ^ (i : ℕ)) pb.gen = y := by { convert pb.constr_pow_aeval hy X; rw aeval_X } lemma constr_pow_algebra_map (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (x : A) : pb.basis.constr A (λ i, y ^ (i : ℕ)) (algebra_map A S x) = algebra_map A S' x := by { convert pb.constr_pow_aeval hy (C x); rw aeval_C } lemma constr_pow_mul [nontrivial S] (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (x x' : S) : pb.basis.constr A (λ i, y ^ (i : ℕ)) (x * x') = pb.basis.constr A (λ i, y ^ (i : ℕ)) x * pb.basis.constr A (λ i, y ^ (i : ℕ)) x' := begin obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, obtain ⟨g, hg, rfl⟩ := pb.exists_eq_aeval x', simp only [← aeval_mul, pb.constr_pow_aeval hy] end /-- `pb.lift y hy` is the algebra map sending `pb.gen` to `y`, where `hy` states the higher powers of `y` are the same as the higher powers of `pb.gen`. -/ noncomputable def lift [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) : S →ₐ[A] S' := { map_one' := by { convert pb.constr_pow_algebra_map hy 1 using 2; rw ring_hom.map_one }, map_zero' := by { convert pb.constr_pow_algebra_map hy 0 using 2; rw ring_hom.map_zero }, map_mul' := pb.constr_pow_mul hy, commutes' := pb.constr_pow_algebra_map hy, .. pb.basis.constr A (λ i, y ^ (i : ℕ)) } @[simp] lemma lift_gen [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) : pb.lift y hy pb.gen = y := pb.constr_pow_gen hy @[simp] lemma lift_aeval [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) (f : polynomial A) : pb.lift y hy (aeval pb.gen f) = aeval y f := pb.constr_pow_aeval hy f /-- `pb.equiv pb' h` is an equivalence of algebras with the same power basis. -/ noncomputable def equiv [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : S ≃ₐ[A] S' := alg_equiv.of_alg_hom (pb.lift pb'.gen (h.symm ▸ minpoly.aeval A pb'.gen)) (pb'.lift pb.gen (h ▸ minpoly.aeval A pb.gen)) (by { ext x, obtain ⟨f, hf, rfl⟩ := pb'.exists_eq_aeval x, simp }) (by { ext x, obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, simp }) @[simp] lemma equiv_aeval [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) (f : polynomial A) : pb.equiv pb' h (aeval pb.gen f) = aeval pb'.gen f := pb.lift_aeval _ (h.symm ▸ minpoly.aeval A _) _ @[simp] lemma equiv_gen [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : pb.equiv pb' h pb.gen = pb'.gen := pb.lift_gen _ (h.symm ▸ minpoly.aeval A _) local attribute [irreducible] power_basis.lift @[simp] lemma equiv_symm [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : (pb.equiv pb' h).symm = pb'.equiv pb h.symm := rfl end equiv end power_basis open power_basis /-- Useful lemma to show `x` generates a power basis: the powers of `x` less than the degree of `x`'s minimal polynomial are linearly independent. -/ lemma is_integral.linear_independent_pow [algebra K S] {x : S} (hx : is_integral K x) : linear_independent K (λ (i : fin (minpoly K x).nat_degree), x ^ (i : ℕ)) := begin rw linear_independent_iff, intros p hp, set f : polynomial K := p.sum (λ i, monomial i) with hf0, have f_def : ∀ (i : fin _), f.coeff i = p i, { intro i, simp only [f, finsupp.sum, coeff_monomial, finset_sum_coeff], rw [finset.sum_eq_single, if_pos rfl], { intros b _ hb, rw if_neg (mt (λ h, _) hb), exact fin.coe_injective h }, { intro hi, split_ifs; { exact finsupp.not_mem_support_iff.mp hi } } }, have f_def' : ∀ i, f.coeff i = if hi : i < _ then p ⟨i, hi⟩ else 0, { intro i, split_ifs with hi, { exact f_def ⟨i, hi⟩ }, simp only [f, finsupp.sum, coeff_monomial, finset_sum_coeff], apply finset.sum_eq_zero, rintro ⟨j, hj⟩ -, apply if_neg (mt _ hi), rintro rfl, exact hj }, suffices : f = 0, { ext i, rw [← f_def, this, coeff_zero, finsupp.zero_apply] }, contrapose hp with hf, intro h, have : (minpoly K x).degree ≤ f.degree, { apply minpoly.degree_le_of_ne_zero K x hf, convert h, simp_rw [finsupp.total_apply, aeval_def, hf0, finsupp.sum, eval₂_finset_sum], apply finset.sum_congr rfl, rintro i -, simp only [algebra.smul_def, eval₂_monomial] }, have : ¬ (minpoly K x).degree ≤ f.degree, { apply not_le_of_lt, rw [degree_eq_nat_degree (minpoly.ne_zero hx), degree_lt_iff_coeff_zero], intros i hi, rw [f_def' i, dif_neg], exact hi.not_lt }, contradiction end lemma is_integral.mem_span_pow [nontrivial R] {x y : S} (hx : is_integral R x) (hy : ∃ f : polynomial R, y = aeval x f) : y ∈ submodule.span R (set.range (λ (i : fin (minpoly R x).nat_degree), x ^ (i : ℕ))) := begin obtain ⟨f, rfl⟩ := hy, apply mem_span_pow'.mpr _, have := minpoly.monic hx, refine ⟨f.mod_by_monic (minpoly R x), lt_of_lt_of_le (degree_mod_by_monic_lt _ this (ne_zero_of_monic this)) degree_le_nat_degree, _⟩, conv_lhs { rw ← mod_by_monic_add_div f this }, simp only [add_zero, zero_mul, minpoly.aeval, aeval_add, alg_hom.map_mul] end
05596b07efe52645789824051b08e34fba9b93d8
0c6b99c0daded009943e570c13367c8cc7d3bcae
/chapter3.lean
1041386edace8ff2cb601555361b1e997bdf381b
[]
no_license
TateKennington/logic-and-proof-exercises
e3c5c5b12b6238b47b0c5acf8717923bd471a535
acca8882026e7b643453eb096d3021cd043005bd
refs/heads/main
1,687,638,489,616
1,626,858,474,000
1,626,858,474,000
371,871,374
4
0
null
null
null
null
UTF-8
Lean
false
false
3,637
lean
section variables A B : Prop variable h : A ∧ B example: B ∧ A := and.intro (and.right h) (and.left h) end section variables Q R : Prop variable h: Q example: (Q → R) → R := assume h1, h1 h end section variables A B: Prop example: ¬(A ∧ B) → (A → ¬B):= assume h, assume h1, assume h2, show false, from h (and.intro h1 h2) end section variables P Q R S T: Prop variable h1: (P ∧ Q) ∧ R variable h2: S ∧ T example: Q ∧ S:= and.intro (and.right (and.left h1)) (and.left h2) end section variables A B C: Prop example: (A → C) ∧ (B → ¬C) → ¬(A ∧ B) := assume h, assume h1, show false, from (and.right h) (and.right h1) ((and.left h) (and.left h1)) end section variables A B C: Prop example: (A ∧ B) → ((A → C) → ¬(B → ¬C)) := assume h1, assume h2, assume h3, show false, from h3 (and.right h1) (h2 (and.left h1)) end section variables A B: Prop example: A ∨ B → B ∨ A := assume h, show B ∨ A, from or.elim h (assume h1: A, show B ∨ A, from or.inr h1) (assume h1: B, show B ∨ A, from or.inl h1) end section variables A B: Prop example: ¬A ∧ ¬B → ¬(A ∨ B) := assume h1, assume h2, show false, from or.elim h2 (assume h3: A, (and.left h1) h3) (assume h3: B, (and.right h1) h3) end section variables A B: Prop variable h: ¬A ∨ ¬B example: ¬(A ∧ B) := assume h1, show false, from or.elim h (assume h2: ¬A, h2 (and.left h1)) (assume h2: ¬B, h2 (and.right h1)) end section variable A: Prop example: ¬(A ↔ ¬A) := assume h: A ↔ ¬A, show false, from have hₐ: ¬A, from assume h₁: A, have h₂: A → ¬A, from iff.elim_left h, show false, from h₂ h₁ h₁, have h₁: ¬A → A, from iff.elim_right h, hₐ (h₁ hₐ) end section variables A B: Prop variable h: A ↔ B example: ¬A ↔ ¬B := have h₁: ¬A → ¬B, from assume hₐ, show ¬B, from assume hₐb, show false, from have h₂: B → A, from iff.elim_right h, hₐ (h₂ hₐb), have h₃: ¬B → ¬A, from assume hₐb, show ¬A, from assume hₐ, show false, from have h₂: A → B, from iff.elim_left h, hₐb (h₂ hₐ), iff.intro h₁ h₃ end section variables P Q R: Prop variable h: (P ∨ Q) → R example: P → R := assume hₚ, show R, from have h₁: (P ∨ Q), from or.intro_left Q hₚ, h h₁ end section variables P Q R: Prop example: ((P ∨ Q) → R) → (P → R) := assume h, assume hₚ, show R, from have h₁: (P ∨ Q), from or.intro_left Q hₚ, h h₁ end section variables A B C: Prop variable h: A ∨ B example: C → (A ∨ B) ∧ C := assume hₐc, and.intro h hₐc end section variables W X Y Z: Prop variable h₁: W → X variable h₂: Y → Z example: W ∨ Y → X ∨ Z := assume h, have h₃: W → X ∨ Z, from assume w, show X ∨ Z, from or.intro_left Z (h₁ w), have h₄: Y → X ∨ Z, from assume y, show X ∨ Z, from or.intro_right X (h₂ y), or.elim h h₃ h₄ end section variables A B: Prop example: (A ∨ (B ∧ A)) → A := assume h, show A, from have h₁: A → A, from assume hₐ, hₐ, have h₂: (B ∧ A) → A, from assume hₐ, and.right hₐ, or.elim h h₁ h₂ end
284bcb1f8a1c757a10ccf1953267c3a80bf1a2e1
dcf093fda1f51c094394c50e182cfb9dec39635a
/path.lean
5997639bd6b66ed15a886b2c92b7ba848d8ee7de
[]
no_license
dselsam/cs103
7ac496d0293befca95d3045add91c5270a5d291f
31ab9784a6f65f226efb702a0da52f907c616a71
refs/heads/master
1,611,092,644,140
1,492,571,015,000
1,492,571,015,000
88,693,969
1
0
null
null
null
null
UTF-8
Lean
false
false
5,009
lean
import data.list data.subtype data.sigma open subtype list prod prod.ops sigma.ops definition edge (V : Type) := V × V definition graph (V : Type) := edge V → Prop definition undirected {V : Type} (g : graph V) := ∀ (u v : V), g (u,v) → g (v,u) namespace path variables {V : Type} [V_decidable_eq : decidable_eq V] include V_decidable_eq definition path : graph V → V → V → Prop := sorry definition repeated_edges {g : graph V} {u v : V} (p : path g u v) : Prop := sorry definition simple_path (g : graph V) (u v : V) := { p : path g u v | ¬ repeated_edges p } definition path_contains_edge {g : graph V} {u v : V} : path g u v → edge V → Prop := sorry lemma simple_path_nonempty (g : graph V) (u v : V) (spath : simple_path g u v) : ∃ e, g e ∧ path_contains_edge (spath.1) e definition cycle (g : graph V) (u : V) := path g u u definition simple_cycle (g : graph V) := Σ (u : V) (p : cycle g u), ¬ repeated_edges p variables {g : graph V} theorem path_refl (g : graph V) : ∀ (v : V), path g v v := sorry theorem path_symm (g : graph V) : ∀ {u v : V}, path g u v → path g v u := sorry theorem path_trans (g : graph V) : ∀ {u v w : V}, path g u v → path g v w → path g u w := sorry definition graph_connected (g : graph V) := ∀ (u v : V), path g u v definition remove_edge (g : graph V) (e : edge V) : graph V := λ (f : edge V), if f.1 = e.1 ∧ f.2 = e.2 ∨ f.1 = e.2 ∧ f.2 = e.1 then false else g f -- An undirected graph G is called k-edge-connected iff G is connected, and there is no set of k – 1 edges that can be removed from G that disconnects it definition two_edge_connected (g : graph V) := graph_connected g ∧ ¬ ∃ (e1 e2 : edge V), g e1 ∧ g e2 ∧ ¬ graph_connected (remove_edge (remove_edge g e1) e2) -- A bridge in a connected, undirected graph G is an edge in G that, if removed, disconnects G. definition has_bridge (g : graph V) : Prop := graph_connected g ∧ ∃ (e : edge V), g e ∧ ¬ graph_connected (remove_edge g e) -- Theorem: An undirected graph G is 2-edge-connected iff it is connected and has no bridges. theorem two_edge_connected_iff (g : graph V) : two_edge_connected g ↔ graph_connected g ∧ ¬ has_bridge g := sorry /- [KS] Theorem: Let G = (V, E) be any graph containing a simple cycle C. Let u, v ∈ V be nodes in G. If u ↔ v, then after deleting any single edge in C from graph G, it is still the case that u ↔ v. Proof: Consider any graph G = (V, E) with a simple cycle C = (x1, x2, …, xn, x1). Consider any u, v ∈ V such that u ↔ v. This means that there must be some simple path (u, y1, y2, …, ym, v) from u to v. Now, suppose that we remove the edge {xi, xi+1} from G. We need to show that u ↔ v in this modified graph. We consider two cases. First, it might be the case that the edge {xi, xi+1} does not appear on the path (u, y1, …, ym, v). In that case, the path (u, y1, …, ym, v) is a valid path from u to v in the new graph, so u ↔ v still holds. Second, it might be the case that the edge {xi, xi+1} appears somewhere in our original path (u, y1, …, ym, v). Since the graph is undirected, the edge might appear as {xi, xi+1} or as {xi+1, xi} when it occurs in the path. Assume without loss of generality that it appears as {xi, xi+1} (otherwise, we can just reverse the ordering of the nodes in the original cycle so as to relabel the edges). This means that we can split the original path into three smaller paths – a path from u to xi, then the edge {xi, xi+1}, and finally a path from xi+1 to v. Thus u ↔ xi and xi+1 ↔ v. Now, since the edge {xi, xi+1} lies on the cycle C, after deleting the edge from the cycle, there is still a path from xi to xi+1. Specifically, we can follow the edges of the cycle in reverse from xi until we reach xi+1. In other words, in this new graph, we must have that xi ↔ xi+1. Since in this new graph u ↔ xi, xi ↔ xi+1, and xi+1 ↔ v, we thus have that u ↔ v in the new graph, as required. -/ lemma path_imp_simple_path (g : graph V) : ∀ (u v : V), path g u v → simple_path g u v := sorry theorem remove_edge_from_simple_cycle (g : graph V) (scycle : simple_cycle g) : ∀ (u v : V), path g u v → ∀ (e : edge V), g e → path (remove_edge g e) u v := -- Proof: Consider any graph G = (V, E) with a simple cycle C = (x1, x2, …, xn, x1). -- Consider any u, v ∈ V such that u ↔ v. take (u v : V) (u_conn_v : path g u v), -- This means that there must be some simple path (u, y1, y2, …, ym, v) from u to v. have u_sconn_v : simple_path g u v, from path_imp_simple_path u_conn_v, -- Now, suppose that we remove the edge {xi, xi+1} from G. let G' := remove_edge g in /-We need to show that u ↔ v in this modified graph. We consider two cases. First, it might be the case that the edge {xi, xi+1} does not appear on the path (u, y1, …, ym, v). In that case, the path (u, y1, …, ym, v) is a valid path from u to v in the new graph, so u ↔ v still holds. -/ end path
6a3df16cd00478abe686ce943d12c40a3e74c283
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/refinePreservesNaturalMVars.lean
952656e504d98985c7ef97a65cd7882433a84e6f
[ "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,516
lean
import Lean /-! Ensures that `refine` does not remove pre-existing natural goals from the goal list. -/ open Lean Meta Elab Tactic Term elab "add_natural_goal" s:ident " : " t:term : tactic => do let g ← mkFreshExprMVar (← elabType t) .natural s.getId appendGoals [g.mvarId!] /-! In the following, `refine` would erroneously close each focused goal, leading to a `(kernel) declaration has metavariables '_example'` error. This occurred because `withCollectingNewGoalsFrom` was only erroring on new natural goals (as determined by `index`), while simultaneously only passing through non-natural goals to construct the resulting goal list. This orphaned old natural metavariables and closed the goal list erroneously. As such, all of the following tests should lead to an `unsolved goals` error, followed by a `no goals` error (instead of a successful focus). -/ example : Bool × Nat := by add_natural_goal d : Bool add_natural_goal e : Nat · refine (?d,?e) · refine ?d · refine ?e example : Bool × Bool := by add_natural_goal d : Bool add_natural_goal e : Bool · refine (?d,?e) · case d => refine ?e · refine ?e /-! Previously, this would error, as `refine (?d, ?e)` erroneously closed the goal, leading to a `no goals` error. Instead, this should succeed. -/ example : Bool × Bool := by add_natural_goal d : Bool add_natural_goal e : Bool · refine (?d,?e) refine ?d refine ?e -- This unifies `?d` and `?e`, so only one goal remains. exact true
4e8c1f5f053375ec9282c0d3e8bd431741a01697
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/mywork/lectures/lecture_22a.lean
f1a8767b384f9b65c6aa23aa75c76eeeaeb4f074
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
3,338
lean
/- State formally and prove the proposition that congruence mod n is an equivalence relation. Follow the steps below. -/ /- First, we need to define congruence mod n. Technically it is defined not only on the natural numbers but on the integers. Here's the English language definition: Given a natural number, n, greater than 1 (a "modulus"), two natural numbers, a and b, are "congruent modulo n", if n is a divisor of their difference: that is, if there is some natural number, k such that a − b = kn). Your first task is to define cong_mod, formally, stating that for any value, n, cong_mod n is a binary relation on natural numbers, as defined above. -/ def cong_mod (n a b : ℤ) : Prop := ∃ k, a - b = k * n /- Second, formally state the proposition that for each natural number, n, "cong_mod n" is an equivalence relation. You should use the "equivalence" predicate on binary relations defined in Lean's library (which is the same as our definition from the last lecture) in writing this propopsition. -/ def cong_mod_n_is_equiv_relation (n : ℤ) : Prop := equivalence (cong_mod n) /- Note that partial evaluation makes cong_mod n into a binary relation: in that it's waiting for two more natural number arguments, let's say, a and b, and when applied to such arguments, it yields the proposition that the two numbers are congruent as defined. -/ #reduce cong_mod (4:ℤ) #reduce cong_mod (4:ℤ) (6:ℤ) (10:ℤ) -- First, translate the goal into ordinary notation -- Now what must you choose as a witness for a proof? -- Let's example : cong_mod (4:ℤ) (6:ℤ) (14:ℤ) := begin unfold cong_mod, apply exists.intro (-2:ℤ), apply rfl, end /- Now assert and prove this proposition to be a theorem, i.e., to have a proof. -/ example (n : ℤ) : cong_mod_n_is_equiv_relation n := begin unfold cong_mod_n_is_equiv_relation, unfold equivalence, split, -- chooses to apply and.elim -- reflexive unfold reflexive, assume k, unfold cong_mod, apply exists.intro (0:ℤ), --Daddy? sorry, -- accept without proof for now -- symmetric split, unfold symmetric cong_mod, assume x y h, cases h with v pf, apply exists.intro (-v),--negative v is the witness --Daddy? have lemma1 : -v * n = -(v * n) := sorry, rw lemma1, rw <-pf, --Daddy? have lemma2 : y - x = -(x - y) := sorry, rw <-lemma2, -- transitive -- you prove it unfold transitive cong_mod, assume x y z h1 h2, cases h1 with h1v h1pf, cases h2 with h2v h2pf, apply exists.intro (h1v+h2v), rw int.distrib_right _ _ _, -- LIBRARY LOOKUP! rw <-h2pf, rw <-h1pf, --Daddy? sorry, end /- A version of congruence mod n restricted to the natural (non-negative whole) numbers. -/ /- Previous problem requires access to negative numbers because it involves a term a-b, which, in ℤ can be negative. If it's negative in ℤ it will simply be truncated to 0 in ℕ, losing critical information. -/ #reduce (6:ℤ) - (11:ℤ) #reduce 6-10 -- oops #reduce 6-11 -- oops #reduce 6-12 -- oops def cong_mod_nat (n a b : ℕ) := a%n = b%n example : cong_mod_nat 4 3 7 := begin unfold cong_mod_nat, exact rfl, end example (n : ℕ): equivalence (cong_mod_nat n) := begin end
9405a152a1d03e207a7b9d2bfb47f127cf3ca26d
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/have.lean
61ae385dc4acde85e1ed078bda708e4020de9115
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
958
lean
variable f : ℕ → ℕ variable H : ∀ (n:ℕ), f n ≤ f (n+1) example : f 0 ≤ f 3 := have f 0 ≤ f 1, from H 0, have f 0 ≤ f 2, from le_trans this (H 1) , show f 0 ≤ f 3, from le_trans this (H 2) example : f 0 ≤ f 3 := have f 0 ≤ f 1, from H 0, have f 0 ≤ f 2, from le_trans (by assumption) (H 1), show f 0 ≤ f 3, from le_trans (by assumption) (H 2) example : f 0 ≥ f 1 → f 1 ≥ f 2 → f 0 = f 2 := assume : f 1 ≤ f 0, assume : f 2 ≤ f 1, show f 0 = f 2, from have f 2 ≤ f 0, from le_trans this ‹f 1 ≤ f 0›, -- \f< and \f> have f 0 ≤ f 2, from le_trans (H 0) (H 1), show f 0 = f 2, from le_antisymm ‹f 0 ≤ f 2› ‹f 2 ≤ f 0› -- \f< and \f> example : f 0 ≤ f 3 := have f 0 ≤ f 1, from H 0, have f 1 ≤ f 2, from H 1, have f 2 ≤ f 3, from H 2, show f 0 ≤ f 3, from le_trans ‹f 0 ≤ f 1› (le_trans ‹f 1 ≤ f 2› ‹f 2 ≤ f 3›)
51acf6bf5ce23e465e40c46962d82eb5813ce370
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RightZero.lean
95390ffc1badf382edd4fc93398ed149f64ab69f
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
6,467
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RightZero structure RightZero (A : Type) : Type := (e : A) (op : (A → (A → A))) (rightZero_op_e : (∀ {x : A} , (op x e) = e)) open RightZero structure Sig (AS : Type) : Type := (eS : AS) (opS : (AS → (AS → AS))) structure Product (A : Type) : Type := (eP : (Prod A A)) (opP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rightZero_op_eP : (∀ {xP : (Prod A A)} , (opP xP eP) = eP)) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RightZero A1)) (Ri2 : (RightZero A2)) : Type := (hom : (A1 → A2)) (pres_e : (hom (e Ri1)) = (e Ri2)) (pres_op : (∀ {x1 x2 : A1} , (hom ((op Ri1) x1 x2)) = ((op Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RightZero A1)) (Ri2 : (RightZero A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_e : (interp (e Ri1) (e Ri2))) (interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Ri1) x1 x2) ((op Ri2) y1 y2)))))) inductive RightZeroTerm : Type | eL : RightZeroTerm | opL : (RightZeroTerm → (RightZeroTerm → RightZeroTerm)) open RightZeroTerm inductive ClRightZeroTerm (A : Type) : Type | sing : (A → ClRightZeroTerm) | eCl : ClRightZeroTerm | opCl : (ClRightZeroTerm → (ClRightZeroTerm → ClRightZeroTerm)) open ClRightZeroTerm inductive OpRightZeroTerm (n : ℕ) : Type | v : ((fin n) → OpRightZeroTerm) | eOL : OpRightZeroTerm | opOL : (OpRightZeroTerm → (OpRightZeroTerm → OpRightZeroTerm)) open OpRightZeroTerm inductive OpRightZeroTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRightZeroTerm2) | sing2 : (A → OpRightZeroTerm2) | eOL2 : OpRightZeroTerm2 | opOL2 : (OpRightZeroTerm2 → (OpRightZeroTerm2 → OpRightZeroTerm2)) open OpRightZeroTerm2 def simplifyCl {A : Type} : ((ClRightZeroTerm A) → (ClRightZeroTerm A)) | eCl := eCl | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRightZeroTerm n) → (OpRightZeroTerm n)) | eOL := eOL | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRightZeroTerm2 n A) → (OpRightZeroTerm2 n A)) | eOL2 := eOL2 | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RightZero A) → (RightZeroTerm → A)) | Ri eL := (e Ri) | Ri (opL x1 x2) := ((op Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RightZero A) → ((ClRightZeroTerm A) → A)) | Ri (sing x1) := x1 | Ri eCl := (e Ri) | Ri (opCl x1 x2) := ((op Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RightZero A) → ((vector A n) → ((OpRightZeroTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars eOL := (e Ri) | Ri vars (opOL x1 x2) := ((op Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RightZero A) → ((vector A n) → ((OpRightZeroTerm2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars eOL2 := (e Ri) | Ri vars (opOL2 x1 x2) := ((op Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RightZeroTerm → Type)} : ((P eL) → ((∀ (x1 x2 : RightZeroTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : RightZeroTerm) , (P x)))) | pel popl eL := pel | pel popl (opL x1 x2) := (popl _ _ (inductionB pel popl x1) (inductionB pel popl x2)) def inductionCl {A : Type} {P : ((ClRightZeroTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P eCl) → ((∀ (x1 x2 : (ClRightZeroTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClRightZeroTerm A)) , (P x))))) | psing pecl popcl (sing x1) := (psing x1) | psing pecl popcl eCl := pecl | psing pecl popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing pecl popcl x1) (inductionCl psing pecl popcl x2)) def inductionOpB {n : ℕ} {P : ((OpRightZeroTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P eOL) → ((∀ (x1 x2 : (OpRightZeroTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpRightZeroTerm n)) , (P x))))) | pv peol popol (v x1) := (pv x1) | pv peol popol eOL := peol | pv peol popol (opOL x1 x2) := (popol _ _ (inductionOpB pv peol popol x1) (inductionOpB pv peol popol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRightZeroTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P eOL2) → ((∀ (x1 x2 : (OpRightZeroTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpRightZeroTerm2 n A)) , (P x)))))) | pv2 psing2 peol2 popol2 (v2 x1) := (pv2 x1) | pv2 psing2 peol2 popol2 (sing2 x1) := (psing2 x1) | pv2 psing2 peol2 popol2 eOL2 := peol2 | pv2 psing2 peol2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 peol2 popol2 x1) (inductionOp pv2 psing2 peol2 popol2 x2)) def stageB : (RightZeroTerm → (Staged RightZeroTerm)) | eL := (Now eL) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRightZeroTerm A) → (Staged (ClRightZeroTerm A))) | (sing x1) := (Now (sing x1)) | eCl := (Now eCl) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRightZeroTerm n) → (Staged (OpRightZeroTerm n))) | (v x1) := (const (code (v x1))) | eOL := (Now eOL) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRightZeroTerm2 n A) → (Staged (OpRightZeroTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | eOL2 := (Now eOL2) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (eT : (Repr A)) (opT : ((Repr A) → ((Repr A) → (Repr A)))) end RightZero
f2bb5e0fd4b71ba37d9441526f0df99389afd7a8
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/algebra/algebra.lean
e8e4d433f4ed579e7f74931dec653ea8fb918e4d
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,001
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.algebra.subalgebra import topology.algebra.module /-! # Topological (sub)algebras A topological algebra over a topological semiring `R` is a topological ring with a compatible continuous scalar multiplication by elements of `R`. We reuse typeclass `has_continuous_smul` for topological algebras. ## Results This is just a minimal stub for now! The topological closure of a subalgebra is still a subalgebra, which as an algebra is a topological algebra. -/ open classical set topological_space algebra open_locale classical universes u v w section topological_algebra variables (R : Type*) [topological_space R] [comm_semiring R] variables (A : Type u) [topological_space A] variables [semiring A] lemma continuous_algebra_map_iff_smul [algebra R A] [topological_semiring A] : continuous (algebra_map R A) ↔ continuous (λ p : R × A, p.1 • p.2) := begin refine ⟨λ h, _, λ h, _⟩, { simp only [algebra.smul_def], exact (h.comp continuous_fst).mul continuous_snd }, { rw algebra_map_eq_smul_one', exact h.comp (continuous_id.prod_mk continuous_const) } end @[continuity] lemma continuous_algebra_map [algebra R A] [topological_semiring A] [has_continuous_smul R A] : continuous (algebra_map R A) := (continuous_algebra_map_iff_smul R A).2 continuous_smul lemma has_continuous_smul_of_algebra_map [algebra R A] [topological_semiring A] (h : continuous (algebra_map R A)) : has_continuous_smul R A := ⟨(continuous_algebra_map_iff_smul R A).1 h⟩ end topological_algebra section topological_algebra variables {R : Type*} [comm_semiring R] variables {A : Type u} [topological_space A] variables [semiring A] variables [algebra R A] [topological_semiring A] /-- The closure of a subalgebra in a topological algebra as a subalgebra. -/ def subalgebra.topological_closure (s : subalgebra R A) : subalgebra R A := { carrier := closure (s : set A), algebra_map_mem' := λ r, s.to_subsemiring.subring_topological_closure (s.algebra_map_mem r), .. s.to_subsemiring.topological_closure } @[simp] lemma subalgebra.topological_closure_coe (s : subalgebra R A) : (s.topological_closure : set A) = closure (s : set A) := rfl instance subalgebra.topological_closure_topological_semiring (s : subalgebra R A) : topological_semiring (s.topological_closure) := s.to_subsemiring.topological_closure_topological_semiring instance subalgebra.topological_closure_topological_algebra [topological_space R] [has_continuous_smul R A] (s : subalgebra R A) : has_continuous_smul R (s.topological_closure) := s.to_submodule.topological_closure_has_continuous_smul lemma subalgebra.subalgebra_topological_closure (s : subalgebra R A) : s ≤ s.topological_closure := subset_closure lemma subalgebra.is_closed_topological_closure (s : subalgebra R A) : is_closed (s.topological_closure : set A) := by convert is_closed_closure lemma subalgebra.topological_closure_minimal (s : subalgebra R A) {t : subalgebra R A} (h : s ≤ t) (ht : is_closed (t : set A)) : s.topological_closure ≤ t := closure_minimal h ht /-- This is really a statement about topological algebra isomorphisms, but we don't have those, so we use the clunky approach of talking about an algebra homomorphism, and a separate homeomorphism, along with a witness that as functions they are the same. -/ lemma subalgebra.topological_closure_comap'_homeomorph (s : subalgebra R A) {B : Type*} [topological_space B] [ring B] [topological_ring B] [algebra R B] (f : B →ₐ[R] A) (f' : B ≃ₜ A) (w : (f : B → A) = f') : s.topological_closure.comap' f = (s.comap' f).topological_closure := begin apply set_like.ext', simp only [subalgebra.topological_closure_coe], simp only [subalgebra.coe_comap, subsemiring.coe_comap, alg_hom.coe_to_ring_hom], rw [w], exact f'.preimage_closure _, end end topological_algebra
ca9d81c3b3534538516f033914d4b30413ae8c93
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/catchThe.lean
1472ca74f96a8ae7d32aa5cdff0f5b3dad8d5a16
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
852
lean
import Lean.Meta new_frontend open Lean open Lean.Meta universes u v w abbrev M := ExceptT String $ MetaM def testM {α} [HasBeq α] [HasToString α] (x : M α) (expected : α) : MetaM Unit := do let r ← x; (match r with | Except.ok a => «unless» (a == expected) $ throwError ("unexpected result " ++ toString a) | Except.error e => throwError ("FAILED: " ++ e)) @[noinline] def act1 : M Nat := throwThe Exception $ Exception.error Syntax.missing "Error at act1" def g1 : M Nat := catchThe Exception («catch» act1 (fun ex => pure 100)) (fun ex => pure 200) #eval testM g1 200 @[noinline] def act2 : M Nat := throw "hello world" def g2 : M Nat := catchThe Exception («catch» act2 (fun ex => pure 100)) (fun ex => pure 200) #eval testM g2 100 def h1 : CoreM Nat := pure 10 #eval h1 def h2 : MetaM Nat := pure 20 #eval h2
907a9e1feee717b264afc0533c86a091ced6b5e0
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/apply_fun.lean
a77b1ba953d4e4b863b0d5d3513f54659c6fe979
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,164
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Keeley Hoek, Patrick Massot -/ import tactic.monotonicity namespace tactic /-- Apply the function `f` given by `e : pexpr` to the local hypothesis `hyp`, which must either be of the form `a = b` or `a ≤ b`, replacing the type of `hyp` with `f a = f b` or `f a ≤ f b`. If `hyp` names an inequality then a new goal `monotone f` is created, unless the name of a proof of this fact is passed as the optional argument `mono_lem`, or the `mono` tactic can prove it. -/ meta def apply_fun_to_hyp (e : pexpr) (mono_lem : option pexpr) (hyp : expr) : tactic unit := do { t ← infer_type hyp, prf ← match t with | `(%%l = %%r) := do ltp ← infer_type l, mv ← mk_mvar, to_expr ``(congr_arg (%%e : %%ltp → %%mv) %%hyp) | `(%%l ≤ %%r) := do Hmono ← match mono_lem with | some mono_lem := tactic.i_to_expr mono_lem | none := do n ← get_unused_name `mono, to_expr ``(monotone %%e) >>= assert n, -- In order to resolve implicit arguments in `%%e`, -- we build (and discard) the expression `%%n %%hyp` before calling the `mono` tactic. swap, n ← get_local n, to_expr ``(%%n %%hyp), swap, do { intro_lst [`x, `y, `h], `[try { dsimp }, mono] } <|> swap, return n end, to_expr ``(%%Hmono %%hyp) | _ := fail!"failed to apply {e} at {hyp}" end, clear hyp, hyp ← note hyp.local_pp_name none prf, -- let's try to force β-reduction at `h` try $ tactic.dsimp_hyp hyp simp_lemmas.mk [] { eta := false, beta := true } } /-- Attempt to "apply" a function `f` represented by the argument `e : pexpr` to the goal. If the goal is of the form `a ≠ b`, we obtain the new goal `f a ≠ f b`. If the goal is of the form `a = b`, we obtain a new goal `f a = f b`, and a subsidiary goal `injective f`. (We attempt to discharge this subsidiary goal automatically, or using the optional argument.) If the goal is of the form `a ≤ b` (or similarly for `a < b`), and `f` is an `order_iso`, we obtain a new goal `f a ≤ f b`. -/ meta def apply_fun_to_goal (e : pexpr) (lem : option pexpr) : tactic unit := do t ← target, match t with | `(%%l ≠ %%r) := to_expr ``(ne_of_apply_ne %%e) >>= apply >> skip | `(¬%%l = %%r) := to_expr ``(ne_of_apply_ne %%e) >>= apply >> skip | `(%%l ≤ %%r) := to_expr ``((order_iso.le_iff_le %%e).mp) >>= apply >> skip | `(%%l < %%r) := to_expr ``((order_iso.lt_iff_lt %%e).mp) >>= apply >> skip | `(%%l = %%r) := focus1 (do to_expr ``(%%e %%l), -- build and discard an application, to fill in implicit arguments n ← get_unused_name `inj, to_expr ``(function.injective %%e) >>= assert n, -- Attempt to discharge the `injective f` goal (focus1 $ assumption <|> (to_expr ``(equiv.injective) >>= apply >> done) <|> -- We require that applying the lemma closes the goal, not just makes progress: (lem.mmap (λ l, to_expr l >>= apply) >> done)) <|> swap, -- return to the main goal if we couldn't discharge `injective f`. n ← get_local n, apply n, clear n) | _ := fail!"failed to apply {e} to the goal" end namespace interactive setup_tactic_parser /-- Apply a function to an equality or inequality in either a local hypothesis or the goal. * If we have `h : a = b`, then `apply_fun f at h` will replace this with `h : f a = f b`. * If we have `h : a ≤ b`, then `apply_fun f at h` will replace this with `h : f a ≤ f b`, and create a subsidiary goal `monotone f`. `apply_fun` will automatically attempt to discharge this subsidiary goal using `mono`, or an explicit solution can be provided with `apply_fun f at h using P`, where `P : monotone f`. * If the goal is `a ≠ b`, `apply_fun f` will replace this with `f a ≠ f b`. * If the goal is `a = b`, `apply_fun f` will replace this with `f a = f b`, and create a subsidiary goal `injective f`. `apply_fun` will automatically attempt to discharge this subsidiary goal using local hypotheses, or if `f` is actually an `equiv`, or an explicit solution can be provided with `apply_fun f using P`, where `P : injective f`. * If the goal is `a ≤ b` (or similarly for `a < b`), and `f` is actually an `order_iso`, `apply_fun f` will replace the goal with `f a ≤ f b`. If `f` is anything else (e.g. just a function, or an `equiv`), `apply_fun` will fail. Typical usage is: ```lean open function example (X Y Z : Type) (f : X → Y) (g : Y → Z) (H : injective $ g ∘ f) : injective f := begin intros x x' h, apply_fun g at h, exact H h end ``` -/ meta def apply_fun (q : parse texpr) (locs : parse location) (lem : parse (tk "using" *> texpr)?) : tactic unit := locs.apply (apply_fun_to_hyp q lem) (apply_fun_to_goal q lem) add_tactic_doc { name := "apply_fun", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_fun], tags := ["context management"] } end interactive end tactic
632620ce16dadb025071eaf119f2220b19110722
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/mk_dec_eq_instance.lean
202647df5c6ffae3646767b010685be4a65f7f8b
[ "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
5,254
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Helper tactic for showing that a type has decidable equality. -/ prelude import init.meta.contradiction_tactic init.meta.constructor_tactic import init.meta.injection_tactic init.meta.relation_tactics import init.meta.rec_util init.meta.interactive namespace tactic open expr environment list /- Retrieve the name of the type we are building a decidable equality proof for. -/ private meta def get_dec_eq_type_name : tactic name := do { (pi x1 i1 d1 (pi x2 i2 d2 b)) ← target >>= whnf, (const n ls) ← return (get_app_fn b), when (n ≠ `decidable) failed, (const I ls) ← return (get_app_fn d1), return I } <|> fail "mk_dec_eq_instance tactic failed, target type is expected to be of the form (decidable_eq ...)" /- Extract (lhs, rhs) from a goal (decidable (lhs = rhs)) -/ private meta def get_lhs_rhs : tactic (expr × expr) := do (app dec lhs_eq_rhs) ← target | fail "mk_dec_eq_instance failed, unexpected case", match_eq lhs_eq_rhs private meta def find_next_target : list expr → list expr → tactic (expr × expr) | (t::ts) (r::rs) := if t = r then find_next_target ts rs else return (t, r) | l1 l2 := failed /- Create an inhabitant of (decidable (lhs = rhs)) -/ private meta def mk_dec_eq_for (lhs : expr) (rhs : expr) : tactic expr := do lhs_type ← infer_type lhs, dec_type ← mk_app `decidable_eq [lhs_type] >>= whnf, do { inst ← mk_instance dec_type, return $ inst lhs rhs } <|> do { f ← pp dec_type, fail $ to_fmt "mk_dec_eq_instance failed, failed to generate instance for" ++ format.nest 2 (format.line ++ f) } /- Target is of the form (decidable (C ... = C ...)) where C is a constructor -/ private meta def dec_eq_same_constructor : name → name → nat → tactic unit | I_name F_name num_rec := do (lhs, rhs) ← get_lhs_rhs, -- Try easy case first, where the proof is just reflexivity (unify lhs rhs >> right >> reflexivity) <|> do { let lhs_list := get_app_args lhs, let rhs_list := get_app_args rhs, when (length lhs_list ≠ length rhs_list) (fail "mk_dec_eq_instance failed, constructor applications have different number of arguments"), (lhs_arg, rhs_arg) ← find_next_target lhs_list rhs_list, rec ← is_type_app_of lhs_arg I_name, inst ← if rec then do { inst_fn ← mk_brec_on_rec_value F_name num_rec, return $ app inst_fn rhs_arg } else do { mk_dec_eq_for lhs_arg rhs_arg }, `[apply @decidable.by_cases _ _ %%inst], -- discharge first (positive) case by recursion intro1 >>= subst >> dec_eq_same_constructor I_name F_name (if rec then num_rec + 1 else num_rec), -- discharge second (negative) case by contradiction intro1, left, -- decidable.is_false intro1 >>= injection, intros, contradiction, return () } /- Easy case: target is of the form (decidable (C_1 ... = C_2 ...)) where C_1 and C_2 are distinct constructors -/ private meta def dec_eq_diff_constructor : tactic unit := left >> intron 1 >> contradiction /- This tactic is invoked for each case of decidable_eq. There n^2 cases, where n is the number of constructors. -/ private meta def dec_eq_case_2 (I_name : name) (F_name : name) : tactic unit := do (lhs, rhs) ← get_lhs_rhs, let lhs_fn := get_app_fn lhs, let rhs_fn := get_app_fn rhs, if lhs_fn = rhs_fn then dec_eq_same_constructor I_name F_name 0 else dec_eq_diff_constructor private meta def dec_eq_case_1 (I_name : name) (F_name : name) : tactic unit := intro `w >>= cases >> all_goals (dec_eq_case_2 I_name F_name) meta def mk_dec_eq_instance_core : tactic unit := do I_name ← get_dec_eq_type_name, env ← get_env, let v_name := `_v, let F_name := `_F, let num_indices := inductive_num_indices env I_name, let idx_names := list.map (λ (p : name × nat), mk_num_name p~>fst p~>snd) (list.zip (list.repeat `idx num_indices) (list.iota num_indices)), -- Use brec_on if type is recursive. -- We store the functional in the variable F. if is_recursive env I_name then intro1 >>= (λ x, induction x (idx_names ++ [v_name, F_name]) (some $ I_name <.> "brec_on") >> return ()) else intro v_name >> return (), -- Apply cases to first element of type (I ...) get_local v_name >>= cases, all_goals (dec_eq_case_1 I_name F_name) meta def mk_dec_eq_instance : tactic unit := do env ← get_env, (pi x1 i1 d1 (pi x2 i2 d2 b)) ← target >>= whnf, (const I_name ls) ← return (get_app_fn d1), when (is_ginductive env I_name ∧ ¬ is_inductive env I_name) $ do { d1' ← whnf d1, (app I_basic_const I_idx) ← return d1', I_idx_type ← infer_type I_idx, new_goal ← to_expr ``(∀ (_idx : %%I_idx_type), decidable_eq (%%I_basic_const _idx)), assert `_basic_dec_eq new_goal, swap, to_expr `(_basic_dec_eq %%I_idx) >>= exact, intro1, return () }, mk_dec_eq_instance_core end tactic /- instances of types in dependent files -/ instance : decidable_eq ordering := by tactic.mk_dec_eq_instance
d1d33ce6e0a19055fbe13e95d34c9854529b1cab
c0807e3249ba126be15fd22370e98ae1a6dac0f0
/src/Blake3.lean
928d2308acfc576af47420e169a5bf162c0c0263
[]
no_license
tydeu/lean-blake3
1297035629db93e13ffa6a4381f9a0dc496f796f
26fab13b7d37a7c473bd3aaf8a585c5b44b64839
refs/heads/main
1,693,063,372,343
1,634,843,383,000
1,634,843,383,000
419,835,491
1
0
null
null
null
null
UTF-8
Lean
false
false
3,315
lean
/- Bindings to the Blake3 hashing library. -/ namespace Blake3 /- BLAKE3 constant values. -/ constant BLAKE3_KEY_LEN: Nat := 32 constant BLAKE3_OUT_LEN: Nat := 32 constant BLAKE3_BLOCK_LEN: Nat := 64 constant BLAKE3_CHUNK_LEN: Nat := 1024 constant BLAKE3_MAX_DEPTH: Nat := 54 /- Simplification rules for ensuring type safety of Blake3Hash -/ @[simp] theorem ByteArray.size_empty : ByteArray.empty.size = 0 := rfl @[simp] theorem ByteArray.size_push (B : ByteArray) (a : UInt8) : (B.push a).size = B.size + 1 := by { cases B; simp only [ByteArray.push, ByteArray.size, Array.size_push] } @[simp] theorem List.to_ByteArray_size : (L : List UInt8) → L.toByteArray.size = L.length | [] => rfl | a::l => by simp [List.toByteArray, to_ByteArray_loop_size] where to_ByteArray_loop_size : (L : List UInt8) → (B : ByteArray) → (List.toByteArray.loop L B).size = L.length + B.size | [], B => by simp [List.toByteArray.loop] | a::l, B => by simp [List.toByteArray.loop, to_ByteArray_loop_size] rw [Nat.add_succ, Nat.succ_add] /- A dependent ByteArray which guarantees the correct byte length. -/ def Blake3Hash : Type := { r : ByteArray // r.size = BLAKE3_OUT_LEN } deriving instance ToString for Blake3Hash instance : Inhabited Blake3Hash where default := ⟨(List.replicate BLAKE3_OUT_LEN 0).toByteArray, by simp⟩ constant HasherPointed : PointedType def Hasher : Type := HasherPointed.type instance : Inhabited Hasher := ⟨HasherPointed.val⟩ /- Perform an unsafe IO operation for use in a pure context. -/ unsafe def unsafeIO' [Inhabited α] (k : IO α) : α := match unsafeIO k with | Except.ok a => a | Except.error e => panic e.toString /- Version of the linked BLAKE3 implementation library. -/ @[extern "lean_blake3_version"] constant internalVersion : Unit → String constant version : String := internalVersion Unit.unit /- Initialize a hasher. -/ @[extern "lean_blake3_initialize"] constant initHasher : Unit → Hasher /- @[extern "blake3_hasher_init_keyed"] -/ /- constant initHasherKeyed (key: Array UInt8) : Hasher -/ /- @[extern "blake3_hasher_init_derive_key"] -/ /- constant initHasherDeriveKey (context: String) : Hasher -/ /- @[extern "blake3_hasher_init_derive_key_raw"] -/ /- constant initHasherDeriveKeyRaw (context: String) (contextLength : USize) : Hasher -/ /- Put more data into the hasher. This can be called several times. -/ /- @[implementedBy hasherUpdateImpl] -/ @[extern "lean_blake3_hasher_update"] constant hasherUpdate (hasher : Hasher) (input : ByteArray) (length : USize) : Hasher /- Finalize the hasher and write the output to an initialized array. -/ @[extern "lean_blake3_hasher_finalize"] constant hasherFinalize : (hasher : Hasher) → (length : USize) → ByteArray /- Finalize the hasher and write the output to an initialized array. -/ /- @[extern "blake3_hasher_finalize_seek"] -/ /- constant hasherFinalizeSeek : (hasher : Hasher) → (seek : UInt64) → (length : USize) → ByteArray -/ /- Hash a ByteArray -/ def hash (input : ByteArray) : Blake3Hash := let hasher := initHasher () let hasher := hasherUpdate hasher input (USize.ofNat input.size) let output := hasherFinalize hasher (USize.ofNat BLAKE3_OUT_LEN) if h : output.size = BLAKE3_OUT_LEN then ⟨output, h⟩ else panic! "Incorrect output size"
9c82a3ae759e73134223dd7968df78dd6ee4bfe6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/abelian/transfer.lean
5134e852c5df956f0f201418028cdd71a369fc08
[ "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,651
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.abelian.basic import category_theory.limits.preserves.shapes.kernels import category_theory.adjunction.limits /-! # Transferring "abelian-ness" across a functor > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. If `C` is an additive category, `D` is an abelian category, we have `F : C ⥤ D` `G : D ⥤ C` (both preserving zero morphisms), `G` is left exact (that is, preserves finite limits), and further we have `adj : G ⊣ F` and `i : F ⋙ G ≅ 𝟭 C`, then `C` is also abelian. See <https://stacks.math.columbia.edu/tag/03A3> ## Notes The hypotheses, following the statement from the Stacks project, may appear suprising: we don't ask that the counit of the adjunction is an isomorphism, but just that we have some potentially unrelated isomorphism `i : F ⋙ G ≅ 𝟭 C`. However Lemma A1.1.1 from [Elephant] shows that in this situation the counit itself must be an isomorphism, and thus that `C` is a reflective subcategory of `D`. Someone may like to formalize that lemma, and restate this theorem in terms of `reflective`. (That lemma has a nice string diagrammatic proof that holds in any bicategory.) -/ noncomputable theory namespace category_theory open category_theory.limits universes v u₁ u₂ namespace abelian_of_adjunction variables {C : Type u₁} [category.{v} C] [preadditive C] variables {D : Type u₂} [category.{v} D] [abelian D] variables (F : C ⥤ D) variables (G : D ⥤ C) [functor.preserves_zero_morphisms G] variables (i : F ⋙ G ≅ 𝟭 C) (adj : G ⊣ F) include i /-- No point making this an instance, as it requires `i`. -/ lemma has_kernels [preserves_finite_limits G] : has_kernels C := { has_limit := λ X Y f, begin have := nat_iso.naturality_1 i f, simp at this, rw ←this, haveI : has_kernel (G.map (F.map f) ≫ i.hom.app _) := limits.has_kernel_comp_mono _ _, apply limits.has_kernel_iso_comp, end } include adj /-- No point making this an instance, as it requires `i` and `adj`. -/ lemma has_cokernels : has_cokernels C := { has_colimit := λ X Y f, begin haveI : preserves_colimits G := adj.left_adjoint_preserves_colimits, have := nat_iso.naturality_1 i f, simp at this, rw ←this, haveI : has_cokernel (G.map (F.map f) ≫ i.hom.app _) := limits.has_cokernel_comp_iso _ _, apply limits.has_cokernel_epi_comp, end } variables [limits.has_cokernels C] /-- Auxiliary construction for `coimage_iso_image` -/ def cokernel_iso {X Y : C} (f : X ⟶ Y) : G.obj (cokernel (F.map f)) ≅ cokernel f := begin -- We have to write an explicit `preserves_colimits` type here, -- as `left_adjoint_preserves_colimits` has universe variables. haveI : preserves_colimits G := adj.left_adjoint_preserves_colimits, calc G.obj (cokernel (F.map f)) ≅ cokernel (G.map (F.map f)) : (as_iso (cokernel_comparison _ G)).symm ... ≅ cokernel (_ ≫ f ≫ _) : cokernel_iso_of_eq (nat_iso.naturality_2 i f).symm ... ≅ cokernel (f ≫ _) : cokernel_epi_comp _ _ ... ≅ cokernel f : cokernel_comp_is_iso _ _ end variables [limits.has_kernels C] [preserves_finite_limits G] /-- Auxiliary construction for `coimage_iso_image` -/ def coimage_iso_image_aux {X Y : C} (f : X ⟶ Y) : kernel (G.map (cokernel.π (F.map f))) ≅ kernel (cokernel.π f) := begin haveI : preserves_colimits G := adj.left_adjoint_preserves_colimits, calc kernel (G.map (cokernel.π (F.map f))) ≅ kernel (cokernel.π (G.map (F.map f)) ≫ cokernel_comparison (F.map f) G) : kernel_iso_of_eq (π_comp_cokernel_comparison _ _).symm ... ≅ kernel (cokernel.π (G.map (F.map f))) : kernel_comp_mono _ _ ... ≅ kernel (cokernel.π (_ ≫ f ≫ _) ≫ (cokernel_iso_of_eq _).hom) : kernel_iso_of_eq (π_comp_cokernel_iso_of_eq_hom (nat_iso.naturality_2 i f)).symm ... ≅ kernel (cokernel.π (_ ≫ f ≫ _)) : kernel_comp_mono _ _ ... ≅ kernel (cokernel.π (f ≫ i.inv.app Y) ≫ (cokernel_epi_comp (i.hom.app X) _).inv) : kernel_iso_of_eq (by simp only [cokernel.π_desc, cokernel_epi_comp_inv]) ... ≅ kernel (cokernel.π (f ≫ _)) : kernel_comp_mono _ _ ... ≅ kernel (inv (i.inv.app Y) ≫ cokernel.π f ≫ (cokernel_comp_is_iso f (i.inv.app Y)).inv) : kernel_iso_of_eq (by simp only [cokernel.π_desc, cokernel_comp_is_iso_inv, iso.hom_inv_id_app_assoc, nat_iso.inv_inv_app]) ... ≅ kernel (cokernel.π f ≫ _) : kernel_is_iso_comp _ _ ... ≅ kernel (cokernel.π f) : kernel_comp_mono _ _ end variables [functor.preserves_zero_morphisms F] /-- Auxiliary definition: the abelian coimage and abelian image agree. We still need to check that this agrees with the canonical morphism. -/ def coimage_iso_image {X Y : C} (f : X ⟶ Y) : abelian.coimage f ≅ abelian.image f := begin haveI : preserves_limits F := adj.right_adjoint_preserves_limits, haveI : preserves_colimits G := adj.left_adjoint_preserves_colimits, calc abelian.coimage f ≅ cokernel (kernel.ι f) : iso.refl _ ... ≅ G.obj (cokernel (F.map (kernel.ι f))) : (cokernel_iso _ _ i adj _).symm ... ≅ G.obj (cokernel (kernel_comparison f F ≫ (kernel.ι (F.map f)))) : G.map_iso (cokernel_iso_of_eq (by simp)) ... ≅ G.obj (cokernel (kernel.ι (F.map f))) : G.map_iso (cokernel_epi_comp _ _) ... ≅ G.obj (abelian.coimage (F.map f)) : iso.refl _ ... ≅ G.obj (abelian.image (F.map f)) : G.map_iso (abelian.coimage_iso_image _) ... ≅ G.obj (kernel (cokernel.π (F.map f))) : iso.refl _ ... ≅ kernel (G.map (cokernel.π (F.map f))) : preserves_kernel.iso _ _ ... ≅ kernel (cokernel.π f) : coimage_iso_image_aux F G i adj f ... ≅ abelian.image f : iso.refl _, end local attribute [simp] cokernel_iso coimage_iso_image coimage_iso_image_aux -- The account of this proof in the Stacks project omits this calculation. lemma coimage_iso_image_hom {X Y : C} (f : X ⟶ Y) : (coimage_iso_image F G i adj f).hom = abelian.coimage_image_comparison f := begin ext, simpa only [←G.map_comp_assoc, coimage_iso_image, nat_iso.inv_inv_app, cokernel_iso, coimage_iso_image_aux, iso.trans_symm, iso.symm_symm_eq, iso.refl_trans, iso.trans_refl, iso.trans_hom, iso.symm_hom, cokernel_comp_is_iso_inv, cokernel_epi_comp_inv, as_iso_hom, functor.map_iso_hom, cokernel_epi_comp_hom, preserves_kernel.iso_hom, kernel_comp_mono_hom, kernel_is_iso_comp_hom, cokernel_iso_of_eq_hom_comp_desc_assoc, cokernel.π_desc_assoc, category.assoc, π_comp_cokernel_iso_of_eq_inv_assoc, π_comp_cokernel_comparison_assoc, kernel.lift_ι, kernel.lift_ι_assoc, kernel_iso_of_eq_hom_comp_ι_assoc, kernel_comparison_comp_ι_assoc, abelian.coimage_image_factorisation] using nat_iso.naturality_1 i f end end abelian_of_adjunction open abelian_of_adjunction /-- If `C` is an additive category, `D` is an abelian category, we have `F : C ⥤ D` `G : D ⥤ C` (both preserving zero morphisms), `G` is left exact (that is, preserves finite limits), and further we have `adj : G ⊣ F` and `i : F ⋙ G ≅ 𝟭 C`, then `C` is also abelian. See <https://stacks.math.columbia.edu/tag/03A3> -/ def abelian_of_adjunction {C : Type u₁} [category.{v} C] [preadditive C] [has_finite_products C] {D : Type u₂} [category.{v} D] [abelian D] (F : C ⥤ D) [functor.preserves_zero_morphisms F] (G : D ⥤ C) [functor.preserves_zero_morphisms G] [preserves_finite_limits G] (i : F ⋙ G ≅ 𝟭 C) (adj : G ⊣ F) : abelian C := begin haveI := has_kernels F G i, haveI := has_cokernels F G i adj, haveI : ∀ {X Y : C} (f : X ⟶ Y), is_iso (abelian.coimage_image_comparison f), { intros X Y f, rw ←coimage_iso_image_hom F G i adj f, apply_instance, }, apply abelian.of_coimage_image_comparison_is_iso, end /-- If `C` is an additive category equivalent to an abelian category `D` via a functor that preserves zero morphisms, then `C` is also abelian. -/ def abelian_of_equivalence {C : Type u₁} [category.{v} C] [preadditive C] [has_finite_products C] {D : Type u₂} [category.{v} D] [abelian D] (F : C ⥤ D) [functor.preserves_zero_morphisms F] [is_equivalence F] : abelian C := abelian_of_adjunction F F.inv F.as_equivalence.unit_iso.symm F.as_equivalence.symm.to_adjunction end category_theory
712789268892913cc33fc0ea751399809c84e07c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/logic/nontrivial.lean
aca6f71a477fedaca718c3fe5a64990eab826a07
[]
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
8,173
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.pi import Mathlib.data.prod import Mathlib.logic.unique import Mathlib.logic.function.basic import Mathlib.PostPort universes u_3 l u_1 u_2 u_4 namespace Mathlib /-! # Nontrivial types A type is *nontrivial* if it contains at least two elements. This is useful in particular for rings (where it is equivalent to the fact that zero is different from one) and for vector spaces (where it is equivalent to the fact that the dimension is positive). We introduce a typeclass `nontrivial` formalizing this property. -/ /-- Predicate typeclass for expressing that a type is not reduced to a single element. In rings, this is equivalent to `0 ≠ 1`. In vector spaces, this is equivalent to positive dimension. -/ class nontrivial (α : Type u_3) where exists_pair_ne : ∃ (x : α), ∃ (y : α), x ≠ y theorem nontrivial_iff {α : Type u_1} : nontrivial α ↔ ∃ (x : α), ∃ (y : α), x ≠ y := { mp := fun (h : nontrivial α) => nontrivial.exists_pair_ne, mpr := fun (h : ∃ (x : α), ∃ (y : α), x ≠ y) => nontrivial.mk h } theorem exists_pair_ne (α : Type u_1) [nontrivial α] : ∃ (x : α), ∃ (y : α), x ≠ y := nontrivial.exists_pair_ne theorem exists_ne {α : Type u_1} [nontrivial α] (x : α) : ∃ (y : α), y ≠ x := sorry -- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`. theorem nontrivial_of_ne {α : Type u_1} (x : α) (y : α) (h : x ≠ y) : nontrivial α := nontrivial.mk (Exists.intro x (Exists.intro y h)) -- `x` and `y` are explicit here, as they are often needed to guide typechecking of `h`. theorem nontrivial_of_lt {α : Type u_1} [preorder α] (x : α) (y : α) (h : x < y) : nontrivial α := nontrivial.mk (Exists.intro x (Exists.intro y (ne_of_lt h))) protected instance nontrivial.to_nonempty {α : Type u_1} [nontrivial α] : Nonempty α := sorry /-- An inhabited type is either nontrivial, or has a unique element. -/ def nontrivial_psum_unique (α : Type u_1) [Inhabited α] : psum (nontrivial α) (unique α) := dite (nontrivial α) (fun (h : nontrivial α) => psum.inl h) fun (h : ¬nontrivial α) => psum.inr (unique.mk { default := Inhabited.default } sorry) theorem subsingleton_iff {α : Type u_1} : subsingleton α ↔ ∀ (x y : α), x = y := { mp := fun (h : subsingleton α) => subsingleton.elim, mpr := fun (h : ∀ (x y : α), x = y) => subsingleton.intro h } theorem not_nontrivial_iff_subsingleton {α : Type u_1} : ¬nontrivial α ↔ subsingleton α := sorry theorem not_subsingleton (α : Type u_1) [h : nontrivial α] : ¬subsingleton α := sorry /-- A type is either a subsingleton or nontrivial. -/ theorem subsingleton_or_nontrivial (α : Type u_1) : subsingleton α ∨ nontrivial α := eq.mpr (id (Eq._oldrec (Eq.refl (subsingleton α ∨ nontrivial α)) (Eq.symm (propext not_nontrivial_iff_subsingleton)))) (eq.mpr (id (Eq._oldrec (Eq.refl (¬nontrivial α ∨ nontrivial α)) (propext (or_comm (¬nontrivial α) (nontrivial α))))) (classical.em (nontrivial α))) theorem false_of_nontrivial_of_subsingleton (α : Type u_1) [nontrivial α] [subsingleton α] : False := sorry protected instance option.nontrivial {α : Type u_1} [Nonempty α] : nontrivial (Option α) := nonempty.elim_to_inhabited fun (inst : Inhabited α) => nontrivial.mk (Exists.intro none (Exists.intro (some Inhabited.default) (id (id fun (ᾰ : none = some Inhabited.default) => option.no_confusion ᾰ)))) /-- Pushforward a `nontrivial` instance along an injective function. -/ protected theorem function.injective.nontrivial {α : Type u_1} {β : Type u_2} [nontrivial α] {f : α → β} (hf : function.injective f) : nontrivial β := sorry /-- Pullback a `nontrivial` instance along a surjective function. -/ protected theorem function.surjective.nontrivial {α : Type u_1} {β : Type u_2} [nontrivial β] {f : α → β} (hf : function.surjective f) : nontrivial α := sorry /-- An injective function from a nontrivial type has an argument at which it does not take a given value. -/ protected theorem function.injective.exists_ne {α : Type u_1} {β : Type u_2} [nontrivial α] {f : α → β} (hf : function.injective f) (y : β) : ∃ (x : α), f x ≠ y := sorry protected instance nontrivial_prod_right {α : Type u_1} {β : Type u_2} [Nonempty α] [nontrivial β] : nontrivial (α × β) := function.surjective.nontrivial prod.snd_surjective protected instance nontrivial_prod_left {α : Type u_1} {β : Type u_2} [nontrivial α] [Nonempty β] : nontrivial (α × β) := function.surjective.nontrivial prod.fst_surjective namespace pi /-- A pi type is nontrivial if it's nonempty everywhere and nontrivial somewhere. -/ theorem nontrivial_at {I : Type u_3} {f : I → Type u_4} (i' : I) [inst : ∀ (i : I), Nonempty (f i)] [nontrivial (f i')] : nontrivial ((i : I) → f i) := function.injective.nontrivial (function.update_injective (fun (i : I) => Classical.choice (inst i)) i') /-- As a convenience, provide an instance automatically if `(f (default I))` is nontrivial. If a different index has the non-trivial type, then use `haveI := nontrivial_at that_index`. -/ protected instance nontrivial {I : Type u_3} {f : I → Type u_4} [Inhabited I] [inst : ∀ (i : I), Nonempty (f i)] [nontrivial (f Inhabited.default)] : nontrivial ((i : I) → f i) := nontrivial_at Inhabited.default end pi protected instance function.nontrivial {α : Type u_1} {β : Type u_2} [h : Nonempty α] [nontrivial β] : nontrivial (α → β) := nonempty.elim h fun (a : α) => pi.nontrivial_at a protected theorem subsingleton.le {α : Type u_1} [preorder α] [subsingleton α] (x : α) (y : α) : x ≤ y := le_of_eq (subsingleton.elim x y) namespace tactic /-- Tries to generate a `nontrivial α` instance by performing case analysis on `subsingleton_or_nontrivial α`, attempting to discharge the subsingleton branch using lemmas with `@[nontriviality]` attribute, including `subsingleton.le` and `eq_iff_true_of_subsingleton`. -/ /-- Tries to generate a `nontrivial α` instance using `nontrivial_of_ne` or `nontrivial_of_lt` and local hypotheses. -/ end tactic namespace tactic.interactive /-- Attempts to generate a `nontrivial α` hypothesis. The tactic first looks for an instance using `apply_instance`. If the goal is an (in)equality, the type `α` is inferred from the goal. Otherwise, the type needs to be specified in the tactic invocation, as `nontriviality α`. The `nontriviality` tactic will first look for strict inequalities amongst the hypotheses, and use these to derive the `nontrivial` instance directly. Otherwise, it will perform a case split on `subsingleton α ∨ nontrivial α`, and attempt to discharge the `subsingleton` goal using `simp [lemmas] with nontriviality`, where `[lemmas]` is a list of additional `simp` lemmas that can be passed to `nontriviality` using the syntax `nontriviality α using [lemmas]`. ``` example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 0 < a := begin nontriviality, -- There is now a `nontrivial R` hypothesis available. assumption, end ``` ``` example {R : Type} [comm_ring R] {r s : R} : r * s = s * r := begin nontriviality, -- There is now a `nontrivial R` hypothesis available. apply mul_comm, end ``` ``` example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : (2 : ℕ) ∣ 4 := begin nontriviality R, -- there is now a `nontrivial R` hypothesis available. dec_trivial end ``` ``` def myeq {α : Type} (a b : α) : Prop := a = b example {α : Type} (a b : α) (h : a = b) : myeq a b := begin success_if_fail { nontriviality α }, -- Fails nontriviality α using [myeq], -- There is now a `nontrivial α` hypothesis available assumption end ``` -/ end tactic.interactive namespace bool protected instance nontrivial : nontrivial Bool := nontrivial.mk (Exists.intro tt (Exists.intro false tt_eq_ff_eq_false))
8b1268251217ea1f53ebdeb725772e5f2e3193a5
78630e908e9624a892e24ebdd21260720d29cf55
/src/logic_propositional/prop_09.lean
e32eea1e4c7d47cef55a4070e2aa294b90813452
[ "CC0-1.0" ]
permissive
tomasz-lisowski/lean-logic-examples
84e612466776be0a16c23a0439ff8ef6114ddbe1
2b2ccd467b49c3989bf6c92ec0358a8d6ee68c5d
refs/heads/master
1,683,334,199,431
1,621,938,305,000
1,621,938,305,000
365,041,573
1
0
null
null
null
null
UTF-8
Lean
false
false
271
lean
namespace prop_09 variables X Y : Prop theorem prop_9 (h : X ∨ Y) (h2 : ¬ X) : Y := show Y, from or.elim h (assume h1: X, show Y, from (classical.by_contradiction (assume Y, h2 h1))) (assume h3: Y, h3) -- end namespace end prop_09
5b278f81a63862407e3e2f67901781b1bc3c3bef
91b8df3b248df89472cc0b753fbe2bac750aefea
/experiments/lean/src/ddl/host/basic.lean
cb2b23009bb26f1cdf31014f3e5c66d8d154077c
[ "Apache-2.0", "LicenseRef-scancode-unknown-license-reference" ]
permissive
yeslogic/fathom
eabe5c4112d3b4d5ec9096a57bb502254ddbdf15
3960a9466150d392c2cb103c5cb5fcffa0200814
refs/heads/main
1,685,349,769,736
1,675,998,621,000
1,675,998,621,000
28,993,871
214
11
Apache-2.0
1,694,044,276,000
1,420,764,938,000
Rust
UTF-8
Lean
false
false
1,253
lean
import ddl.basic namespace ddl.host /- The type syntax of the host language -/ inductive type (ℓ : Type) : Type | bool {} : type | nat {} : type | union_nil {} : type | union_cons : ℓ → type → type → type | struct_nil {} : type | struct_cons : ℓ → type → type → type | array {} : type → type namespace type variables {ℓ : Type} def lookup (l : ℓ) [decidable_eq ℓ] : type ℓ → option (type ℓ) | (struct_cons l' t tr) := if l = l' then some t else lookup tr | _ := none end type /- Binary operators -/ inductive binop : Type | add | mul /- The expression syntax of the host language -/ inductive expr (ℓ : Type) : Type | bool {} : bool → expr | nat {} : ℕ → expr | app_binop {} : binop → expr → expr → expr | proj : expr → ℓ → expr instance has_coe_to_bool {ℓ} : has_coe bool (expr ℓ) := ⟨expr.bool⟩ instance has_coe_to_nat {ℓ} : has_coe ℕ (expr ℓ) := ⟨expr.nat⟩ namespace expr variables {ℓ : Type} instance : has_add (expr ℓ) := ⟨app_binop binop.add⟩ instance : has_mul (expr ℓ) := ⟨app_binop binop.mul⟩ end expr end ddl.host
0f75c9fc5aab41dd4e87e70d93a62fdcaba659f2
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/types/lift.hlean
147df4507029f3b26fa41b3b918ab5068dee1f6b
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
4,888
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about lift -/ import ..function open eq equiv equiv.ops is_equiv is_trunc namespace lift universe variables u v variables {A : Type.{u}} (z z' : lift.{u v} A) protected definition eta : up (down z) = z := by induction z; reflexivity protected definition code [unfold 2 3] : lift A → lift A → Type | code (up a) (up a') := a = a' protected definition decode [unfold 2 3] : Π(z z' : lift A), lift.code z z' → z = z' | decode (up a) (up a') := λc, ap up c variables {z z'} protected definition encode [unfold 3 4 5] (p : z = z') : lift.code z z' := by induction p; induction z; esimp variables (z z') definition lift_eq_equiv : (z = z') ≃ lift.code z z' := equiv.MK lift.encode !lift.decode abstract begin intro c, induction z with a, induction z' with a', esimp at *, induction c, reflexivity end end abstract begin intro p, induction p, induction z, reflexivity end end section variables {a a' : A} definition eq_of_up_eq_up [unfold 4] (p : up a = up a') : a = a' := lift.encode p definition lift_transport {P : A → Type} (p : a = a') (z : lift (P a)) : p ▸ z = up (p ▸ down z) := by induction p; induction z; reflexivity end variables {A' : Type} (f : A → A') (g : lift A → lift A') definition lift_functor [unfold 4] : lift A → lift A' | lift_functor (up a) := up (f a) definition is_equiv_lift_functor [constructor] [Hf : is_equiv f] : is_equiv (lift_functor f) := adjointify (lift_functor f) (lift_functor f⁻¹) abstract begin intro z', induction z' with a', esimp, exact ap up !right_inv end end abstract begin intro z, induction z with a, esimp, exact ap up !left_inv end end definition lift_equiv_lift_of_is_equiv [constructor] [Hf : is_equiv f] : lift A ≃ lift A' := equiv.mk _ (is_equiv_lift_functor f) definition lift_equiv_lift [constructor] (f : A ≃ A') : lift A ≃ lift A' := equiv.mk _ (is_equiv_lift_functor f) definition lift_equiv_lift_refl (A : Type) : lift_equiv_lift (erfl : A ≃ A) = erfl := by apply equiv_eq'; intro z; induction z with a; reflexivity definition lift_inv_functor [unfold-full] (a : A) : A' := down (g (up a)) definition is_equiv_lift_inv_functor [constructor] [Hf : is_equiv g] : is_equiv (lift_inv_functor g) := adjointify (lift_inv_functor g) (lift_inv_functor g⁻¹) abstract begin intro z', rewrite [▸*,lift.eta,right_inv g], end end abstract begin intro z', rewrite [▸*,lift.eta,left_inv g], end end definition equiv_of_lift_equiv_lift [constructor] (g : lift A ≃ lift A') : A ≃ A' := equiv.mk _ (is_equiv_lift_inv_functor g) definition lift_functor_left_inv : lift_inv_functor (lift_functor f) = f := eq_of_homotopy (λa, idp) definition lift_functor_right_inv : lift_functor (lift_inv_functor g) = g := begin apply eq_of_homotopy, intro z, induction z with a, esimp, apply lift.eta end variables (A A') definition is_equiv_lift_functor_fn [constructor] : is_equiv (lift_functor : (A → A') → (lift A → lift A')) := adjointify lift_functor lift_inv_functor lift_functor_right_inv lift_functor_left_inv definition lift_imp_lift_equiv [constructor] : (lift A → lift A') ≃ (A → A') := (equiv.mk _ (is_equiv_lift_functor_fn A A'))⁻¹ᵉ -- can we deduce this from lift_imp_lift_equiv? definition lift_equiv_lift_equiv [constructor] : (lift A ≃ lift A') ≃ (A ≃ A') := equiv.MK equiv_of_lift_equiv_lift lift_equiv_lift abstract begin intro f, apply equiv_eq, reflexivity end end abstract begin intro g, apply equiv_eq, esimp, apply eq_of_homotopy, intro z, induction z with a, esimp, apply lift.eta end end definition lift_eq_lift_equiv.{u1 u2} (A A' : Type.{u1}) : (lift.{u1 u2} A = lift.{u1 u2} A') ≃ (A = A') := !eq_equiv_equiv ⬝e !lift_equiv_lift_equiv ⬝e !eq_equiv_equiv⁻¹ᵉ definition is_embedding_lift [instance] : is_embedding lift := begin apply is_embedding.mk, intro A A', fapply is_equiv.homotopy_closed, exact to_inv !lift_eq_lift_equiv, exact _, { intro p, induction p, esimp [lift_eq_lift_equiv,equiv.trans,equiv.symm,eq_equiv_equiv], rewrite [equiv_of_eq_refl,lift_equiv_lift_refl], apply ua_refl} end -- is_trunc_lift is defined in init.trunc end lift
6eb1bcf2d01ed728494240ac7927f65ee11cf791
367134ba5a65885e863bdc4507601606690974c1
/src/group_theory/monoid_localization.lean
62272387ffb01ad53a1dbeba2c00c2248022d15d
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
54,941
lean
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import group_theory.congruence import group_theory.submonoid import algebra.group.units import algebra.punit_instances /-! # Localizations of commutative monoids Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so we can generalize localizations to commutative monoids. We characterize the localization of a commutative monoid `M` at a submonoid `S` up to isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a monoid homomorphism `f : M →* N` satisfying 3 properties: 1. For all `y ∈ S`, `f y` is a unit; 2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`; 3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`. Given such a localization map `f : M →* N`, we can define the surjection `localization_map.mk'` sending `(x, y) : M × S` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `N` induced by a homomorphism from `M` which maps elements of `S` to invertible elements of the codomain. Similarly, given commutative monoids `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : M →* P` such that `g(S) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `N` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We also define the quotient of `M × S` by the unique congruence relation (equivalence relation preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S` satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s` whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard localization relation. This defines the localization as a quotient type, `localization`, but the majority of subsequent lemmas in the file are given in terms of localizations up to isomorphism, using maps which satisfy the characteristic predicate. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. The infimum form of the localization congruence relation is chosen as 'canonical' here, since it shortens some proofs. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_monoid_of_mk'` and associated lemmas. These show the quotient map `mk : M → S → localization S` equals the surjection `localization_map.mk'` induced by the map `monoid_of : localization_map S (localization S)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_monoid_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. ## Tags localization, monoid localization, quotient monoid, congruence relation, characteristic predicate, commutative monoid -/ set_option old_structure_cmd true namespace add_submonoid variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N] /-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends add_monoid_hom M N := (map_add_units' : ∀ y : S, is_add_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c) /-- The add_monoid hom underlying a `localization_map` of `add_comm_monoid`s. -/ add_decl_doc localization_map.to_add_monoid_hom end add_submonoid variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N] {P : Type*} [comm_monoid P] namespace submonoid /-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends monoid_hom M N := (map_units' : ∀ y : S, is_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c) attribute [to_additive add_submonoid.localization_map] submonoid.localization_map attribute [to_additive add_submonoid.localization_map.to_add_monoid_hom] submonoid.localization_map.to_monoid_hom /-- The monoid hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_monoid_hom end submonoid namespace localization run_cmd to_additive.map_namespace `localization `add_localization /-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/ @[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S` an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`."] def r (S : submonoid M) : con (M × S) := Inf {c | ∀ y : S, c 1 (y, y)} /-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`. -/ @[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`."] def r' : con (M × S) := begin refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c, iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩, .. }, { rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use b.2 * t₁ * t₂, simp only [submonoid.coe_mul], calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl ... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl } ... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } }, { rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use t₁ * t₂, calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) : by ac_refl ... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } } end /-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `localization.r`) or explicitly (see `localization.r'`). -/ @[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `add_localization.r`) or explicitly (see `add_localization.r'`)."] theorem r_eq_r' : r S = r' S := le_antisymm (Inf_le $ λ _, ⟨1, by simp⟩) $ le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩, begin rw [← mul_one (p, q), ← mul_one y], refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _, convert b.symm (b.mul (b.refl y) (H (q * t))) using 1, rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc], refl end variables {S} @[to_additive] lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c := by rw r_eq_r' S; refl end localization /-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/ @[to_additive add_localization "The localization of an `add_comm_monoid` at one of its submonoids (as a quotient type)."] def localization := (localization.r S).quotient namespace localization @[to_additive] instance inhabited : inhabited (localization S) := con.quotient.inhabited @[to_additive] instance : comm_monoid (localization S) := (r S).comm_monoid variables {S} /-- Given a `comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence class of `(x, y)` in the localization of `M` at `S`. -/ @[to_additive "Given an `add_comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence class of `(x, y)` in the localization of `M` at `S`."] def mk (x : M) (y : S) : localization S := (r S).mk' (x, y) @[elab_as_eliminator, to_additive] theorem ind {p : localization S → Prop} (H : ∀ (y : M × S), p (mk y.1 y.2)) (x) : p x := by rcases x; convert H x; exact prod.mk.eta.symm @[elab_as_eliminator, to_additive] theorem induction_on {p : localization S → Prop} (x) (H : ∀ (y : M × S), p (mk y.1 y.2)) : p x := ind H x @[elab_as_eliminator, to_additive] theorem induction_on₂ {p : localization S → localization S → Prop} (x y) (H : ∀ (x y : M × S), p (mk x.1 x.2) (mk y.1 y.2)) : p x y := induction_on x $ λ x, induction_on y $ H x @[elab_as_eliminator, to_additive] theorem induction_on₃ {p : localization S → localization S → localization S → Prop} (x y z) (H : ∀ (x y z : M × S), p (mk x.1 x.2) (mk y.1 y.2) (mk z.1 z.2)) : p x y z := induction_on₂ x y $ λ x y, induction_on z $ H x y @[to_additive] lemma one_rel (y : S) : r S 1 (y, y) := λ b hb, hb y @[to_additive] theorem r_of_eq {x y : M × S} (h : y.1 * x.2 = x.1 * y.2) : r S x y := r_iff_exists.2 ⟨1, by rw h⟩ end localization variables {S N} namespace monoid_hom /-- Makes a localization map from a `comm_monoid` hom satisfying the characteristic predicate. -/ @[to_additive "Makes a localization map from an `add_comm_monoid` hom satisfying the characteristic predicate."] def to_localization_map (f : M →* N) (H1 : ∀ y : S, is_unit (f y)) (H2 : ∀ z, ∃ x : M × S, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : S, x * c = y * c) : submonoid.localization_map S N := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end monoid_hom namespace submonoid namespace localization_map /-- Short for `to_monoid_hom`; used to apply a localization map as a function. -/ @[to_additive "Short for `to_add_monoid_hom`; used to apply a localization map as a function."] abbreviation to_map (f : localization_map S N) := f.to_monoid_hom @[to_additive, ext] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) : f = g := by cases f; cases g; simp only; exact funext h attribute [ext] add_submonoid.localization_map.ext @[to_additive] lemma ext_iff {f g : localization_map S N} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ @[to_additive] lemma to_map_injective : function.injective (@localization_map.to_map _ _ S N _) := λ _ _ h, ext $ monoid_hom.ext_iff.1 h @[to_additive] lemma map_units (f : localization_map S N) (y : S) : is_unit (f.to_map y) := f.4 y @[to_additive] lemma surj (f : localization_map S N) (z : N) : ∃ x : M × S, z * f.to_map x.2 = f.to_map x.1 := f.5 z @[to_additive] lemma eq_iff_exists (f : localization_map S N) {x y} : f.to_map x = f.to_map y ↔ ∃ c : S, x * c = y * c := f.6 x y /-- Given a localization map `f : M →* N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x * (f y)⁻¹ = z`. -/ @[to_additive "Given a localization map `f : M →+ N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x - f y = z`."] noncomputable def sec (f : localization_map S N) (z : N) : M × S := classical.some $ f.surj z @[to_additive] lemma sec_spec {f : localization_map S N} (z : N) : z * f.to_map (f.sec z).2 = f.to_map (f.sec z).1 := classical.some_spec $ f.surj z @[to_additive] lemma sec_spec' {f : localization_map S N} (z : N) : f.to_map (f.sec z).1 = f.to_map (f.sec z).2 * z := by rw [mul_comm, sec_spec] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `w * (f y)⁻¹ = z ↔ w = f y * z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `w - f y = z ↔ w = f y + z`."] lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z ↔ w = f y * z := by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) h _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w * (f y)⁻¹ ↔ z * f y = w`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w - f y ↔ z + f y = w`."] lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : z = w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ ↔ z * f y = w := by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ * (f y₁)⁻¹ = f x₂ * (f y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁)`. -/ @[simp, to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ - f y₁ = f x₂ - f y₂ ↔ f (x₁ + y₂) = f (x₂ + y₁)`."] lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} : f x₁ * ↑(is_unit.lift_right (f.mrestrict S) h y₁)⁻¹ = f x₂ * ↑(is_unit.lift_right (f.mrestrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) := by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂, f.map_mul, f.map_mul] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y, z ∈ S`, we have `(f y)⁻¹ = (f z)⁻¹ → f y = f z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y, z ∈ S`, we have `- (f y) = - (f z) → f y = f z`."] lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z} (h : (is_unit.lift_right (f.mrestrict S) hf y)⁻¹ = (is_unit.lift_right (f.mrestrict S) hf z)⁻¹) : f y = f z := by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h]; convert units.inv_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) hf _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y ∈ S`, `(f y)⁻¹` is unique. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y ∈ S`, `- (f y)` is unique."] lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S} {z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z := by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H] variables (f : localization_map S N) @[to_additive] lemma map_right_cancel {x y} {c : S} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := begin rw [f.to_map.map_mul, f.to_map.map_mul] at h, cases f.map_units c with u hu, rw ←hu at h, exact (units.mul_right_inj u).1 h, end @[to_additive] lemma map_left_cancel {x y} {c : S} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.map_right_cancel $ by rw [mul_comm _ x, mul_comm _ y, h] /-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to `f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S` to `f x - f y`."] noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N := f.to_map x * ↑(is_unit.lift_right (f.to_map.mrestrict S) f.map_units y)⁻¹ @[to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := (mul_inv_left f.map_units _ _ _).2 $ show _ = _ * (_ * _ * (_ * _)), by rw [←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, mul_assoc, mul_assoc, mul_comm _ (f.to_map x₂), ←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, submonoid.coe_mul, f.to_map.map_mul, f.to_map.map_mul]; ac_refl @[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.to_map x := by rw [mk', monoid_hom.map_one]; exact mul_one _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z * f y = f x`, then `f x * (f y)⁻¹ = z`. -/ @[simp, to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z + f y = f x`, then `f x - f y = z`."] lemma mk'_sec (z : N) : f.mk' (f.sec z).1 (f.sec z).2 = z := show _ * _ = _, by rw [←sec_spec, mul_inv_left, mul_comm] @[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z := ⟨(f.sec z).1, (f.sec z).2, f.mk'_sec z⟩ @[to_additive] lemma mk'_spec (x) (y : S) : f.mk' x y * f.to_map y = f.to_map x := show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.to_map y), ←mul_assoc, mul_inv_left, mul_comm] @[to_additive] lemma mk'_spec' (x) (y : S) : f.to_map y * f.mk' x y = f.to_map x := by rw [mul_comm, mk'_spec] @[to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := ⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩ @[to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm] @[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := ⟨λ H, by rw [f.to_map.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc, mul_comm (f.to_map _), ←mul_assoc, mk'_spec, f.to_map.map_mul], λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.to_map y₁), ←mul_assoc, ←f.to_map.map_mul, ←H, f.to_map.map_mul, mul_inv_right f.map_units]⟩ @[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c := f.mk'_eq_iff_eq.trans $ f.eq_iff_exists @[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) := by rw [f.eq, localization.r_iff_exists] @[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.eq_iff_exists.trans g.eq_iff_exists.symm @[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.eq'.trans g.eq'.symm /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `f x₁ * (f y₁)⁻¹ * f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ * y₂ * c = x₂ * y₁ * c`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `(f x₁ - f y₁) + f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ + y₂ + c = x₂ + y₁ + c`."] lemma exists_of_sec_mk' (x) (y : S) : ∃ c : S, x * (f.sec $ f.mk' x y).2 * c = (f.sec $ f.mk' x y).1 * y * c := f.eq_iff_exists.1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm @[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.mk'_eq_iff_eq.2 $ H ▸ rfl @[simp, to_additive] lemma mk'_self' (y : S) : f.mk' (y : M) y = 1 := show _ * _ = _, by rw [mul_inv_left, mul_one] @[simp, to_additive] lemma mk'_self (x) (H : x ∈ S) : f.mk' x ⟨x, H⟩ = 1 := by convert mk'_self' _ _; refl @[to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) : f.to_map x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y := by rw [←mk'_one, ←mk'_mul, one_mul] @[to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) : f.mk' x₂ y * f.to_map x₁ = f.mk' (x₁ * x₂) y := by rw [mul_comm, mul_mk'_eq_mk'_of_mul] @[to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) : f.to_map x * f.mk' 1 y = f.mk' x y := by rw [mul_mk'_eq_mk'_of_mul, mul_one] @[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) : f.mk' (x * y) y = f.to_map x := by rw [←mul_mk'_one_eq_mk', f.to_map.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self', mul_one] @[to_additive] lemma mk'_mul_cancel_left (x) (y : S) : f.mk' ((y : M) * x) y = f.to_map x := by rw [mul_comm, mk'_mul_cancel_right] @[to_additive] lemma is_unit_comp (j : N →* P) (y : S) : is_unit (j.comp f.to_map y) := ⟨units.map j $ is_unit.lift_right (f.to_map.mrestrict S) f.map_units y, show j _ = j _, from congr_arg j $ (is_unit.coe_lift_right (f.to_map.mrestrict S) f.map_units _)⟩ variables {g : M →* P} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g(S) ⊆ units P`, `f x = f y → g x = g y` for all `x y : M`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g(S) ⊆ add_units P`, `f x = f y → g x = g y` for all `x y : M`."] lemma eq_of_eq (hg : ∀ y : S, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := begin obtain ⟨c, hc⟩ := f.eq_iff_exists.1 h, rw [←mul_one (g x), ←is_unit.mul_lift_right_inv (g.mrestrict S) hg c], show _ * (g c * _) = _, rw [←mul_assoc, ←g.map_mul, hc, mul_inv_left hg, g.map_mul, mul_comm], end /-- Given `comm_monoid`s `M, P`, localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, and `g : M →* P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`. -/ @[to_additive "Given `add_comm_monoid`s `M, P`, localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, and `g : M →+ P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`."] lemma comp_eq_of_eq {T : submonoid P} {Q : Type*} [comm_monoid Q] (hg : ∀ y : S, g y ∈ T) (k : localization_map T Q) {x y} (h : f.to_map x = f.to_map y) : k.to_map (g x) = k.to_map (g y) := f.eq_of_eq (λ y : S, show is_unit (k.to_map.comp g y), from k.map_units ⟨g y, hg y⟩) h variables (hg : ∀ y : S, is_unit (g y)) /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x - g y`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def lift : N →* P := { to_fun := λ z, g (f.sec z).1 * ↑(is_unit.lift_right (g.mrestrict S) hg (f.sec z).2)⁻¹, map_one' := by rw [mul_inv_left, mul_one]; exact f.eq_of_eq hg (by rw [←sec_spec, one_mul]), map_mul' := λ x y, begin rw [mul_inv_left hg, ←mul_assoc, ←mul_assoc, mul_inv_right hg, mul_comm _ (g (f.sec y).1), ←mul_assoc, ←mul_assoc, mul_inv_right hg], repeat { rw ←g.map_mul }, exact f.eq_of_eq hg (by repeat { rw f.to_map.map_mul <|> rw sec_spec' }; ac_refl) end } variables {S g} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : M, y ∈ S`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x - f y` to `g x - g y` for all `x : M, y ∈ S`."] lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.mrestrict S) hg y)⁻¹ := (mul_inv hg).2 $ f.eq_of_eq hg $ by rw [f.to_map.map_mul, f.to_map.map_mul, sec_spec', mul_assoc, f.mk'_spec, mul_comm] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec (z v) : f.lift hg z = v ↔ g (f.sec z).1 = g (f.sec z).2 * v := mul_inv_left hg _ _ v /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v w : P`, we have `f.lift hg z * w = v ↔ g x * w = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v w : P`, we have `f.lift hg z + w = v ↔ g x + w = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec_mul (z w v) : f.lift hg z * w = v ↔ g (f.sec z).1 * w = g (f.sec z).2 * v := begin rw mul_comm, show _ * (_ * _) = _ ↔ _, rw [←mul_assoc, mul_inv_left hg, mul_comm], end @[to_additive] lemma lift_mk'_spec (x v) (y : S) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := by rw f.lift_mk' hg; exact mul_inv_left hg _ _ _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `f.lift hg z * g y = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `f.lift hg z + g y = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_right (z) : f.lift hg z * g (f.sec z).2 = g (f.sec z).1 := show _ * _ * _ = _, by erw [mul_assoc, is_unit.lift_right_inv_mul, mul_one] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `g y * f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `g y + f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_left (z) : g (f.sec z).2 * f.lift hg z = g (f.sec z).1 := by rw [mul_comm, lift_mul_right] @[simp, to_additive] lemma lift_eq (x : M) : f.lift hg (f.to_map x) = g x := by rw [lift_spec, ←g.map_mul]; exact f.eq_of_eq hg (by rw [sec_spec', f.to_map.map_mul]) @[to_additive] lemma lift_eq_iff {x y : M × S} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := by rw [lift_mk', lift_mk', mul_inv hg] @[simp, to_additive] lemma lift_comp : (f.lift hg).comp f.to_map = g := by ext; exact f.lift_eq hg _ @[simp, to_additive] lemma lift_of_comp (j : N →* P) : f.lift (f.is_unit_comp j) = j := begin ext, rw lift_spec, show j _ = j _ * _, erw [←j.map_mul, sec_spec'], end @[to_additive] lemma epic_of_localization_map {j k : N →* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := begin rw [←f.lift_of_comp j, ←f.lift_of_comp k], congr' 1 with x, exact h x, end @[to_additive] lemma lift_unique {j : N →* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := begin ext, rw [lift_spec, ←hj, ←hj, ←j.map_mul], apply congr_arg, rw ←sec_spec', end @[simp, to_additive] lemma lift_id (x) : f.lift f.map_units x = x := monoid_hom.ext_iff.1 (f.lift_of_comp $ monoid_hom.id N) x /-- Given two localization maps `f : M →* N, k : M →* P` for a submonoid `S ⊆ M`, the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P` induced by `k`. -/ @[simp, to_additive] lemma lift_left_inverse {k : localization_map S P} (z : N) : k.lift f.map_units (f.lift k.map_units z) = z := begin rw lift_spec, cases f.surj z with x hx, conv_rhs {congr, skip, rw f.eq_mk'_iff_mul_eq.2 hx}, rw [mk', ←mul_assoc, mul_inv_right f.map_units, ←f.to_map.map_mul, ←f.to_map.map_mul], apply k.eq_of_eq f.map_units, rw [k.to_map.map_mul, k.to_map.map_mul, ←sec_spec, mul_assoc, lift_spec_mul], repeat { rw ←k.to_map.map_mul }, apply f.eq_of_eq k.map_units, repeat { rw f.to_map.map_mul }, rw [sec_spec', ←hx], ac_refl, end @[to_additive] lemma lift_surjective_iff : function.surjective (f.lift hg) ↔ ∀ v : P, ∃ x : M × S, v * g x.2 = g x.1 := begin split, { intros H v, obtain ⟨z, hz⟩ := H v, obtain ⟨x, hx⟩ := f.surj z, use x, rw [←hz, f.eq_mk'_iff_mul_eq.2 hx, lift_mk', mul_assoc, mul_comm _ (g ↑x.2)], erw [is_unit.mul_lift_right_inv (g.mrestrict S) hg, mul_one] }, { intros H v, obtain ⟨x, hx⟩ := H v, use f.mk' x.1 x.2, rw [lift_mk', mul_inv_left hg, mul_comm, ←hx] } end @[to_additive] lemma lift_injective_iff : function.injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := begin split, { intros H x y, split, { exact f.eq_of_eq hg }, { intro h, rw [←f.lift_eq hg, ←f.lift_eq hg] at h, exact H h }}, { intros H z w h, obtain ⟨x, hx⟩ := f.surj z, obtain ⟨y, hy⟩ := f.surj w, rw [←f.mk'_sec z, ←f.mk'_sec w], exact (mul_inv f.map_units).2 ((H _ _).2 $ (mul_inv hg).1 h) } end variables {T : submonoid P} (hy : ∀ y : S, g y ∈ T) {Q : Type*} [comm_monoid Q] (k : localization_map T Q) /-- Given a `comm_monoid` homomorphism `g : M →* P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →* N` and `k : P →* Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a `add_comm_monoid` homomorphism `g : M →+ P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced add_monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →+ N` and `k : P →+ Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) - k (g y)`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def map : N →* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} @[to_additive] lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp, to_additive] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ @[to_additive] lemma map_mk' (x) (y : S) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := begin rw [map, lift_mk', mul_inv_left], { show k.to_map (g x) = k.to_map (g y) * _, rw mul_mk'_eq_mk'_of_mul, exact (k.mk'_mul_cancel_left (g x) ⟨(g y), hy y⟩).symm }, end /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) * u` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) + u` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_spec (z u) : f.map hy k z = u ↔ k.to_map (g (f.sec z).1) = k.to_map (g (f.sec z).2) * u := f.lift_spec (λ y, k.map_units ⟨g y, hy y⟩) _ _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `f.map hy k z * k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `f.map hy k z + k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_right (z) : f.map hy k z * (k.to_map (g (f.sec z).2)) = k.to_map (g (f.sec z).1) := f.lift_mul_right (λ y, k.map_units ⟨g y, hy y⟩) _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `k (g y) * f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `k (g y) + f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_left (z) : k.to_map (g (f.sec z).2) * f.map hy k z = k.to_map (g (f.sec z).1) := by rw [mul_comm, f.map_mul_right] @[simp, to_additive] lemma map_id (z : N) : f.map (λ y, show monoid_hom.id M y ∈ S, from y.2) f z = z := f.lift_id z /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_comp_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := begin ext z, show j.to_map _ * _ = j.to_map (l _) * _, { rw [mul_inv_left, ←mul_assoc, mul_inv_right], show j.to_map _ * j.to_map (l (g _)) = j.to_map (l _) * _, rw [←j.to_map.map_mul, ←j.to_map.map_mul, ←l.map_mul, ←l.map_mul], exact k.comp_eq_of_eq hl j (by rw [k.to_map.map_mul, k.to_map.map_mul, sec_spec', mul_assoc, map_mul_right]) }, end /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl section away_map variables (x : M) /-- Given `x : M`, the type of `comm_monoid` homomorphisms `f : M →* N` such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`. -/ @[reducible, to_additive "Given `x : M`, the type of `add_comm_monoid` homomorphisms `f : M →+ N` such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`."] def away_map (N' : Type*) [comm_monoid N'] := localization_map (powers x) N' variables (F : away_map x N) /-- Given `x : M` and a localization map `F : M →* N` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def away_map.inv_self : N := F.mk' 1 ⟨x, mem_powers _⟩ /-- Given `x : M`, a localization map `F : M →* N` away from `x`, and a map of `comm_monoid`s `g : M →* P` such that `g x` is invertible, the homomorphism induced from `N` to `P` sending `z : N` to `g y * (g x)⁻ⁿ`, where `y : M, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def away_map.lift (hg : is_unit (g x)) : N →* P := F.lift $ λ y, show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.map (monoid_hom.of $ ((^ n) : P → P)) hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : M) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : M` and localization maps `F : M →* N, G : M →* P` away from `x` and `x * y` respectively, the homomorphism induced from `N` to `P`. -/ noncomputable def away_to_away_right (y : M) (G : away_map (x * y) P) : N →* P := F.lift x $ show is_unit (G.to_map x), from is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away_map end localization_map end submonoid namespace add_submonoid namespace localization_map section away_map variables {A : Type*} [add_comm_monoid A] (x : A) {B : Type*} [add_comm_monoid B] (F : away_map x B) {C : Type*} [add_comm_monoid C] {g : A →+ C} /-- Given `x : A` and a localization map `F : A →+ B` away from `x`, `neg_self` is `- (F x)`. -/ noncomputable def away_map.neg_self : B := F.mk' 0 ⟨x, mem_multiples _⟩ /-- Given `x : A`, a localization map `F : A →+ B` away from `x`, and a map of `add_comm_monoid`s `g : A →+ C` such that `g x` is invertible, the homomorphism induced from `B` to `C` sending `z : B` to `g y - n • g x`, where `y : A, n : ℕ` are such that `z = F y - n • F x`. -/ noncomputable def away_map.lift (hg : is_add_unit (g x)) : B →+ C := F.lift $ λ y, show is_add_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_nsmul], exact is_add_unit.map (add_monoid_hom.of $ (λ x, n •ℕ x)) hg, end @[simp] lemma away_map.lift_eq (hg : is_add_unit (g x)) (a : A) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_add_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : A` and localization maps `F : A →+ B, G : A →+ C` away from `x` and `x + y` respectively, the homomorphism induced from `B` to `C`. -/ noncomputable def away_to_away_right (y : A) (G : away_map (x + y) C) : B →+ C := F.lift x $ show is_add_unit (G.to_map x), from is_add_unit_of_add_eq_zero (G.to_map x) (G.mk' y ⟨x + y, mem_multiples _⟩) $ by rw [add_mk'_eq_mk'_of_add, mk'_self] end away_map end localization_map end add_submonoid namespace submonoid namespace localization_map variables (f : S.localization_map N) {g : M →* P} (hg : ∀ (y : S), is_unit (g y)) {T : submonoid P} {Q : Type*} [comm_monoid Q] /-- If `f : M →* N` and `k : M →* P` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `P`. -/ @[to_additive "If `f : M →+ N` and `k : M →+ R` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `R`."] noncomputable def mul_equiv_of_localizations (k : localization_map S P) : N ≃* P := ⟨f.lift k.map_units, k.lift f.map_units, f.lift_left_inverse, k.lift_left_inverse, monoid_hom.map_mul _⟩ @[simp, to_additive] lemma mul_equiv_of_localizations_apply {k : localization_map S P} {x} : f.mul_equiv_of_localizations k x = f.lift k.map_units x := rfl @[simp, to_additive] lemma mul_equiv_of_localizations_symm_apply {k : localization_map S P} {x} : (f.mul_equiv_of_localizations k).symm x = k.lift f.map_units x := rfl @[to_additive] lemma mul_equiv_of_localizations_symm_eq_mul_equiv_of_localizations {k : localization_map S P} : (k.mul_equiv_of_localizations f).symm = f.mul_equiv_of_localizations k := rfl /-- If `f : M →* N` is a localization map for a submonoid `S` and `k : N ≃* P` is an isomorphism of `comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`. -/ @[to_additive "If `f : M →+ N` is a localization map for a submonoid `S` and `k : N ≃+ P` is an isomorphism of `add_comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`."] def of_mul_equiv_of_localizations (k : N ≃* P) : localization_map S P := (k.to_monoid_hom.comp f.to_map).to_localization_map (λ y, is_unit_comp f k.to_monoid_hom y) (λ v, let ⟨z, hz⟩ := k.to_equiv.surjective v in let ⟨x, hx⟩ := f.surj z in ⟨x, show v * k _ = k _, by rw [←hx, k.map_mul, ←hz]; refl⟩) (λ x y, k.apply_eq_iff_eq.trans f.eq_iff_exists) @[simp, to_additive] lemma of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : (f.of_mul_equiv_of_localizations k).to_map x = k (f.to_map x) := rfl @[to_additive] lemma of_mul_equiv_of_localizations_eq {k : N ≃* P} : (f.of_mul_equiv_of_localizations k).to_map = k.to_monoid_hom.comp f.to_map := rfl @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : k.symm ((f.of_mul_equiv_of_localizations k).to_map x) = f.to_map x := k.symm_apply_apply (f.to_map x) @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply' {k : P ≃* N} (x) : k ((f.of_mul_equiv_of_localizations k.symm).to_map x) = f.to_map x := k.apply_symm_apply (f.to_map x) @[to_additive] lemma of_mul_equiv_of_localizations_eq_iff_eq {k : N ≃* P} {x y} : (f.of_mul_equiv_of_localizations k).to_map x = y ↔ f.to_map x = k.symm y := k.to_equiv.eq_symm_apply.symm @[to_additive add_equiv_of_localizations_right_inv] lemma mul_equiv_of_localizations_right_inv (k : localization_map S P) : f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k) = k := to_map_injective $ f.lift_comp k.map_units @[to_additive add_equiv_of_localizations_right_inv_apply, simp] lemma mul_equiv_of_localizations_right_inv_apply {k : localization_map S P} {x} : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k)).to_map x = k.to_map x := ext_iff.1 (f.mul_equiv_of_localizations_right_inv k) x @[to_additive] lemma mul_equiv_of_localizations_left_inv (k : N ≃* P) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) = k := mul_equiv.ext $ monoid_hom.ext_iff.1 $ f.lift_of_comp k.to_monoid_hom @[simp, to_additive] lemma mul_equiv_of_localizations_left_inv_apply {k : N ≃* P} (x) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) x = k x := by rw mul_equiv_of_localizations_left_inv @[simp, to_additive] lemma of_mul_equiv_of_localizations_id : f.of_mul_equiv_of_localizations (mul_equiv.refl N) = f := by ext; refl @[to_additive] lemma of_mul_equiv_of_localizations_comp {k : N ≃* P} {j : P ≃* Q} : (f.of_mul_equiv_of_localizations (k.trans j)).to_map = j.to_monoid_hom.comp (f.of_mul_equiv_of_localizations k).to_map := by ext; refl /-- Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`. -/ @[to_additive "Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`."] def of_mul_equiv_of_dom {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : localization_map T N := let H' : S.comap k.to_monoid_hom = T := H ▸ (submonoid.ext' $ T.1.preimage_image_eq k.to_equiv.injective) in (f.to_map.comp k.to_monoid_hom).to_localization_map (λ y, let ⟨z, hz⟩ := f.map_units ⟨k y, H ▸ set.mem_image_of_mem k y.2⟩ in ⟨z, hz⟩) (λ z, let ⟨x, hx⟩ := f.surj z in let ⟨v, hv⟩ := k.to_equiv.surjective x.1 in let ⟨w, hw⟩ := k.to_equiv.surjective x.2 in ⟨(v, ⟨w, H' ▸ show k w ∈ S, from hw.symm ▸ x.2.2⟩), show z * f.to_map (k.to_equiv w) = f.to_map (k.to_equiv v), by erw [hv, hw, hx]; refl⟩) (λ x y, show f.to_map _ = f.to_map _ ↔ _, by erw f.eq_iff_exists; exact ⟨λ ⟨c, hc⟩, let ⟨d, hd⟩ := k.to_equiv.surjective c in ⟨⟨d, H' ▸ show k d ∈ S, from hd.symm ▸ c.2⟩, by erw [←hd, ←k.map_mul, ←k.map_mul] at hc; exact k.to_equiv.injective hc⟩, λ ⟨c, hc⟩, ⟨⟨k c, H ▸ set.mem_image_of_mem k c.2⟩, by erw ←k.map_mul; rw [hc, k.map_mul]; refl⟩⟩) @[simp, to_additive] lemma of_mul_equiv_of_dom_apply {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map x = f.to_map (k x) := rfl @[to_additive] lemma of_mul_equiv_of_dom_eq {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : (f.of_mul_equiv_of_dom H).to_map = f.to_map.comp k.to_monoid_hom := rfl @[to_additive] lemma of_mul_equiv_of_dom_comp_symm {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k.symm x) = f.to_map x := congr_arg f.to_map $ k.apply_symm_apply x @[to_additive] lemma of_mul_equiv_of_dom_comp {k : M ≃* P} (H : T.map k.symm.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k x) = f.to_map x := congr_arg f.to_map $ k.symm_apply_apply x /-- A special case of `f ∘ id = f`, `f` a localization map. -/ @[simp, to_additive "A special case of `f ∘ id = f`, `f` a localization map."] lemma of_mul_equiv_of_dom_id : f.of_mul_equiv_of_dom (show S.map (mul_equiv.refl M).to_monoid_hom = S, from submonoid.ext $ λ x, ⟨λ ⟨y, hy, h⟩, h ▸ hy, λ h, ⟨x, h, rfl⟩⟩) = f := by ext; refl /-- Given localization maps `f : M →* N, k : P →* U` for submonoids `S, T` respectively, an isomorphism `j : M ≃* P` such that `j(S) = T` induces an isomorphism of localizations `N ≃* U`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ U` for submonoids `S, T` respectively, an isomorphism `j : M ≃+ P` such that `j(S) = T` induces an isomorphism of localizations `N ≃+ U`."] noncomputable def mul_equiv_of_mul_equiv (k : localization_map T Q) {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : N ≃* Q := f.mul_equiv_of_localizations $ k.of_mul_equiv_of_dom H @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq_map_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H x = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl @[to_additive] lemma mul_equiv_of_mul_equiv_eq_map {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.mul_equiv_of_mul_equiv k H).to_monoid_hom = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H (f.to_map x) = k.to_map (j x) := f.map_eq (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ @[simp, to_additive] lemma mul_equiv_of_mul_equiv_mk' {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x y) : f.mul_equiv_of_mul_equiv k H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.map_mk' (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ _ @[simp, to_additive] lemma of_mul_equiv_of_mul_equiv_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map x = k.to_map (j x) := ext_iff.1 (f.mul_equiv_of_localizations_right_inv (k.of_mul_equiv_of_dom H)) x @[to_additive] lemma of_mul_equiv_of_mul_equiv {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map = k.to_map.comp j.to_monoid_hom := monoid_hom.ext $ f.of_mul_equiv_of_mul_equiv_apply H end localization_map end submonoid namespace localization variables (S) /-- Natural hom sending `x : M`, `M` a `comm_monoid`, to the equivalence class of `(x, 1)` in the localization of `M` at a submonoid. -/ @[to_additive "Natural homomorphism sending `x : M`, `M` an `add_comm_monoid`, to the equivalence class of `(x, 0)` in the localization of `M` at a submonoid."] def monoid_of : submonoid.localization_map S (localization S) := { map_units' := λ y, is_unit_iff_exists_inv.2 ⟨mk 1 y, (r S).eq.2 $ show r S (_, 1 * y) 1, by simpa using (r S).symm (one_rel y)⟩, surj' := λ z, induction_on z $ λ x, ⟨x, (r S).eq.2 $ show r S (x.1 * x.2, x.2 * 1) (x.1, 1), by rw [mul_comm x.2, ←mul_one (x.1, (1 : S))]; exact (r S).mul ((r S).refl (x.1, 1)) ((r S).symm $ one_rel x.2)⟩, eq_iff_exists' := λ x y, (r S).eq.trans $ r_iff_exists.trans $ show (∃ (c : S), x * 1 * c = y * 1 * c) ↔ _, by rw [mul_one, mul_one], ..(r S).mk'.comp $ monoid_hom.inl M S } variables {S} @[to_additive] lemma mk_one_eq_monoid_of_mk (x) : mk x 1 = (monoid_of S).to_map x := rfl @[to_additive] lemma mk_eq_monoid_of_mk'_apply (x y) : mk x y = (monoid_of S).mk' x y := show _ = _ * _, from (submonoid.localization_map.mul_inv_right (monoid_of S).map_units _ _ _).2 $ begin rw [←mk_one_eq_monoid_of_mk, ←mk_one_eq_monoid_of_mk, show mk x y * mk y 1 = mk (x * y) (1 * y), by rw mul_comm 1 y; refl, show mk x 1 = mk (x * 1) ((1 : S) * 1), by rw [mul_one, mul_one]], exact (con.eq _).2 (con.symm _ $ (localization.r S).mul (con.refl _ (x, 1)) $ one_rel _), end @[simp, to_additive] lemma mk_eq_monoid_of_mk' : mk = (monoid_of S).mk' := funext $ λ _, funext $ λ _, mk_eq_monoid_of_mk'_apply _ _ variables (f : submonoid.localization_map S N) /-- Given a localization map `f : M →* N` for a submonoid `S`, we get an isomorphism between the localization of `M` at `S` as a quotient type and `N`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S`, we get an isomorphism between the localization of `M` at `S` as a quotient type and `N`."] noncomputable def mul_equiv_of_quotient (f : submonoid.localization_map S N) : localization S ≃* N := (monoid_of S).mul_equiv_of_localizations f variables {f} @[simp, to_additive] lemma mul_equiv_of_quotient_apply (x) : mul_equiv_of_quotient f x = (monoid_of S).lift f.map_units x := rfl @[simp, to_additive] lemma mul_equiv_of_quotient_mk' (x y) : mul_equiv_of_quotient f ((monoid_of S).mk' x y) = f.mk' x y := (monoid_of S).lift_mk' _ _ _ @[to_additive] lemma mul_equiv_of_quotient_mk (x y) : mul_equiv_of_quotient f (mk x y) = f.mk' x y := by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_mk' _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_monoid_of (x) : mul_equiv_of_quotient f ((monoid_of S).to_map x) = f.to_map x := (monoid_of S).lift_eq _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_symm_mk' (x y) : (mul_equiv_of_quotient f).symm (f.mk' x y) = (monoid_of S).mk' x y := f.lift_mk' _ _ _ @[to_additive] lemma mul_equiv_of_quotient_symm_mk (x y) : (mul_equiv_of_quotient f).symm (f.mk' x y) = mk x y := by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_symm_mk' _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_symm_monoid_of (x) : (mul_equiv_of_quotient f).symm (f.to_map x) = (monoid_of S).to_map x := f.lift_eq _ _ section away variables (x : M) /-- Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient. -/ @[reducible, to_additive "Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient."] def away := localization (submonoid.powers x) /-- Given `x : M`, `inv_self` is `x⁻¹` in the localization (as a quotient type) of `M` at the submonoid generated by `x`. -/ @[to_additive "Given `x : M`, `neg_self` is `-x` in the localization (as a quotient type) of `M` at the submonoid generated by `x`."] def away.inv_self : away x := mk 1 ⟨x, submonoid.mem_powers _⟩ /-- Given `x : M`, the natural hom sending `y : M`, `M` a `comm_monoid`, to the equivalence class of `(y, 1)` in the localization of `M` at the submonoid generated by `x`. -/ @[reducible, to_additive "Given `x : M`, the natural hom sending `y : M`, `M` an `add_comm_monoid`, to the equivalence class of `(y, 0)` in the localization of `M` at the submonoid generated by `x`."] def away.monoid_of : submonoid.localization_map.away_map x (away x) := monoid_of (submonoid.powers x) @[simp, to_additive] lemma away.mk_eq_monoid_of_mk' : mk = (away.monoid_of x).mk' := mk_eq_monoid_of_mk' /-- Given `x : M` and a localization map `f : M →* N` away from `x`, we get an isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type and `N`. -/ @[to_additive "Given `x : M` and a localization map `f : M →+ N` away from `x`, we get an isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type and `N`."] noncomputable def away.mul_equiv_of_quotient (f : submonoid.localization_map.away_map x N) : away x ≃* N := mul_equiv_of_quotient f end away end localization
616efc43456e51184d7a4ba25a23c37775ee864d
4727251e0cd73359b15b664c3170e5d754078599
/src/group_theory/perm/option.lean
1cfa369403d7cdf1d7c8f35c4f7d07915205e1e3
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,001
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.perm.sign import logic.equiv.option /-! # Permutations of `option α` -/ open equiv @[simp] lemma equiv.option_congr_one {α : Type*} : (1 : perm α).option_congr = 1 := equiv.option_congr_refl @[simp] lemma equiv.option_congr_swap {α : Type*} [decidable_eq α] (x y : α) : option_congr (swap x y) = swap (some x) (some y) := begin ext (_ | i), { simp [swap_apply_of_ne_of_ne] }, { by_cases hx : i = x, simp [hx, swap_apply_of_ne_of_ne], by_cases hy : i = y; simp [hx, hy, swap_apply_of_ne_of_ne], } end @[simp] lemma equiv.option_congr_sign {α : Type*} [decidable_eq α] [fintype α] (e : perm α) : perm.sign e.option_congr = perm.sign e := begin apply perm.swap_induction_on e, { simp [perm.one_def] }, { intros f x y hne h, simp [h, hne, perm.mul_def, ←equiv.option_congr_trans] } end @[simp] lemma map_equiv_remove_none {α : Type*} [decidable_eq α] (σ : perm (option α)) : (remove_none σ).option_congr = swap none (σ none) * σ := begin ext1 x, have : option.map ⇑(remove_none σ) x = (swap none (σ none)) (σ x), { cases x, { simp }, { cases h : σ (some x), { simp [remove_none_none _ h], }, { have hn : σ (some x) ≠ none := by simp [h], have hσn : σ (some x) ≠ σ none := σ.injective.ne (by simp), simp [remove_none_some _ ⟨_, h⟩, ←h, swap_apply_of_ne_of_ne hn hσn] } } }, simpa using this, end /-- Permutations of `option α` are equivalent to fixing an `option α` and permuting the remaining with a `perm α`. The fixed `option α` is swapped with `none`. -/ @[simps] def equiv.perm.decompose_option {α : Type*} [decidable_eq α] : perm (option α) ≃ option α × perm α := { to_fun := λ σ, (σ none, remove_none σ), inv_fun := λ i, swap none i.1 * i.2.option_congr, left_inv := λ σ, by simp, right_inv := λ ⟨x, σ⟩, begin have : remove_none (swap none x * σ.option_congr) = σ := equiv.option_congr_injective (by simp [←mul_assoc]), simp [←perm.eq_inv_iff_eq, this], end } lemma equiv.perm.decompose_option_symm_of_none_apply {α : Type*} [decidable_eq α] (e : perm α) (i : option α) : equiv.perm.decompose_option.symm (none, e) i = i.map e := by simp lemma equiv.perm.decompose_option_symm_sign {α : Type*} [decidable_eq α] [fintype α] (e : perm α) : perm.sign (equiv.perm.decompose_option.symm (none, e)) = perm.sign e := by simp /-- The set of all permutations of `option α` can be constructed by augmenting the set of permutations of `α` by each element of `option α` in turn. -/ lemma finset.univ_perm_option {α : Type*} [decidable_eq α] [fintype α] : @finset.univ (perm $ option α) _ = (finset.univ : finset $ option α × perm α).map equiv.perm.decompose_option.symm.to_embedding := (finset.univ_map_equiv_to_embedding _).symm
ff8423fdc00a68d6f0ec6a6b530a32469fa8712f
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/tactic/doc_commands.lean
d1e9a634285c33536f79affe9419122e6ac85e0c
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
16,888
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import tactic.fix_reflect_string /-! # Documentation commands We generate html documentation from mathlib. It is convenient to collect lists of tactics, commands, notes, etc. To facilitate this, we declare these documentation entries in the library using special commands. * `library_note` adds a note describing a certain feature or design decision. These can be referenced in doc strings with the text `note [name of note]`. * `add_tactic_doc` adds an entry documenting an interactive tactic, command, hole command, or attribute. Since these commands are used in files imported by `tactic.core`, this file has no imports. ## Implementation details `library_note note_id note_msg` creates a declaration `` `library_note.i `` for some `i`. This declaration is a pair of strings `note_id` and `note_msg`, and it gets tagged with the `library_note` attribute. Similarly, `add_tactic_doc` creates a declaration `` `tactic_doc.i `` that stores the provided information. -/ /-- A rudimentary hash function on strings. -/ def string.hash (s : string) : ℕ := s.fold 1 (λ h c, (33*h + c.val) % unsigned_sz) /-- `mk_hashed_name nspace id` hashes the string `id` to a value `i` and returns the name `nspace._i` -/ meta def string.mk_hashed_name (nspace : name) (id : string) : name := nspace <.> ("_" ++ to_string id.hash) open tactic /-- `copy_doc_string fr to` copies the docstring from the declaration named `fr` to each declaration named in the list `to`. -/ meta def tactic.copy_doc_string (fr : name) (to : list name) : tactic unit := do fr_ds ← doc_string fr, to.mmap' $ λ tgt, add_doc_string tgt fr_ds open lean lean.parser interactive /-- `copy_doc_string source → target_1 target_2 ... target_n` copies the doc string of the declaration named `source` to each of `target_1`, `target_2`, ..., `target_n`. -/ @[user_command] meta def copy_doc_string_cmd (_ : parse (tk "copy_doc_string")) : parser unit := do fr ← parser.ident, tk "->", to ← parser.many parser.ident, expr.const fr _ ← resolve_name fr, to ← parser.of_tactic (to.mmap $ λ n, expr.const_name <$> resolve_name n), tactic.copy_doc_string fr to /-! ### The `library_note` command -/ /-- A user attribute `library_note` for tagging decls of type `string × string` for use in note output. -/ @[user_attribute] meta def library_note_attr : user_attribute := { name := `library_note, descr := "Notes about library features to be included in documentation" } /-- `mk_reflected_definition name val` constructs a definition declaration by reflection. Example: ``mk_reflected_definition `foo 17`` constructs the definition declaration corresponding to `def foo : ℕ := 17` -/ meta def mk_reflected_definition (decl_name : name) {type} [reflected type] (body : type) [reflected body] : declaration := mk_definition decl_name (reflect type).collect_univ_params (reflect type) (reflect body) /-- If `note_name` and `note` are `pexpr`s representing strings, `add_library_note note_name note` adds a declaration of type `string × string` and tags it with the `library_note` attribute. -/ meta def tactic.add_library_note (note_name note : string) : tactic unit := do let decl_name := note_name.mk_hashed_name `library_note, add_decl $ mk_reflected_definition decl_name (note_name, note), library_note_attr.set decl_name () tt none /-- `tactic.eval_pexpr e α` evaluates the pre-expression `e` to a VM object of type `α`. -/ meta def tactic.eval_pexpr (α) [reflected α] (e : pexpr) : tactic α := to_expr ``(%%e : %%(reflect α)) ff ff >>= eval_expr α open tactic /-- A command to add library notes. Syntax: ``` /-- note message -/ library_note "note id" ``` -/ @[user_command] meta def library_note (mi : interactive.decl_meta_info) (_ : parse (tk "library_note")) : parser unit := do note_name ← parser.pexpr, note_name ← eval_pexpr string note_name, some doc_string ← pure mi.doc_string | fail "library_note requires a doc string", add_library_note note_name doc_string /-- Collects all notes in the current environment. Returns a list of pairs `(note_id, note_content)` -/ meta def tactic.get_library_notes : tactic (list (string × string)) := attribute.get_instances `library_note >>= list.mmap (λ dcl, mk_const dcl >>= eval_expr (string × string)) /-! ### The `add_tactic_doc_entry` command -/ /-- The categories of tactic doc entry. -/ @[derive [decidable_eq, has_reflect]] inductive doc_category | tactic | cmd | hole_cmd | attr /-- Format a `doc_category` -/ meta def doc_category.to_string : doc_category → string | doc_category.tactic := "tactic" | doc_category.cmd := "command" | doc_category.hole_cmd := "hole_command" | doc_category.attr := "attribute" meta instance : has_to_format doc_category := ⟨↑doc_category.to_string⟩ /-- The information used to generate a tactic doc entry -/ @[derive has_reflect] structure tactic_doc_entry := (name : string) (category : doc_category) (decl_names : list _root_.name) (tags : list string := []) (description : string := "") (inherit_description_from : option _root_.name := none) /-- format a `tactic_doc_entry` -/ meta def tactic_doc_entry.to_string : tactic_doc_entry → string | ⟨name, category, decl_names, tags, description, _⟩ := let decl_names := decl_names.map (repr ∘ to_string), tags := tags.map repr in "{" ++ to_string (format!"\"name\": {repr name}, \"category\": \"{category}\", \"decl_names\":{decl_names}, \"tags\": {tags}, \"description\": {repr description}") ++ "}" meta instance : has_to_string tactic_doc_entry := ⟨tactic_doc_entry.to_string⟩ /-- `update_description_from tde inh_id` replaces the `description` field of `tde` with the doc string of the declaration named `inh_id`. -/ meta def tactic_doc_entry.update_description_from (tde : tactic_doc_entry) (inh_id : name) : tactic tactic_doc_entry := do ds ← doc_string inh_id <|> fail (to_string inh_id ++ " has no doc string"), return { description := ds .. tde } /-- `update_description tde` replaces the `description` field of `tde` with: * the doc string of `tde.inherit_description_from`, if this field has a value * the doc string of the entry in `tde.decl_names`, if this field has length 1 If neither of these conditions are met, it returns `tde`. -/ meta def tactic_doc_entry.update_description (tde : tactic_doc_entry) : tactic tactic_doc_entry := match tde.inherit_description_from, tde.decl_names with | some inh_id, _ := tde.update_description_from inh_id | none, [inh_id] := tde.update_description_from inh_id | none, _ := return tde end /-- A user attribute `tactic_doc` for tagging decls of type `tactic_doc_entry` for use in doc output -/ @[user_attribute] meta def tactic_doc_entry_attr : user_attribute := { name := `tactic_doc, descr := "Information about a tactic to be included in documentation" } /-- Collects everything in the environment tagged with the attribute `tactic_doc`. -/ meta def tactic.get_tactic_doc_entries : tactic (list tactic_doc_entry) := attribute.get_instances `tactic_doc >>= list.mmap (λ dcl, mk_const dcl >>= eval_expr tactic_doc_entry) /-- `add_tactic_doc tde` adds a declaration to the environment with `tde` as its body and tags it with the `tactic_doc` attribute. If `tde.decl_names` has exactly one entry `` `decl`` and if `tde.description` is the empty string, `add_tactic_doc` uses the doc string of `decl` as the description. -/ meta def tactic.add_tactic_doc (tde : tactic_doc_entry) : tactic unit := do when (tde.description = "" ∧ tde.inherit_description_from.is_none ∧ tde.decl_names.length ≠ 1) $ fail ("A tactic doc entry must either:\n" ++ " 1. have a description written as a doc-string for the `add_tactic_doc` invocation, or\n" ++ " 2. have a single declaration in the `decl_names` field, to inherit a description from, or\n" ++ " 3. explicitly indicate the declaration to inherit the description from using `inherit_description_from`."), tde ← if tde.description = "" then tde.update_description else return tde, let decl_name := (tde.name ++ tde.category.to_string).mk_hashed_name `tactic_doc, add_decl $ mk_definition decl_name [] `(tactic_doc_entry) (reflect tde), tactic_doc_entry_attr.set decl_name () tt none /-- A command used to add documentation for a tactic, command, hole command, or attribute. Usage: after defining an interactive tactic, command, or attribute, add its documentation as follows. ```lean /-- describe what the command does here -/ add_tactic_doc { name := "display name of the tactic", category := cat, decl_names := [`dcl_1, `dcl_2], tags := ["tag_1", "tag_2"] } ``` The argument to `add_tactic_doc` is a structure of type `tactic_doc_entry`. * `name` refers to the display name of the tactic; it is used as the header of the doc entry. * `cat` refers to the category of doc entry. Options: `doc_category.tactic`, `doc_category.cmd`, `doc_category.hole_cmd`, `doc_category.attr` * `decl_names` is a list of the declarations associated with this doc. For instance, the entry for `linarith` would set ``decl_names := [`tactic.interactive.linarith]``. Some entries may cover multiple declarations. It is only necessary to list the interactive versions of tactics. * `tags` is an optional list of strings used to categorize entries. * The doc string is the body of the entry. It can be formatted with markdown. What you are reading now is the description of `add_tactic_doc`. If only one related declaration is listed in `decl_names` and if this invocation of `add_tactic_doc` does not have a doc string, the doc string of that declaration will become the body of the tactic doc entry. If there are multiple declarations, you can select the one to be used by passing a name to the `inherit_description_from` field. If you prefer a tactic to have a doc string that is different then the doc entry, you should write the doc entry as a doc string for the `add_tactic_doc` invocation. Note that providing a badly formed `tactic_doc_entry` to the command can result in strange error messages. -/ @[user_command] meta def add_tactic_doc_command (mi : interactive.decl_meta_info) (_ : parse $ tk "add_tactic_doc") : parser unit := do pe ← parser.pexpr, e ← eval_pexpr tactic_doc_entry pe, let e : tactic_doc_entry := match mi.doc_string with | some desc := { description := desc, ..e } | none := e end, tactic.add_tactic_doc e . /-- At various places in mathlib, we leave implementation notes that are referenced from many other files. To keep track of these notes, we use the command `library_note`. This makes it easy to retrieve a list of all notes, e.g. for documentation output. These notes can be referenced in mathlib with the syntax `Note [note id]`. Often, these references will be made in code comments (`--`) that won't be displayed in docs. If such a reference is made in a doc string or module doc, it will be linked to the corresponding note in the doc display. Syntax: ``` /-- note message -/ library_note "note id" ``` An example from `meta.expr`: ``` /-- Some declarations work with open expressions, i.e. an expr that has free variables. Terms will free variables are not well-typed, and one should not use them in tactics like `infer_type` or `unify`. You can still do syntactic analysis/manipulation on them. The reason for working with open types is for performance: instantiating variables requires iterating through the expression. In one performance test `pi_binders` was more than 6x quicker than `mk_local_pis` (when applied to the type of all imported declarations 100x). -/ library_note "open expressions" ``` This note can be referenced near a usage of `pi_binders`: ``` -- See Note [open expressions] /-- behavior of f -/ def f := pi_binders ... ``` -/ add_tactic_doc { name := "library_note", category := doc_category.cmd, decl_names := [`library_note, `tactic.add_library_note], tags := ["documentation"], inherit_description_from := `library_note } add_tactic_doc { name := "add_tactic_doc", category := doc_category.cmd, decl_names := [`add_tactic_doc_command, `tactic.add_tactic_doc], tags := ["documentation"], inherit_description_from := `add_tactic_doc_command } add_tactic_doc { name := "copy_doc_string", category := doc_category.cmd, decl_names := [`copy_doc_string_cmd, `tactic.copy_doc_string], tags := ["documentation"], inherit_description_from := `copy_doc_string_cmd } -- add docs to core tactics /-- The congruence closure tactic `cc` tries to solve the goal by chaining equalities from context and applying congruence (i.e. if `a = b`, then `f a = f b`). It is a finishing tactic, i.e. it is meant to close the current goal, not to make some inconclusive progress. A mostly trivial example would be: ```lean example (a b c : ℕ) (f : ℕ → ℕ) (h: a = b) (h' : b = c) : f a = f c := by cc ``` As an example requiring some thinking to do by hand, consider: ```lean example (f : ℕ → ℕ) (x : ℕ) (H1 : f (f (f x)) = x) (H2 : f (f (f (f (f x)))) = x) : f x = x := by cc ``` The tactic works by building an equality matching graph. It's a graph where the vertices are terms and they are linked by edges if they are known to be equal. Once you've added all the equalities in your context, you take the transitive closure of the graph and, for each connected component (i.e. equivalence class) you can elect a term that will represent the whole class and store proofs that the other elements are equal to it. You then take the transitive closure of these equalities under the congruence lemmas. The `cc` implementation in Lean does a few more tricks: for example it derives `a=b` from `nat.succ a = nat.succ b`, and `nat.succ a != nat.zero` for any `a`. * The starting reference point is Nelson, Oppen, [Fast decision procedures based on congruence closure](http://www.cs.colorado.edu/~bec/courses/csci5535-s09/reading/nelson-oppen-congruence.pdf), Journal of the ACM (1980) * The congruence lemmas for dependent type theory as used in Lean are described in [Congruence closure in intensional type theory](https://leanprover.github.io/papers/congr.pdf) (de Moura, Selsam IJCAR 2016). -/ add_tactic_doc { name := "cc (congruence closure)", category := doc_category.tactic, decl_names := [`tactic.interactive.cc], tags := ["core", "finishing"] } /-- `conv {...}` allows the user to perform targeted rewriting on a goal or hypothesis, by focusing on particular subexpressions. See <https://leanprover-community.github.io/extras/conv.html> for more details. Inside `conv` blocks, mathlib currently additionally provides * `erw`, * `ring`, `ring2` and `ring_exp`, * `norm_num`, * `norm_cast`, * `apply_congr`, and * `conv` (within another `conv`). `apply_congr` applies congruence lemmas to step further inside expressions, and sometimes gives between results than the automatically generated congruence lemmas used by `congr`. Using `conv` inside a `conv` block allows the user to return to the previous state of the outer `conv` block after it is finished. Thus you can continue editing an expression without having to start a new `conv` block and re-scoping everything. For example: ```lean example (a b c d : ℕ) (h₁ : b = c) (h₂ : a + c = a + d) : a + b = a + d := by conv { to_lhs, conv { congr, skip, rw h₁, }, rw h₂, } ``` Without `conv`, the above example would need to be proved using two successive `conv` blocks, each beginning with `to_lhs`. Also, as a shorthand, `conv_lhs` and `conv_rhs` are provided, so that ```lean example : 0 + 0 = 0 := begin conv_lhs { simp } end ``` just means ```lean example : 0 + 0 = 0 := begin conv { to_lhs, simp } end ``` and likewise for `to_rhs`. -/ add_tactic_doc { name := "conv", category := doc_category.tactic, decl_names := [`tactic.interactive.conv], tags := ["core"] } add_tactic_doc { name := "simp", category := doc_category.tactic, decl_names := [`tactic.interactive.simp], tags := ["core", "simplification"] } /-- The `add_decl_doc` command is used to add a doc string to an existing declaration. ```lean def foo := 5 /-- Doc string for foo. -/ add_decl_doc foo ``` -/ @[user_command] meta def add_decl_doc_command (mi : interactive.decl_meta_info) (_ : parse $ tk "add_decl_doc") : parser unit := do n ← parser.ident, n ← resolve_constant n, some doc ← pure mi.doc_string | fail "add_decl_doc requires a doc string", add_doc_string n doc add_tactic_doc { name := "add_decl_doc", category := doc_category.cmd, decl_names := [``add_decl_doc_command], tags := ["documentation"] }
4e334a55114f3717cf9b841442ffdbdd69fff27e
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/bicategory/coherence.lean
17e06361f01b26e5e83a4b6c7b705f5586f70b98
[ "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
9,120
lean
/- Copyright (c) 2022 Yuma Mizuno. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yuma Mizuno, Junyan Xu -/ import category_theory.path_category import category_theory.functor.fully_faithful import category_theory.bicategory.free import category_theory.bicategory.locally_discrete /-! # The coherence theorem for bicategories In this file, we prove the coherence theorem for bicategories, stated in the following form: the free bicategory over any quiver is locally thin. The proof is almost the same as the proof of the coherence theorem for monoidal categories that has been previously formalized in mathlib, which is based on the proof described by Ilya Beylin and Peter Dybjer. The idea is to view a path on a quiver as a normal form of a 1-morphism in the free bicategory on the same quiver. A normalization procedure is then described by `normalize : pseudofunctor (free_bicategory B) (locally_discrete (paths B))`, which is a pseudofunctor from the free bicategory to the locally discrete bicategory on the path category. It turns out that this pseudofunctor is locally an equivalence of categories, and the coherence theorem follows immediately from this fact. ## Main statements * `locally_thin` : the free bicategory is locally thin, that is, there is at most one 2-morphism between two fixed 1-morphisms. ## References * [Ilya Beylin and Peter Dybjer, Extracting a proof of coherence for monoidal categories from a proof of normalization for monoids][beylin1996] -/ open quiver (path) quiver.path namespace category_theory open bicategory category open_locale bicategory universes v u namespace free_bicategory variables {B : Type u} [quiver.{v+1} B] /-- Auxiliary definition for `inclusion_path`. -/ @[simp] def inclusion_path_aux {a : B} : ∀ {b : B}, path a b → hom a b | _ nil := hom.id a | _ (cons p f) := (inclusion_path_aux p).comp (hom.of f) /-- The discrete category on the paths includes into the category of 1-morphisms in the free bicategory. -/ def inclusion_path (a b : B) : discrete (path.{v+1} a b) ⥤ hom a b := discrete.functor inclusion_path_aux /-- The inclusion from the locally discrete bicategory on the path category into the free bicategory as a prelax functor. This will be promoted to a pseudofunctor after proving the coherence theorem. See `inclusion`. -/ def preinclusion (B : Type u) [quiver.{v+1} B] : prelax_functor (locally_discrete (paths B)) (free_bicategory B) := { obj := id, map := λ a b, (inclusion_path a b).obj, map₂ := λ a b, (inclusion_path a b).map } @[simp] lemma preinclusion_obj (a : B) : (preinclusion B).obj a = a := rfl @[simp] lemma preinclusion_map₂ {a b : B} (f g : discrete (path.{v+1} a b)) (η : f ⟶ g) : (preinclusion B).map₂ η = eq_to_hom (congr_arg _ (discrete.ext _ _ (discrete.eq_of_hom η))) := begin rcases η with ⟨⟨⟩⟩, cases discrete.ext _ _ η, exact (inclusion_path a b).map_id _ end /-- The normalization of the composition of `p : path a b` and `f : hom b c`. `p` will eventually be taken to be `nil` and we then get the normalization of `f` alone, but the auxiliary `p` is necessary for Lean to accept the definition of `normalize_iso` and the `whisker_left` case of `normalize_aux_congr` and `normalize_naturality`. -/ @[simp] def normalize_aux {a : B} : ∀ {b c : B}, path a b → hom b c → path a c | _ _ p (hom.of f) := p.cons f | _ _ p (hom.id b) := p | _ _ p (hom.comp f g) := normalize_aux (normalize_aux p f) g /- We may define ``` def normalize_aux' : ∀ {a b : B}, hom a b → path a b | _ _ (hom.of f) := f.to_path | _ _ (hom.id b) := nil | _ _ (hom.comp f g) := (normalize_aux' f).comp (normalize_aux' g) ``` and define `normalize_aux p f` to be `p.comp (normalize_aux' f)` and this will be equal to the above definition, but the equality proof requires `comp_assoc`, and it thus lacks the correct definitional property to make the definition of `normalize_iso` typecheck. ``` example {a b c : B} (p : path a b) (f : hom b c) : normalize_aux p f = p.comp (normalize_aux' f) := by { induction f, refl, refl, case comp : _ _ _ _ _ ihf ihg { rw [normalize_aux, ihf, ihg], apply comp_assoc } } ``` -/ /-- A 2-isomorphism between a partially-normalized 1-morphism in the free bicategory to the fully-normalized 1-morphism. -/ @[simp] def normalize_iso {a : B} : ∀ {b c : B} (p : path a b) (f : hom b c), (preinclusion B).map ⟨p⟩ ≫ f ≅ (preinclusion B).map ⟨normalize_aux p f⟩ | _ _ p (hom.of f) := iso.refl _ | _ _ p (hom.id b) := ρ_ _ | _ _ p (hom.comp f g) := (α_ _ _ _).symm ≪≫ whisker_right_iso (normalize_iso p f) g ≪≫ normalize_iso (normalize_aux p f) g /-- Given a 2-morphism between `f` and `g` in the free bicategory, we have the equality `normalize_aux p f = normalize_aux p g`. -/ lemma normalize_aux_congr {a b c : B} (p : path a b) {f g : hom b c} (η : f ⟶ g) : normalize_aux p f = normalize_aux p g := begin rcases η, apply @congr_fun _ _ (λ p, normalize_aux p f), clear p, induction η, case vcomp { apply eq.trans; assumption }, /- p ≠ nil required! See the docstring of `normalize_aux`. -/ case whisker_left : _ _ _ _ _ _ _ ih { funext, apply congr_fun ih }, case whisker_right : _ _ _ _ _ _ _ ih { funext, apply congr_arg2 _ (congr_fun ih p) rfl }, all_goals { funext, refl } end /-- The 2-isomorphism `normalize_iso p f` is natural in `f`. -/ lemma normalize_naturality {a b c : B} (p : path a b) {f g : hom b c} (η : f ⟶ g) : (preinclusion B).map ⟨p⟩ ◁ η ≫ (normalize_iso p g).hom = (normalize_iso p f).hom ≫ (preinclusion B).map₂ (eq_to_hom (discrete.ext _ _ (normalize_aux_congr p η))) := begin rcases η, induction η, case id : { simp }, case vcomp : _ _ _ _ _ _ _ ihf ihg { rw [mk_vcomp, bicategory.whisker_left_comp], slice_lhs 2 3 { rw ihg }, slice_lhs 1 2 { rw ihf }, simp }, case whisker_left : _ _ _ _ _ _ _ ih /- p ≠ nil required! See the docstring of `normalize_aux`. -/ { dsimp, simp_rw [associator_inv_naturality_right_assoc, whisker_exchange_assoc, ih, assoc] }, case whisker_right : _ _ _ _ _ h η ih { dsimp, rw [associator_inv_naturality_middle_assoc, ←comp_whisker_right_assoc, ih, comp_whisker_right], have := dcongr_arg (λ x, (normalize_iso x h).hom) (normalize_aux_congr p (quot.mk _ η)), dsimp at this, simp [this] }, all_goals { dsimp, dsimp [id_def, comp_def], simp } end @[simp] lemma normalize_aux_nil_comp {a b c : B} (f : hom a b) (g : hom b c) : normalize_aux nil (f.comp g) = (normalize_aux nil f).comp (normalize_aux nil g) := begin induction g generalizing a, case id { refl }, case of { refl }, case comp : _ _ _ g _ ihf ihg { erw [ihg (f.comp g), ihf f, ihg g, comp_assoc] } end /-- The normalization pseudofunctor for the free bicategory on a quiver `B`. -/ def normalize (B : Type u) [quiver.{v+1} B] : pseudofunctor (free_bicategory B) (locally_discrete (paths B)) := { obj := id, map := λ a b f, ⟨normalize_aux nil f⟩, map₂ := λ a b f g η, eq_to_hom $ discrete.ext _ _ $ normalize_aux_congr nil η, map_id := λ a, eq_to_iso $ discrete.ext _ _ rfl, map_comp := λ a b c f g, eq_to_iso $ discrete.ext _ _ $ normalize_aux_nil_comp f g } /-- Auxiliary definition for `normalize_equiv`. -/ def normalize_unit_iso (a b : free_bicategory B) : 𝟭 (a ⟶ b) ≅ (normalize B).map_functor a b ⋙ inclusion_path a b := nat_iso.of_components (λ f, (λ_ f).symm ≪≫ normalize_iso nil f) begin intros f g η, erw [left_unitor_inv_naturality_assoc, assoc], congr' 1, exact normalize_naturality nil η end /-- Normalization as an equivalence of categories. -/ def normalize_equiv (a b : B) : hom a b ≌ discrete (path.{v+1} a b) := equivalence.mk ((normalize _).map_functor a b) (inclusion_path a b) (normalize_unit_iso a b) (discrete.nat_iso (λ f, eq_to_iso (by { induction f; induction f; tidy }))) /-- The coherence theorem for bicategories. -/ instance locally_thin {a b : free_bicategory B} (f g : a ⟶ b) : subsingleton (f ⟶ g) := ⟨λ η θ, (normalize_equiv a b).functor.map_injective (subsingleton.elim _ _)⟩ /-- Auxiliary definition for `inclusion`. -/ def inclusion_map_comp_aux {a b : B} : ∀ {c : B} (f : path a b) (g : path b c), (preinclusion _).map (⟨f⟩ ≫ ⟨g⟩) ≅ (preinclusion _).map ⟨f⟩ ≫ (preinclusion _).map ⟨g⟩ | _ f nil := (ρ_ ((preinclusion _).map ⟨f⟩)).symm | _ f (cons g₁ g₂) := whisker_right_iso (inclusion_map_comp_aux f g₁) (hom.of g₂) ≪≫ α_ _ _ _ /-- The inclusion pseudofunctor from the locally discrete bicategory on the path category into the free bicategory. -/ def inclusion (B : Type u) [quiver.{v+1} B] : pseudofunctor (locally_discrete (paths B)) (free_bicategory B) := { map_id := λ a, iso.refl (𝟙 a), map_comp := λ a b c f g, inclusion_map_comp_aux f.as g.as, -- All the conditions for 2-morphisms are trivial thanks to the coherence theorem! .. preinclusion B } end free_bicategory end category_theory
c8b3b1f9f7b529b54195adf965c022c6630895eb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/geometry/euclidean/inversion.lean
92cd13a99fa17f3f9ef4dc189871f685c602b0f2
[ "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,526
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.inner_product_space.basic /-! # Inversion in an affine space > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define inversion in a sphere in an affine space. This map sends each point `x` to the point `y` such that `y -ᵥ c = (R / dist x c) ^ 2 • (x -ᵥ c)`, where `c` and `R` are the center and the radius the sphere. In many applications, it is convenient to assume that the inversions swaps the center and the point at infinity. In order to stay in the original affine space, we define the map so that it sends center to itself. Currently, we prove only a few basic lemmas needed to prove Ptolemy's inequality, see `euclidean_geometry.mul_dist_le_mul_dist_add_mul_dist`. -/ noncomputable theory open metric real function namespace euclidean_geometry variables {V P : Type*} [normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] {a b c d x y z : P} {R : ℝ} include V /-- Inversion in a sphere in an affine space. This map sends each point `x` to the point `y` such that `y -ᵥ c = (R / dist x c) ^ 2 • (x -ᵥ c)`, where `c` and `R` are the center and the radius the sphere. -/ def inversion (c : P) (R : ℝ) (x : P) : P := (R / dist x c) ^ 2 • (x -ᵥ c) +ᵥ c lemma inversion_vsub_center (c : P) (R : ℝ) (x : P) : inversion c R x -ᵥ c = (R / dist x c) ^ 2 • (x -ᵥ c) := vadd_vsub _ _ @[simp] lemma inversion_self (c : P) (R : ℝ) : inversion c R c = c := by simp [inversion] @[simp] lemma inversion_dist_center (c x : P) : inversion c (dist x c) x = x := begin rcases eq_or_ne x c with rfl|hne, { apply inversion_self }, { rw [inversion, div_self, one_pow, one_smul, vsub_vadd], rwa [dist_ne_zero] } end lemma inversion_of_mem_sphere (h : x ∈ metric.sphere c R) : inversion c R x = x := h.out ▸ inversion_dist_center c x /-- Distance from the image of a point under inversion to the center. This formula accidentally works for `x = c`. -/ lemma dist_inversion_center (c x : P) (R : ℝ) : dist (inversion c R x) c = R ^ 2 / dist x c := begin rcases eq_or_ne x c with (rfl|hx), { simp }, have : dist x c ≠ 0, from dist_ne_zero.2 hx, field_simp [inversion, norm_smul, abs_div, ← dist_eq_norm_vsub, sq, mul_assoc] end /-- Distance from the center of an inversion to the image of a point under the inversion. This formula accidentally works for `x = c`. -/ lemma dist_center_inversion (c x : P) (R : ℝ) : dist c (inversion c R x) = R ^ 2 / dist c x := by rw [dist_comm c, dist_comm c, dist_inversion_center] @[simp] lemma inversion_inversion (c : P) {R : ℝ} (hR : R ≠ 0) (x : P) : inversion c R (inversion c R x) = x := begin rcases eq_or_ne x c with rfl|hne, { rw [inversion_self, inversion_self] }, { rw [inversion, dist_inversion_center, inversion_vsub_center, smul_smul, ← mul_pow, div_mul_div_comm, div_mul_cancel _ (dist_ne_zero.2 hne), ← sq, div_self, one_pow, one_smul, vsub_vadd], exact pow_ne_zero _ hR } end lemma inversion_involutive (c : P) {R : ℝ} (hR : R ≠ 0) : involutive (inversion c R) := inversion_inversion c hR lemma inversion_surjective (c : P) {R : ℝ} (hR : R ≠ 0) : surjective (inversion c R) := (inversion_involutive c hR).surjective lemma inversion_injective (c : P) {R : ℝ} (hR : R ≠ 0) : injective (inversion c R) := (inversion_involutive c hR).injective lemma inversion_bijective (c : P) {R : ℝ} (hR : R ≠ 0) : bijective (inversion c R) := (inversion_involutive c hR).bijective /-- Distance between the images of two points under an inversion. -/ lemma dist_inversion_inversion (hx : x ≠ c) (hy : y ≠ c) (R : ℝ) : dist (inversion c R x) (inversion c R y) = (R ^ 2 / (dist x c * dist y c)) * dist x y := begin dunfold inversion, simp_rw [dist_vadd_cancel_right, dist_eq_norm_vsub V _ c], simpa only [dist_vsub_cancel_right] using dist_div_norm_sq_smul (vsub_ne_zero.2 hx) (vsub_ne_zero.2 hy) R end /-- **Ptolemy's inequality**: in a quadrangle `ABCD`, `|AC| * |BD| ≤ |AB| * |CD| + |BC| * |AD|`. If `ABCD` is a convex cyclic polygon, then this inequality becomes an equality, see `euclidean_geometry.mul_dist_add_mul_dist_eq_mul_dist_of_cospherical`. -/ lemma mul_dist_le_mul_dist_add_mul_dist (a b c d : P) : dist a c * dist b d ≤ dist a b * dist c d + dist b c * dist a d := begin -- If one of the points `b`, `c`, `d` is equal to `a`, then the inequality is trivial. rcases eq_or_ne b a with rfl|hb, { rw [dist_self, zero_mul, zero_add] }, rcases eq_or_ne c a with rfl|hc, { rw [dist_self, zero_mul], apply_rules [add_nonneg, mul_nonneg, dist_nonneg] }, rcases eq_or_ne d a with rfl|hd, { rw [dist_self, mul_zero, add_zero, dist_comm d, dist_comm d, mul_comm] }, /- Otherwise, we apply the triangle inequality to `euclidean_geometry.inversion a 1 b`, `euclidean_geometry.inversion a 1 c`, and `euclidean_geometry.inversion a 1 d`. -/ have H := dist_triangle (inversion a 1 b) (inversion a 1 c) (inversion a 1 d), rw [dist_inversion_inversion hb hd, dist_inversion_inversion hb hc, dist_inversion_inversion hc hd, one_pow] at H, rw [← dist_pos] at hb hc hd, rw [← div_le_div_right (mul_pos hb (mul_pos hc hd))], convert H; { field_simp [hb.ne', hc.ne', hd.ne', dist_comm a], ring } end end euclidean_geometry
55171b1aeae2da558e63b463543ac39eb91f1411
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/ac_rfl.lean
d7e2df4677db6d4dc87cdaaf544090c2fe0b0752
[ "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
3,973
lean
open Lean instance : IsAssociative (α := Nat) HAdd.hAdd := ⟨Nat.add_assoc⟩ instance : IsCommutative (α := Nat) HAdd.hAdd := ⟨Nat.add_comm⟩ instance : IsNeutral HAdd.hAdd 0 := ⟨Nat.zero_add, Nat.add_zero⟩ instance : IsAssociative (α := Nat) HMul.hMul := ⟨Nat.mul_assoc⟩ instance : IsCommutative (α := Nat) HMul.hMul := ⟨Nat.mul_comm⟩ instance : IsNeutral HMul.hMul 1 := ⟨Nat.one_mul, Nat.mul_one⟩ theorem max_assoc (n m k : Nat) : max (max n m) k = max n (max m k) := by simp [max]; split <;> split <;> try split <;> try rfl case inl hmn nhkn hkm => exact False.elim $ nhkn $ Nat.lt_trans hkm hmn . rfl case inl nhmn nhkm hkn => apply absurd hkn; simp [Nat.not_lt_eq] at *; exact Nat.le_trans nhmn nhkm theorem max_comm (n m : Nat) : max n m = max m n := by simp [max]; split <;> split <;> try rfl case inl h₁ h₂ => apply absurd (Nat.lt_trans h₁ h₂); apply Nat.lt_irrefl case inr h₁ h₂ => simp [Nat.not_lt_eq] at *; apply Nat.le_antisymm <;> assumption theorem max_idem (n : Nat) : max n n = n := by simp [max] theorem Nat.zero_max (n : Nat) : max 0 n = n := by simp [max]; rfl theorem Nat.max_zero (n : Nat) : max n 0 = n := by rw [max_comm, Nat.zero_max] instance : IsAssociative (α := Nat) max := ⟨max_assoc⟩ instance : IsCommutative (α := Nat) max := ⟨max_comm⟩ instance : IsIdempotent (α := Nat) max := ⟨max_idem⟩ instance : IsNeutral max 0 := ⟨Nat.zero_max, Nat.max_zero⟩ instance : IsAssociative And := ⟨λ p q r => propext ⟨λ ⟨⟨hp, hq⟩, hr⟩ => ⟨hp, hq, hr⟩, λ ⟨hp, hq, hr⟩ => ⟨⟨hp, hq⟩, hr⟩⟩⟩ instance : IsCommutative And := ⟨λ p q => propext ⟨λ ⟨hp, hq⟩ => ⟨hq, hp⟩, λ ⟨hq, hp⟩ => ⟨hp, hq⟩⟩⟩ instance : IsIdempotent And := ⟨λ p => propext ⟨λ ⟨hp, _⟩ => hp, λ hp => ⟨hp, hp⟩⟩⟩ instance : IsNeutral And True := ⟨λ p => propext ⟨λ ⟨_, hp⟩ => hp, λ hp => ⟨True.intro, hp⟩⟩, λ p => propext ⟨λ ⟨hp, _⟩ => hp, λ hp => ⟨hp, True.intro⟩⟩⟩ theorem or_assoc (p q r : Prop) : ((p ∨ q) ∨ r) = (p ∨ q ∨ r) := propext ⟨λ hpqr => hpqr.elim (λ hpq => hpq.elim Or.inl $ λ hq => Or.inr $ Or.inl hq) $ λ hr => Or.inr $ Or.inr hr, λ hpqr => hpqr.elim (λ hp => Or.inl $ Or.inl hp) $ λ hqr => hqr.elim (λ hq => Or.inl $ Or.inr hq) Or.inr⟩ instance : IsAssociative Or := ⟨or_assoc⟩ instance : IsCommutative Or := ⟨λ p q => propext ⟨λ hpq => hpq.elim Or.inr Or.inl, λ hqp => hqp.elim Or.inr Or.inl⟩⟩ instance : IsIdempotent Or := ⟨λ p => propext ⟨λ hp => hp.elim id id, Or.inl⟩⟩ instance : IsNeutral Or False := ⟨λ p => propext ⟨λ hfp => hfp.elim False.elim id, Or.inr⟩, λ p => propext ⟨λ hpf => hpf.elim id False.elim, Or.inl⟩⟩ example (x y z : Nat) : x + y + 0 + z = z + (x + y) := by ac_rfl example (x y z : Nat) : (x + y) * (0 + z) = (x + y) * z:= by ac_rfl example (x y z : Nat) : (x + y) * (0 + z) = 1 * z * (y + 0 + x) := by ac_rfl theorem ex₁ (x y z : Nat) : max (0 + (max x (max z (max (0 + 0) ((max 1 0) + 0 + 0) * y)))) y = max (max x y) z := by ac_rfl #print ex₁ example (x y : Nat) : 1 + 0 + 0 = 0 + 1 := by ac_rfl example (x y : Nat) : (x + y = 42) = (y + x = 42) := by ac_rfl example (x y : Nat) (P : Prop) : (x + y = 42 → P) = (y + x = 42 → P) := by ac_rfl inductive Vector (α : Type u) : Nat → Type u where | nil : Vector α 0 | cons : α → Vector α n → Vector α (n+1) def f (n : Nat) (xs : Vector α n) := xs -- Repro: Dependent types trigger incorrect proofs theorem ex₂ (n m : Nat) (xs : Vector α (n+m)) (ys : Vector α (m+n)) : (f (n+m) xs, f (m+n) ys, n+m) = (f (n+m) xs, f (m+n) ys, m+n) := by ac_rfl -- Repro: Binders also trigger invalid proofs theorem ex₃ (n : Nat) : (fun x => n + x) = (fun x => x + n) := by ac_rfl #print ex₃ -- Repro: the Prop universe doesn't work example (p q : Prop) : (p ∨ p ∨ q ∧ True) = (q ∨ p) := by ac_rfl
d0e1fef771a1e44ceceb182c5d30d64f553cc00b
46125763b4dbf50619e8846a1371029346f4c3db
/src/measure_theory/probability_mass_function.lean
89b155b6f090fc34a4db7c9c10232f838e61a2d9
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
4,785
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Probability mass function -- discrete probability measures -/ import topology.instances.nnreal topology.instances.ennreal topology.algebra.infinite_sum noncomputable theory variables {α : Type*} {β : Type*} {γ : Type*} open_locale classical /-- Probability mass functions, i.e. discrete probability measures -/ def {u} pmf (α : Type u) : Type u := { f : α → nnreal // has_sum f 1 } namespace pmf instance : has_coe_to_fun (pmf α) := ⟨λp, α → nnreal, λp a, p.1 a⟩ @[ext] protected lemma ext : ∀{p q : pmf α}, (∀a, p a = q a) → p = q | ⟨f, hf⟩ ⟨g, hg⟩ eq := subtype.eq $ funext eq lemma has_sum_coe_one (p : pmf α) : has_sum p 1 := p.2 lemma summable_coe (p : pmf α) : summable p := summable_spec p.has_sum_coe_one @[simp] lemma tsum_coe (p : pmf α) : (∑a, p a) = 1 := tsum_eq_has_sum p.has_sum_coe_one def support (p : pmf α) : set α := {a | p.1 a ≠ 0} def pure (a : α) : pmf α := ⟨λa', if a' = a then 1 else 0, has_sum_ite_eq _ _⟩ @[simp] lemma pure_apply (a a' : α) : pure a a' = (if a' = a then 1 else 0) := rfl instance [inhabited α] : inhabited (pmf α) := ⟨pure (default α)⟩ lemma coe_le_one (p : pmf α) (a : α) : p a ≤ 1 := has_sum_le (by intro b; split_ifs; simp [h]; exact le_refl _) (has_sum_ite_eq a (p a)) p.2 protected lemma bind.summable (p : pmf α) (f : α → pmf β) (b : β) : summable (λa:α, p a * f a b) := begin refine nnreal.summable_of_le (assume a, _) p.summable_coe, suffices : p a * f a b ≤ p a * 1, { simpa }, exact mul_le_mul_of_nonneg_left ((f a).coe_le_one _) (p a).2 end def bind (p : pmf α) (f : α → pmf β) : pmf β := ⟨λb, (∑a, p a * f a b), begin apply ennreal.has_sum_coe.1, simp only [ennreal.coe_tsum (bind.summable p f _)], rw [has_sum_iff_of_summable ennreal.summable, ennreal.tsum_comm], simp [ennreal.mul_tsum, (ennreal.coe_tsum (f _).summable_coe).symm, (ennreal.coe_tsum p.summable_coe).symm] end⟩ @[simp] lemma bind_apply (p : pmf α) (f : α → pmf β) (b : β) : p.bind f b = (∑a, p a * f a b) := rfl lemma coe_bind_apply (p : pmf α) (f : α → pmf β) (b : β) : (p.bind f b : ennreal) = (∑a, p a * f a b) := eq.trans (ennreal.coe_tsum $ bind.summable p f b) $ by simp @[simp] lemma pure_bind (a : α) (f : α → pmf β) : (pure a).bind f = f a := have ∀b a', ite (a' = a) 1 0 * f a' b = ite (a' = a) (f a b) 0, from assume b a', by split_ifs; simp; subst h; simp, by ext b; simp [this] @[simp] lemma bind_pure (p : pmf α) : p.bind pure = p := have ∀a a', (p a * ite (a' = a) 1 0) = ite (a = a') (p a') 0, from assume a a', begin split_ifs; try { subst a }; try { subst a' }; simp * at * end, by ext b; simp [this] @[simp] lemma bind_bind (p : pmf α) (f : α → pmf β) (g : β → pmf γ) : (p.bind f).bind g = p.bind (λa, (f a).bind g) := begin ext b, simp only [ennreal.coe_eq_coe.symm, coe_bind_apply, ennreal.mul_tsum.symm, ennreal.tsum_mul.symm], rw [ennreal.tsum_comm], simp [mul_assoc, mul_left_comm, mul_comm] end lemma bind_comm (p : pmf α) (q : pmf β) (f : α → β → pmf γ) : p.bind (λa, q.bind (f a)) = q.bind (λb, p.bind (λa, f a b)) := begin ext b, simp only [ennreal.coe_eq_coe.symm, coe_bind_apply, ennreal.mul_tsum.symm, ennreal.tsum_mul.symm], rw [ennreal.tsum_comm], simp [mul_assoc, mul_left_comm, mul_comm] end def map (f : α → β) (p : pmf α) : pmf β := bind p (pure ∘ f) lemma bind_pure_comp (f : α → β) (p : pmf α) : bind p (pure ∘ f) = map f p := rfl lemma map_id (p : pmf α) : map id p = p := by simp [map] lemma map_comp (p : pmf α) (f : α → β) (g : β → γ) : (p.map f).map g = p.map (g ∘ f) := by simp [map] lemma pure_map (a : α) (f : α → β) : (pure a).map f = pure (f a) := by simp [map] def seq (f : pmf (α → β)) (p : pmf α) : pmf β := f.bind (λm, p.bind $ λa, pure (m a)) def of_multiset (s : multiset α) (hs : s ≠ 0) : pmf α := ⟨λa, s.count a / s.card, have s.to_finset.sum (λa, (s.count a : ℝ) / s.card) = 1, by simp [div_eq_inv_mul, finset.mul_sum.symm, (finset.sum_nat_cast _ _).symm, hs], have s.to_finset.sum (λa, (s.count a : nnreal) / s.card) = 1, by rw [← nnreal.eq_iff, nnreal.coe_one, ← this, nnreal.coe_sum]; simp, begin rw ← this, apply has_sum_sum_of_ne_finset_zero, simp {contextual := tt}, end⟩ def of_fintype [fintype α] (f : α → nnreal) (h : finset.univ.sum f = 1) : pmf α := ⟨f, h ▸ has_sum_sum_of_ne_finset_zero (by simp)⟩ def bernoulli (p : nnreal) (h : p ≤ 1) : pmf bool := of_fintype (λb, cond b p (1 - p)) (nnreal.eq $ by simp [h]) end pmf
6b8f53bf5e7571f137df5ebada6cb5b6a0bbdb7e
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/Meta/Tactic/Injection.lean
89f4fbdcb5f6b44755158c3af8c900627d730a55
[ "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
4,146
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.AppBuilder import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.Assert import Lean.Meta.Tactic.Intro namespace Lean.Meta def getCtorNumPropFields (ctorInfo : ConstructorVal) : MetaM Nat := do forallTelescopeReducing ctorInfo.type fun xs _ => do let mut numProps := 0 for i in [:ctorInfo.numFields] do if (← isProp (← inferType xs[ctorInfo.numParams + i])) then numProps := numProps + 1 return numProps inductive InjectionResultCore where | solved | subgoal (mvarId : MVarId) (numNewEqs : Nat) def injectionCore (mvarId : MVarId) (fvarId : FVarId) : MetaM InjectionResultCore := withMVarContext mvarId do checkNotAssigned mvarId `injection let decl ← getLocalDecl fvarId let type ← whnf decl.type match type.eq? with | none => throwTacticEx `injection mvarId "equality expected" | some (α, a, b) => let a ← whnf a let b ← whnf b let target ← getMVarType mvarId let env ← getEnv match a.isConstructorApp? env, b.isConstructorApp? env with | some aCtor, some bCtor => let val ← mkNoConfusion target (mkFVar fvarId) if aCtor.name != bCtor.name then assignExprMVar mvarId val pure InjectionResultCore.solved else let valType ← inferType val let valType ← whnf valType match valType with | Expr.forallE _ newTarget _ _ => let newTarget := newTarget.headBeta let tag ← getMVarTag mvarId let newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag assignExprMVar mvarId (mkApp val newMVar) let mvarId ← tryClear newMVar.mvarId! fvarId /- Recall that `noConfusion` does not include equalities for propositions since they are trivial due to proof irrelevance. -/ let numPropFields ← getCtorNumPropFields aCtor return InjectionResultCore.subgoal mvarId (aCtor.numFields - numPropFields) | _ => throwTacticEx `injection mvarId "ill-formed noConfusion auxiliary construction" | _, _ => throwTacticEx `injection mvarId "equality of constructor applications expected" inductive InjectionResult where | solved | subgoal (mvarId : MVarId) (newEqs : Array FVarId) (remainingNames : List Name) private def heqToEq (mvarId : MVarId) (fvarId : FVarId) : MetaM (FVarId × MVarId) := withMVarContext mvarId do let decl ← getLocalDecl fvarId let type ← whnf decl.type match type.heq? with | none => pure (fvarId, mvarId) | some (α, a, β, b) => if (← isDefEq α β) then let pr ← mkEqOfHEq (mkFVar fvarId) let eq ← mkEq a b let mvarId ← assert mvarId decl.userName eq pr let mvarId ← clear mvarId fvarId let (fvarId, mvarId) ← intro1P mvarId pure (fvarId, mvarId) else pure (fvarId, mvarId) def injectionIntro : Nat → MVarId → Array FVarId → List Name → MetaM InjectionResult | 0, mvarId, fvarIds, remainingNames => pure $ InjectionResult.subgoal mvarId fvarIds remainingNames | n+1, mvarId, fvarIds, name::remainingNames => do let (fvarId, mvarId) ← intro mvarId name let (fvarId, mvarId) ← heqToEq mvarId fvarId injectionIntro n mvarId (fvarIds.push fvarId) remainingNames | n+1, mvarId, fvarIds, [] => do let (fvarId, mvarId) ← intro1 mvarId let (fvarId, mvarId) ← heqToEq mvarId fvarId injectionIntro n mvarId (fvarIds.push fvarId) [] def injection (mvarId : MVarId) (fvarId : FVarId) (newNames : List Name := []) (useUnusedNames : Bool := true) : MetaM InjectionResult := do match (← injectionCore mvarId fvarId) with | InjectionResultCore.solved => pure InjectionResult.solved | InjectionResultCore.subgoal mvarId numEqs => trace[Meta.debug]! "before injectionIntro\n{MessageData.ofGoal mvarId}" injectionIntro numEqs mvarId #[] newNames end Lean.Meta
8346be9be1a52b4e36d38317fcc99e8ff9fa60d8
c0807e3249ba126be15fd22370e98ae1a6dac0f0
/tests/Tests.lean
513378e4da701e92fc9586b4a71409663d7afae3
[]
no_license
tydeu/lean-blake3
1297035629db93e13ffa6a4381f9a0dc496f796f
26fab13b7d37a7c473bd3aaf8a585c5b44b64839
refs/heads/main
1,693,063,372,343
1,634,843,383,000
1,634,843,383,000
419,835,491
1
0
null
null
null
null
UTF-8
Lean
false
false
23
lean
import Tests.HashString
0e74b50e4cbce039b24fb1e76ef1055e500f0efa
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/autoBoundImplicits1.lean
7352980ebf380c74ba7ae5589fa5ca8e3321c94f
[ "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
2,353
lean
def myid (a : α) := a -- works set_option relaxedAutoImplicit false #check myid 10 #check myid true theorem ex1 (a : α) : myid a = a := rfl def cnst (b : β) : α → β := fun _ => b -- works theorem ex2 (b : β) (a : α) : cnst b a = b := rfl def Vec (α : Type) (n : Nat) := { a : Array α // a.size = n } def mkVec : Vec α 0 := ⟨ #[], rfl ⟩ def Vec.map (xs : Vec α n) (f : α → β) : Vec β n := ⟨ xs.val.map f, sorry ⟩ /- unbound implicit locals must be greek or lower case letters followed by numerical digits -/ def Vec.map2 (xs : Vec α size /- error: unknown identifier size -/) (f : α → β) : Vec β n := ⟨ xs.val.map f, sorry ⟩ set_option autoImplicit false in def Vec.map3 (xs : Vec α n) (f : α → β) : Vec β n := -- Errors, unknown identifiers 'α', 'n', 'β' ⟨ xs.val.map f, sorry ⟩ def double [Add α] (a : α) := a + a variable (xs : Vec α n) -- works def f := xs #check @f #check f mkVec #check f (α := Nat) mkVec def g (a : α) := xs.val.push a theorem ex3 : g ⟨#[0], rfl⟩ 1 = #[0, 1] := rfl inductive Tree (α β : Type) := | leaf1 : α → Tree α β | leaf2 : β → Tree α β | node : Tree α β → Tree α β → Tree α β inductive TreeElem1 : α → Tree α β → Prop | leaf1 : (a : α) → TreeElem1 a (Tree.leaf1 (β := β) a) | nodeLeft : (a : α) → (left : Tree α β) → (right : Tree α β) → TreeElem1 a left → TreeElem1 a (Tree.node left right) | nodeRight : (a : α) → (left : Tree α β) → (right : Tree α β) → TreeElem1 a right → TreeElem1 a (Tree.node left right) inductive TreeElem2 : β → Tree α β → Prop | leaf2 : (b : β) → TreeElem2 b (Tree.leaf2 (α := α) b) | nodeLeft : (b : β) → (left : Tree α β) → (right : Tree α β) → TreeElem2 b left → TreeElem2 b (Tree.node left right) | nodeRight : (b : β) → (left : Tree α β) → (right : Tree α β) → TreeElem2 b right → TreeElem2 b (Tree.node left right) namespace Ex1 def findSomeRevM? [Monad m] (as : Array α) (f : α → m (Option β)) : m (Option β) := pure none def findSomeRev? (as : Array α) (f : α → Option β) : Option β := Id.run <| findSomeRevM? as f end Ex1 def apply {α : Type u₁} {β : α → Type u₂} (f : (a : α) → β a) (a : α) : β a := f a def pair (a : α₁) := (a, a)
9beb8aaeeff36b7321504df139421894613c9455
a721fe7446524f18ba361625fc01033d9c8b7a78
/elaborate/add_comm_0.lean
c97fcf277084ed25b8999b4ccd87e248c209891a
[]
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
1,311
lean
λ (m n : mynat), mynat.rec (eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) (mynat.rec (eq.refl zero) (λ (m_n : mynat) (m_ih : add zero m_n = m_n), eq.rec (eq.refl (succ m_n)) (eq.rec (eq.refl (succ (add zero m_n) = succ m_n)) (eq.rec (eq.refl (succ (add zero m_n) = succ m_n)) m_ih))) m)))) (λ (n_n : mynat) (n_ih : add m n_n = add n_n m), eq.rec (eq.rec (eq.refl (succ (add n_n m))) (eq.rec (eq.refl (succ (add m n_n) = succ (add n_n m))) (eq.rec (eq.refl (succ (add m n_n) = succ (add n_n m))) n_ih))) (eq.rec (eq.refl (succ (add m n_n) = add (succ n_n) m)) (eq.rec (eq.refl (succ (add m n_n) = add (succ n_n) m)) (mynat.rec (eq.refl (succ n_n)) (λ (n_n_1 : mynat) (n_ih : add (succ n_n) n_n_1 = succ (add n_n n_n_1)), eq.rec (eq.refl (succ (succ (add n_n n_n_1)))) (eq.rec (eq.refl (succ (add (succ n_n) n_n_1) = succ (succ (add n_n n_n_1)))) (eq.rec (eq.refl (succ (add (succ n_n) n_n_1) = succ (succ (add n_n n_n_1)))) n_ih))) m)))) n
815bc1a36a4d56d23ca34116041cfeac87e5a85d
36938939954e91f23dec66a02728db08a7acfcf9
/lean4/app/SMTParser.lean
aa1527bb17f0ff9d2d2f8d1da821da2918986a00
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
7,862
lean
import Galois.Data.RBMap import Galois.Data.SExp import SMTLIB.Syntax import LeanLLVM.AST import X86Semantics.Common import ReoptVCG.WordSize namespace ReoptVCG section universes u v open WellFormedSExp open SMT ------------------------------------------------------------------------ -- Expression inductive Atom : Type u | nat : Nat → Atom | ident : String → Atom def Atom.toString : Atom → String | Atom.nat n => n.repr | Atom.ident nm => nm instance Atom.hasToString : HasToString Atom := ⟨Atom.toString⟩ def readAtom (str : String) : Except String Atom := if str.isEmpty then Except.error "an Atom must contain one or more characters" else match str.toSubstring.toNat? with | some n => pure $ Atom.nat n | none => pure $ Atom.ident str def readSExp (str:String) : Except String (SExp Atom) := do ss ← WellFormedSExp.SExp.readSExps readAtom str; match ss with | [] => Except.error "no s-expressions were found in the string" | [s] => pure s | _ => Except.error $ "multiple s-expressions were found in the string: " ++ str /-- An expression in the SMT bitvector theory with variables/constants which may appear in block preconditions. --/ inductive BlockExpr : sort → Type u | stackHigh : BlockExpr sort.bv64 -- ^ Denotes the high address on the stack. -- -- This is the address the return address is stored at, and -- the curent frame. | initGPReg64 : x86.reg64 → BlockExpr sort.bv64 -- ^ Denotes the value of a 64-bit general purpose register -- at the start of the block execution. | fnStartGPReg64 : x86.reg64 → BlockExpr sort.bv64 -- ^ Denotes the value of a general purpose when the function starts. -- -- Note. We do not support all registers here, only the registers -- in `calleeSavedGPRegs` | mcStack (a : BlockExpr sort.bv64) (w:WordSize) : BlockExpr w.sort -- ^ @MCStack a w@ denotes @w@-bit value stored at the address @a@. -- -- The width @w@ should be @8@, @16@, @32@, or @64@. -- -- Our memory model only tracks the mc-only variables, so if the -- address is not a stack-only variable, then the value just -- means some arbitrary value. | llvmVar (nm : LLVM.Ident) (tp : sort) : BlockExpr tp -- ^ This denotes the value of an LLVM Phi variable when the -- block starts. | eq {tp : sort} : BlockExpr tp → BlockExpr tp → BlockExpr sort.smt_bool | bvAdd {n : Nat} : BlockExpr (sort.bitvec n) → BlockExpr (sort.bitvec n) → BlockExpr (sort.bitvec n) | bvSub {n : Nat} : BlockExpr (sort.bitvec n) → BlockExpr (sort.bitvec n) → BlockExpr (sort.bitvec n) -- | @BVDecimal v w@ denotes the @w@-bit value @v@ which should -- satisfy the property that @v < 2^w@. | bvDecimal (v w : Nat) : BlockExpr (sort.bitvec w) /-- Map from LLVM ident names to their sorts--/ abbrev LLVMTyEnv := RBMap LLVM.Ident SMT.sort (λ x y => x<y) namespace BlockExpr private def ppLLVMIdent : LLVM.Ident → String | LLVM.Ident.named nm => nm | LLVM.Ident.anon n => n.repr -- was `evalExpr` partial def fromSExp (llvmTyEnv : LLVMTyEnv) : (SExp Atom) → Except String (Sigma BlockExpr) | SExp.list [SExp.atom (Atom.ident "="), x, y] => do ⟨xtp, xe⟩ ← fromSExp x; ⟨ytp, ye⟩ ← fromSExp y; if h : (xtp = ytp) then let hEq : BlockExpr xtp = BlockExpr ytp := h ▸ rfl; Except.ok ⟨sort.smt_bool, eq (cast hEq xe) ye⟩ else Except.error $ "The two operands in the term `" ++ (SExp.list [SExp.atom (Atom.ident "="), x, y]).toString ++ "` must have the same type, but the first" ++ " was of type " ++ xtp.toString ++ " and the second was of type " ++ ytp.toString | SExp.list [SExp.atom (Atom.ident "bvsub"), x, y] => do xRes ← fromSExp x; yRes ← fromSExp y; match xRes, yRes with | ⟨sort.bitvec xw, xe⟩, ⟨sort.bitvec yw, ye⟩ => if h : xw = yw then let hEq : BlockExpr (sort.bitvec xw) = BlockExpr (sort.bitvec yw) := h ▸ rfl; Except.ok ⟨sort.bitvec yw, bvSub (cast hEq xe) ye⟩ else Except.error $ "The two operands in the term `" ++ (SExp.list [SExp.atom (Atom.ident "bvsub"), x, y]).toString ++ "` must both be bitvectors of the same length, but the first" ++ " was of length " ++ xw.repr ++ " and the second was of length " ++ yw.repr | ⟨xtp, xe⟩, ⟨ytp, ye⟩ => Except.error $ "The two operands in the term `" ++ (SExp.list [SExp.atom (Atom.ident "bvsub"), x, y]).toString ++ "` must both be bitvectors of the same length, but the first" ++ " was of type " ++ xtp.toString ++ " and the second was of type " ++ ytp.toString | (SExp.list [SExp.atom (Atom.ident "_"), SExp.atom (Atom.ident bvLit), SExp.atom (Atom.nat width)]) => match bvLit.data with | 'b'::'v'::nChars => let nStr := nChars.asString; match nStr.toSubstring.toNat? with | some n => let val : Nat := Nat.land n $ (Nat.pow 2 width) - 1; Except.ok ⟨sort.bitvec width, bvDecimal val width⟩ | none => Except.error $ "a bitvector literal should have a natural number adjacent to `bv`" ++ " but found " ++ bvLit | _ => Except.error $ "unrecognized SMT expression: " ++ bvLit | SExp.list [SExp.atom (Atom.ident "mcstack"), sa, sw] => do ⟨tp, a⟩ ← fromSExp sa; if h : tp = sort.bv64 then do let hEq : BlockExpr tp = BlockExpr sort.bv64 := h ▸ rfl; w ← match sw with | SExp.list [SExp.atom (Atom.ident "_"), SExp.atom (Atom.ident "BitVec"), SExp.atom (Atom.nat w)] => match WordSize.fromNat w with | some width => Except.ok width | none => Except.error "mcstack could not interpret memory type." | _ => Except.error "mcstack could not interpret memory type"; Except.ok ⟨w.sort, BlockExpr.mcStack (cast hEq a) w⟩ else Except.error $ "Expected 64-bit address as first argument to mcstack" ++ " but found a " ++ tp.toString | SExp.list [SExp.atom (Atom.ident "fnstart"), regExpr] => match regExpr with | SExp.atom (Atom.ident regName) => match x86.reg64.fromName regName with | some r => Except.ok ⟨sort.bv64, BlockExpr.fnStartGPReg64 r⟩ | none => Except.error $ "could not interpret register name " ++ regName | _ => Except.error $ "could not interpret register name " ++ regExpr.toString | SExp.list [SExp.atom (Atom.ident "llvm"), llvmExpr] => match llvmExpr with | SExp.atom (Atom.ident llvmName) => let nm := LLVM.Ident.named llvmName; match llvmTyEnv.find? nm with | some tp => Except.ok ⟨tp, BlockExpr.llvmVar nm tp⟩ | none => Except.error $ "Could not interpret llvm variable " ++ llvmExpr.toString ++ "\nKnown variables: " ++ (llvmTyEnv.keys.map ppLLVMIdent).toString | _ => Except.error $ "Could not interpret llvm variable " ++ llvmExpr.toString ++ "\nKnown variables: " ++ (llvmTyEnv.keys.map ppLLVMIdent).toString | SExp.atom (Atom.ident "stack_high") => Except.ok ⟨sort.bv64, BlockExpr.stackHigh⟩ | SExp.atom (Atom.ident nm) => match x86.reg64.fromName nm with | some r => Except.ok ⟨sort.bv64, BlockExpr.initGPReg64 r⟩ | none => Except.error $ "Could not interpret identifier as a variable: " ++ nm | sexpr => Except.error $ "Could not interpret expression: " ++ sexpr.toString -- was simply `fromText` in Haskell, was a moment ago in lean4 `Expr.fromString` def parseAs (tp : sort) (llvmTyEnv : LLVMTyEnv) (input : String) : Except String (BlockExpr tp) := do ⟨tp', e⟩ ← readSExp input >>= fromSExp llvmTyEnv; if h : tp' = tp then let hEq : BlockExpr tp' = BlockExpr tp := h ▸ rfl; Except.ok $ cast hEq e else Except.error $ "expected " ++ input ++ " to be of type " ++ tp.toString ++ ", but it is of type " ++ tp'.toString end BlockExpr end end ReoptVCG
9f4d02fb4edc87ed6d8782f56a886cbee9b61740
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/init/pointed.hlean
4b0cc5eb7e0a72121443a961c0e3f07b73a0956f
[ "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
3,605
hlean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn The definition of pointed types. This file is here to avoid circularities in the import graph -/ prelude import init.trunc open eq equiv is_equiv is_trunc structure pointed [class] (A : Type) := (point : A) structure pType := (carrier : Type) (Point : carrier) notation `Type*` := pType namespace pointed attribute pType.carrier [coercion] variables {A : Type} definition pt [reducible] [unfold 2] [H : pointed A] := point A definition Point [reducible] [unfold 1] (A : Type*) := pType.Point A abbreviation carrier [unfold 1] (A : Type*) := pType.carrier A protected definition Mk [constructor] {A : Type} (a : A) := pType.mk A a protected definition MK [constructor] (A : Type) (a : A) := pType.mk A a protected definition mk' [constructor] (A : Type) [H : pointed A] : Type* := pType.mk A (point A) definition pointed_carrier [instance] [constructor] (A : Type*) : pointed A := pointed.mk (Point A) end pointed open pointed section universe variable u structure ptrunctype (n : trunc_index) extends trunctype.{u} n, pType.{u} definition is_trunc_ptrunctype [instance] {n : ℕ₋₂} (X : ptrunctype n) : is_trunc n (ptrunctype.to_pType X) := trunctype.struct X end notation n `-Type*` := ptrunctype n abbreviation pSet [parsing_only] := 0-Type* notation `Set*` := pSet namespace pointed protected definition ptrunctype.mk' [constructor] (n : trunc_index) (A : Type) [pointed A] [is_trunc n A] : n-Type* := ptrunctype.mk A _ pt protected definition pSet.mk [constructor] := @ptrunctype.mk (-1.+1) protected definition pSet.mk' [constructor] := ptrunctype.mk' (-1.+1) definition ptrunctype_of_trunctype [constructor] {n : trunc_index} (A : n-Type) (a : A) : n-Type* := ptrunctype.mk A _ a definition ptrunctype_of_pType [constructor] {n : trunc_index} (A : Type*) (H : is_trunc n A) : n-Type* := ptrunctype.mk A _ pt definition pSet_of_Set [constructor] (A : Set) (a : A) : Set* := ptrunctype.mk A _ a definition pSet_of_pType [constructor] (A : Type*) (H : is_set A) : Set* := ptrunctype.mk A _ pt attribute ptrunctype._trans_of_to_pType ptrunctype.to_pType ptrunctype.to_trunctype [unfold 2] -- Any contractible type is pointed definition pointed_of_is_contr [instance] [priority 800] [constructor] (A : Type) [H : is_contr A] : pointed A := pointed.mk !center end pointed /- pointed maps -/ structure pmap (A B : Type*) := (to_fun : A → B) (resp_pt : to_fun (Point A) = Point B) namespace pointed abbreviation respect_pt [unfold 3] := @pmap.resp_pt notation `map₊` := pmap infix ` →* `:30 := pmap attribute pmap.to_fun [coercion] end pointed open pointed /- pointed homotopies -/ structure phomotopy {A B : Type*} (f g : A →* B) := (homotopy : f ~ g) (homotopy_pt : homotopy pt ⬝ respect_pt g = respect_pt f) namespace pointed variables {A B : Type*} {f g : A →* B} infix ` ~* `:50 := phomotopy abbreviation to_homotopy_pt [unfold 5] := @phomotopy.homotopy_pt abbreviation to_homotopy [coercion] [unfold 5] (p : f ~* g) : Πa, f a = g a := phomotopy.homotopy p /- pointed equivalences -/ structure pequiv (A B : Type*) extends equiv A B, pmap A B attribute pequiv._trans_of_to_pmap pequiv._trans_of_to_equiv pequiv.to_pmap pequiv.to_equiv [unfold 3] infix ` ≃* `:25 := pequiv attribute pequiv.to_pmap [coercion] attribute pequiv.to_is_equiv [instance] end pointed
d08ebf9b115d739366606fe6414f14330e195dc4
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/witt_vector/frobenius.lean
6d0ed81da992cdfbcc34e87783d0bf3c690adf36
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
13,300
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 data.nat.multiplicity import ring_theory.witt_vector.basic import ring_theory.witt_vector.is_poly /-! ## The Frobenius operator If `R` has characteristic `p`, then there is a ring endomorphism `frobenius R p` that raises `r : R` to the power `p`. By applying `witt_vector.map` to `frobenius R p`, we obtain a ring endomorphism `𝕎 R →+* 𝕎 R`. It turns out that this endomorphism can be described by polynomials over `ℤ` that do not depend on `R` or the fact that it has characteristic `p`. In this way, we obtain a Frobenius endomorphism `witt_vector.frobenius_fun : 𝕎 R → 𝕎 R` for every commutative ring `R`. Unfortunately, the aforementioned polynomials can not be obtained using the machinery of `witt_structure_int` that was developed in `structure_polynomial.lean`. We therefore have to define the polynomials by hand, and check that they have the required property. In case `R` has characteristic `p`, we show in `frobenius_fun_eq_map_frobenius` that `witt_vector.frobenius_fun` is equal to `witt_vector.map (frobenius R p)`. ### Main definitions and results * `frobenius_poly`: the polynomials that describe the coefficients of `frobenius_fun`; * `frobenius_fun`: the Frobenius endomorphism on Witt vectors; * `frobenius_fun_is_poly`: the tautological assertion that Frobenius is a polynomial function; * `frobenius_fun_eq_map_frobenius`: the fact that in characteristic `p`, Frobenius is equal to `witt_vector.map (frobenius R p)`. TODO: Show that `witt_vector.frobenius_fun` is a ring homomorphism, and bundle it into `witt_vector.frobenius`. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ namespace witt_vector variables {p : ℕ} {R S : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] local notation `𝕎` := witt_vector p -- type as `\bbW` noncomputable theory open mv_polynomial finset open_locale big_operators variables (p) include hp /-- The rational polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. These polynomials actually have integral coefficients, see `frobenius_poly` and `map_frobenius_poly`. -/ def frobenius_poly_rat (n : ℕ) : mv_polynomial ℕ ℚ := bind₁ (witt_polynomial p ℚ ∘ λ n, n + 1) (X_in_terms_of_W p ℚ n) lemma bind₁_frobenius_poly_rat_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly_rat p) (witt_polynomial p ℚ n) = (witt_polynomial p ℚ (n+1)) := begin delta frobenius_poly_rat, rw [← bind₁_bind₁, bind₁_X_in_terms_of_W_witt_polynomial, bind₁_X_right], end /-- An auxilliary definition, to avoid an excessive amount of finiteness proofs for `multiplicity p n`. -/ private def pnat_multiplicity (n : ℕ+) : ℕ := (multiplicity p n).get $ multiplicity.finite_nat_iff.mpr $ ⟨ne_of_gt hp.one_lt, n.2⟩ local notation `v` := pnat_multiplicity /-- An auxilliary polynomial over the integers, that satisfies `(frobenius_poly_aux p n - X n ^ p) / p = frobenius_poly p n`. This makes it easy to show that `frobenius_poly p n` is congruent to `X n ^ p` modulo `p`. -/ noncomputable def frobenius_poly_aux : ℕ → mv_polynomial ℕ ℤ | n := X (n + 1) - ∑ i : fin n, have _ := i.is_lt, ∑ j in range (p ^ (n - i)), (X i ^ p) ^ (p ^ (n - i) - (j + 1)) * (frobenius_poly_aux i) ^ (j + 1) * C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) * ↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) lemma frobenius_poly_aux_eq (n : ℕ) : frobenius_poly_aux p n = X (n + 1) - ∑ i in range n, ∑ j in range (p ^ (n - i)), (X i ^ p) ^ (p ^ (n - i) - (j + 1)) * (frobenius_poly_aux p i) ^ (j + 1) * C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) * ↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) := by { rw [frobenius_poly_aux, ← fin.sum_univ_eq_sum_range] } /-- The polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. -/ def frobenius_poly (n : ℕ) : mv_polynomial ℕ ℤ := X n ^ p + C ↑p * (frobenius_poly_aux p n) /- Our next goal is to prove ``` lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n ``` This lemma has a rather long proof, but it mostly boils down to applying induction, and then using the following two key facts at the right point. -/ /-- A key divisibility fact for the proof of `witt_vector.map_frobenius_poly`. -/ lemma map_frobenius_poly.key₁ (n j : ℕ) (hj : j < p ^ (n)) : p ^ (n - v p ⟨j + 1, j.succ_pos⟩) ∣ (p ^ n).choose (j + 1) := begin apply multiplicity.pow_dvd_of_le_multiplicity, have aux : (multiplicity p ((p ^ n).choose (j + 1))).dom, { rw [← multiplicity.finite_iff_dom, multiplicity.finite_nat_iff], exact ⟨hp.ne_one, nat.choose_pos hj⟩, }, rw [← enat.coe_get aux, enat.coe_le_coe, nat.sub_le_left_iff_le_add, ← enat.coe_le_coe, enat.coe_add, pnat_multiplicity, enat.coe_get, enat.coe_get, add_comm], exact (nat.prime.multiplicity_choose_prime_pow hp hj j.succ_pos).ge, end /-- A key numerical identity needed for the proof of `witt_vector.map_frobenius_poly`. -/ lemma map_frobenius_poly.key₂ {n i j : ℕ} (hi : i < n) (hj : j < p ^ (n - i)) : j - (v p ⟨j + 1, j.succ_pos⟩) + n = i + j + (n - i - v p ⟨j + 1, j.succ_pos⟩) := begin generalize h : (v p ⟨j + 1, j.succ_pos⟩) = m, suffices : m ≤ n - i ∧ m ≤ j, { cases this, unfreezingI { clear_dependent p }, omega }, split, { rw [← h, ← enat.coe_le_coe, pnat_multiplicity, enat.coe_get, ← (nat.prime.multiplicity_choose_prime_pow hp hj j.succ_pos)], apply le_add_left, refl }, { obtain ⟨c, hc⟩ : p ^ m ∣ j + 1, { rw [← h], exact multiplicity.pow_multiplicity_dvd _, }, obtain ⟨c, rfl⟩ : ∃ k : ℕ, c = k + 1, { apply nat.exists_eq_succ_of_ne_zero, rintro rfl, simpa only using hc }, rw [mul_add, mul_one] at hc, apply nat.le_of_lt_succ, calc m < p ^ m : nat.lt_pow_self hp.one_lt m ... ≤ j + 1 : by { rw ← nat.sub_eq_of_eq_add hc, apply nat.sub_le } } end lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n := begin rw [frobenius_poly, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_pow, map_C, map_X, ring_hom.eq_int_cast, int.cast_coe_nat, frobenius_poly_rat], apply nat.strong_induction_on n, clear n, intros n IH, rw [X_in_terms_of_W_eq], simp only [alg_hom.map_sum, alg_hom.map_sub, alg_hom.map_mul, alg_hom.map_pow, bind₁_C_right], have h1 : (↑p ^ n) * (⅟ (↑p : ℚ) ^ n) = 1 := by rw [←mul_pow, mul_inv_of_self, one_pow], rw [bind₁_X_right, function.comp_app, witt_polynomial_eq_sum_C_mul_X_pow, sum_range_succ, sum_range_succ, nat.sub_self, nat.add_sub_cancel_left, pow_zero, pow_one, pow_one, sub_mul, add_mul, add_mul, mul_assoc, mul_assoc, mul_comm _ (C (⅟ ↑p ^ n)), mul_comm _ (C (⅟ ↑p ^ n)), ←mul_assoc, ←mul_assoc, ←C_mul, ←C_mul, pow_succ, mul_assoc ↑p (↑p ^ n) (⅟ ↑p ^ n), h1, mul_one, C_1, one_mul, ←add_assoc, add_comm _ (X n ^ p), add_assoc, ←add_sub, add_right_inj], rw [frobenius_poly_aux_eq, ring_hom.map_sub, map_X, mul_sub, ←add_sub, sub_eq_add_neg, add_right_inj, neg_eq_iff_neg_eq, neg_sub], simp only [ring_hom.map_sum, mul_sum, sum_mul, ←sum_sub_distrib], apply sum_congr rfl, intros i hi, rw mem_range at hi, rw [← IH i hi], clear IH, rw [add_comm (X i ^ p), add_pow, sum_range_succ', pow_zero, nat.sub_zero, nat.choose_zero_right, one_mul, nat.cast_one, mul_one, mul_add, add_mul, nat.succ_sub (le_of_lt hi), nat.succ_eq_add_one (n - i), pow_succ, pow_mul, add_sub_cancel, mul_sum, sum_mul], apply sum_congr rfl, intros j hj, rw mem_range at hj, rw [ring_hom.map_mul, ring_hom.map_mul, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, map_C, map_X, mul_pow], rw [mul_comm (C ↑p ^ i), mul_comm _ ((X i ^ p) ^ _), mul_comm (C ↑p ^ (j + 1)), mul_comm (C ↑p)], simp only [mul_assoc], apply congr_arg, apply congr_arg, rw [←C_eq_coe_nat], simp only [←ring_hom.map_pow, ←C_mul], rw C_inj, simp only [inv_of_eq_inv, ring_hom.eq_int_cast, inv_pow', int.cast_coe_nat, nat.cast_mul], rw [rat.coe_nat_div _ _ (map_frobenius_poly.key₁ p (n - i) j hj)], simp only [nat.cast_pow, pow_add, pow_one], suffices : ((p ^ (n - i)).choose (j + 1) * p ^ (j - v p ⟨j + 1, j.succ_pos⟩) * p * p ^ n : ℚ) = p ^ j * p * ((p ^ (n - i)).choose (j + 1) * p ^ i) * p ^ (n - i - v p ⟨j + 1, j.succ_pos⟩), { have aux : ∀ k : ℕ, (p ^ k : ℚ) ≠ 0, { intro, apply pow_ne_zero, exact_mod_cast hp.ne_zero }, simpa [aux, -one_div] with field_simps using this.symm }, rw [mul_comm _ (p : ℚ), mul_assoc, mul_assoc, ← pow_add, map_frobenius_poly.key₂ p hi hj], ring_exp end lemma frobenius_poly_zmod (n : ℕ) : mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n) = X n ^ p := begin rw [frobenius_poly, ring_hom.map_add, ring_hom.map_pow, ring_hom.map_mul, map_X, map_C], simp only [int.cast_coe_nat, add_zero, ring_hom.eq_int_cast, zmod.nat_cast_self, zero_mul, C_0], end @[simp] lemma bind₁_frobenius_poly_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly p) (witt_polynomial p ℤ n) = (witt_polynomial p ℤ (n+1)) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_bind₁, map_frobenius_poly, bind₁_frobenius_poly_rat_witt_polynomial, map_witt_polynomial], end variables {p} /-- `frobenius_fun` is the function underlying the ring endomorphism `frobenius : 𝕎 R →+* frobenius 𝕎 R`. -/ def frobenius_fun (x : 𝕎 R) : 𝕎 R := mk p $ λ n, mv_polynomial.aeval x.coeff (frobenius_poly p n) lemma coeff_frobenius_fun (x : 𝕎 R) (n : ℕ) : coeff (frobenius_fun x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) := by rw [frobenius_fun, coeff_mk] variables (p) /-- `frobenius_fun` is tautologically a polynomial function. See also `frobenius_is_poly`. -/ @[is_poly] lemma frobenius_fun_is_poly : is_poly p (λ R _Rcr, @frobenius_fun p R _ _Rcr) := ⟨⟨frobenius_poly p, by { introsI, funext n, apply coeff_frobenius_fun }⟩⟩ variable {p} @[ghost_simps] lemma ghost_component_frobenius_fun (n : ℕ) (x : 𝕎 R) : ghost_component n (frobenius_fun x) = ghost_component (n + 1) x := by simp only [ghost_component_apply, frobenius_fun, coeff_mk, ← bind₁_frobenius_poly_witt_polynomial, aeval_bind₁] /-- If `R` has characteristic `p`, then there is a ring endomorphism that raises `r : R` to the power `p`. By applying `witt_vector.map` to this endomorphism, we obtain a ring endomorphism `frobenius R p : 𝕎 R →+* 𝕎 R`. The underlying function of this morphism is `witt_vector.frobenius_fun`. -/ def frobenius : 𝕎 R →+* 𝕎 R := { to_fun := frobenius_fun, map_zero' := begin refine is_poly.ext ((frobenius_fun_is_poly p).comp (witt_vector.zero_is_poly)) ((witt_vector.zero_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0, ghost_simp end, map_one' := begin refine is_poly.ext ((frobenius_fun_is_poly p).comp (witt_vector.one_is_poly)) ((witt_vector.one_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0, ghost_simp end, map_add' := by ghost_calc _ _; ghost_simp, map_mul' := by ghost_calc _ _; ghost_simp } lemma coeff_frobenius (x : 𝕎 R) (n : ℕ) : coeff (frobenius x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) := coeff_frobenius_fun _ _ @[ghost_simps] lemma ghost_component_frobenius (n : ℕ) (x : 𝕎 R) : ghost_component n (frobenius x) = ghost_component (n + 1) x := ghost_component_frobenius_fun _ _ variables (p) /-- `frobenius` is tautologically a polynomial function. -/ @[is_poly] lemma frobenius_is_poly : is_poly p (λ R _Rcr, @frobenius p R _ _Rcr) := frobenius_fun_is_poly _ section char_p variables [char_p R p] @[simp] lemma coeff_frobenius_char_p (x : 𝕎 R) (n : ℕ) : coeff (frobenius x) n = (x.coeff n) ^ p := begin rw [coeff_frobenius], -- outline of the calculation, proofs follow below calc aeval (λ k, x.coeff k) (frobenius_poly p n) = aeval (λ k, x.coeff k) (mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n)) : _ ... = aeval (λ k, x.coeff k) (X n ^ p : mv_polynomial ℕ (zmod p)) : _ ... = (x.coeff n) ^ p : _, { conv_rhs { rw [aeval_eq_eval₂_hom, eval₂_hom_map_hom] }, apply eval₂_hom_congr (ring_hom.ext_int _ _) rfl rfl }, { rw frobenius_poly_zmod }, { rw [alg_hom.map_pow, aeval_X] } end lemma frobenius_eq_map_frobenius : @frobenius p R _ _ = map (_root_.frobenius R p) := begin ext x n, simp only [coeff_frobenius_char_p, map_coeff, frobenius_def], end @[simp] lemma frobenius_zmodp (x : 𝕎 (zmod p)) : (frobenius x) = x := by simp only [ext_iff, coeff_frobenius_char_p, zmod.pow_card, eq_self_iff_true, forall_const] end char_p end witt_vector
d609cac6d845f3fb0be313c315208343872b1662
63abd62053d479eae5abf4951554e1064a4c45b4
/src/tactic/norm_num.lean
2d42386325d80e04f832ffb3f4120210362cc838
[ "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
65,987
lean
/- Copyright (c) 2017 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Mario Carneiro -/ import data.rat.cast import data.rat.meta_defs import data.nat.prime /-! # `norm_num` Evaluating arithmetic expressions including `*`, `+`, `-`, `^`, `≤`. -/ universes u v w namespace tactic /-- Reflexivity conversion: given `e` returns `(e, ⊢ e = e)` -/ meta def refl_conv (e : expr) : tactic (expr × expr) := do p ← mk_eq_refl e, return (e, p) /-- Transitivity conversion: given two conversions (which take an expression `e` and returns `(e', ⊢ e = e')`), produces another conversion that combines them with transitivity, treating failures as reflexivity conversions. -/ meta def trans_conv (t₁ t₂ : expr → tactic (expr × expr)) (e : expr) : tactic (expr × expr) := (do (e₁, p₁) ← t₁ e, (do (e₂, p₂) ← t₂ e₁, p ← mk_eq_trans p₁ p₂, return (e₂, p)) <|> return (e₁, p₁)) <|> t₂ e namespace instance_cache /-- Faster version of `mk_app ``bit0 [e]`. -/ meta def mk_bit0 (c : instance_cache) (e : expr) : tactic (instance_cache × expr) := do (c, ai) ← c.get ``has_add, return (c, (expr.const ``bit0 [c.univ]).mk_app [c.α, ai, e]) /-- Faster version of `mk_app ``bit1 [e]`. -/ meta def mk_bit1 (c : instance_cache) (e : expr) : tactic (instance_cache × expr) := do (c, ai) ← c.get ``has_add, (c, oi) ← c.get ``has_one, return (c, (expr.const ``bit1 [c.univ]).mk_app [c.α, oi, ai, e]) end instance_cache end tactic open tactic namespace norm_num variable {α : Type u} lemma subst_into_add {α} [has_add α] (l r tl tr t) (prl : (l : α) = tl) (prr : r = tr) (prt : tl + tr = t) : l + r = t := by rw [prl, prr, prt] lemma subst_into_mul {α} [has_mul α] (l r tl tr t) (prl : (l : α) = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t := by rw [prl, prr, prt] lemma subst_into_neg {α} [has_neg α] (a ta t : α) (pra : a = ta) (prt : -ta = t) : -a = t := by simp [pra, prt] /-- The result type of `match_numeral`, either `0`, `1`, or a top level decomposition of `bit0 e` or `bit1 e`. The `other` case means it is not a numeral. -/ meta inductive match_numeral_result | zero | one | bit0 (e : expr) | bit1 (e : expr) | other /-- Unfold the top level constructor of the numeral expression. -/ meta def match_numeral : expr → match_numeral_result | `(bit0 %%e) := match_numeral_result.bit0 e | `(bit1 %%e) := match_numeral_result.bit1 e | `(@has_zero.zero _ _) := match_numeral_result.zero | `(@has_one.one _ _) := match_numeral_result.one | _ := match_numeral_result.other theorem zero_succ {α} [semiring α] : (0 + 1 : α) = 1 := zero_add _ theorem one_succ {α} [semiring α] : (1 + 1 : α) = 2 := rfl theorem bit0_succ {α} [semiring α] (a : α) : bit0 a + 1 = bit1 a := rfl theorem bit1_succ {α} [semiring α] (a b : α) (h : a + 1 = b) : bit1 a + 1 = bit0 b := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] section open match_numeral_result /-- Given `a`, `b` natural numerals, proves `⊢ a + 1 = b`, assuming that this is provable. (It may prove garbage instead of failing if `a + 1 = b` is false.) -/ meta def prove_succ : instance_cache → expr → expr → tactic (instance_cache × expr) | c e r := match match_numeral e with | zero := c.mk_app ``zero_succ [] | one := c.mk_app ``one_succ [] | bit0 e := c.mk_app ``bit0_succ [e] | bit1 e := do let r := r.app_arg, (c, p) ← prove_succ c e r, c.mk_app ``bit1_succ [e, r, p] | _ := failed end end theorem zero_adc {α} [semiring α] (a b : α) (h : a + 1 = b) : 0 + a + 1 = b := by rwa zero_add theorem adc_zero {α} [semiring α] (a b : α) (h : a + 1 = b) : a + 0 + 1 = b := by rwa add_zero theorem one_add {α} [semiring α] (a b : α) (h : a + 1 = b) : 1 + a = b := by rwa add_comm theorem add_bit0_bit0 {α} [semiring α] (a b c : α) (h : a + b = c) : bit0 a + bit0 b = bit0 c := h ▸ by simp [bit0, add_left_comm, add_assoc] theorem add_bit0_bit1 {α} [semiring α] (a b c : α) (h : a + b = c) : bit0 a + bit1 b = bit1 c := h ▸ by simp [bit0, bit1, add_left_comm, add_assoc] theorem add_bit1_bit0 {α} [semiring α] (a b c : α) (h : a + b = c) : bit1 a + bit0 b = bit1 c := h ▸ by simp [bit0, bit1, add_left_comm, add_comm] theorem add_bit1_bit1 {α} [semiring α] (a b c : α) (h : a + b + 1 = c) : bit1 a + bit1 b = bit0 c := h ▸ by simp [bit0, bit1, add_left_comm, add_comm] theorem adc_one_one {α} [semiring α] : (1 + 1 + 1 : α) = 3 := rfl theorem adc_bit0_one {α} [semiring α] (a b : α) (h : a + 1 = b) : bit0 a + 1 + 1 = bit0 b := h ▸ by simp [bit0, add_left_comm, add_assoc] theorem adc_one_bit0 {α} [semiring α] (a b : α) (h : a + 1 = b) : 1 + bit0 a + 1 = bit0 b := h ▸ by simp [bit0, add_left_comm, add_assoc] theorem adc_bit1_one {α} [semiring α] (a b : α) (h : a + 1 = b) : bit1 a + 1 + 1 = bit1 b := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_one_bit1 {α} [semiring α] (a b : α) (h : a + 1 = b) : 1 + bit1 a + 1 = bit1 b := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit0_bit0 {α} [semiring α] (a b c : α) (h : a + b = c) : bit0 a + bit0 b + 1 = bit1 c := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit1_bit0 {α} [semiring α] (a b c : α) (h : a + b + 1 = c) : bit1 a + bit0 b + 1 = bit0 c := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit0_bit1 {α} [semiring α] (a b c : α) (h : a + b + 1 = c) : bit0 a + bit1 b + 1 = bit0 c := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit1_bit1 {α} [semiring α] (a b c : α) (h : a + b + 1 = c) : bit1 a + bit1 b + 1 = bit1 c := h ▸ by simp [bit1, bit0, add_left_comm, add_assoc] section open match_numeral_result meta mutual def prove_add_nat, prove_adc_nat with prove_add_nat : instance_cache → expr → expr → expr → tactic (instance_cache × expr) | c a b r := do match match_numeral a, match_numeral b with | zero, _ := c.mk_app ``zero_add [b] | _, zero := c.mk_app ``add_zero [a] | _, one := prove_succ c a r | one, _ := do (c, p) ← prove_succ c b r, c.mk_app ``one_add [b, r, p] | bit0 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``add_bit0_bit0 [a, b, r, p] | bit0 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``add_bit0_bit1 [a, b, r, p] | bit1 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``add_bit1_bit0 [a, b, r, p] | bit1 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``add_bit1_bit1 [a, b, r, p] | _, _ := failed end with prove_adc_nat : instance_cache → expr → expr → expr → tactic (instance_cache × expr) | c a b r := do match match_numeral a, match_numeral b with | zero, _ := do (c, p) ← prove_succ c b r, c.mk_app ``zero_adc [b, r, p] | _, zero := do (c, p) ← prove_succ c b r, c.mk_app ``adc_zero [b, r, p] | one, one := c.mk_app ``adc_one_one [] | bit0 a, one := do let r := r.app_arg, (c, p) ← prove_succ c a r, c.mk_app ``adc_bit0_one [a, r, p] | one, bit0 b := do let r := r.app_arg, (c, p) ← prove_succ c b r, c.mk_app ``adc_one_bit0 [b, r, p] | bit1 a, one := do let r := r.app_arg, (c, p) ← prove_succ c a r, c.mk_app ``adc_bit1_one [a, r, p] | one, bit1 b := do let r := r.app_arg, (c, p) ← prove_succ c b r, c.mk_app ``adc_one_bit1 [b, r, p] | bit0 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``adc_bit0_bit0 [a, b, r, p] | bit0 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``adc_bit0_bit1 [a, b, r, p] | bit1 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``adc_bit1_bit0 [a, b, r, p] | bit1 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``adc_bit1_bit1 [a, b, r, p] | _, _ := failed end /-- Given `a`,`b`,`r` natural numerals, proves `⊢ a + b = r`. -/ add_decl_doc prove_add_nat /-- Given `a`,`b`,`r` natural numerals, proves `⊢ a + b + 1 = r`. -/ add_decl_doc prove_adc_nat /-- Given `a`,`b` natural numerals, returns `(r, ⊢ a + b = r)`. -/ meta def prove_add_nat' (c : instance_cache) (a b : expr) : tactic (instance_cache × expr × expr) := do na ← a.to_nat, nb ← b.to_nat, (c, r) ← c.of_nat (na + nb), (c, p) ← prove_add_nat c a b r, return (c, r, p) end theorem bit0_mul {α} [semiring α] (a b c : α) (h : a * b = c) : bit0 a * b = bit0 c := h ▸ by simp [bit0, add_mul] theorem mul_bit0' {α} [semiring α] (a b c : α) (h : a * b = c) : a * bit0 b = bit0 c := h ▸ by simp [bit0, mul_add] theorem mul_bit0_bit0 {α} [semiring α] (a b c : α) (h : a * b = c) : bit0 a * bit0 b = bit0 (bit0 c) := bit0_mul _ _ _ (mul_bit0' _ _ _ h) theorem mul_bit1_bit1 {α} [semiring α] (a b c d e : α) (hc : a * b = c) (hd : a + b = d) (he : bit0 c + d = e) : bit1 a * bit1 b = bit1 e := by rw [← he, ← hd, ← hc]; simp [bit1, bit0, mul_add, add_mul, add_left_comm, add_assoc] section open match_numeral_result /-- Given `a`,`b` natural numerals, returns `(r, ⊢ a * b = r)`. -/ meta def prove_mul_nat : instance_cache → expr → expr → tactic (instance_cache × expr × expr) | ic a b := match match_numeral a, match_numeral b with | zero, _ := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``zero_mul [b], return (ic, z, p) | _, zero := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``mul_zero [a], return (ic, z, p) | one, _ := do (ic, p) ← ic.mk_app ``one_mul [b], return (ic, b, p) | _, one := do (ic, p) ← ic.mk_app ``mul_one [a], return (ic, a, p) | bit0 a, bit0 b := do (ic, c, p) ← prove_mul_nat ic a b, (ic, p) ← ic.mk_app ``mul_bit0_bit0 [a, b, c, p], (ic, c') ← ic.mk_bit0 c, (ic, c') ← ic.mk_bit0 c', return (ic, c', p) | bit0 a, _ := do (ic, c, p) ← prove_mul_nat ic a b, (ic, p) ← ic.mk_app ``bit0_mul [a, b, c, p], (ic, c') ← ic.mk_bit0 c, return (ic, c', p) | _, bit0 b := do (ic, c, p) ← prove_mul_nat ic a b, (ic, p) ← ic.mk_app ``mul_bit0' [a, b, c, p], (ic, c') ← ic.mk_bit0 c, return (ic, c', p) | bit1 a, bit1 b := do (ic, c, pc) ← prove_mul_nat ic a b, (ic, d, pd) ← prove_add_nat' ic a b, (ic, c') ← ic.mk_bit0 c, (ic, e, pe) ← prove_add_nat' ic c' d, (ic, p) ← ic.mk_app ``mul_bit1_bit1 [a, b, c, d, e, pc, pd, pe], (ic, e') ← ic.mk_bit1 e, return (ic, e', p) | _, _ := failed end end section open match_numeral_result /-- Given `a` a positive natural numeral, returns `⊢ 0 < a`. -/ meta def prove_pos_nat (c : instance_cache) : expr → tactic (instance_cache × expr) | e := match match_numeral e with | one := c.mk_app ``zero_lt_one' [] | bit0 e := do (c, p) ← prove_pos_nat e, c.mk_app ``bit0_pos [e, p] | bit1 e := do (c, p) ← prove_pos_nat e, c.mk_app ``bit1_pos' [e, p] | _ := failed end end /-- Given `a` a rational numeral, returns `⊢ 0 < a`. -/ meta def prove_pos (c : instance_cache) : expr → tactic (instance_cache × expr) | `(%%e₁ / %%e₂) := do (c, p₁) ← prove_pos_nat c e₁, (c, p₂) ← prove_pos_nat c e₂, c.mk_app ``div_pos [e₁, e₂, p₁, p₂] | e := prove_pos_nat c e /-- `match_neg (- e) = some e`, otherwise `none` -/ meta def match_neg : expr → option expr | `(- %%e) := some e | _ := none /-- `match_sign (- e) = inl e`, `match_sign 0 = inr ff`, otherwise `inr tt` -/ meta def match_sign : expr → expr ⊕ bool | `(- %%e) := sum.inl e | `(has_zero.zero) := sum.inr ff | _ := sum.inr tt theorem ne_zero_of_pos {α} [ordered_add_comm_group α] (a : α) : 0 < a → a ≠ 0 := ne_of_gt theorem ne_zero_neg {α} [add_group α] (a : α) : a ≠ 0 → -a ≠ 0 := mt neg_eq_zero.1 /-- Given `a` a rational numeral, returns `⊢ a ≠ 0`. -/ meta def prove_ne_zero' (c : instance_cache) : expr → tactic (instance_cache × expr) | a := match match_neg a with | some a := do (c, p) ← prove_ne_zero' a, c.mk_app ``ne_zero_neg [a, p] | none := do (c, p) ← prove_pos c a, c.mk_app ``ne_zero_of_pos [a, p] end theorem clear_denom_div {α} [division_ring α] (a b b' c d : α) (h₀ : b ≠ 0) (h₁ : b * b' = d) (h₂ : a * b' = c) : (a / b) * d = c := by rwa [← h₁, ← mul_assoc, div_mul_cancel _ h₀] /-- Given `a` nonnegative rational and `d` a natural number, returns `(b, ⊢ a * d = b)`. (`d` should be a multiple of the denominator of `a`, so that `b` is a natural number.) -/ meta def prove_clear_denom' (prove_ne_zero : instance_cache → expr → ℚ → tactic (instance_cache × expr)) (c : instance_cache) (a d : expr) (na : ℚ) (nd : ℕ) : tactic (instance_cache × expr × expr) := if na.denom = 1 then prove_mul_nat c a d else do [_, _, a, b] ← return a.get_app_args, (c, b') ← c.of_nat (nd / na.denom), (c, p₀) ← prove_ne_zero c b (rat.of_int na.denom), (c, _, p₁) ← prove_mul_nat c b b', (c, r, p₂) ← prove_mul_nat c a b', (c, p) ← c.mk_app ``clear_denom_div [a, b, b', r, d, p₀, p₁, p₂], return (c, r, p) theorem nonneg_pos {α} [ordered_cancel_add_comm_monoid α] (a : α) : 0 < a → 0 ≤ a := le_of_lt theorem lt_one_bit0 {α} [linear_ordered_semiring α] (a : α) (h : 1 ≤ a) : 1 < bit0 a := lt_of_lt_of_le one_lt_two (bit0_le_bit0.2 h) theorem lt_one_bit1 {α} [linear_ordered_semiring α] (a : α) (h : 0 < a) : 1 < bit1 a := one_lt_bit1.2 h theorem lt_bit0_bit0 {α} [linear_ordered_semiring α] (a b : α) : a < b → bit0 a < bit0 b := bit0_lt_bit0.2 theorem lt_bit0_bit1 {α} [linear_ordered_semiring α] (a b : α) (h : a ≤ b) : bit0 a < bit1 b := lt_of_le_of_lt (bit0_le_bit0.2 h) (lt_add_one _) theorem lt_bit1_bit0 {α} [linear_ordered_semiring α] (a b : α) (h : a + 1 ≤ b) : bit1 a < bit0 b := lt_of_lt_of_le (by simp [bit0, bit1, zero_lt_one, add_assoc]) (bit0_le_bit0.2 h) theorem lt_bit1_bit1 {α} [linear_ordered_semiring α] (a b : α) : a < b → bit1 a < bit1 b := bit1_lt_bit1.2 theorem le_one_bit0 {α} [linear_ordered_semiring α] (a : α) (h : 1 ≤ a) : 1 ≤ bit0 a := le_of_lt (lt_one_bit0 _ h) -- deliberately strong hypothesis because bit1 0 is not a numeral theorem le_one_bit1 {α} [linear_ordered_semiring α] (a : α) (h : 0 < a) : 1 ≤ bit1 a := le_of_lt (lt_one_bit1 _ h) theorem le_bit0_bit0 {α} [linear_ordered_semiring α] (a b : α) : a ≤ b → bit0 a ≤ bit0 b := bit0_le_bit0.2 theorem le_bit0_bit1 {α} [linear_ordered_semiring α] (a b : α) (h : a ≤ b) : bit0 a ≤ bit1 b := le_of_lt (lt_bit0_bit1 _ _ h) theorem le_bit1_bit0 {α} [linear_ordered_semiring α] (a b : α) (h : a + 1 ≤ b) : bit1 a ≤ bit0 b := le_of_lt (lt_bit1_bit0 _ _ h) theorem le_bit1_bit1 {α} [linear_ordered_semiring α] (a b : α) : a ≤ b → bit1 a ≤ bit1 b := bit1_le_bit1.2 theorem sle_one_bit0 {α} [linear_ordered_semiring α] (a : α) : 1 ≤ a → 1 + 1 ≤ bit0 a := bit0_le_bit0.2 theorem sle_one_bit1 {α} [linear_ordered_semiring α] (a : α) : 1 ≤ a → 1 + 1 ≤ bit1 a := le_bit0_bit1 _ _ theorem sle_bit0_bit0 {α} [linear_ordered_semiring α] (a b : α) : a + 1 ≤ b → bit0 a + 1 ≤ bit0 b := le_bit1_bit0 _ _ theorem sle_bit0_bit1 {α} [linear_ordered_semiring α] (a b : α) (h : a ≤ b) : bit0 a + 1 ≤ bit1 b := bit1_le_bit1.2 h theorem sle_bit1_bit0 {α} [linear_ordered_semiring α] (a b : α) (h : a + 1 ≤ b) : bit1 a + 1 ≤ bit0 b := (bit1_succ a _ rfl).symm ▸ bit0_le_bit0.2 h theorem sle_bit1_bit1 {α} [linear_ordered_semiring α] (a b : α) (h : a + 1 ≤ b) : bit1 a + 1 ≤ bit1 b := (bit1_succ a _ rfl).symm ▸ le_bit0_bit1 _ _ h /-- Given `a` a rational numeral, returns `⊢ 0 ≤ a`. -/ meta def prove_nonneg (ic : instance_cache) : expr → tactic (instance_cache × expr) | e@`(has_zero.zero) := ic.mk_app ``le_refl [e] | e := if ic.α = `(ℕ) then return (ic, `(nat.zero_le).mk_app [e]) else do (ic, p) ← prove_pos ic e, ic.mk_app ``nonneg_pos [e, p] section open match_numeral_result /-- Given `a` a rational numeral, returns `⊢ 1 ≤ a`. -/ meta def prove_one_le_nat (ic : instance_cache) : expr → tactic (instance_cache × expr) | a := match match_numeral a with | one := ic.mk_app ``le_refl [a] | bit0 a := do (ic, p) ← prove_one_le_nat a, ic.mk_app ``le_one_bit0 [a, p] | bit1 a := do (ic, p) ← prove_pos_nat ic a, ic.mk_app ``le_one_bit1 [a, p] | _ := failed end meta mutual def prove_le_nat, prove_sle_nat (ic : instance_cache) with prove_le_nat : expr → expr → tactic (instance_cache × expr) | a b := if a = b then ic.mk_app ``le_refl [a] else match match_numeral a, match_numeral b with | zero, _ := prove_nonneg ic b | one, bit0 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``le_one_bit0 [b, p] | one, bit1 b := do (ic, p) ← prove_pos_nat ic b, ic.mk_app ``le_one_bit1 [b, p] | bit0 a, bit0 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``le_bit0_bit0 [a, b, p] | bit0 a, bit1 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``le_bit0_bit1 [a, b, p] | bit1 a, bit0 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``le_bit1_bit0 [a, b, p] | bit1 a, bit1 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``le_bit1_bit1 [a, b, p] | _, _ := failed end with prove_sle_nat : expr → expr → tactic (instance_cache × expr) | a b := match match_numeral a, match_numeral b with | zero, _ := prove_nonneg ic b | one, bit0 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``sle_one_bit0 [b, p] | one, bit1 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``sle_one_bit1 [b, p] | bit0 a, bit0 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``sle_bit0_bit0 [a, b, p] | bit0 a, bit1 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``sle_bit0_bit1 [a, b, p] | bit1 a, bit0 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``sle_bit1_bit0 [a, b, p] | bit1 a, bit1 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``sle_bit1_bit1 [a, b, p] | _, _ := failed end /-- Given `a`,`b` natural numerals, proves `⊢ a ≤ b`. -/ add_decl_doc prove_le_nat /-- Given `a`,`b` natural numerals, proves `⊢ a + 1 ≤ b`. -/ add_decl_doc prove_sle_nat /-- Given `a`,`b` natural numerals, proves `⊢ a < b`. -/ meta def prove_lt_nat (ic : instance_cache) : expr → expr → tactic (instance_cache × expr) | a b := match match_numeral a, match_numeral b with | zero, _ := prove_pos ic b | one, bit0 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``lt_one_bit0 [b, p] | one, bit1 b := do (ic, p) ← prove_pos_nat ic b, ic.mk_app ``lt_one_bit1 [b, p] | bit0 a, bit0 b := do (ic, p) ← prove_lt_nat a b, ic.mk_app ``lt_bit0_bit0 [a, b, p] | bit0 a, bit1 b := do (ic, p) ← prove_le_nat ic a b, ic.mk_app ``lt_bit0_bit1 [a, b, p] | bit1 a, bit0 b := do (ic, p) ← prove_sle_nat ic a b, ic.mk_app ``lt_bit1_bit0 [a, b, p] | bit1 a, bit1 b := do (ic, p) ← prove_lt_nat a b, ic.mk_app ``lt_bit1_bit1 [a, b, p] | _, _ := failed end end theorem clear_denom_lt {α} [linear_ordered_semiring α] (a a' b b' d : α) (h₀ : 0 < d) (ha : a * d = a') (hb : b * d = b') (h : a' < b') : a < b := lt_of_mul_lt_mul_right (by rwa [ha, hb]) (le_of_lt h₀) /-- Given `a`,`b` nonnegative rational numerals, proves `⊢ a < b`. -/ meta def prove_lt_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_lt_nat ic a b else do let nd := na.denom.lcm nb.denom, (ic, d) ← ic.of_nat nd, (ic, p₀) ← prove_pos ic d, (ic, a', pa) ← prove_clear_denom' (λ ic e _, prove_ne_zero' ic e) ic a d na nd, (ic, b', pb) ← prove_clear_denom' (λ ic e _, prove_ne_zero' ic e) ic b d nb nd, (ic, p) ← prove_lt_nat ic a' b', ic.mk_app ``clear_denom_lt [a, a', b, b', d, p₀, pa, pb, p] lemma lt_neg_pos {α} [ordered_add_comm_group α] (a b : α) (ha : 0 < a) (hb : 0 < b) : -a < b := lt_trans (neg_neg_of_pos ha) hb /-- Given `a`,`b` rational numerals, proves `⊢ a < b`. -/ meta def prove_lt_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr) := match match_sign a, match_sign b with | sum.inl a, sum.inl b := do (ic, p) ← prove_lt_nonneg_rat ic a b (-na) (-nb), ic.mk_app ``neg_lt_neg [a, b, p] | sum.inl a, sum.inr ff := do (ic, p) ← prove_pos ic a, ic.mk_app ``neg_neg_of_pos [a, p] | sum.inl a, sum.inr tt := do (ic, pa) ← prove_pos ic a, (ic, pb) ← prove_pos ic b, ic.mk_app ``lt_neg_pos [a, b, pa, pb] | sum.inr ff, _ := prove_pos ic b | sum.inr tt, _ := prove_lt_nonneg_rat ic a b na nb end theorem clear_denom_le {α} [linear_ordered_semiring α] (a a' b b' d : α) (h₀ : 0 < d) (ha : a * d = a') (hb : b * d = b') (h : a' ≤ b') : a ≤ b := le_of_mul_le_mul_right (by rwa [ha, hb]) h₀ /-- Given `a`,`b` nonnegative rational numerals, proves `⊢ a ≤ b`. -/ meta def prove_le_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_le_nat ic a b else do let nd := na.denom.lcm nb.denom, (ic, d) ← ic.of_nat nd, (ic, p₀) ← prove_pos ic d, (ic, a', pa) ← prove_clear_denom' (λ ic e _, prove_ne_zero' ic e) ic a d na nd, (ic, b', pb) ← prove_clear_denom' (λ ic e _, prove_ne_zero' ic e) ic b d nb nd, (ic, p) ← prove_le_nat ic a' b', ic.mk_app ``clear_denom_le [a, a', b, b', d, p₀, pa, pb, p] lemma le_neg_pos {α} [ordered_add_comm_group α] (a b : α) (ha : 0 ≤ a) (hb : 0 ≤ b) : -a ≤ b := le_trans (neg_nonpos_of_nonneg ha) hb /-- Given `a`,`b` rational numerals, proves `⊢ a ≤ b`. -/ meta def prove_le_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr) := match match_sign a, match_sign b with | sum.inl a, sum.inl b := do (ic, p) ← prove_le_nonneg_rat ic a b (-na) (-nb), ic.mk_app ``neg_le_neg [a, b, p] | sum.inl a, sum.inr ff := do (ic, p) ← prove_nonneg ic a, ic.mk_app ``neg_nonpos_of_nonneg [a, p] | sum.inl a, sum.inr tt := do (ic, pa) ← prove_nonneg ic a, (ic, pb) ← prove_nonneg ic b, ic.mk_app ``le_neg_pos [a, b, pa, pb] | sum.inr ff, _ := prove_nonneg ic b | sum.inr tt, _ := prove_le_nonneg_rat ic a b na nb end /-- Given `a`,`b` rational numerals, proves `⊢ a ≠ b`. This version tries to prove `⊢ a < b` or `⊢ b < a`, and so is not appropriate for types without an order relation. -/ meta def prove_ne_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr) := if na < nb then do (ic, p) ← prove_lt_rat ic a b na nb, ic.mk_app ``ne_of_lt [a, b, p] else do (ic, p) ← prove_lt_rat ic b a nb na, ic.mk_app ``ne_of_gt [a, b, p] theorem nat_cast_zero {α} [semiring α] : ↑(0 : ℕ) = (0 : α) := nat.cast_zero theorem nat_cast_one {α} [semiring α] : ↑(1 : ℕ) = (1 : α) := nat.cast_one theorem nat_cast_bit0 {α} [semiring α] (a : ℕ) (a' : α) (h : ↑a = a') : ↑(bit0 a) = bit0 a' := h ▸ nat.cast_bit0 _ theorem nat_cast_bit1 {α} [semiring α] (a : ℕ) (a' : α) (h : ↑a = a') : ↑(bit1 a) = bit1 a' := h ▸ nat.cast_bit1 _ theorem int_cast_zero {α} [ring α] : ↑(0 : ℤ) = (0 : α) := int.cast_zero theorem int_cast_one {α} [ring α] : ↑(1 : ℤ) = (1 : α) := int.cast_one theorem int_cast_bit0 {α} [ring α] (a : ℤ) (a' : α) (h : ↑a = a') : ↑(bit0 a) = bit0 a' := h ▸ int.cast_bit0 _ theorem int_cast_bit1 {α} [ring α] (a : ℤ) (a' : α) (h : ↑a = a') : ↑(bit1 a) = bit1 a' := h ▸ int.cast_bit1 _ theorem rat_cast_bit0 {α} [division_ring α] [char_zero α] (a : ℚ) (a' : α) (h : ↑a = a') : ↑(bit0 a) = bit0 a' := h ▸ rat.cast_bit0 _ theorem rat_cast_bit1 {α} [division_ring α] [char_zero α] (a : ℚ) (a' : α) (h : ↑a = a') : ↑(bit1 a) = bit1 a' := h ▸ rat.cast_bit1 _ /-- Given `a' : α` a natural numeral, returns `(a : ℕ, ⊢ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_nat_uncast (ic nc : instance_cache) : ∀ (a' : expr), tactic (instance_cache × instance_cache × expr × expr) | a' := match match_numeral a' with | match_numeral_result.zero := do (nc, e) ← nc.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``nat_cast_zero [], return (ic, nc, e, p) | match_numeral_result.one := do (nc, e) ← nc.mk_app ``has_one.one [], (ic, p) ← ic.mk_app ``nat_cast_one [], return (ic, nc, e, p) | match_numeral_result.bit0 a' := do (ic, nc, a, p) ← prove_nat_uncast a', (nc, a0) ← nc.mk_bit0 a, (ic, p) ← ic.mk_app ``nat_cast_bit0 [a, a', p], return (ic, nc, a0, p) | match_numeral_result.bit1 a' := do (ic, nc, a, p) ← prove_nat_uncast a', (nc, a1) ← nc.mk_bit1 a, (ic, p) ← ic.mk_app ``nat_cast_bit1 [a, a', p], return (ic, nc, a1, p) | _ := failed end /-- Given `a' : α` a natural numeral, returns `(a : ℤ, ⊢ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_int_uncast_nat (ic zc : instance_cache) : ∀ (a' : expr), tactic (instance_cache × instance_cache × expr × expr) | a' := match match_numeral a' with | match_numeral_result.zero := do (zc, e) ← zc.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``int_cast_zero [], return (ic, zc, e, p) | match_numeral_result.one := do (zc, e) ← zc.mk_app ``has_one.one [], (ic, p) ← ic.mk_app ``int_cast_one [], return (ic, zc, e, p) | match_numeral_result.bit0 a' := do (ic, zc, a, p) ← prove_int_uncast_nat a', (zc, a0) ← zc.mk_bit0 a, (ic, p) ← ic.mk_app ``int_cast_bit0 [a, a', p], return (ic, zc, a0, p) | match_numeral_result.bit1 a' := do (ic, zc, a, p) ← prove_int_uncast_nat a', (zc, a1) ← zc.mk_bit1 a, (ic, p) ← ic.mk_app ``int_cast_bit1 [a, a', p], return (ic, zc, a1, p) | _ := failed end /-- Given `a' : α` a natural numeral, returns `(a : ℚ, ⊢ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_rat_uncast_nat (ic qc : instance_cache) (cz_inst : expr) : ∀ (a' : expr), tactic (instance_cache × instance_cache × expr × expr) | a' := match match_numeral a' with | match_numeral_result.zero := do (qc, e) ← qc.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``rat.cast_zero [], return (ic, qc, e, p) | match_numeral_result.one := do (qc, e) ← qc.mk_app ``has_one.one [], (ic, p) ← ic.mk_app ``rat.cast_one [], return (ic, qc, e, p) | match_numeral_result.bit0 a' := do (ic, qc, a, p) ← prove_rat_uncast_nat a', (qc, a0) ← qc.mk_bit0 a, (ic, p) ← ic.mk_app ``rat_cast_bit0 [cz_inst, a, a', p], return (ic, qc, a0, p) | match_numeral_result.bit1 a' := do (ic, qc, a, p) ← prove_rat_uncast_nat a', (qc, a1) ← qc.mk_bit1 a, (ic, p) ← ic.mk_app ``rat_cast_bit1 [cz_inst, a, a', p], return (ic, qc, a1, p) | _ := failed end theorem rat_cast_div {α} [division_ring α] [char_zero α] (a b : ℚ) (a' b' : α) (ha : ↑a = a') (hb : ↑b = b') : ↑(a / b) = a' / b' := ha ▸ hb ▸ rat.cast_div _ _ /-- Given `a' : α` a nonnegative rational numeral, returns `(a : ℚ, ⊢ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_rat_uncast_nonneg (ic qc : instance_cache) (cz_inst a' : expr) (na' : ℚ) : tactic (instance_cache × instance_cache × expr × expr) := if na'.denom = 1 then prove_rat_uncast_nat ic qc cz_inst a' else do [_, _, a', b'] ← return a'.get_app_args, (ic, qc, a, pa) ← prove_rat_uncast_nat ic qc cz_inst a', (ic, qc, b, pb) ← prove_rat_uncast_nat ic qc cz_inst b', (qc, e) ← qc.mk_app ``has_div.div [a, b], (ic, p) ← ic.mk_app ``rat_cast_div [cz_inst, a, b, a', b', pa, pb], return (ic, qc, e, p) theorem int_cast_neg {α} [ring α] (a : ℤ) (a' : α) (h : ↑a = a') : ↑-a = -a' := h ▸ int.cast_neg _ theorem rat_cast_neg {α} [division_ring α] (a : ℚ) (a' : α) (h : ↑a = a') : ↑-a = -a' := h ▸ rat.cast_neg _ /-- Given `a' : α` an integer numeral, returns `(a : ℤ, ⊢ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_int_uncast (ic zc : instance_cache) (a' : expr) : tactic (instance_cache × instance_cache × expr × expr) := match match_neg a' with | some a' := do (ic, zc, a, p) ← prove_int_uncast_nat ic zc a', (zc, e) ← zc.mk_app ``has_neg.neg [a], (ic, p) ← ic.mk_app ``int_cast_neg [a, a', p], return (ic, zc, e, p) | none := prove_int_uncast_nat ic zc a' end /-- Given `a' : α` a rational numeral, returns `(a : ℚ, ⊢ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_rat_uncast (ic qc : instance_cache) (cz_inst a' : expr) (na' : ℚ) : tactic (instance_cache × instance_cache × expr × expr) := match match_neg a' with | some a' := do (ic, qc, a, p) ← prove_rat_uncast_nonneg ic qc cz_inst a' (-na'), (qc, e) ← qc.mk_app ``has_neg.neg [a], (ic, p) ← ic.mk_app ``rat_cast_neg [a, a', p], return (ic, qc, e, p) | none := prove_rat_uncast_nonneg ic qc cz_inst a' na' end theorem nat_cast_ne {α} [semiring α] [char_zero α] (a b : ℕ) (a' b' : α) (ha : ↑a = a') (hb : ↑b = b') (h : a ≠ b) : a' ≠ b' := ha ▸ hb ▸ mt nat.cast_inj.1 h theorem int_cast_ne {α} [ring α] [char_zero α] (a b : ℤ) (a' b' : α) (ha : ↑a = a') (hb : ↑b = b') (h : a ≠ b) : a' ≠ b' := ha ▸ hb ▸ mt int.cast_inj.1 h theorem rat_cast_ne {α} [division_ring α] [char_zero α] (a b : ℚ) (a' b' : α) (ha : ↑a = a') (hb : ↑b = b') (h : a ≠ b) : a' ≠ b' := ha ▸ hb ▸ mt rat.cast_inj.1 h /-- Given `a`,`b` rational numerals, proves `⊢ a ≠ b`. Currently it tries two methods: * Prove `⊢ a < b` or `⊢ b < a`, if the base type has an order * Embed `↑(a':ℚ) = a` and `↑(b':ℚ) = b`, and then prove `a' ≠ b'`. This requires that the base type be `char_zero`, and also that it be a `division_ring` so that the coercion from `ℚ` is well defined. We may also add coercions to `ℤ` and `ℕ` as well in order to support `char_zero` rings and semirings. -/ meta def prove_ne : instance_cache → expr → expr → ℚ → ℚ → tactic (instance_cache × expr) | ic a b na nb := prove_ne_rat ic a b na nb <|> do cz_inst ← mk_mapp ``char_zero [ic.α, none, none] >>= mk_instance, if na.denom = 1 ∧ nb.denom = 1 then if na ≥ 0 ∧ nb ≥ 0 then do guard (ic.α ≠ `(ℕ)), nc ← mk_instance_cache `(ℕ), (ic, nc, a', pa) ← prove_nat_uncast ic nc a, (ic, nc, b', pb) ← prove_nat_uncast ic nc b, (nc, p) ← prove_ne_rat nc a' b' na nb, ic.mk_app ``nat_cast_ne [cz_inst, a', b', a, b, pa, pb, p] else do guard (ic.α ≠ `(ℤ)), zc ← mk_instance_cache `(ℤ), (ic, zc, a', pa) ← prove_int_uncast ic zc a, (ic, zc, b', pb) ← prove_int_uncast ic zc b, (zc, p) ← prove_ne_rat zc a' b' na nb, ic.mk_app ``int_cast_ne [cz_inst, a', b', a, b, pa, pb, p] else do guard (ic.α ≠ `(ℚ)), qc ← mk_instance_cache `(ℚ), (ic, qc, a', pa) ← prove_rat_uncast ic qc cz_inst a na, (ic, qc, b', pb) ← prove_rat_uncast ic qc cz_inst b nb, (qc, p) ← prove_ne_rat qc a' b' na nb, ic.mk_app ``rat_cast_ne [cz_inst, a', b', a, b, pa, pb, p] /-- Given `a` a rational numeral, returns `⊢ a ≠ 0`. -/ meta def prove_ne_zero (ic : instance_cache) : expr → ℚ → tactic (instance_cache × expr) | a na := do (ic, z) ← ic.mk_app ``has_zero.zero [], prove_ne ic a z na 0 /-- Given `a` nonnegative rational and `d` a natural number, returns `(b, ⊢ a * d = b)`. (`d` should be a multiple of the denominator of `a`, so that `b` is a natural number.) -/ meta def prove_clear_denom : instance_cache → expr → expr → ℚ → ℕ → tactic (instance_cache × expr × expr) := prove_clear_denom' prove_ne_zero theorem clear_denom_add {α} [division_ring α] (a a' b b' c c' d : α) (h₀ : d ≠ 0) (ha : a * d = a') (hb : b * d = b') (hc : c * d = c') (h : a' + b' = c') : a + b = c := mul_right_cancel' h₀ $ by rwa [add_mul, ha, hb, hc] /-- Given `a`,`b`,`c` nonnegative rational numerals, returns `⊢ a + b = c`. -/ meta def prove_add_nonneg_rat (ic : instance_cache) (a b c : expr) (na nb nc : ℚ) : tactic (instance_cache × expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_add_nat ic a b c else do let nd := na.denom.lcm nb.denom, (ic, d) ← ic.of_nat nd, (ic, p₀) ← prove_ne_zero ic d (rat.of_int nd), (ic, a', pa) ← prove_clear_denom ic a d na nd, (ic, b', pb) ← prove_clear_denom ic b d nb nd, (ic, c', pc) ← prove_clear_denom ic c d nc nd, (ic, p) ← prove_add_nat ic a' b' c', ic.mk_app ``clear_denom_add [a, a', b, b', c, c', d, p₀, pa, pb, pc, p] theorem add_pos_neg_pos {α} [add_group α] (a b c : α) (h : c + b = a) : a + -b = c := h ▸ by simp theorem add_pos_neg_neg {α} [add_group α] (a b c : α) (h : c + a = b) : a + -b = -c := h ▸ by simp theorem add_neg_pos_pos {α} [add_group α] (a b c : α) (h : a + c = b) : -a + b = c := h ▸ by simp theorem add_neg_pos_neg {α} [add_group α] (a b c : α) (h : b + c = a) : -a + b = -c := h ▸ by simp theorem add_neg_neg {α} [add_group α] (a b c : α) (h : b + a = c) : -a + -b = -c := h ▸ by simp /-- Given `a`,`b`,`c` rational numerals, returns `⊢ a + b = c`. -/ meta def prove_add_rat (ic : instance_cache) (ea eb ec : expr) (a b c : ℚ) : tactic (instance_cache × expr) := match match_neg ea, match_neg eb, match_neg ec with | some ea, some eb, some ec := do (ic, p) ← prove_add_nonneg_rat ic eb ea ec (-b) (-a) (-c), ic.mk_app ``add_neg_neg [ea, eb, ec, p] | some ea, none, some ec := do (ic, p) ← prove_add_nonneg_rat ic eb ec ea b (-c) (-a), ic.mk_app ``add_neg_pos_neg [ea, eb, ec, p] | some ea, none, none := do (ic, p) ← prove_add_nonneg_rat ic ea ec eb (-a) c b, ic.mk_app ``add_neg_pos_pos [ea, eb, ec, p] | none, some eb, some ec := do (ic, p) ← prove_add_nonneg_rat ic ec ea eb (-c) a (-b), ic.mk_app ``add_pos_neg_neg [ea, eb, ec, p] | none, some eb, none := do (ic, p) ← prove_add_nonneg_rat ic ec eb ea c (-b) a, ic.mk_app ``add_pos_neg_pos [ea, eb, ec, p] | _, _, _ := prove_add_nonneg_rat ic ea eb ec a b c end /-- Given `a`,`b` rational numerals, returns `(c, ⊢ a + b = c)`. -/ meta def prove_add_rat' (ic : instance_cache) (a b : expr) : tactic (instance_cache × expr × expr) := do na ← a.to_rat, nb ← b.to_rat, let nc := na + nb, (ic, c) ← ic.of_rat nc, (ic, p) ← prove_add_rat ic a b c na nb nc, return (ic, c, p) theorem clear_denom_simple_nat {α} [division_ring α] (a : α) : (1:α) ≠ 0 ∧ a * 1 = a := ⟨one_ne_zero, mul_one _⟩ theorem clear_denom_simple_div {α} [division_ring α] (a b : α) (h : b ≠ 0) : b ≠ 0 ∧ a / b * b = a := ⟨h, div_mul_cancel _ h⟩ /-- Given `a` a nonnegative rational numeral, returns `(b, c, ⊢ a * b = c)` where `b` and `c` are natural numerals. (`b` will be the denominator of `a`.) -/ meta def prove_clear_denom_simple (c : instance_cache) (a : expr) (na : ℚ) : tactic (instance_cache × expr × expr × expr) := if na.denom = 1 then do (c, d) ← c.mk_app ``has_one.one [], (c, p) ← c.mk_app ``clear_denom_simple_nat [a], return (c, d, a, p) else do [α, _, a, b] ← return a.get_app_args, (c, p₀) ← prove_ne_zero c b (rat.of_int na.denom), (c, p) ← c.mk_app ``clear_denom_simple_div [a, b, p₀], return (c, b, a, p) theorem clear_denom_mul {α} [field α] (a a' b b' c c' d₁ d₂ d : α) (ha : d₁ ≠ 0 ∧ a * d₁ = a') (hb : d₂ ≠ 0 ∧ b * d₂ = b') (hc : c * d = c') (hd : d₁ * d₂ = d) (h : a' * b' = c') : a * b = c := mul_right_cancel' ha.1 $ mul_right_cancel' hb.1 $ by rw [mul_assoc c, hd, hc, ← h, ← ha.2, ← hb.2, ← mul_assoc, mul_right_comm a] /-- Given `a`,`b` nonnegative rational numerals, returns `(c, ⊢ a * b = c)`. -/ meta def prove_mul_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr × expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_mul_nat ic a b else do let nc := na * nb, (ic, c) ← ic.of_rat nc, (ic, d₁, a', pa) ← prove_clear_denom_simple ic a na, (ic, d₂, b', pb) ← prove_clear_denom_simple ic b nb, (ic, d, pd) ← prove_mul_nat ic d₁ d₂, nd ← d.to_nat, (ic, c', pc) ← prove_clear_denom ic c d nc nd, (ic, _, p) ← prove_mul_nat ic a' b', (ic, p) ← ic.mk_app ``clear_denom_mul [a, a', b, b', c, c', d₁, d₂, d, pa, pb, pc, pd, p], return (ic, c, p) theorem mul_neg_pos {α} [ring α] (a b c : α) (h : a * b = c) : -a * b = -c := h ▸ by simp theorem mul_pos_neg {α} [ring α] (a b c : α) (h : a * b = c) : a * -b = -c := h ▸ by simp theorem mul_neg_neg {α} [ring α] (a b c : α) (h : a * b = c) : -a * -b = c := h ▸ by simp /-- Given `a`,`b` rational numerals, returns `(c, ⊢ a * b = c)`. -/ meta def prove_mul_rat (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr × expr) := match match_sign a, match_sign b with | sum.inl a, sum.inl b := do (ic, c, p) ← prove_mul_nonneg_rat ic a b (-na) (-nb), (ic, p) ← ic.mk_app ``mul_neg_neg [a, b, c, p], return (ic, c, p) | sum.inr ff, _ := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``zero_mul [b], return (ic, z, p) | _, sum.inr ff := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``mul_zero [a], return (ic, z, p) | sum.inl a, sum.inr tt := do (ic, c, p) ← prove_mul_nonneg_rat ic a b (-na) nb, (ic, p) ← ic.mk_app ``mul_neg_pos [a, b, c, p], (ic, c') ← ic.mk_app ``has_neg.neg [c], return (ic, c', p) | sum.inr tt, sum.inl b := do (ic, c, p) ← prove_mul_nonneg_rat ic a b na (-nb), (ic, p) ← ic.mk_app ``mul_pos_neg [a, b, c, p], (ic, c') ← ic.mk_app ``has_neg.neg [c], return (ic, c', p) | sum.inr tt, sum.inr tt := prove_mul_nonneg_rat ic a b na nb end theorem inv_neg {α} [division_ring α] (a b : α) (h : a⁻¹ = b) : (-a)⁻¹ = -b := h ▸ by simp only [inv_eq_one_div, one_div_neg_eq_neg_one_div] theorem inv_one {α} [division_ring α] : (1 : α)⁻¹ = 1 := inv_one theorem inv_one_div {α} [division_ring α] (a : α) : (1 / a)⁻¹ = a := by rw [one_div, inv_inv'] theorem inv_div_one {α} [division_ring α] (a : α) : a⁻¹ = 1 / a := inv_eq_one_div _ theorem inv_div {α} [division_ring α] (a b : α) : (a / b)⁻¹ = b / a := by simp only [inv_eq_one_div, one_div_div] /-- Given `a` a rational numeral, returns `(b, ⊢ a⁻¹ = b)`. -/ meta def prove_inv : instance_cache → expr → ℚ → tactic (instance_cache × expr × expr) | ic e n := match match_sign e with | sum.inl e := do (ic, e', p) ← prove_inv ic e (-n), (ic, r) ← ic.mk_app ``has_neg.neg [e'], (ic, p) ← ic.mk_app ``inv_neg [e, e', p], return (ic, r, p) | sum.inr ff := do (ic, p) ← ic.mk_app ``inv_zero [], return (ic, e, p) | sum.inr tt := if n.num = 1 then if n.denom = 1 then do (ic, p) ← ic.mk_app ``inv_one [], return (ic, e, p) else do let e := e.app_arg, (ic, p) ← ic.mk_app ``inv_one_div [e], return (ic, e, p) else if n.denom = 1 then do (ic, p) ← ic.mk_app ``inv_div_one [e], e ← infer_type p, return (ic, e.app_arg, p) else do [_, _, a, b] ← return e.get_app_args, (ic, e') ← ic.mk_app ``has_div.div [b, a], (ic, p) ← ic.mk_app ``inv_div [a, b], return (ic, e', p) end theorem div_eq {α} [division_ring α] (a b b' c : α) (hb : b⁻¹ = b') (h : a * b' = c) : a / b = c := by rwa ← hb at h /-- Given `a`,`b` rational numerals, returns `(c, ⊢ a / b = c)`. -/ meta def prove_div (ic : instance_cache) (a b : expr) (na nb : ℚ) : tactic (instance_cache × expr × expr) := do (ic, b', pb) ← prove_inv ic b nb, (ic, c, p) ← prove_mul_rat ic a b' na nb⁻¹, (ic, p) ← ic.mk_app ``div_eq [a, b, b', c, pb, p], return (ic, c, p) /-- Given `a` a rational numeral, returns `(b, ⊢ -a = b)`. -/ meta def prove_neg (ic : instance_cache) (a : expr) : tactic (instance_cache × expr × expr) := match match_sign a with | sum.inl a := do (ic, p) ← ic.mk_app ``neg_neg [a], return (ic, a, p) | sum.inr ff := do (ic, p) ← ic.mk_app ``neg_zero [], return (ic, a, p) | sum.inr tt := do (ic, a') ← ic.mk_app ``has_neg.neg [a], p ← mk_eq_refl a', return (ic, a', p) end theorem sub_pos {α} [add_group α] (a b b' c : α) (hb : -b = b') (h : a + b' = c) : a - b = c := by rwa ← hb at h theorem sub_neg {α} [add_group α] (a b c : α) (h : a + b = c) : a - -b = c := by rwa sub_neg_eq_add /-- Given `a`,`b` rational numerals, returns `(c, ⊢ a - b = c)`. -/ meta def prove_sub (ic : instance_cache) (a b : expr) : tactic (instance_cache × expr × expr) := match match_sign b with | sum.inl b := do (ic, c, p) ← prove_add_rat' ic a b, (ic, p) ← ic.mk_app ``sub_neg [a, b, c, p], return (ic, c, p) | sum.inr ff := do (ic, p) ← ic.mk_app ``sub_zero [a], return (ic, a, p) | sum.inr tt := do (ic, b', pb) ← prove_neg ic b, (ic, c, p) ← prove_add_rat' ic a b', (ic, p) ← ic.mk_app ``sub_pos [a, b, b', c, pb, p], return (ic, c, p) end theorem sub_nat_pos (a b c : ℕ) (h : b + c = a) : a - b = c := h ▸ nat.add_sub_cancel_left _ _ theorem sub_nat_neg (a b c : ℕ) (h : a + c = b) : a - b = 0 := nat.sub_eq_zero_of_le $ h ▸ nat.le_add_right _ _ /-- Given `a : nat`,`b : nat` natural numerals, returns `(c, ⊢ a - b = c)`. -/ meta def prove_sub_nat (ic : instance_cache) (a b : expr) : tactic (expr × expr) := do na ← a.to_nat, nb ← b.to_nat, if nb ≤ na then do (ic, c) ← ic.of_nat (na - nb), (ic, p) ← prove_add_nat ic b c a, return (c, `(sub_nat_pos).mk_app [a, b, c, p]) else do (ic, c) ← ic.of_nat (nb - na), (ic, p) ← prove_add_nat ic a c b, return (`(0 : ℕ), `(sub_nat_neg).mk_app [a, b, c, p]) /-- This is needed because when `a` and `b` are numerals lean is more likely to unfold them than unfold the instances in order to prove that `add_group_has_sub = int.has_sub`. -/ theorem int_sub_hack (a b c : ℤ) (h : @has_sub.sub ℤ add_group_has_sub a b = c) : a - b = c := h /-- Given `a : ℤ`, `b : ℤ` integral numerals, returns `(c, ⊢ a - b = c)`. -/ meta def prove_sub_int (ic : instance_cache) (a b : expr) : tactic (expr × expr) := do (_, c, p) ← prove_sub ic a b, return (c, `(int_sub_hack).mk_app [a, b, c, p]) /-- Evaluates the basic field operations `+`,`neg`,`-`,`*`,`inv`,`/` on numerals. Also handles nat subtraction. Does not do recursive simplification; that is, `1 + 1 + 1` will not simplify but `2 + 1` will. This is handled by the top level `simp` call in `norm_num.derive`. -/ meta def eval_field : expr → tactic (expr × expr) | `(%%e₁ + %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, let n₃ := n₁ + n₂, (c, e₃) ← c.of_rat n₃, (_, p) ← prove_add_rat c e₁ e₂ e₃ n₁ n₂ n₃, return (e₃, p) | `(%%e₁ * %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, prod.snd <$> prove_mul_rat c e₁ e₂ n₁ n₂ | `(- %%e) := do c ← infer_type e >>= mk_instance_cache, prod.snd <$> prove_neg c e | `(@has_sub.sub %%α %%inst %%a %%b) := do c ← mk_instance_cache α, if α = `(nat) then prove_sub_nat c a b else if inst = `(int.has_sub) then prove_sub_int c a b else prod.snd <$> prove_sub c a b | `(has_inv.inv %%e) := do n ← e.to_rat, c ← infer_type e >>= mk_instance_cache, prod.snd <$> prove_inv c e n | `(%%e₁ / %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, prod.snd <$> prove_div c e₁ e₂ n₁ n₂ | _ := failed lemma pow_bit0 [monoid α] (a c' c : α) (b : ℕ) (h : a ^ b = c') (h₂ : c' * c' = c) : a ^ bit0 b = c := h₂ ▸ by simp [pow_bit0, h] lemma pow_bit1 [monoid α] (a c₁ c₂ c : α) (b : ℕ) (h : a ^ b = c₁) (h₂ : c₁ * c₁ = c₂) (h₃ : c₂ * a = c) : a ^ bit1 b = c := by rw [← h₃, ← h₂]; simp [pow_bit1, h] section open match_numeral_result /-- Given `a` a rational numeral and `b : nat`, returns `(c, ⊢ a ^ b = c)`. -/ meta def prove_pow (a : expr) (na : ℚ) : instance_cache → expr → tactic (instance_cache × expr × expr) | ic b := match match_numeral b with | zero := do (ic, p) ← ic.mk_app ``pow_zero [a], (ic, o) ← ic.mk_app ``has_one.one [], return (ic, o, p) | one := do (ic, p) ← ic.mk_app ``pow_one [a], return (ic, a, p) | bit0 b := do (ic, c', p) ← prove_pow ic b, nc' ← expr.to_rat c', (ic, c, p₂) ← prove_mul_rat ic c' c' nc' nc', (ic, p) ← ic.mk_app ``pow_bit0 [a, c', c, b, p, p₂], return (ic, c, p) | bit1 b := do (ic, c₁, p) ← prove_pow ic b, nc₁ ← expr.to_rat c₁, (ic, c₂, p₂) ← prove_mul_rat ic c₁ c₁ nc₁ nc₁, (ic, c, p₃) ← prove_mul_rat ic c₂ a (nc₁ * nc₁) na, (ic, p) ← ic.mk_app ``pow_bit1 [a, c₁, c₂, c, b, p, p₂, p₃], return (ic, c, p) | _ := failed end end /-- Evaluates expressions of the form `a ^ b`, `monoid.pow a b` or `nat.pow a b`. -/ meta def eval_pow : expr → tactic (expr × expr) | `(@has_pow.pow %%α _ %%m %%e₁ %%e₂) := do n₁ ← e₁.to_rat, c ← infer_type e₁ >>= mk_instance_cache, match m with | `(@monoid.has_pow %%_ %%_) := prod.snd <$> prove_pow e₁ n₁ c e₂ | _ := failed end | `(monoid.pow %%e₁ %%e₂) := do n₁ ← e₁.to_rat, c ← infer_type e₁ >>= mk_instance_cache, prod.snd <$> prove_pow e₁ n₁ c e₂ | _ := failed /-- Given `⊢ p`, returns `(true, ⊢ p = true)`. -/ meta def true_intro (p : expr) : tactic (expr × expr) := prod.mk `(true) <$> mk_app ``eq_true_intro [p] /-- Given `⊢ ¬ p`, returns `(false, ⊢ p = false)`. -/ meta def false_intro (p : expr) : tactic (expr × expr) := prod.mk `(false) <$> mk_app ``eq_false_intro [p] theorem not_refl_false_intro {α} (a : α) : (a ≠ a) = false := eq_false_intro $ not_not_intro rfl /-- Evaluates the inequality operations `=`,`<`,`>`,`≤`,`≥`,`≠` on numerals. -/ meta def eval_ineq : expr → tactic (expr × expr) | `(%%e₁ < %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ < n₂ then do (_, p) ← prove_lt_rat c e₁ e₂ n₁ n₂, true_intro p else if n₁ = n₂ then do (_, p) ← c.mk_app ``lt_irrefl [e₁], false_intro p else do (c, p') ← prove_lt_rat c e₂ e₁ n₂ n₁, (_, p) ← c.mk_app ``not_lt_of_gt [e₁, e₂, p'], false_intro p | `(%%e₁ ≤ %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ ≤ n₂ then do (_, p) ← if n₁ = n₂ then c.mk_app ``le_refl [e₁] else prove_le_rat c e₁ e₂ n₁ n₂, true_intro p else do (c, p) ← prove_lt_rat c e₂ e₁ n₂ n₁, (_, p) ← c.mk_app ``not_le_of_gt [e₁, e₂, p], false_intro p | `(%%e₁ = %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ = n₂ then mk_eq_refl e₁ >>= true_intro else do (_, p) ← prove_ne c e₁ e₂ n₁ n₂, false_intro p | `(%%e₁ > %%e₂) := mk_app ``has_lt.lt [e₂, e₁] >>= eval_ineq | `(%%e₁ ≥ %%e₂) := mk_app ``has_le.le [e₂, e₁] >>= eval_ineq | `(%%e₁ ≠ %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ = n₂ then prod.mk `(false) <$> mk_app ``not_refl_false_intro [e₁] else do (_, p) ← prove_ne c e₁ e₂ n₁ n₂, true_intro p | _ := failed theorem nat_succ_eq (a b c : ℕ) (h₁ : a = b) (h₂ : b + 1 = c) : nat.succ a = c := by rwa h₁ /-- Evaluates the expression `nat.succ ... (nat.succ n)` where `n` is a natural numeral. (We could also just handle `nat.succ n` here and rely on `simp` to work bottom up, but we figure that towers of successors coming from e.g. `induction` are a common case.) -/ meta def prove_nat_succ (ic : instance_cache) : expr → tactic (instance_cache × ℕ × expr × expr) | `(nat.succ %%a) := do (ic, n, b, p₁) ← prove_nat_succ a, let n' := n + 1, (ic, c) ← ic.of_nat n', (ic, p₂) ← prove_add_nat ic b `(1) c, return (ic, n', c, `(nat_succ_eq).mk_app [a, b, c, p₁, p₂]) | e := do n ← e.to_nat, p ← mk_eq_refl e, return (ic, n, e, p) lemma nat_div (a b q r m : ℕ) (hm : q * b = m) (h : r + m = a) (h₂ : r < b) : a / b = q := by rw [← h, ← hm, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) h₂), nat.div_eq_of_lt h₂, zero_add] lemma int_div (a b q r m : ℤ) (hm : q * b = m) (h : r + m = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a / b = q := by rw [← h, ← hm, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt h₁ h₂)), int.div_eq_zero_of_lt h₁ h₂, zero_add] lemma nat_mod (a b q r m : ℕ) (hm : q * b = m) (h : r + m = a) (h₂ : r < b) : a % b = r := by rw [← h, ← hm, nat.add_mul_mod_self_right, nat.mod_eq_of_lt h₂] lemma int_mod (a b q r m : ℤ) (hm : q * b = m) (h : r + m = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a % b = r := by rw [← h, ← hm, int.add_mul_mod_self, int.mod_eq_of_lt h₁ h₂] lemma int_div_neg (a b c' c : ℤ) (h : a / b = c') (h₂ : -c' = c) : a / -b = c := h₂ ▸ h ▸ int.div_neg _ _ lemma int_mod_neg (a b c : ℤ) (h : a % b = c) : a % -b = c := (int.mod_neg _ _).trans h /-- Given `a`,`b` numerals in `nat` or `int`, * `prove_div_mod ic a b ff` returns `(c, ⊢ a / b = c)` * `prove_div_mod ic a b tt` returns `(c, ⊢ a % b = c)` -/ meta def prove_div_mod (ic : instance_cache) : expr → expr → bool → tactic (instance_cache × expr × expr) | a b mod := match match_neg b with | some b := do (ic, c', p) ← prove_div_mod a b mod, if mod then return (ic, c', `(int_mod_neg).mk_app [a, b, c', p]) else do (ic, c, p₂) ← prove_neg ic c', return (ic, c, `(int_div_neg).mk_app [a, b, c', c, p, p₂]) | none := do nb ← b.to_nat, na ← a.to_int, let nq := na / nb, let nr := na % nb, let nm := nq * nr, (ic, q) ← ic.of_int nq, (ic, r) ← ic.of_int nr, (ic, m, pm) ← prove_mul_rat ic q b (rat.of_int nq) (rat.of_int nb), (ic, p) ← prove_add_rat ic r m a (rat.of_int nr) (rat.of_int nm) (rat.of_int na), (ic, p') ← prove_lt_nat ic r b, if ic.α = `(nat) then if mod then return (ic, r, `(nat_mod).mk_app [a, b, q, r, m, pm, p, p']) else return (ic, q, `(nat_div).mk_app [a, b, q, r, m, pm, p, p']) else if ic.α = `(int) then do (ic, p₀) ← prove_nonneg ic r, if mod then return (ic, r, `(int_mod).mk_app [a, b, q, r, m, pm, p, p₀, p']) else return (ic, q, `(int_div).mk_app [a, b, q, r, m, pm, p, p₀, p']) else failed end theorem dvd_eq_nat (a b c : ℕ) (p) (h₁ : b % a = c) (h₂ : (c = 0) = p) : (a ∣ b) = p := (propext $ by rw [← h₁, nat.dvd_iff_mod_eq_zero]).trans h₂ theorem dvd_eq_int (a b c : ℤ) (p) (h₁ : b % a = c) (h₂ : (c = 0) = p) : (a ∣ b) = p := (propext $ by rw [← h₁, int.dvd_iff_mod_eq_zero]).trans h₂ /-- Evaluates some extra numeric operations on `nat` and `int`, specifically `nat.succ`, `/` and `%`, and `∣` (divisibility). -/ meta def eval_nat_int_ext : expr → tactic (expr × expr) | e@`(nat.succ _) := do ic ← mk_instance_cache `(ℕ), (_, _, ep) ← prove_nat_succ ic e, return ep | `(%%a / %%b) := do c ← infer_type a >>= mk_instance_cache, prod.snd <$> prove_div_mod c a b ff | `(%%a % %%b) := do c ← infer_type a >>= mk_instance_cache, prod.snd <$> prove_div_mod c a b tt | `(%%a ∣ %%b) := do α ← infer_type a, ic ← mk_instance_cache α, th ← if α = `(nat) then return (`(dvd_eq_nat):expr) else if α = `(int) then return `(dvd_eq_int) else failed, (ic, c, p₁) ← prove_div_mod ic b a tt, (ic, z) ← ic.mk_app ``has_zero.zero [], (e', p₂) ← mk_app ``eq [c, z] >>= eval_ineq, return (e', th.mk_app [a, b, c, e', p₁, p₂]) | _ := failed lemma not_prime_helper (a b n : ℕ) (h : a * b = n) (h₁ : 1 < a) (h₂ : 1 < b) : ¬ nat.prime n := by rw ← h; exact nat.not_prime_mul h₁ h₂ lemma is_prime_helper (n : ℕ) (h₁ : 1 < n) (h₂ : nat.min_fac n = n) : nat.prime n := nat.prime_def_min_fac.2 ⟨h₁, h₂⟩ lemma min_fac_bit0 (n : ℕ) : nat.min_fac (bit0 n) = 2 := by simp [nat.min_fac_eq, show 2 ∣ bit0 n, by simp [bit0_eq_two_mul n]] /-- A predicate representing partial progress in a proof of `min_fac`. -/ def min_fac_helper (n k : ℕ) : Prop := 0 < k ∧ bit1 k ≤ nat.min_fac (bit1 n) theorem min_fac_helper.n_pos {n k : ℕ} (h : min_fac_helper n k) : 0 < n := nat.pos_iff_ne_zero.2 $ λ e, by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2 lemma min_fac_ne_bit0 {n k : ℕ} : nat.min_fac (bit1 n) ≠ bit0 k := by rw bit0_eq_two_mul; exact λ e, absurd ((nat.dvd_add_iff_right (by simp [bit0_eq_two_mul n])).2 (dvd_trans ⟨_, e⟩ (nat.min_fac_dvd _))) dec_trivial lemma min_fac_helper_0 (n : ℕ) (h : 0 < n) : min_fac_helper n 1 := begin refine ⟨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symm⟩, refine @lt_of_le_of_ne ℕ _ _ _ (nat.min_fac_pos _) _, intro e, have := nat.min_fac_prime _, { rw ← e at this, exact nat.not_prime_one this }, { exact ne_of_gt (nat.bit1_lt h) } end lemma min_fac_helper_1 {n k k' : ℕ} (e : k + 1 = k') (np : nat.min_fac (bit1 n) ≠ bit1 k) (h : min_fac_helper n k) : min_fac_helper n k' := begin rw ← e, refine ⟨nat.succ_pos _, (lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _) min_fac_ne_bit0.symm : bit0 (k+1) < _)⟩, { rw add_right_comm, exact h.2 }, { rw add_right_comm, exact np.symm } end lemma min_fac_helper_2 (n k k' : ℕ) (e : k + 1 = k') (np : ¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, intro e₁, rw ← e₁ at np, exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos) end lemma min_fac_helper_3 (n k k' c : ℕ) (e : k + 1 = k') (nc : bit1 n % bit1 k = c) (c0 : 0 < c) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, refine mt _ (ne_of_gt c0), intro e₁, rw [← nc, ← nat.dvd_iff_mod_eq_zero, ← e₁], apply nat.min_fac_dvd end lemma min_fac_helper_4 (n k : ℕ) (hd : bit1 n % bit1 k = 0) (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k := by rw ← nat.dvd_iff_mod_eq_zero at hd; exact le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2 lemma min_fac_helper_5 (n k k' : ℕ) (e : bit1 k * bit1 k = k') (hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n := begin refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2 ⟨nat.bit1_lt h.n_pos, _⟩)).2, rw ← e at hd, intros m m2 hm md, have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm), rw nat.le_sqrt at this, exact not_le_of_lt hd this end /-- Given `e` a natural numeral and `d : nat` a factor of it, return `⊢ ¬ prime e`. -/ meta def prove_non_prime (e : expr) (n d₁ : ℕ) : tactic expr := do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, let d₂ := n / d₁, let e₂ := reflect d₂, (c, e', p) ← prove_mul_nat c e₁ e₂, guard (e' =ₐ e), (c, p₂) ← prove_lt_nat c `(1) e₂, return $ `(not_prime_helper).mk_app [e₁, e₂, e, p, p₁, p₂] /-- Given `a`,`a1 := bit1 a`, `n1` the value of `a1`, `b` and `p : min_fac_helper a b`, returns `(c, ⊢ min_fac a1 = c)`. -/ meta def prove_min_fac_aux (a a1 : expr) (n1 : ℕ) : instance_cache → expr → expr → tactic (instance_cache × expr × expr) | ic b p := do k ← b.to_nat, let k1 := bit1 k, let b1 := `(bit1:ℕ→ℕ).mk_app [b], if n1 < k1*k1 then do (ic, e', p₁) ← prove_mul_nat ic b1 b1, (ic, p₂) ← prove_lt_nat ic a1 e', return (ic, a1, `(min_fac_helper_5).mk_app [a, b, e', p₁, p₂, p]) else let d := k1.min_fac in if to_bool (d < k1) then do let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', p₂ ← prove_non_prime b1 k1 d, prove_min_fac_aux ic e' $ `(min_fac_helper_2).mk_app [a, b, e', p₁, p₂, p] else do let nc := n1 % k1, (ic, c, pc) ← prove_div_mod ic a1 b1 tt, if nc = 0 then return (ic, b1, `(min_fac_helper_4).mk_app [a, b, pc, p]) else do (ic, p₀) ← prove_pos ic c, let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', prove_min_fac_aux ic e' $ `(min_fac_helper_3).mk_app [a, b, e', c, p₁, pc, p₀, p] /-- Given `a` a natural numeral, returns `(b, ⊢ min_fac a = b)`. -/ meta def prove_min_fac (ic : instance_cache) (e : expr) : tactic (instance_cache × expr × expr) := match match_numeral e with | match_numeral_result.zero := return (ic, `(2:ℕ), `(nat.min_fac_zero)) | match_numeral_result.one := return (ic, `(1:ℕ), `(nat.min_fac_one)) | match_numeral_result.bit0 e := return (ic, `(2), `(min_fac_bit0).mk_app [e]) | match_numeral_result.bit1 e := do n ← e.to_nat, c ← mk_instance_cache `(nat), (c, p) ← prove_pos c e, let a1 := `(bit1:ℕ→ℕ).mk_app [e], prove_min_fac_aux e a1 (bit1 n) c `(1) (`(min_fac_helper_0).mk_app [e, p]) | _ := failed end /-- Evaluates the `prime` and `min_fac` functions. -/ meta def eval_prime : expr → tactic (expr × expr) | `(nat.prime %%e) := do n ← e.to_nat, match n with | 0 := false_intro `(nat.not_prime_zero) | 1 := false_intro `(nat.not_prime_one) | _ := let d₁ := n.min_fac in if d₁ < n then prove_non_prime e n d₁ >>= false_intro else do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, (c, e₁, p) ← prove_min_fac c e, true_intro $ `(is_prime_helper).mk_app [e, p₁, p] end | `(nat.min_fac %%e) := do ic ← mk_instance_cache `(ℕ), prod.snd <$> prove_min_fac ic e | _ := failed /-- This version of `derive` does not fail when the input is already a numeral -/ meta def derive.step (e : expr) : tactic (expr × expr) := eval_field e <|> eval_nat_int_ext e <|> eval_pow e <|> eval_ineq e <|> eval_prime e /-- An attribute for adding additional extensions to `norm_num`. To use this attribute, put `@[norm_num]` on a tactic of type `expr → tactic (expr × expr)`; the tactic will be called on subterms by `norm_num`, and it is responsible for identifying that the expression is a numerical function applied to numerals, for example `nat.fib 17`, and should return the reduced numerical expression (which must be in `norm_num`-normal form: a natural or rational numeral, i.e. `37`, `12 / 7` or `-(2 / 3)`, although this can be an expression in any type), and the proof that the original expression is equal to the rewritten expression. Failure is used to indicate that this tactic does not apply to the term. For performance reasons, it is best to detect non-applicability as soon as possible so that the next tactic can have a go, so generally it will start with a pattern match and then checking that the arguments to the term are numerals or of the appropriate form, followed by proof construction, which should not fail. Propositions are treated like any other term. The normal form for propositions is `true` or `false`, so it should produce a proof of the form `p = true` or `p = false`. `eq_true_intro` can be used to help here. -/ @[user_attribute] protected meta def attr : user_attribute (expr → tactic (expr × expr)) unit := { name := `norm_num, descr := "Add norm_num derivers", cache_cfg := { mk_cache := λ ns, do { t ← ns.mfoldl (λ (t : expr → tactic (expr × expr)) n, do t' ← eval_expr (expr → tactic (expr × expr)) (expr.const n []), pure (λ e, t' e <|> t e)) (λ _, failed), pure (λ e, derive.step e <|> t e) }, dependencies := [] } } add_tactic_doc { name := "norm_num", category := doc_category.attr, decl_names := [`norm_num.attr], tags := ["arithmetic", "decision_procedure"] } /-- Look up the `norm_num` extensions in the cache and return a tactic extending `derive.step` with additional reduction procedures. -/ meta def get_step : tactic (expr → tactic (expr × expr)) := norm_num.attr.get_cache /-- Simplify an expression bottom-up using `step` to simplify the subexpressions. -/ meta def derive' (step : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | e := do e ← instantiate_mvars e, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ _, failed) (λ _ _ _ _ e, do (new_e, pr) ← step e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, tt)) `eq e, return (e', pr) /-- Simplify an expression bottom-up using the default `norm_num` set to simplify the subexpressions. -/ meta def derive (e : expr) : tactic (expr × expr) := do f ← get_step, derive' f e end norm_num /-- Basic version of `norm_num` that does not call `simp`. It uses the provided `step` tactic to simplify the expression; use `get_step` to get the default `norm_num` set and `derive.step` for the basic builtin set of simplifications. -/ meta def tactic.norm_num1 (step : expr → tactic (expr × expr)) (loc : interactive.loc) : tactic unit := do ns ← loc.get_locals, tt ← tactic.replace_at (norm_num.derive' step) ns loc.include_goal | fail "norm_num failed to simplify", when loc.include_goal $ try tactic.triv, when (¬ ns.empty) $ try tactic.contradiction /-- Normalize numerical expressions. It uses the provided `step` tactic to simplify the expression; use `get_step` to get the default `norm_num` set and `derive.step` for the basic builtin set of simplifications. -/ meta def tactic.norm_num (step : expr → tactic (expr × expr)) (hs : list simp_arg_type) (l : interactive.loc) : tactic unit := repeat1 $ orelse' (tactic.norm_num1 step l) $ interactive.simp_core {} (tactic.norm_num1 step (interactive.loc.ns [none])) ff (simp_arg_type.except ``one_div :: hs) [] l namespace tactic.interactive open norm_num interactive interactive.types /-- Basic version of `norm_num` that does not call `simp`. -/ meta def norm_num1 (loc : parse location) : tactic unit := do f ← get_step, tactic.norm_num1 f loc /-- Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `ℕ`, `ℤ`, `ℚ`, `ℝ`, `ℂ` and some general algebraic types, and can prove goals of the form `A = B`, `A ≠ B`, `A < B` and `A ≤ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. -/ meta def norm_num (hs : parse simp_arg_list) (l : parse location) : tactic unit := do f ← get_step, tactic.norm_num f hs l add_hint_tactic "norm_num" /-- Normalizes a numerical expression and tries to close the goal with the result. -/ meta def apply_normed (x : parse texpr) : tactic unit := do x₁ ← to_expr x, (x₂,_) ← derive x₁, tactic.exact x₂ /-- Normalises numerical expressions. It supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `ℕ`, `ℤ`, `ℚ`, `ℝ`, `ℂ`, and can prove goals of the form `A = B`, `A ≠ B`, `A < B` and `A ≤ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. ```lean import data.real.basic example : (2 : ℝ) + 2 = 4 := by norm_num example : (12345.2 : ℝ) ≠ 12345.3 := by norm_num example : (73 : ℝ) < 789/2 := by norm_num example : 123456789 + 987654321 = 1111111110 := by norm_num example (R : Type*) [ring R] : (2 : R) + 2 = 4 := by norm_num example (F : Type*) [linear_ordered_field F] : (2 : F) + 2 < 5 := by norm_num example : nat.prime (2^13 - 1) := by norm_num example : ¬ nat.prime (2^11 - 1) := by norm_num example (x : ℝ) (h : x = 123 + 456) : x = 579 := by norm_num at h; assumption ``` The variant `norm_num1` does not call `simp`. Both `norm_num` and `norm_num1` can be called inside the `conv` tactic. The tactic `apply_normed` normalises a numerical expression and tries to close the goal with the result. Compare: ```lean def a : ℕ := 2^100 #print a -- 2 ^ 100 def normed_a : ℕ := by apply_normed 2^100 #print normed_a -- 1267650600228229401496703205376 ``` -/ add_tactic_doc { name := "norm_num", category := doc_category.tactic, decl_names := [`tactic.interactive.norm_num1, `tactic.interactive.norm_num, `tactic.interactive.apply_normed], tags := ["arithmetic", "decision procedure"] } end tactic.interactive namespace conv.interactive open conv interactive tactic.interactive open norm_num (derive) /-- Basic version of `norm_num` that does not call `simp`. -/ meta def norm_num1 : conv unit := replace_lhs derive /-- Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `ℕ`, `ℤ`, `ℚ`, `ℝ`, `ℂ` and some general algebraic types, and can prove goals of the form `A = B`, `A ≠ B`, `A < B` and `A ≤ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. -/ meta def norm_num (hs : parse simp_arg_list) : conv unit := repeat1 $ orelse' norm_num1 $ conv.interactive.simp ff (simp_arg_type.except ``one_div :: hs) [] { discharger := tactic.interactive.norm_num1 (loc.ns [none]) } end conv.interactive
27e8c6559793067dd3633a847141b0c5b9cc600d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/445.lean
1e93c8e1e96ae58f5a837883052e030daf887c3d
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
112
lean
constants (A : Type) (a : A) constants (B : Type) (b₁ b₂ : B) (H : b₁ = b₂) check a check b₁ check H
c5efecc3c758391809288f11f951b5d51854bee1
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/05_Interacting_with_Lean.org.18.lean
7ceeadcfe408965c67d0f783b98eab82ed174600
[]
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
635
lean
/- page 73 -/ import standard import data.nat data.int open nat int variables a b : int variables m n : nat -- BEGIN check 123 -- 123 : num check (123 : nat) -- 123 : ℕ check (123 : int) -- 123 : ℤ check a + n -- a + n : ℤ check n + a -- n + a : ℤ check a + 123 -- a + 123 : ℤ set_option pp.coercions true check 123 -- 123 : num check (123 : nat) -- of_num 123 : ℕ check (123 : int) -- of_nat (of_num 123) : ℤ check a + n -- a + of_nat n : ℤ check n + a -- of_nat n + a : ℤ check a + 123 -- a + of_nat (of_num 123) : ℤ -- END
690b0c786f17308db7a5cd8b3745cbd971ee2ffa
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/async_tactic.lean
0f86f1dfe11ab310f480b1e6a466bb3fac765493
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,851
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ prelude import init.meta.tactic import init.meta.interactive namespace tactic private meta def report {α} (s : tactic_state) : option (unit → format) → α | (some fmt) := undefined_core $ to_string $ fmt () ++ format.line ++ to_fmt s | none := undefined_core "silent failure" private meta def run_or_fail {α} (s : tactic_state) (tac : tactic α) : α := match tac s with | (result.success a s) := a | (result.exception fmt _ s') := report s' fmt end meta def run_async {α : Type} (tac : tactic α) : tactic (task α) := do s ← read, return $ task.delay $ λ _, match tac s with | (result.success a s) := a | (result.exception fmt _ s') := report s' fmt end meta def prove_goal_async (tac : tactic unit) : tactic unit := do ctx ← local_context, revert_lst ctx, tgt ← target, tgt ← instantiate_mvars tgt, env ← get_env, tgt ← return $ env^.unfold_untrusted_macros tgt, when tgt^.has_meta_var (fail "goal contains metavariables"), params ← return tgt^.collect_univ_params, lemma_name ← new_aux_decl_name, proof ← run_async (do goal_meta ← mk_meta_var tgt, set_goals [goal_meta], monad.for' ctx (λc, intro c^.local_pp_name), tac, proof ← instantiate_mvars goal_meta, proof ← return $ env^.unfold_untrusted_macros proof, when proof^.has_meta_var $ fail "async proof failed: contains metavariables", return proof), add_decl $ declaration.thm lemma_name params tgt proof, exact (expr.const lemma_name (params^.for level.param)) namespace interactive open interactive.types /-- Proves the first goal asynchronously as a separate lemma. -/ meta def async (tac : irtactic) : tactic unit := prove_goal_async tac end interactive end tactic
6a41dd5c32b77caf2341078dfb630c3f3ccc016b
dac4e6671410f506c880986cbb2212dd7f5b3dfd
/hanoi_project/nga.lean
6ca0a56405b33b73d3a2b3573300129a17c5f75a
[ "CC-BY-4.0" ]
permissive
thalesant/formalabstracts-2018
e6ddfe8b3ce5c6f055ddcccf345bf55c41f850c1
d206dfa32a6b4a84aacc3a5500a144757e6d3454
refs/heads/master
1,642,678,879,231
1,561,648,713,000
1,561,648,713,000
97,608,420
1
0
null
1,564,063,995,000
1,500,388,250,000
Lean
UTF-8
Lean
false
false
22
lean
#print "Good morning!"
dc325b06736772213deb244d33cc9dfb479b5079
193da933cf42f2f9188bb47e3c973205bc2abc5c
/AA_Algebras/01_dm_bool/2b_dm_bool_test.lean
46d6ee891cf6163dca0aeb42ca0ca699191aa41f
[]
no_license
pandaman64/cs-dm
aa4e2621c7a19e2dae911bc237c33e02fcb0c7a3
bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c
refs/heads/master
1,647,620,340,607
1,570,055,187,000
1,570,055,187,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,222
lean
/- To use definitions from another lean, you must "import" that file: directly or indirectly. If you don't import, then the symbols in that file are not defined in this one. To see, uncomment the following line. Hover to see the error and understand what it's saying. As an aside, importing from a file reveals the definitions at the top-level namespace. If some sumbols in that file are defined in namespaces, then either the names in the file must qualified, or the namespace has to be opened. -/ -- #check dm_bool /- To use definitions established in another .lean file, "import" the file by name. Lean has to know where to look to find the file. It has several ways to do this. We use a simple one: we prefix the file name with a dot (period) to tell lean to look for the current directory, the same directory as this file (dm_bool_test.lean). -/ import .dm_bool /- Having imported from that file, you can now use the definitions from there, provided you use qualified names. As an example, the type, dm_bool, is defined in dm_bool.lean, and within the edu.virginia.cs.dm namespace. -/ #check edu.virginia.cs.dm.dm_bool /- If you want to use identifiers defined from that file without explicit qualification, you have to "open" the namespace. -/ open edu.virginia.cs.dm /- Having imported from dm_bool.lean and opened the edu.virginia.cs.dm namespace, we can now access definitons from that file without the namespace qualifiers. -/ #check dm_bool /- That's what you go through when you want the module you're writing (in this file) to be able to use definitions from another module (another file) without qualifiers. -/ /- You may still need to use qualifiers to access definitions from that module that are nested inside other namespaces. Here is an example. -/ -- #check dm_tt /- doesn't work -/ #check dm_bool.dm_tt -- open dm_bool /- doesn't work! explain. -/ /- We can now use the dm_bool type in this mofule. file. We'll also want to use the functions that implement the operators defined in that file. -/ #check and_boolean /- we are now set up to write test cases for the dm_bool-related code in that module. -/ /- A test case is a little program and two values: the value expected according to this test case, and the value actually obtained when the program is run. Here's an example. We understand the following program to be an expression that reduces to the conjuection of the Boolean true and false values, which is false. That's the expected value. Now hover over the #check to see the answer actually obtained by "running. (evaluating) the program. -/ #reduce and_boolean dm_bool.dm_tt dm_bool.dm_ff /- The actual result equals the expeced result. So we say that the program passed the test (case). -/ /- All that this this means, however is that "the test case did not succeed in flagging a possible programming bug." So in a sense a test case the a program passes is a failure. -/ /- Testing succeeds when it finds important bugs in your program: bugs that you didn't know about and that you would think need to be fixed. -/ /- EXERCISE: WRITE TEST CASES FOR ALL OF THE OPERATORS IN OUR IMPLEMENTATION OF BOOLEAN ALGEBRA. -/
3161f2a9abf1e30d106414386ceda30456f9f3cc
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/PostPort/Pow.lean
3c1e48e6e85c3952563ae56010f82ddfc6de6ad6
[]
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
428
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Mathlib.Lean3Lib.init.core namespace Mathlib universes u v variable {α : Type u} {β : Type v} noncomputable instance [has_pow α β] : HPow α β α := ⟨pow⟩ noncomputable instance [has_pow α α] : Pow α := ⟨pow⟩ end Mathlib
fa59bf794486dbcc9971adc4b4ead42a1d0cbec1
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/tactic_paper_supplemental_material/monoid_cancellation/cancellation_solver.lean
312b57c072ef26da714ba417922726c904150878
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
1,586
lean
import .smt local infix * := star open tactic smt_tactic attribute [ematch] one_star meta def qf_monoid := using_smt $ smt_tactic.intros; eblast section variables {a b c d : m} lemma plus_unit_p : a = b → o * a = b := by qf_monoid lemma plus_assoc_p1 : a * (b * c) = d → (a * b) * c = d := by qf_monoid lemma plus_assoc_p2 : b * (a * c) = d → (a * b) * c = d := by qf_monoid lemma plus_comm_p : a * b = c → b * a = c := by qf_monoid lemma plus_unit_c : a = b → a = o * b := by qf_monoid lemma plus_assoc_c1 : d = a * (b * c) → (a * b) * c = d := by qf_monoid lemma plus_assoc_c2 : d = b * (a * c) → (a * b) * c = d := by qf_monoid lemma plus_comm_c : c = a * b → c = b * a := by qf_monoid lemma plus_cancel : a = c → b = d → a * b = c * d := by qf_monoid lemma plus_refl : a = a := by qf_monoid end open tactic expr meta def iter_right := applyc `plus_unit_c <|> applyc `plus_assoc_c1 >> iter_right <|> applyc `plus_assoc_c2 >> iter_right <|> applyc `plus_cancel >> reflexivity meta def iter_left := applyc `plus_unit_p <|> applyc `plus_assoc_p1 >> iter_left <|> applyc `plus_assoc_p2 >> iter_left <|> iter_right <|> applyc `plus_comm_p >> iter_right meta def cancel := iter_left <|> applyc `plus_comm_p >> iter_left meta def solve : tactic unit := repeat $ reflexivity <|> cancel meta def solve_single := reflexivity <|> cancel lemma monoid_ex1 (a b c d: m): (a * b * c) * c = c * (b * c) * a := begin applyc `plus_assoc_p1, applyc `plus_assoc_p1, applyc `plus_comm_p, applyc `plus_assoc_c1, applyc `plus_assoc_c1, sorry end
755a570210d1f5290868561603d9c69f2870df9c
957a80ea22c5abb4f4670b250d55534d9db99108
/library/data/vector.lean
64b9abe2756ba41ce85df61254a08486b7cc05e1
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
4,563
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 Tuples are lists of a fixed size. It is implemented as a subtype. -/ prelude import init.data.list init.data.subtype init.meta.interactive init.data.fin universes u v w def vector (α : Type u) (n : ℕ) := { l : list α // l.length = n } namespace vector variables {α : Type u} {β : Type v} {φ : Type w} variable {n : ℕ} instance [decidable_eq α] : decidable_eq (vector α n) := begin unfold vector, apply_instance end @[pattern] def nil : vector α 0 := ⟨[], rfl⟩ @[pattern] def cons : α → vector α n → vector α (nat.succ n) | a ⟨ v, h ⟩ := ⟨ a::v, congr_arg nat.succ h ⟩ @[reducible] def length (v : vector α n) : ℕ := n notation a :: b := cons a b open nat def head : vector α (nat.succ n) → α | ⟨ [], h ⟩ := by contradiction | ⟨ a :: v, h ⟩ := a theorem head_cons (a : α) : Π (v : vector α n), head (a :: v) = a | ⟨ l, h ⟩ := rfl def tail : vector α n → vector α (n - 1) | ⟨ [], h ⟩ := ⟨ [], congr_arg pred h ⟩ | ⟨ a :: v, h ⟩ := ⟨ v, congr_arg pred h ⟩ theorem tail_cons (a : α) : Π (v : vector α n), tail (a :: v) = v | ⟨ l, h ⟩ := rfl @[simp] theorem cons_head_tail : ∀ v : vector α (succ n), (head v :: tail v) = v | ⟨ [], h ⟩ := by contradiction | ⟨ a :: v, h ⟩ := rfl def to_list (v : vector α n) : list α := v.1 def nth : Π (v : vector α n), fin n → α | ⟨ l, h ⟩ i := l.nth_le i.1 (by rw h; exact i.2) def append {n m : nat} : vector α n → vector α m → vector α (n + m) | ⟨ l₁, h₁ ⟩ ⟨ l₂, h₂ ⟩ := ⟨ l₁ ++ l₂, by simp * ⟩ @[elab_as_eliminator] def elim {α} {C : Π {n}, vector α n → Sort u} (H : ∀l : list α, C ⟨l, rfl⟩) {n : nat} : Π (v : vector α n), C v | ⟨l, h⟩ := match n, h with ._, rfl := H l end /- map -/ def map (f : α → β) : vector α n → vector β n | ⟨ l, h ⟩ := ⟨ list.map f l, by simp * ⟩ @[simp] theorem map_nil (f : α → β) : map f nil = nil := rfl theorem map_cons (f : α → β) (a : α) : Π (v : vector α n), map f (a::v) = f a :: map f v | ⟨l,h⟩ := rfl def map₂ (f : α → β → φ) : vector α n → vector β n → vector φ n | ⟨ x, _ ⟩ ⟨ y, _ ⟩ := ⟨ list.map₂ f x y, by simp * ⟩ def repeat (a : α) (n : ℕ) : vector α n := ⟨ list.repeat a n, list.length_repeat a n ⟩ def drop (i : ℕ) : vector α n → vector α (n - i) | ⟨l, p⟩ := ⟨ list.drop i l, by simp * ⟩ def take (i : ℕ) : vector α n → vector α (min i n) | ⟨l, p⟩ := ⟨ list.take i l, by simp * ⟩ def remove_nth (i : fin n) : vector α n → vector α (n - 1) | ⟨l, p⟩ := ⟨ list.remove_nth l i.1, by rw [l.length_remove_nth i.1]; rw p; exact i.2 ⟩ def of_fn : Π {n}, (fin n → α) → vector α n | 0 f := nil | (n+1) f := f 0 :: of_fn (λi, f i.succ) section accum open prod variable {σ : Type} def map_accumr (f : α → σ → σ × β) : vector α n → σ → σ × vector β n | ⟨ x, px ⟩ c := let res := list.map_accumr f x c in ⟨ res.1, res.2, by simp * ⟩ def map_accumr₂ {α β σ φ : Type} (f : α → β → σ → σ × φ) : vector α n → vector β n → σ → σ × vector φ n | ⟨ x, px ⟩ ⟨ y, py ⟩ c := let res := list.map_accumr₂ f x y c in ⟨ res.1, res.2, by simp * ⟩ end accum protected theorem eq {n : ℕ} : ∀ (a1 a2 : vector α n), to_list a1 = to_list a2 → a1 = a2 | ⟨x, h1⟩ ⟨._, h2⟩ rfl := rfl protected theorem eq_nil (v : vector α 0) : v = nil := v.eq nil (list.eq_nil_of_length_eq_zero v.2) @[simp] theorem to_list_mk (v : list α) (P : list.length v = n) : to_list (subtype.mk v P) = v := rfl @[simp] theorem to_list_nil : to_list nil = @list.nil α := rfl @[simp] theorem to_list_length (v : vector α n) : (to_list v).length = n := v.2 @[simp] theorem to_list_cons (a : α) (v : vector α n) : to_list (a :: v) = a :: to_list v := begin cases v, reflexivity end @[simp] theorem to_list_append {n m : nat} (v : vector α n) (w : vector α m) : to_list (append v w) = to_list v ++ to_list w := begin cases v, cases w, reflexivity end @[simp] theorem to_list_drop {n m : ℕ} (v : vector α m) : to_list (drop n v) = list.drop n (to_list v) := begin cases v, reflexivity end @[simp] theorem to_list_take {n m : ℕ} (v : vector α m) : to_list (take n v) = list.take n (to_list v) := begin cases v, reflexivity end end vector
632b429fb15dbfb03d23cd6559811a7b39518602
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/ring_theory/witt_vector/structure_polynomial.lean
56c0ebbbc5b430b468135a01ee2fd18504fd0cce
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,512
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import data.matrix.notation import field_theory.mv_polynomial import field_theory.finite.polynomial import number_theory.basic import ring_theory.witt_vector.witt_polynomial /-! # Witt structure polynomials In this file we prove the main theorem that makes the whole theory of Witt vectors work. Briefly, consider a polynomial `Φ : mv_polynomial idx ℤ` over the integers, with polynomials variables indexed by an arbitrary type `idx`. Then there exists a unique family of polynomials `φ : ℕ → mv_polynomial (idx × ℕ) Φ` such that for all `n : ℕ` we have (`witt_structure_int_exists_unique`) ``` bind₁ φ (witt_polynomial p ℤ n) = bind₁ (λ i, (rename (prod.mk i) (witt_polynomial p ℤ n))) Φ ``` In other words: evaluating the `n`-th Witt polynomial on the family `φ` is the same as evaluating `Φ` on the (appropriately renamed) `n`-th Witt polynomials. N.b.: As far as we know, these polynomials do not have a name in the literature, so we have decided to call them the “Witt structure polynomials”. See `witt_structure_int`. ## Special cases With the main result of this file in place, we apply it to certain special polynomials. For example, by taking `Φ = X tt + X ff` resp. `Φ = X tt * X ff` we obtain families of polynomials `witt_add` resp. `witt_mul` (with type `ℕ → mv_polynomial (bool × ℕ) ℤ`) that will be used in later files to define the addition and multiplication on the ring of Witt vectors. ## Outline of the proof The proof of `witt_structure_int_exists_unique` is rather technical, and takes up most of this file. We start by proving the analogous version for polynomials with rational coefficients, instead of integer coefficients. In this case, the solution is rather easy, since the Witt polynomials form a faithful change of coordinates in the polynomial ring `mv_polynomial ℕ ℚ`. We therefore obtain a family of polynomials `witt_structure_rat Φ` for every `Φ : mv_polynomial idx ℚ`. If `Φ` has integer coefficients, then the polynomials `witt_structure_rat Φ n` do so as well. Proving this claim is the essential core of this file, and culminates in `map_witt_structure_int`, which proves that upon mapping the coefficients of `witt_structure_int Φ n` from the integers to the rationals, one obtains `witt_structure_rat Φ n`. Ultimately, the proof of `map_witt_structure_int` relies on ``` dvd_sub_pow_of_dvd_sub {R : Type*} [comm_ring R] {p : ℕ} {a b : R} : (p : R) ∣ a - b → ∀ (k : ℕ), (p : R) ^ (k + 1) ∣ a ^ p ^ k - b ^ p ^ k ``` ## Main results * `witt_structure_rat Φ`: the family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℚ` associated with `Φ : mv_polynomial idx ℚ` and satisfying the property explained above. * `witt_structure_rat_prop`: the proof that `witt_structure_rat` indeed satisfies the property. * `witt_structure_int Φ`: the family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℤ` associated with `Φ : mv_polynomial idx ℤ` and satisfying the property explained above. * `map_witt_structure_int`: the proof that the integral polynomials `with_structure_int Φ` are equal to `witt_structure_rat Φ` when mapped to polynomials with rational coefficients. * `witt_structure_int_prop`: the proof that `witt_structure_int` indeed satisfies the property. * Five families of polynomials that will be used to define the ring structure on the ring of Witt vectors: - `witt_vector.witt_zero` - `witt_vector.witt_one` - `witt_vector.witt_add` - `witt_vector.witt_mul` - `witt_vector.witt_neg` (We also define `witt_vector.witt_sub`, and later we will prove that it describes subtraction, which is defined as `λ a b, a + -b`. See `witt_vector.sub_coeff` for this proof.) ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ open mv_polynomial open set open finset (range) open finsupp (single) -- This lemma reduces a bundled morphism to a "mere" function, -- and consequently the simplifier cannot use a lot of powerful simp-lemmas. -- We disable this locally, and probably it should be disabled globally in mathlib. local attribute [-simp] coe_eval₂_hom variables {p : ℕ} {R : Type*} {idx : Type*} [comm_ring R] open_locale witt open_locale big_operators section p_prime variables (p) [hp : fact p.prime] include hp /-- `witt_structure_rat Φ` is a family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℚ` that are uniquely characterised by the property that ``` bind₁ (witt_structure_rat p Φ) (witt_polynomial p ℚ n) = bind₁ (λ i, (rename (prod.mk i) (witt_polynomial p ℚ n))) Φ ``` In other words: evaluating the `n`-th Witt polynomial on the family `witt_structure_rat Φ` is the same as evaluating `Φ` on the (appropriately renamed) `n`-th Witt polynomials. See `witt_structure_rat_prop` for this property, and `witt_structure_rat_exists_unique` for the fact that `witt_structure_rat` gives the unique family of polynomials with this property. These polynomials turn out to have integral coefficients, but it requires some effort to show this. See `witt_structure_int` for the version with integral coefficients, and `map_witt_structure_int` for the fact that it is equal to `witt_structure_rat` when mapped to polynomials over the rationals. -/ noncomputable def witt_structure_rat (Φ : mv_polynomial idx ℚ) (n : ℕ) : mv_polynomial (idx × ℕ) ℚ := bind₁ (λ k, bind₁ (λ i, rename (prod.mk i) (W_ ℚ k)) Φ) (X_in_terms_of_W p ℚ n) theorem witt_structure_rat_prop (Φ : mv_polynomial idx ℚ) (n : ℕ) : bind₁ (witt_structure_rat p Φ) (W_ ℚ n) = bind₁ (λ i, (rename (prod.mk i) (W_ ℚ n))) Φ := calc bind₁ (witt_structure_rat p Φ) (W_ ℚ n) = bind₁ (λ k, bind₁ (λ i, (rename (prod.mk i)) (W_ ℚ k)) Φ) (bind₁ (X_in_terms_of_W p ℚ) (W_ ℚ n)) : by { rw bind₁_bind₁, apply eval₂_hom_congr (ring_hom.ext_rat _ _) rfl rfl } ... = bind₁ (λ i, (rename (prod.mk i) (W_ ℚ n))) Φ : by rw [bind₁_X_in_terms_of_W_witt_polynomial p _ n, bind₁_X_right] theorem witt_structure_rat_exists_unique (Φ : mv_polynomial idx ℚ) : ∃! (φ : ℕ → mv_polynomial (idx × ℕ) ℚ), ∀ (n : ℕ), bind₁ φ (W_ ℚ n) = bind₁ (λ i, (rename (prod.mk i) (W_ ℚ n))) Φ := begin refine ⟨witt_structure_rat p Φ, _, _⟩, { intro n, apply witt_structure_rat_prop }, { intros φ H, funext n, rw show φ n = bind₁ φ (bind₁ (W_ ℚ) (X_in_terms_of_W p ℚ n)), { rw [bind₁_witt_polynomial_X_in_terms_of_W p, bind₁_X_right] }, rw [bind₁_bind₁], exact eval₂_hom_congr (ring_hom.ext_rat _ _) (funext H) rfl }, end lemma witt_structure_rat_rec_aux (Φ : mv_polynomial idx ℚ) (n : ℕ) : witt_structure_rat p Φ n * C (p ^ n : ℚ) = bind₁ (λ b, rename (λ i, (b, i)) (W_ ℚ n)) Φ - ∑ i in range n, C (p ^ i : ℚ) * (witt_structure_rat p Φ i) ^ p ^ (n - i) := begin have := X_in_terms_of_W_aux p ℚ n, replace := congr_arg (bind₁ (λ k : ℕ, bind₁ (λ i, rename (prod.mk i) (W_ ℚ k)) Φ)) this, rw [alg_hom.map_mul, bind₁_C_right] at this, convert this using 1, clear this, conv_rhs { simp only [alg_hom.map_sub, bind₁_X_right] }, rw sub_right_inj, simp only [alg_hom.map_sum, alg_hom.map_mul, bind₁_C_right, alg_hom.map_pow], refl end /-- Write `witt_structure_rat p φ n` in terms of `witt_structure_rat p φ i` for `i < n`. -/ lemma witt_structure_rat_rec (Φ : mv_polynomial idx ℚ) (n : ℕ) : (witt_structure_rat p Φ n) = C (1 / p ^ n : ℚ) * (bind₁ (λ b, (rename (λ i, (b, i)) (W_ ℚ n))) Φ - ∑ i in range n, C (p ^ i : ℚ) * (witt_structure_rat p Φ i) ^ p ^ (n - i)) := begin calc witt_structure_rat p Φ n = C (1 / p ^ n : ℚ) * (witt_structure_rat p Φ n * C (p ^ n : ℚ)) : _ ... = _ : by rw witt_structure_rat_rec_aux, rw [mul_left_comm, ← C_mul, div_mul_cancel, C_1, mul_one], exact pow_ne_zero _ (nat.cast_ne_zero.2 hp.1.ne_zero), end /-- `witt_structure_int Φ` is a family of polynomials `ℕ → mv_polynomial (idx × ℕ) ℚ` that are uniquely characterised by the property that ``` bind₁ (witt_structure_int p Φ) (witt_polynomial p ℚ n) = bind₁ (λ i, (rename (prod.mk i) (witt_polynomial p ℚ n))) Φ ``` In other words: evaluating the `n`-th Witt polynomial on the family `witt_structure_int Φ` is the same as evaluating `Φ` on the (appropriately renamed) `n`-th Witt polynomials. See `witt_structure_int_prop` for this property, and `witt_structure_int_exists_unique` for the fact that `witt_structure_int` gives the unique family of polynomials with this property. -/ noncomputable def witt_structure_int (Φ : mv_polynomial idx ℤ) (n : ℕ) : mv_polynomial (idx × ℕ) ℤ := finsupp.map_range rat.num (rat.coe_int_num 0) (witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) n) variable {p} lemma bind₁_rename_expand_witt_polynomial (Φ : mv_polynomial idx ℤ) (n : ℕ) (IH : ∀ m : ℕ, m < (n + 1) → map (int.cast_ring_hom ℚ) (witt_structure_int p Φ m) = witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) m) : bind₁ (λ b, rename (λ i, (b, i)) (expand p (W_ ℤ n))) Φ = bind₁ (λ i, expand p (witt_structure_int p Φ i)) (W_ ℤ n) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_bind₁, map_rename, map_expand, rename_expand, map_witt_polynomial], have key := (witt_structure_rat_prop p (map (int.cast_ring_hom ℚ) Φ) n).symm, apply_fun expand p at key, simp only [expand_bind₁] at key, rw key, clear key, apply eval₂_hom_congr' rfl _ rfl, rintro i hi -, rw [witt_polynomial_vars, finset.mem_range] at hi, simp only [IH i hi], end lemma C_p_pow_dvd_bind₁_rename_witt_polynomial_sub_sum (Φ : mv_polynomial idx ℤ) (n : ℕ) (IH : ∀ m : ℕ, m < n → map (int.cast_ring_hom ℚ) (witt_structure_int p Φ m) = witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) m) : C ↑(p ^ n) ∣ (bind₁ (λ (b : idx), rename (λ i, (b, i)) (witt_polynomial p ℤ n)) Φ - ∑ i in range n, C (↑p ^ i) * witt_structure_int p Φ i ^ p ^ (n - i)) := begin cases n, { simp only [is_unit_one, int.coe_nat_zero, int.coe_nat_succ, zero_add, pow_zero, C_1, is_unit.dvd] }, -- prepare a useful equation for rewriting have key := bind₁_rename_expand_witt_polynomial Φ n IH, apply_fun (map (int.cast_ring_hom (zmod (p ^ (n + 1))))) at key, conv_lhs at key { simp only [map_bind₁, map_rename, map_expand, map_witt_polynomial] }, -- clean up and massage rw [nat.succ_eq_add_one, C_dvd_iff_zmod, ring_hom.map_sub, sub_eq_zero, map_bind₁], simp only [map_rename, map_witt_polynomial, witt_polynomial_zmod_self], rw key, clear key IH, rw [bind₁, aeval_witt_polynomial, ring_hom.map_sum, ring_hom.map_sum, finset.sum_congr rfl], intros k hk, rw finset.mem_range at hk, simp only [← sub_eq_zero, ← ring_hom.map_sub, ← C_dvd_iff_zmod, C_eq_coe_nat, ← mul_sub, ← int.nat_cast_eq_coe_nat, ← nat.cast_pow], rw show p ^ (n + 1) = p ^ k * p ^ (n - k + 1), { rw ← pow_add, congr' 1, omega }, rw [nat.cast_mul, nat.cast_pow, nat.cast_pow], apply mul_dvd_mul_left, rw show p ^ (n + 1 - k) = p * p ^ (n - k), { rw ← pow_succ, congr' 1, omega }, rw [pow_mul], -- the machine! apply dvd_sub_pow_of_dvd_sub, rw [← C_eq_coe_nat, int.nat_cast_eq_coe_nat, C_dvd_iff_zmod, ring_hom.map_sub, sub_eq_zero, map_expand, ring_hom.map_pow, mv_polynomial.expand_zmod], end variables (p) @[simp] lemma map_witt_structure_int (Φ : mv_polynomial idx ℤ) (n : ℕ) : map (int.cast_ring_hom ℚ) (witt_structure_int p Φ n) = witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) n := begin apply nat.strong_induction_on n, clear n, intros n IH, rw [witt_structure_int, map_map_range_eq_iff, int.coe_cast_ring_hom], intro c, rw [witt_structure_rat_rec, coeff_C_mul, mul_comm, mul_div_assoc', mul_one], have sum_induction_steps : map (int.cast_ring_hom ℚ) (∑ i in range n, C (p ^ i : ℤ) * (witt_structure_int p Φ i) ^ p ^ (n - i)) = ∑ i in range n, C (p ^ i : ℚ) * (witt_structure_rat p (map (int.cast_ring_hom ℚ) Φ) i) ^ p ^ (n - i), { rw [ring_hom.map_sum], apply finset.sum_congr rfl, intros i hi, rw finset.mem_range at hi, simp only [IH i hi, ring_hom.map_mul, ring_hom.map_pow, map_C], refl }, simp only [← sum_induction_steps, ← map_witt_polynomial p (int.cast_ring_hom ℚ), ← map_rename, ← map_bind₁, ← ring_hom.map_sub, coeff_map], rw show (p : ℚ)^n = ((p^n : ℕ) : ℤ), by norm_cast, rw [← rat.denom_eq_one_iff, ring_hom.eq_int_cast, rat.denom_div_cast_eq_one_iff], swap, { exact_mod_cast pow_ne_zero n hp.1.ne_zero }, revert c, rw [← C_dvd_iff_dvd_coeff], exact C_p_pow_dvd_bind₁_rename_witt_polynomial_sub_sum Φ n IH, end variables (p) theorem witt_structure_int_prop (Φ : mv_polynomial idx ℤ) (n) : bind₁ (witt_structure_int p Φ) (witt_polynomial p ℤ n) = bind₁ (λ i, rename (prod.mk i) (W_ ℤ n)) Φ := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, have := witt_structure_rat_prop p (map (int.cast_ring_hom ℚ) Φ) n, simpa only [map_bind₁, ← eval₂_hom_map_hom, eval₂_hom_C_left, map_rename, map_witt_polynomial, alg_hom.coe_to_ring_hom, map_witt_structure_int], end lemma eq_witt_structure_int (Φ : mv_polynomial idx ℤ) (φ : ℕ → mv_polynomial (idx × ℕ) ℤ) (h : ∀ n, bind₁ φ (witt_polynomial p ℤ n) = bind₁ (λ i, rename (prod.mk i) (W_ ℤ n)) Φ) : φ = witt_structure_int p Φ := begin funext k, apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, rw map_witt_structure_int, refine congr_fun _ k, apply unique_of_exists_unique (witt_structure_rat_exists_unique p (map (int.cast_ring_hom ℚ) Φ)), { intro n, specialize h n, apply_fun map (int.cast_ring_hom ℚ) at h, simpa only [map_bind₁, ← eval₂_hom_map_hom, eval₂_hom_C_left, map_rename, map_witt_polynomial, alg_hom.coe_to_ring_hom] using h, }, { intro n, apply witt_structure_rat_prop } end theorem witt_structure_int_exists_unique (Φ : mv_polynomial idx ℤ) : ∃! (φ : ℕ → mv_polynomial (idx × ℕ) ℤ), ∀ (n : ℕ), bind₁ φ (witt_polynomial p ℤ n) = bind₁ (λ i : idx, (rename (prod.mk i) (W_ ℤ n))) Φ := ⟨witt_structure_int p Φ, witt_structure_int_prop _ _, eq_witt_structure_int _ _⟩ theorem witt_structure_prop (Φ : mv_polynomial idx ℤ) (n) : aeval (λ i, map (int.cast_ring_hom R) (witt_structure_int p Φ i)) (witt_polynomial p ℤ n) = aeval (λ i, rename (prod.mk i) (W n)) Φ := begin convert congr_arg (map (int.cast_ring_hom R)) (witt_structure_int_prop p Φ n) using 1; rw hom_bind₁; apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, { refl }, { simp only [map_rename, map_witt_polynomial] } end lemma witt_structure_int_rename {σ : Type*} (Φ : mv_polynomial idx ℤ) (f : idx → σ) (n : ℕ) : witt_structure_int p (rename f Φ) n = rename (prod.map f id) (witt_structure_int p Φ n) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_rename, map_witt_structure_int, witt_structure_rat, rename_bind₁, rename_rename, bind₁_rename], refl end @[simp] lemma constant_coeff_witt_structure_rat_zero (Φ : mv_polynomial idx ℚ) : constant_coeff (witt_structure_rat p Φ 0) = constant_coeff Φ := by simp only [witt_structure_rat, bind₁, map_aeval, X_in_terms_of_W_zero, constant_coeff_rename, constant_coeff_witt_polynomial, aeval_X, constant_coeff_comp_algebra_map, eval₂_hom_zero', ring_hom.id_apply] lemma constant_coeff_witt_structure_rat (Φ : mv_polynomial idx ℚ) (h : constant_coeff Φ = 0) (n : ℕ) : constant_coeff (witt_structure_rat p Φ n) = 0 := by simp only [witt_structure_rat, eval₂_hom_zero', h, bind₁, map_aeval, constant_coeff_rename, constant_coeff_witt_polynomial, constant_coeff_comp_algebra_map, ring_hom.id_apply, constant_coeff_X_in_terms_of_W] @[simp] lemma constant_coeff_witt_structure_int_zero (Φ : mv_polynomial idx ℤ) : constant_coeff (witt_structure_int p Φ 0) = constant_coeff Φ := begin have inj : function.injective (int.cast_ring_hom ℚ), { intros m n, exact int.cast_inj.mp, }, apply inj, rw [← constant_coeff_map, map_witt_structure_int, constant_coeff_witt_structure_rat_zero, constant_coeff_map], end lemma constant_coeff_witt_structure_int (Φ : mv_polynomial idx ℤ) (h : constant_coeff Φ = 0) (n : ℕ) : constant_coeff (witt_structure_int p Φ n) = 0 := begin have inj : function.injective (int.cast_ring_hom ℚ), { intros m n, exact int.cast_inj.mp, }, apply inj, rw [← constant_coeff_map, map_witt_structure_int, constant_coeff_witt_structure_rat, ring_hom.map_zero], rw [constant_coeff_map, h, ring_hom.map_zero], end variable (R) -- we could relax the fintype on `idx`, but then we need to cast from finset to set. -- for our applications `idx` is always finite. lemma witt_structure_rat_vars [fintype idx] (Φ : mv_polynomial idx ℚ) (n : ℕ) : (witt_structure_rat p Φ n).vars ⊆ finset.univ.product (finset.range (n + 1)) := begin rw witt_structure_rat, intros x hx, simp only [finset.mem_product, true_and, finset.mem_univ, finset.mem_range], obtain ⟨k, hk, hx'⟩ := mem_vars_bind₁ _ _ hx, obtain ⟨i, -, hx''⟩ := mem_vars_bind₁ _ _ hx', obtain ⟨j, hj, rfl⟩ := mem_vars_rename _ _ hx'', rw [witt_polynomial_vars, finset.mem_range] at hj, replace hk := X_in_terms_of_W_vars_subset p _ hk, rw finset.mem_range at hk, exact lt_of_lt_of_le hj hk, end -- we could relax the fintype on `idx`, but then we need to cast from finset to set. -- for our applications `idx` is always finite. lemma witt_structure_int_vars [fintype idx] (Φ : mv_polynomial idx ℤ) (n : ℕ) : (witt_structure_int p Φ n).vars ⊆ finset.univ.product (finset.range (n + 1)) := begin have : function.injective (int.cast_ring_hom ℚ) := int.cast_injective, rw [← vars_map_of_injective _ this, map_witt_structure_int], apply witt_structure_rat_vars, end end p_prime
8a27e8c410cea0ac144983dbf75d64408c11e608
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/1050.lean
25fc31940d42ce916b4234e5cf5ef05ce0ac9f85
[ "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
950
lean
universe u inductive Foo : Type u → Type u → Type (u+1) | foo {as bs cs: Type u}: Foo as bs → Foo bs cs → Foo as cs namespace Foo mutual def bar : {as bs: Type u} → Foo.{u} as bs → bs := bar_aux def bar_aux: {as bs: Type u} → Foo.{u} as bs → bs | as, bs, foo _ x => x.bar end termination_by bar => 0 bar_aux => 0 decreasing_by sorry end Foo namespace Foo mutual def bar1 : {as bs: Type u} → Foo.{u} as bs → bs | as, bs, x => bar_aux1 x def bar_aux1 : {as bs: Type u} → Foo.{u} as bs → bs | as, bs, foo _ x => x.bar1 end termination_by bar1 => 0 bar_aux1 => 0 decreasing_by sorry end Foo namespace Foo mutual def bar2 : Foo as bs → bs | x => bar_aux2 x def bar_aux2 : Foo as bs → bs | foo _ x => x.bar2 end termination_by bar2 x => (sizeOf x, 1) bar_aux2 x => (sizeOf x, 0) end Foo
5cd655dae821273b355b48fa17844be3f8ebca90
37a833c924892ee3ecb911484775a6d6ebb8984d
/src/category_theory/presheaves/locally_ringed.lean
0114976684dc38fb06cb9c57cfbce09bd4364b18
[]
no_license
silky/lean-category-theory
28126e80564a1f99e9c322d86b3f7d750da0afa1
0f029a2364975f56ac727d31d867a18c95c22fd8
refs/heads/master
1,589,555,811,646
1,554,673,665,000
1,554,673,665,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
979
lean
import category_theory.presheaves.sheaves import category_theory.presheaves.stalk import category_theory.examples.CommRing.products import category_theory.examples.rings import ring_theory.ideals universes v open category_theory.examples open category_theory.presheaves open category_theory.limits variables (X : Top.{v}) instance : has_colimits.{v+1 v} CommRing := sorry def structure_sheaf := sheaf.{v+1 v} X CommRing structure ringed_space := (𝒪 : structure_sheaf X) structure locally_ringed_space extends ringed_space X := (locality : ∀ x : X, is_local_ring (stalk_at.{v+1 v} 𝒪.presheaf x).1) -- coercion from sheaf to presheaf? def ringed_space.of_topological_space : ringed_space X := { 𝒪 := { presheaf := { obj := λ U, sorry /- ring of continuous functions U → ℂ -/, map := sorry, map_id' := sorry, map_comp' := sorry }, sheaf_condition := sorry, } }
a45d80b171f423ab7de4c3909bf99aa067955948
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/invertible.lean
3713bfd6d689179b8c4c3106f2750a913e330a81
[ "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
9,971
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.group.units import algebra.ring.basic /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a two-sided multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. For constructions of the invertible element given a characteristic, see `algebra/char_p/invertible` and other lemmas in that file. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. Since `invertible a` is not a `Prop` (but it is a `subsingleton`), we have to be careful about coherence issues: we should avoid having multiple non-defeq instances for `invertible a` in the same context. This file plays it safe and uses `def` rather than `instance` for most definitions, users can choose which instances to use at the point of use. For example, here's how you can use an `invertible 1` instance: ```lean variables {α : Type*} [monoid α] def something_that_needs_inverses (x : α) [invertible x] := sorry section local attribute [instance] invertible_one def something_one := something_that_needs_inverses 1 end ``` ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma inv_of_mul_self_assoc [monoid α] (a b : α) [invertible a] : ⅟a * (a * b) = b := by rw [←mul_assoc, inv_of_mul_self, one_mul] @[simp] lemma mul_inv_of_self_assoc [monoid α] (a b : α) [invertible a] : a * (⅟a * b) = b := by rw [←mul_assoc, mul_inv_of_self, one_mul] @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma inv_of_eq_left_inv [monoid α] {a b : α} [invertible a] (hac : b * a = 1) : ⅟a = b := (left_inv_eq_right_inv hac (mul_inv_of_self _)).symm lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- If `r` is invertible and `s = r`, then `s` is invertible. -/ def invertible.copy [monoid α] {r : α} (hr : invertible r) (s : α) (hs : s = r) : invertible s := { inv_of := ⅟r, inv_of_mul_self := by rw [hs, inv_of_mul_self], mul_inv_of_self := by rw [hs, mul_inv_of_self] } /-- An `invertible` element is a unit. -/ @[simps] def unit_of_invertible [monoid α] (a : α) [invertible a] : αˣ := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Units are invertible in their associated monoid. -/ def units.invertible [monoid α] (u : αˣ) : invertible (u : α) := { inv_of := ↑(u⁻¹), inv_of_mul_self := u.inv_mul, mul_inv_of_self := u.mul_inv } @[simp] lemma inv_of_units [monoid α] (u : αˣ) [invertible (u : α)] : ⅟(u : α) = ↑(u⁻¹) := inv_of_eq_right_inv u.mul_inv lemma is_unit.nonempty_invertible [monoid α] {a : α} (h : is_unit a) : nonempty (invertible a) := let ⟨x, hx⟩ := h in ⟨x.invertible.copy _ hx.symm⟩ /-- Convert `is_unit` to `invertible` using `classical.choice`. Prefer `casesI h.nonempty_invertible` over `letI := h.invertible` if you want to avoid choice. -/ noncomputable def is_unit.invertible [monoid α] {a : α} (h : is_unit a) : invertible a := classical.choice h.nonempty_invertible @[simp] lemma nonempty_invertible_iff_is_unit [monoid α] (a : α) : nonempty (invertible a) ↔ is_unit a := ⟨nonempty.rec $ @is_unit_of_invertible _ _ _, is_unit.nonempty_invertible⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨1, mul_one _, one_mul _⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) @[simp] lemma one_sub_inv_of_two [ring α] [invertible (2:α)] : 1 - (⅟2:α) = ⅟2 := (is_unit_of_invertible (2:α)).mul_right_inj.1 $ by rw [mul_sub, mul_inv_of_self, mul_one, bit0, add_sub_cancel] /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) theorem commute.inv_of_right [monoid α] {a b : α} [invertible b] (h : commute a b) : commute a (⅟b) := calc a * (⅟b) = (⅟b) * (b * a * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (a * b * ((⅟b))) : by rw h.eq ... = (⅟b) * a : by simp [mul_assoc] theorem commute.inv_of_left [monoid α] {a b : α} [invertible b] (h : commute b a) : commute (⅟b) a := calc (⅟b) * a = (⅟b) * (a * b * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (b * a * (⅟b)) : by rw h.eq ... = a * (⅟b) : by simp [mul_assoc] lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm section monoid_with_zero variable [monoid_with_zero α] /-- A variant of `ring.inverse_unit`. -/ @[simp] lemma ring.inverse_invertible (x : α) [invertible x] : ring.inverse x = ⅟x := ring.inverse_unit (unit_of_invertible _) lemma nonzero_of_invertible (a : α) [nontrivial α] [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a end monoid_with_zero section group_with_zero variable [group_with_zero α] /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] }
3784e0689c97519ac98b486259beb3fce04c920d
704c0b4221e71a413a9270e3160af0813f426c91
/src/refinement/tableau_class.lean
1777d90925c871ec8de41c8a34974bdcc617f5ad
[]
no_license
CohenCyril/LP
d39b766199d2176b592ff223f9c00cdcb4d5f76d
4dca829aa0fe94c0627410b88d9dad54e6ed2c63
refs/heads/master
1,596,132,000,357
1,568,817,913,000
1,568,817,913,000
210,310,499
0
0
null
1,569,229,479,000
1,569,229,479,000
null
UTF-8
Lean
false
false
1,127
lean
import simplex class is_tableau (X : ℕ → ℕ → Type) : Type := (to_tableau {m n : ℕ} : X m n → tableau m n) (pivot {m n : ℕ} : X m n → fin m → fin n → X m n) (pivot_col {m n : ℕ} (T : X m n) (obj : fin m) : option (fin n)) (pivot_row {m n : ℕ} (T : X m n) (obj : fin m) : fin n → option (fin m)) (to_tableau_pivot {m n : ℕ} (T : X m n) (i : fin m) (j : fin n) : to_tableau (pivot T i j) = (to_tableau T).pivot i j) (to_tableau_pivot_col {m n : ℕ} (T : X m n) : pivot_col T = (to_tableau T).pivot_col) (to_tableau_pivot_row {m n : ℕ} (T : X m n) : pivot_row T = (to_tableau T).pivot_row) namespace is_tableau section parameters {m n : ℕ} {X : ℕ → ℕ → Type} variable [is_tableau X] def to_matrix (T : X m n) : matrix (fin m) (fin n) ℚ := (to_tableau T).to_matrix def const (T : X m n) : matrix (fin m) (fin 1) ℚ := (to_tableau T).const def to_partition (T : X m n) : partition m n := (to_tableau T).to_partition def restricted (T : X m n) : finset (fin (m + n)) := (to_tableau T).restricted def dead (T : X m n) : finset (fin n) := (to_tableau T).dead end end is_tableau
351caa05428b4d8e29b0d090f5d827340a1dd38d
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/option/basic.lean
a64fd190b0bc19b6ab5c14879097f0307fbf3cd5
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
18,799
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 logic.is_empty import tactic.basic import logic.relator /-! # Option of a type This file develops the basic theory of option types. If `α` is a type, then `option α` can be understood as the type with one more element than `α`. `option α` has terms `some a`, where `a : α`, and `none`, which is the added element. This is useful in multiple ways: * It is the prototype of addition of terms to a type. See for example `with_bot α` which uses `none` as an element smaller than all others. * It can be used to define failsafe partial functions, which return `some the_result_we_expect` if we can find `the_result_we_expect`, and `none` if there is no meaningful result. This forces any subsequent use of the partial function to explicitly deal with the exceptions that make it return `none`. * `option` is a monad. We love monads. `part` is an alternative to `option` that can be seen as the type of `true`/`false` values along with a term `a : α` if the value is `true`. ## Implementation notes `option` is currently defined in core Lean, but this will change in Lean 4. -/ namespace option variables {α : Type*} {β : Type*} {γ : Type*} lemma coe_def : (coe : α → option α) = some := rfl lemma some_ne_none (x : α) : some x ≠ none := λ h, option.no_confusion h protected lemma «forall» {p : option α → Prop} : (∀ x, p x) ↔ p none ∧ ∀ x, p (some x) := ⟨λ h, ⟨h _, λ x, h _⟩, λ h x, option.cases_on x h.1 h.2⟩ protected lemma «exists» {p : option α → Prop} : (∃ x, p x) ↔ p none ∨ ∃ x, p (some x) := ⟨λ ⟨x, hx⟩, (option.cases_on x or.inl $ λ x hx, or.inr ⟨x, hx⟩) hx, λ h, h.elim (λ h, ⟨_, h⟩) (λ ⟨x, hx⟩, ⟨_, hx⟩)⟩ @[simp] theorem get_mem : ∀ {o : option α} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : α} : ∀ {o : option α} (h : is_some o), a ∈ o → option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : α) : a ∉ (none : option α) := λ h, option.no_confusion h @[simp] lemma some_get : ∀ {x : option α} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : α) (h : is_some (some x)) : option.get h = x := rfl @[simp] lemma get_or_else_some (x y : α) : option.get_or_else (some x) y = x := rfl @[simp] lemma get_or_else_none (x : α) : option.get_or_else none x = x := rfl @[simp] lemma get_or_else_coe (x y : α) : option.get_or_else ↑x y = x := rfl lemma get_or_else_of_ne_none {x : option α} (hx : x ≠ none) (y : α) : some (x.get_or_else y) = x := by cases x; [contradiction, rw get_or_else_some] @[simp] lemma coe_get {o : option α} (h : o.is_some) : ((option.get h : α) : option α) = o := option.some_get h theorem mem_unique {o : option α} {a b : α} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem eq_of_mem_of_mem {a : α} {o1 o2 : option α} (h1 : a ∈ o1) (h2 : a ∈ o2) : o1 = o2 := h1.trans h2.symm theorem mem.left_unique : relator.left_unique ((∈) : α → option α → Prop) := λ a o b, mem_unique theorem some_injective (α : Type*) : function.injective (@some α) := λ _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem map_injective {f : α → β} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some a₂) H := by rw Hf (option.some.inj H) @[ext] theorem ext : ∀ {o₁ o₂ : option α}, (∀ a, a ∈ o₁ ↔ a ∈ o₂) → o₁ = o₂ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option α} : o = none ↔ (∀ a, a ∉ o) := ⟨λ e a h, by rw e at h; cases h, λ h, ext $ by simpa⟩ @[simp] theorem none_bind {α β} (f : α → option β) : none >>= f = none := rfl @[simp] theorem some_bind {α β} (a : α) (f : α → option β) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : α → option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → option β) : (some a).bind f = f a := rfl @[simp] theorem bind_some : ∀ x : option α, x >>= some = x := @bind_pure α option _ _ @[simp] theorem bind_eq_some {α β} {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option α} {f : α → option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_none' {o : option α} {f : α → option β} : o.bind f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := by simp only [eq_none_iff_forall_not_mem, not_exists, not_and, mem_def, bind_eq_some'] @[simp] theorem bind_eq_none {α β} {o : option α} {f : α → option β} : o >>= f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := bind_eq_none' lemma bind_comm {α β γ} {f : α → β → option γ} (a : option α) (b : option β) : a.bind (λx, b.bind (f x)) = b.bind (λy, a.bind (λx, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option α) (f : α → option β) (g : β → option γ) : (x.bind f).bind g = x.bind (λ y, (f y).bind g) := by cases x; refl lemma join_eq_some {x : option (option α)} {a : α} : x.join = some a ↔ x = some (some a) := by simp lemma join_ne_none {x : option (option α)} : x.join ≠ none ↔ ∃ z, x = some (some z) := by simp lemma join_ne_none' {x : option (option α)} : ¬(x.join = none) ↔ ∃ z, x = some (some z) := by simp lemma join_eq_none {o : option (option α)} : o.join = none ↔ o = none ∨ o = some none := by rcases o with _|_|_; simp lemma bind_id_eq_join {x : option (option α)} : x >>= id = x.join := by simp lemma join_eq_join : mjoin = @join α := funext (λ x, by rw [mjoin, bind_id_eq_join]) lemma bind_eq_bind {α β : Type*} {f : α → option β} {x : option α} : x >>= f = x.bind f := rfl @[simp] lemma map_eq_map {α β} {f : α → β} : (<$>) f = option.map f := rfl theorem map_none {α β} {f : α → β} : f <$> none = none := rfl theorem map_some {α β} {a : α} {f : α → β} : f <$> some a = some (f a) := rfl theorem map_coe {α β} {a : α} {f : α → β} : f <$> (a : option α) = ↑(f a) := rfl @[simp] theorem map_none' {f : α → β} : option.map f none = none := rfl @[simp] theorem map_some' {a : α} {f : α → β} : option.map f (some a) = some (f a) := rfl @[simp] theorem map_coe' {a : α} {f : α → β} : option.map f (a : option α) = ↑(f a) := rfl theorem map_eq_some {α β} {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option α} {f : α → β} {b : β} : x.map f = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp lemma map_eq_none {α β} {x : option α} {f : α → β} : f <$> x = none ↔ x = none := by { cases x; simp only [map_none, map_some, eq_self_iff_true] } @[simp] lemma map_eq_none' {x : option α} {f : α → β} : x.map f = none ↔ x = none := by { cases x; simp only [map_none', map_some', eq_self_iff_true] } lemma map_congr {f g : α → β} {x : option α} (h : ∀ a ∈ x, f a = g a) : option.map f x = option.map g x := by { cases x; simp only [map_none', map_some', h, mem_def] } @[simp] theorem map_id' : option.map (@id α) = id := map_id @[simp] lemma map_map (h : β → γ) (g : α → β) (x : option α) : option.map h (option.map g x) = option.map (h ∘ g) x := by { cases x; simp only [map_none', map_some'] } lemma comp_map (h : β → γ) (g : α → β) (x : option α) : option.map (h ∘ g) x = option.map h (option.map g x) := (map_map _ _ _).symm @[simp] lemma map_comp_map (f : α → β) (g : β → γ) : option.map g ∘ option.map f = option.map (g ∘ f) := by { ext x, rw comp_map } lemma mem_map_of_mem {α β : Type*} {a : α} {x : option α} (g : α → β) (h : a ∈ x) : g a ∈ x.map g := mem_def.mpr ((mem_def.mp h).symm ▸ map_some') lemma bind_map_comm {α β} {x : option (option α) } {f : α → β} : x >>= option.map f = x.map (option.map f) >>= id := by { cases x; simp } lemma join_map_eq_map_join {f : α → β} {x : option (option α)} : (x.map (option.map f)).join = x.join.map f := by { rcases x with _ | _ | x; simp } lemma join_join {x : option (option (option α))} : x.join.join = (x.map join).join := by { rcases x with _ | _ | _ | x; simp } lemma mem_of_mem_join {a : α} {x : option (option α)} (h : a ∈ x.join) : some a ∈ x := mem_def.mpr ((mem_def.mp h).symm ▸ join_eq_some.mp h) section pmap variables {p : α → Prop} (f : Π (a : α), p a → β) (x : option α) @[simp] lemma pbind_eq_bind (f : α → option β) (x : option α) : x.pbind (λ a _, f a) = x.bind f := by { cases x; simp only [pbind, none_bind', some_bind'] } lemma map_bind {α β γ} (f : β → γ) (x : option α) (g : α → option β) : option.map f (x >>= g) = (x >>= λ a, option.map f (g a)) := by simp_rw [←map_eq_map, ←bind_pure_comp_eq_map,is_lawful_monad.bind_assoc] lemma map_bind' (f : β → γ) (x : option α) (g : α → option β) : option.map f (x.bind g) = x.bind (λ a, option.map f (g a)) := by { cases x; simp } lemma map_pbind (f : β → γ) (x : option α) (g : Π a, a ∈ x → option β) : option.map f (x.pbind g) = (x.pbind (λ a H, option.map f (g a H))) := by { cases x; simp only [pbind, map_none'] } lemma pbind_map (f : α → β) (x : option α) (g : Π (b : β), b ∈ x.map f → option γ) : pbind (option.map f x) g = x.pbind (λ a h, g (f a) (mem_map_of_mem _ h)) := by { cases x; refl } @[simp] lemma pmap_none (f : Π (a : α), p a → β) {H} : pmap f (@none α) H = none := rfl @[simp] lemma pmap_some (f : Π (a : α), p a → β) {x : α} (h : p x) : pmap f (some x) = λ _, some (f x h) := rfl lemma mem_pmem {a : α} (h : ∀ a ∈ x, p a) (ha : a ∈ x) : f a (h a ha) ∈ pmap f x h := by { rw mem_def at ha ⊢, subst ha, refl } lemma pmap_map (g : γ → α) (x : option γ) (H) : pmap f (x.map g) H = pmap (λ a h, f (g a) h) x (λ a h, H _ (mem_map_of_mem _ h)) := by { cases x; simp only [map_none', map_some', pmap] } lemma map_pmap (g : β → γ) (f : Π a, p a → β) (x H) : option.map g (pmap f x H) = pmap (λ a h, g (f a h)) x H := by { cases x; simp only [map_none', map_some', pmap] } @[simp] lemma pmap_eq_map (p : α → Prop) (f : α → β) (x H) : @pmap _ _ p (λ a _, f a) x H = option.map f x := by { cases x; simp only [map_none', map_some', pmap] } lemma pmap_bind {α β γ} {x : option α} {g : α → option β} {p : β → Prop} {f : Π b, p b → γ} (H) (H' : ∀ (a : α) b ∈ g a, b ∈ x >>= g) : pmap f (x >>= g) H = (x >>= λa, pmap f (g a) (λ b h, H _ (H' a _ h))) := by { cases x; simp only [pmap, none_bind, some_bind] } lemma bind_pmap {α β γ} {p : α → Prop} (f : Π a, p a → β) (x : option α) (g : β → option γ) (H) : (pmap f x H) >>= g = x.pbind (λ a h, g (f a (H _ h))) := by { cases x; simp only [pmap, none_bind, some_bind, pbind] } variables {f x} lemma pbind_eq_none {f : Π (a : α), a ∈ x → option β} (h' : ∀ a ∈ x, f a H = none → x = none) : x.pbind f = none ↔ x = none := begin cases x, { simp }, { simp only [pbind, iff_false], intro h, cases h' x rfl h } end lemma pbind_eq_some {f : Π (a : α), a ∈ x → option β} {y : β} : x.pbind f = some y ↔ ∃ (z ∈ x), f z H = some y := begin cases x, { simp }, { simp only [pbind], split, { intro h, use x, simpa only [mem_def, exists_prop_of_true] using h }, { rintro ⟨z, H, hz⟩, simp only [mem_def] at H, simpa only [H] using hz } } end @[simp] lemma pmap_eq_none_iff {h} : pmap f x h = none ↔ x = none := by { cases x; simp } @[simp] lemma pmap_eq_some_iff {hf} {y : β} : pmap f x hf = some y ↔ ∃ (a : α) (H : x = some a), f a (hf a H) = y := begin cases x, { simp only [not_mem_none, exists_false, pmap, not_false_iff, exists_prop_of_false] }, { split, { intro h, simp only [pmap] at h, exact ⟨x, rfl, h⟩ }, { rintro ⟨a, H, rfl⟩, simp only [mem_def] at H, simp only [H, pmap] } } end @[simp] lemma join_pmap_eq_pmap_join {f : Π a, p a → β} {x : option (option α)} (H) : (pmap (pmap f) x H).join = pmap f x.join (λ a h, H (some a) (mem_of_mem_join h) _ rfl) := by { rcases x with _ | _ | x; simp } end pmap @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : α) (x : option α) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : α) (x : option α) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option α) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option α) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option α) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option α) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some α none = ff := rfl @[simp] theorem is_some_some {a : α} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none α none = tt := rfl @[simp] theorem is_none_some {a : α} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option α} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma eq_some_iff_get_eq {o : option α} {a : α} : o = some a ↔ ∃ h : o.is_some, option.get h = a := by cases o; simp lemma not_is_some_iff_eq_none {o : option α} : ¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option α} : o ≠ none ↔ o.is_some := by cases o; simp lemma ne_none_iff_exists {o : option α} : o ≠ none ↔ ∃ (x : α), some x = o := by {cases o; simp} lemma ne_none_iff_exists' {o : option α} : o ≠ none ↔ ∃ (x : α), o = some x := ne_none_iff_exists.trans $ exists_congr $ λ _, eq_comm lemma bex_ne_none {p : option α → Prop} : (∃ x ≠ none, p x) ↔ ∃ x, p (some x) := ⟨λ ⟨x, hx, hp⟩, ⟨get $ ne_none_iff_is_some.1 hx, by rwa [some_get]⟩, λ ⟨x, hx⟩, ⟨some x, some_ne_none x, hx⟩⟩ lemma ball_ne_none {p : option α → Prop} : (∀ x ≠ none, p x) ↔ ∀ x, p (some x) := ⟨λ h x, h (some x) (some_ne_none x), λ h x hx, by simpa only [some_get] using h (get $ ne_none_iff_is_some.1 hx)⟩ theorem iget_mem [inhabited α] : ∀ {o : option α}, is_some o → o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited α] {a : α} : ∀ {o : option α}, a ∈ o → o.iget = a | _ rfl := rfl lemma get_or_else_default_eq_iget [inhabited α] (o : option α) : o.get_or_else default = o.iget := by cases o; refl @[simp] theorem guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] (u) : _root_.guard p = some u ↔ p := begin cases u, by_cases p; simp [_root_.guard, h]; refl <|> contradiction, end theorem lift_or_get_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, lift_or_get f o₁ o₂ = o₁ ∨ lift_or_get f o₁ o₂ = o₂ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b @[simp] lemma lift_or_get_none_left {f} {b : option α} : lift_or_get f none b = b := by cases b; refl @[simp] lemma lift_or_get_none_right {f} {a : option α} : lift_or_get f a none = a := by cases a; refl @[simp] lemma lift_or_get_some_some {f} {a b : α} : lift_or_get f (some a) (some b) = f a b := rfl /-- Given an element of `a : option α`, a default element `b : β` and a function `α → β`, apply this function to `a` if it comes from `α`, and return `b` otherwise. -/ def cases_on' : option α → β → (α → β) → β | none n s := n | (some a) n s := s a @[simp] lemma cases_on'_none (x : β) (f : α → β) : cases_on' none x f = x := rfl @[simp] lemma cases_on'_some (x : β) (f : α → β) (a : α) : cases_on' (some a) x f = f a := rfl @[simp] lemma cases_on'_coe (x : β) (f : α → β) (a : α) : cases_on' (a : option α) x f = f a := rfl @[simp] lemma cases_on'_none_coe (f : option α → β) (o : option α) : cases_on' o (f none) (f ∘ coe) = f o := by cases o; refl @[simp] lemma get_or_else_map (f : α → β) (x : α) (o : option α) : get_or_else (o.map f) (f x) = f (get_or_else o x) := by cases o; refl lemma orelse_eq_some (o o' : option α) (x : α) : (o <|> o') = some x ↔ o = some x ∨ (o = none ∧ o' = some x) := begin cases o, { simp only [true_and, false_or, eq_self_iff_true, none_orelse] }, { simp only [some_orelse, or_false, false_and] } end lemma orelse_eq_some' (o o' : option α) (x : α) : o.orelse o' = some x ↔ o = some x ∨ (o = none ∧ o' = some x) := option.orelse_eq_some o o' x @[simp] lemma orelse_eq_none (o o' : option α) : (o <|> o') = none ↔ (o = none ∧ o' = none) := begin cases o, { simp only [true_and, none_orelse, eq_self_iff_true] }, { simp only [some_orelse, false_and], } end @[simp] lemma orelse_eq_none' (o o' : option α) : o.orelse o' = none ↔ (o = none ∧ o' = none) := option.orelse_eq_none o o' section open_locale classical /-- An arbitrary `some a` with `a : α` if `α` is nonempty, and otherwise `none`. -/ noncomputable def choice (α : Type*) : option α := if h : nonempty α then some h.some else none lemma choice_eq {α : Type*} [subsingleton α] (a : α) : choice α = some a := begin dsimp [choice], rw dif_pos (⟨a⟩ : nonempty α), congr, end lemma choice_eq_none (α : Type*) [is_empty α] : choice α = none := dif_neg (not_nonempty_iff_imp_false.mpr is_empty_elim) lemma choice_is_some_iff_nonempty {α : Type*} : (choice α).is_some ↔ nonempty α := begin fsplit, { intro h, exact ⟨option.get h⟩, }, { intro h, dsimp only [choice], rw dif_pos h, exact is_some_some }, end end @[simp] lemma to_list_some (a : α) : (a : option α).to_list = [a] := rfl @[simp] lemma to_list_none (α : Type*) : (none : option α).to_list = [] := rfl @[simp] lemma elim_none_some (f : option α → β) : option.elim (f none) (f ∘ some) = f := funext $ λ o, by cases o; refl end option
31af991f577aad2fd7214e2e08263cd8a880c270
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/polynomial/coeff.lean
2f0628977a1336a197d0de8ad87adf80d1650a00
[ "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
5,861
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.monomial import data.finset.nat_antidiagonal /-! # Theory of univariate polynomials The theorems include formulas for computing coefficients, such as `coeff_add`, `coeff_sum`, `coeff_mul` -/ noncomputable theory open finsupp finset add_monoid_algebra open_locale big_operators namespace polynomial universes u v variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ} variables [semiring R] {p q r : polynomial R} section coeff lemma coeff_one (n : ℕ) : coeff (1 : polynomial R) n = if 0 = n then 1 else 0 := coeff_monomial @[simp] lemma coeff_add (p q : polynomial R) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := rfl lemma coeff_sum [semiring S] (n : ℕ) (f : ℕ → R → polynomial S) : coeff (p.sum f) n = p.sum (λ a b, coeff (f a b) n) := finsupp.sum_apply @[simp] lemma coeff_smul (p : polynomial R) (r : R) (n : ℕ) : coeff (r • p) n = r * coeff p n := finsupp.smul_apply lemma mem_support_iff_coeff_ne_zero : n ∈ p.support ↔ p.coeff n ≠ 0 := by { rw mem_support_to_fun, refl } lemma not_mem_support_iff_coeff_zero : n ∉ p.support ↔ p.coeff n = 0 := by { rw [mem_support_to_fun, not_not], refl, } variable (R) /-- The nth coefficient, as a linear map. -/ def lcoeff (n : ℕ) : polynomial R →ₗ[R] R := { to_fun := λ f, coeff f n, map_add' := λ f g, coeff_add f g n, map_smul' := λ r p, coeff_smul p r n } variable {R} @[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial R) : lcoeff R n f = coeff f n := rfl @[simp] lemma finset_sum_coeff {ι : Type*} (s : finset ι) (f : ι → polynomial R) (n : ℕ) : coeff (∑ b in s, f b) n = ∑ b in s, coeff (f b) n := (s.sum_hom (λ q : polynomial R, lcoeff R n q)).symm lemma coeff_mul (p q : polynomial R) (n : ℕ) : coeff (p * q) n = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 := have hite : ∀ a : ℕ × ℕ, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 0 ≠ 0 → a.1 + a.2 = n, from λ a ha, by_contradiction (λ h, absurd (eq.refl (0 : R)) (by rwa if_neg h at ha)), calc coeff (p * q) n = ∑ a in p.support, ∑ b in q.support, ite (a + b = n) (coeff p a * coeff q b) 0 : by { simp only [add_monoid_algebra.mul_def, coeff_sum, coeff_single], refl } ... = ∑ v in p.support.product q.support, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0 : by rw sum_product ... = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 : begin refine sum_bij_ne_zero (λ x _ _, x) (λ x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (λ _ _ _ _ _ _ h, h) (λ x h₁ h₂, ⟨x, _, _, rfl⟩) _, { rw [mem_product, mem_support_iff, mem_support_iff], exact ne_zero_and_ne_zero_of_mul h₂ }, { rw nat.mem_antidiagonal at h₁, rwa [if_pos h₁] }, { intros x h hx, rw [if_pos (hite x hx)] } end @[simp] lemma mul_coeff_zero (p q : polynomial R) : coeff (p * q) 0 = coeff p 0 * coeff q 0 := by simp [coeff_mul] lemma coeff_mul_X_zero (p : polynomial R) : coeff (p * X) 0 = 0 := by simp lemma coeff_X_mul_zero (p : polynomial R) : coeff (X * p) 0 = 0 := by simp lemma coeff_C_mul_X (x : R) (k n : ℕ) : coeff (C x * X^k : polynomial R) n = if n = k then x else 0 := by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr @[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n := begin conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] }, rw [add_monoid_algebra.mul_def, ←monomial_zero_left, monomial, sum_single_index], { simp only [coeff_single, finsupp.mul_sum, coeff_sum], apply sum_congr rfl, assume i hi, by_cases i = n; simp [h] }, { simp [finsupp.sum] } end lemma C_mul' (a : R) (f : polynomial R) : C a * f = a • f := ext $ λ n, coeff_C_mul f @[simp] lemma coeff_mul_C (p : polynomial R) (n : ℕ) (a : R) : coeff (p * C a) n = coeff p n * a := begin conv_rhs { rw [← @finsupp.sum_single _ _ _ p, coeff_sum] }, rw [add_monoid_algebra.mul_def, ←monomial_zero_left], simp_rw [sum_single_index], { simp only [coeff_single, finsupp.sum_mul, coeff_sum], apply sum_congr rfl, assume i hi, by_cases i = n; simp [h], }, end lemma monomial_one_eq_X_pow : ∀{n}, monomial n (1 : R) = X^n | 0 := rfl | (n+1) := calc monomial (n + 1) (1 : R) = monomial n 1 * X : by rw [X, monomial_mul_monomial, mul_one] ... = X^n * X : by rw [monomial_one_eq_X_pow] ... = X^(n+1) : by simp only [pow_add, pow_one] lemma monomial_eq_smul_X {n} : monomial n (a : R) = a • X^n := begin calc monomial n a = monomial n (a * 1) : by simp ... = a • monomial n 1 : (smul_single' _ _ _).symm ... = a • X^n : by rw monomial_one_eq_X_pow end lemma coeff_X_pow (k n : ℕ) : coeff (X^k : polynomial R) n = if n = k then 1 else 0 := by rw [← monomial_one_eq_X_pow]; simp [monomial, single, eq_comm, coeff]; congr @[simp] lemma coeff_X_pow_self (n : ℕ) : coeff (X^n : polynomial R) n = 1 := by simp [coeff_X_pow] theorem coeff_mul_X_pow (p : polynomial R) (n d : ℕ) : coeff (p * polynomial.X ^ n) (d + n) = coeff p d := begin rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2, rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 }, { exact λ h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim } end @[simp] theorem coeff_mul_X (p : polynomial R) (n : ℕ) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n theorem mul_X_pow_eq_zero {p : polynomial R} {n : ℕ} (H : p * X ^ n = 0) : p = 0 := ext $ λ k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n) end coeff end polynomial
fda83c952bbc5ab48fd00d147cc1feb1fa38c489
6b45072eb2b3db3ecaace2a7a0241ce81f815787
/data/seq/parallel.lean
69b2413719294fd8b6b24dad6062b7c7d2bfef89
[]
no_license
avigad/library_dev
27b47257382667b5eb7e6476c4f5b0d685dd3ddc
9d8ac7c7798ca550874e90fed585caad030bbfac
refs/heads/master
1,610,452,468,791
1,500,712,839,000
1,500,713,478,000
69,311,142
1
0
null
1,474,942,903,000
1,474,942,902,000
null
UTF-8
Lean
false
false
12,242
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Parallel computation of a computable sequence of computations by a diagonal enumeration. The important theorems of this operation are proven as terminates_parallel and exists_of_mem_parallel. (This operation is nondeterministic in the sense that it does not honor sequence equivalence (irrelevance of computation time).) -/ import data.seq.wseq universes u v namespace computation open wseq variables {α : Type u} {β : Type v} def parallel.aux2 : list (computation α) → α ⊕ list (computation α) := list.foldr (λc o, match o with | sum.inl a := sum.inl a | sum.inr ls := rmap (λ c', c' :: ls) (destruct c) end) (sum.inr []) def parallel.aux1 : list (computation α) × wseq (computation α) → α ⊕ list (computation α) × wseq (computation α) | (l, S) := rmap (λ l', match seq.destruct S with | none := (l', nil) | some (none, S') := (l', S') | some (some c, S') := (c::l', S') end) (parallel.aux2 l) -- parallel computation of an infinite stream of computations, -- taking the first result def parallel (S : wseq (computation α)) : computation α := corec parallel.aux1 ([], S) lemma terminates_parallel.aux : ∀ {l : list (computation α)} {S c}, c ∈ l → terminates c → terminates (corec parallel.aux1 (l, S)) := begin have lem1 : ∀ l S, (∃ (a : α), parallel.aux2 l = sum.inl a) → terminates (corec parallel.aux1 (l, S)), { intros l S e, cases e with a e, have this : corec parallel.aux1 (l, S) = return a, { apply destruct_eq_ret, simp [parallel.aux1], rw e, simp [rmap] }, rw this, apply_instance }, intros l S c m T, revert l S, apply @terminates_rec_on _ _ c T _ _, { intros a l S m, apply lem1, revert m, induction l with c l IH; intro; simp at m, { contradiction }, cases m with e m, { rw ←e, simp [parallel.aux2], cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a' ls, exacts [⟨a', rfl⟩, ⟨a, rfl⟩] }, { cases IH m with a' e, simp [parallel.aux2], simp [parallel.aux2] at e, rw e, exact ⟨a', rfl⟩ } }, { intros s IH l S m, have H1 : ∀ l', parallel.aux2 l = sum.inr l' → s ∈ l', { revert m, induction l with c l IH'; intros m l' e'; simp at m, { contradiction }, cases m with e m; simp [parallel.aux2] at e', { rw ←e at e', cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a' ls; injection e' with e', rw ←e', simp }, { ginduction list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with e a' ls; rw e at e', { contradiction }, have := IH' m _ e, simp [parallel.aux2] at e', cases destruct c; injection e', rw ←h, simp [this] } }, ginduction parallel.aux2 l with h a l', { exact lem1 _ _ ⟨a, h⟩ }, { have H2 : corec parallel.aux1 (l, S) = think _, { apply destruct_eq_think, simp [parallel.aux1], rw h, simp [rmap] }, rw H2, apply @computation.think_terminates _ _ _, have := H1 _ h, cases seq.destruct S, { simp [parallel.aux1], apply IH, exact this }, { cases a with c S', cases c with c; simp [parallel.aux1]; apply IH; simp [this] } } } end theorem terminates_parallel {S : wseq (computation α)} {c} (h : c ∈ S) [T : terminates c] : terminates (parallel S) := suffices ∀ n (l : list (computation α)) S c, c ∈ l ∨ some (some c) = seq.nth S n → terminates c → terminates (corec parallel.aux1 (l, S)), from let ⟨n, h⟩ := h in this n [] S c (or.inr h) T, begin intro n, induction n with n IH; intros l S c o T, { cases o, { exact terminates_parallel.aux a T }, have H : seq.destruct S = some (some c, _), { unfold seq.destruct has_map.map, rw ←a, simp [option_map, option_bind] }, ginduction (parallel.aux2 l) with h a l'; have C : corec parallel.aux1 (l, S) = _, { apply destruct_eq_ret, simp [parallel.aux1], rw [h], simp [rmap] }, { rw C, apply_instance }, { apply destruct_eq_think, simp [parallel.aux1], rw [h, H], simp [rmap] }, { rw C, apply @computation.think_terminates _ _ _, apply terminates_parallel.aux _ T, simp } }, { cases o, { exact terminates_parallel.aux a T }, ginduction (parallel.aux2 l) with h a l'; have C : corec parallel.aux1 (l, S) = _, { apply destruct_eq_ret, simp [parallel.aux1], rw [h], simp [rmap] }, { rw C, apply_instance }, { apply destruct_eq_think, simp [parallel.aux1], rw [h], simp [rmap] }, { rw C, apply @computation.think_terminates _ _ _, have TT : ∀ l', terminates (corec parallel.aux1 (l', S.tail)), { intro, apply IH _ _ _ (or.inr _) T, rw a, cases S with f al, refl }, ginduction seq.nth S 0 with e o, { have D : seq.destruct S = none, { dsimp [seq.destruct], rw e, refl }, rw D, simp [parallel.aux1], have TT := TT l', rwa [seq.destruct_eq_nil D, seq.tail_nil] at TT }, { have D : seq.destruct S = some (o, S.tail), { dsimp [seq.destruct], rw e, refl }, rw D, cases o with c; simp [parallel.aux1, TT] } } } end theorem exists_of_mem_parallel {S : wseq (computation α)} {a} (h : a ∈ parallel S) : ∃ c ∈ S, a ∈ c := suffices ∀ C, a ∈ C → ∀ (l : list (computation α)) S, corec parallel.aux1 (l, S) = C → ∃ c, (c ∈ l ∨ c ∈ S) ∧ a ∈ c, from let ⟨c, h1, h2⟩ := this _ h [] S rfl in ⟨c, h1.resolve_left id, h2⟩, begin let F : list (computation α) → α ⊕ list (computation α) → Prop, { intros l a, cases a with a l', exact ∃ c ∈ l, a ∈ c, exact ∀ a', (∃ c ∈ l', a' ∈ c) → (∃ c ∈ l, a' ∈ c) }, have lem1 : ∀ (l : list (computation α)), F l (parallel.aux2 l), { intro l, induction l with c l IH; simp [parallel.aux2], { intros a h, cases h with c h, cases h with hn, exact false.elim hn }, { simp [parallel.aux2] at IH, cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a ls; simp [parallel.aux2], { cases IH with c' cl, cases cl with cl ac, refine ⟨c', or.inr cl, ac⟩ }, { ginduction destruct c with h a c'; simp [rmap], { refine ⟨c, list.mem_cons_self _ _, _⟩, rw destruct_eq_ret h, apply ret_mem }, { intros a' h, cases h with d h, cases h with dm ad, simp at dm, cases dm with e dl, { rw e at ad, refine ⟨c, list.mem_cons_self _ _, _⟩, rw destruct_eq_think h, exact think_mem ad }, { cases IH a' ⟨d, dl, ad⟩ with d dm, cases dm with dm ad, exact ⟨d, or.inr dm, ad⟩ } } } } }, intros C aC, refine mem_rec_on aC _ (λ C' IH, _); intros l S e; have e' := congr_arg destruct e; have := lem1 l; simp [parallel.aux1] at e'; cases parallel.aux2 l with a' l'; injection e', { rw h at this, cases this with c cl, cases cl with cl ac, exact ⟨c, or.inl cl, ac⟩ }, { ginduction seq.destruct S with e a; rw e at h, { exact let ⟨d, o, ad⟩ := IH _ _ h, ⟨c, cl, ac⟩ := this a ⟨d, o.resolve_right (not_mem_nil _), ad⟩ in ⟨c, or.inl cl, ac⟩ }, { cases a with o S', cases o with c; simp [parallel.aux1] at h; cases IH _ _ h with d dm; cases dm with o ad; cases o with dl dS', { exact let ⟨c, cl, ac⟩ := this a ⟨d, dl, ad⟩ in ⟨c, or.inl cl, ac⟩ }, { refine ⟨d, or.inr _, ad⟩, rw seq.destruct_eq_cons e, exact seq.mem_cons_of_mem _ dS' }, { simp at dl, cases dl with dc dl, { rw dc at ad, refine ⟨c, or.inr _, ad⟩, rw seq.destruct_eq_cons e, apply seq.mem_cons }, { exact let ⟨c, cl, ac⟩ := this a ⟨d, dl, ad⟩ in ⟨c, or.inl cl, ac⟩ } }, { refine ⟨d, or.inr _, ad⟩, rw seq.destruct_eq_cons e, exact seq.mem_cons_of_mem _ dS' } } } end lemma map_parallel (f : α → β) (S) : map f (parallel S) = parallel (S.map (map f)) := begin refine eq_of_bisim (λ c1 c2, ∃ l S, c1 = map f (corec parallel.aux1 (l, S)) ∧ c2 = corec parallel.aux1 (l.map (map f), S.map (map f))) _ ⟨[], S, rfl, rfl⟩, intros c1 c2 h, exact match c1, c2, h with ._, ._, ⟨l, S, rfl, rfl⟩ := begin clear _match, have : parallel.aux2 (l.map (map f)) = lmap f (rmap (list.map (map f)) (parallel.aux2 l)), { simp [parallel.aux2], induction l with c l IH; simp, rw [IH], dsimp [function.comp], cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l; simp [parallel.aux2], cases destruct c; simp }, simp [parallel.aux1], rw this, cases parallel.aux2 l with a l'; simp, apply S.cases_on _ (λ c S, _) (λ S, _); simp; simp [parallel.aux1]; exact ⟨_, _, rfl, rfl⟩ end end end theorem parallel_empty (S : wseq (computation α)) (h : S.head ~> none) : parallel S = empty _ := eq_empty_of_not_terminates $ λ ⟨a, m⟩, let ⟨c, cs, ac⟩ := exists_of_mem_parallel m, ⟨n, nm⟩ := exists_nth_of_mem cs, ⟨c', h'⟩ := head_some_of_nth_some nm in by injection h h' -- The reason this isn't trivial from exists_of_mem_parallel is because it eliminates to Sort def parallel_rec {S : wseq (computation α)} (C : α → Sort v) (H : ∀ s ∈ S, ∀ a ∈ s, C a) {a} (h : a ∈ parallel S) : C a := begin let T : wseq (computation (α × computation α)) := S.map (λc, c.map (λ a, (a, c))), have : S = T.map (map (λ c, c.1)), { rw [←wseq.map_comp], refine (wseq.map_id _).symm.trans (congr_arg (λ f, wseq.map f S) _), apply funext, intro c, dsimp [id, function.comp], rw [←map_comp], exact (map_id _).symm }, have pe := congr_arg parallel this, rw ←map_parallel at pe, have h' := h, rw pe at h', have : terminates (parallel T) := (terminates_map_iff _ _).1 ⟨_, h'⟩, ginduction get (parallel T) with e a' c, have : a ∈ c ∧ c ∈ S, { cases exists_of_mem_map h' with d h, cases h with dT cd, rw get_eq_of_mem _ dT at e, cases e, dsimp at cd, cases cd, cases exists_of_mem_parallel dT with d' h, cases h with dT' ad', cases wseq.exists_of_mem_map dT' with c' h, cases h with cs' e', rw ←e' at ad', cases exists_of_mem_map ad' with a' h, cases h with ac' e', injection e' with i1 i2, constructor, rwa [i1, i2] at ac', rwa i2 at cs' }, cases this with ac cs, apply H _ cs _ ac end theorem parallel_promises {S : wseq (computation α)} {a} (H : ∀ s ∈ S, s ~> a) : parallel S ~> a := λ a' ma', let ⟨c, cs, ac⟩ := exists_of_mem_parallel ma' in H _ cs ac theorem mem_parallel {S : wseq (computation α)} {a} (H : ∀ s ∈ S, s ~> a) {c} (cs : c ∈ S) (ac : a ∈ c) : a ∈ parallel S := by have := terminates_of_mem ac; have := terminates_parallel cs; exact mem_of_promises _ (parallel_promises H) lemma parallel_congr_lem {S T : wseq (computation α)} {a} (H : S.lift_rel equiv T) : (∀ s ∈ S, s ~> a) ↔ (∀ t ∈ T, t ~> a) := ⟨λ h1 t tT, let ⟨s, sS, se⟩ := wseq.exists_of_lift_rel_right H tT in (promises_congr se _).1 (h1 _ sS), λ h2 s sS, let ⟨t, tT, se⟩ := wseq.exists_of_lift_rel_left H sS in (promises_congr se _).2 (h2 _ tT)⟩ -- The parallel operation is only deterministic when all computation paths lead to the same value theorem parallel_congr_left {S T : wseq (computation α)} {a} (h1 : ∀ s ∈ S, s ~> a) (H : S.lift_rel equiv T) : parallel S ~ parallel T := let h2 := (parallel_congr_lem H).1 h1 in λ a', ⟨λh, by have aa := parallel_promises h1 h; rw ←aa; rw ←aa at h; exact let ⟨s, sS, as⟩ := exists_of_mem_parallel h, ⟨t, tT, st⟩ := wseq.exists_of_lift_rel_left H sS, aT := (st _).1 as in mem_parallel h2 tT aT, λh, by have aa := parallel_promises h2 h; rw ←aa; rw ←aa at h; exact let ⟨s, sS, as⟩ := exists_of_mem_parallel h, ⟨t, tT, st⟩ := wseq.exists_of_lift_rel_right H sS, aT := (st _).2 as in mem_parallel h1 tT aT⟩ theorem parallel_congr_right {S T : wseq (computation α)} {a} (h2 : ∀ t ∈ T, t ~> a) (H : S.lift_rel equiv T) : parallel S ~ parallel T := parallel_congr_left ((parallel_congr_lem H).2 h2) H end computation
6efd9a6ae8467910e5e72ce17015804a4bf22824
4a3109c7bb55b7ea698278061cbb921b8db93b7f
/mm0-lean/mm0/fol.lean
4ef2571867ceaf3a128a983c64041db724338136
[ "CC0-1.0" ]
permissive
LongJohnCoder/mm0
d233200cd7baae9c521cacfbbe162b2ff3bd993f
0c556a21af92d552e859fd42794f57398c3964cf
refs/heads/master
1,600,930,472,594
1,574,627,400,000
1,574,627,400,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,925
lean
import zfc def deps := list bool def deps.single (n : ℕ) : deps := nat.rec ([tt]) (λ _, list.cons ff) n -- | 0 := [tt] -- | (n+1) := ff :: deps.single n def deps.union (d1 : deps) : deps → deps := list.rec id (λ a l₁ IH l₂, list.cases_on l₂ l₁ (λ b l₂, (a || b) :: IH l₂)) d1 -- | [] l := l -- | l [] := l -- | (a :: l₁) (b :: l₂) := (a || b) :: deps.union l₁ l₂ def deps.contains (d : deps) : ℕ → bool := list.rec (λ _, ff) (λ a l₁ IH n, nat.cases_on n a IH) d def deps.disjoint (d : deps) (l : list ℕ) : bool := l.all (λ n, bnot (d.contains n)) def deps.filter_out (d : deps) {α} : list α → list α := list.rec id (λ a d' IH l, list.cases_on l ([]) $ λ b l', cond a (IH l') (b :: IH l')) d def deps.below (d : deps) (n : ℕ) : bool := d.length ≤ n inductive mm0_sort | set | wff | Class inductive {u} mm0_context : ℕ → Type | empty : mm0_context 0 | lvar {n} : mm0_context n → mm0_context (n + 1) | rvar {n} (d : deps) : mm0_sort → d.below n → mm0_context n → mm0_context n def mm0_context.rsize : ∀ {n}, mm0_context n → ℕ | _ mm0_context.empty := 0 | _ (mm0_context.lvar c) := c.rsize | _ (mm0_context.rvar _ _ _ c) := c.rsize.succ def mm0_context.sort_rev : ∀ {n} (c : mm0_context n), ℕ → option mm0_sort | _ mm0_context.empty _ := none | _ (mm0_context.lvar c) i := c.sort_rev i | _ (mm0_context.rvar d s h c) 0 := some s | _ (mm0_context.rvar d s h c) (i+1) := c.sort_rev i def mm0_context.sort {n} (c : mm0_context n) (i : ℕ) : option mm0_sort := c.rsize.psub i.succ >>= c.sort_rev def mm0_context.rvar_deps_rev : ∀ {n} (c : mm0_context n), ℕ → deps | _ mm0_context.empty _ := ([]) | _ (mm0_context.lvar c) i := c.rvar_deps_rev i | _ (mm0_context.rvar d s h c) 0 := d | _ (mm0_context.rvar d s h c) (i+1) := c.rvar_deps_rev i def mm0_context.rvar_deps {n} (c : mm0_context n) (i : ℕ) : deps := c.rvar_deps_rev (c.rsize - i.succ) def mm0_value : mm0_sort → Type 1 | mm0_sort.set := ulift ℕ | mm0_sort.wff := fol.formula zfc.L_ZFC | mm0_sort.Class := fol.formula zfc.L_ZFC local infix ` ∈' `:100 := fol.formula_of_relation zfc.ZFC_el namespace fol def move0 (k n : ℕ) : ℕ := if n = k then 0 else n + 1 def map_lift (f : ℕ → ℕ) (k n : ℕ) : ℕ := if n < k then n else f (n - k) + k namespace preterm def map_vars {L} (f : ℕ → ℕ) : ∀ {l}, preterm L l → preterm L l | _ &k := &(f k) | _ (func f) := func f | _ (app t₁ t₂) := app (map_vars t₁) (map_vars t₂) end preterm def preterm.deps {L} : ∀ {n}, preterm L n → deps | _ (preterm.var k) := deps.single k | _ (preterm.func f) := ([]) | _ (preterm.app t s) := t.deps.union s.deps def preformula.deps {L} : ∀ {n}, preformula L n → deps | _ preformula.falsum := ([]) | _ (preformula.equal t₁ t₂) := t₁.deps.union t₂.deps | _ (preformula.rel R) := ([]) | _ (preformula.apprel f t) := f.deps.union f.deps | _ (preformula.imp f₁ f₂) := f₁.deps.union f₂.deps | _ (preformula.all f) := f.deps.tail namespace preformula def map_vars {L} (f : ℕ → ℕ) : ∀ {l}, preformula L l → ℕ → preformula L l | _ falsum m := falsum | _ (t₁ ≃ t₂) m := t₁.map_vars (map_lift f m) ≃ t₂.map_vars (map_lift f m) | _ (rel R) m := rel R | _ (apprel r t) m := apprel (map_vars r m) (t.map_vars (map_lift f m)) | _ (f₁ ⟹ f₂) m := map_vars f₁ m ⟹ map_vars f₂ m | _ (∀' f) m := ∀' map_vars f (m+1) end preformula def ax_1 {L} {Γ : set (formula L)} {A B : formula L} : Γ ⊢ A ⟹ (B ⟹ A) := impI $ impI axm2 def ax_2 {L} {Γ : set (formula L)} {A B C : formula L} : Γ ⊢ (A ⟹ (B ⟹ C)) ⟹ ((A ⟹ B) ⟹ (A ⟹ C)) := impI $ impI $ impI $ impE _ (impE _ (weakening1 axm2) axm1) $ impE _ axm2 axm1 def ax_3 {L} {Γ : set (formula L)} {A B : formula L} : Γ ⊢ (∼A ⟹ ∼B) ⟹ (B ⟹ A) := impI $ impI $ falsumE $ impE _ (impE _ (weakening1 axm2) axm1) axm2 end fol def mm0_value.deps : ∀ {s}, mm0_value s → deps | mm0_sort.set ⟨n⟩ := deps.single n | mm0_sort.wff f := fol.preformula.deps f | mm0_sort.Class f := (fol.preformula.deps f).tail inductive mm0_context.value_empty (bv : list ℕ) : list ℕ → Type 1 | refl : mm0_context.value_empty bv def mm0_context.value' : ∀ {n}, mm0_context n → Type 1 | _ mm0_context.empty := punit | _ (mm0_context.lvar c) := mm0_context.value' c × ℕ | _ (mm0_context.rvar d s h c) := mm0_context.value' c × mm0_value s def mm0_context.value_bv : ∀ {n c}, @mm0_context.value' n c → list ℕ → list ℕ | _ mm0_context.empty _ r := r | _ (@mm0_context.lvar n c) v r := mm0_context.value_bv v.1 (v.2 :: r) | _ (mm0_context.rvar d s h c) v r := mm0_context.value_bv v.1 r def mm0_context.value_ok (bv : list ℕ) : ∀ {n} c, @mm0_context.value' n c → bool | _ mm0_context.empty _ := true | _ (mm0_context.lvar c) v := mm0_context.value_ok c v.1 | _ (mm0_context.rvar d s h c) v := v.2.deps.disjoint (deps.filter_out d bv) && mm0_context.value_ok c v.1 def mm0_context.value_bv_ok {n} (c) (v : @mm0_context.value' n c) : bool := let bv := mm0_context.value_bv v ([]) in bv.nodup && mm0_context.value_ok bv c v def mm0_context.value {n} (c : mm0_context n) : Type 1 := {v : @mm0_context.value' n c | mm0_context.value_bv_ok c v} inductive mm0_prim_term : Type | wtru : mm0_prim_term | wi : mm0_prim_term | wn : mm0_prim_term | wal : mm0_prim_term | wceq : mm0_prim_term | wcel : mm0_prim_term | cab : mm0_prim_term def mm0_prim_term.tgt : mm0_prim_term → mm0_sort | mm0_prim_term.wtru := mm0_sort.wff | mm0_prim_term.wi := mm0_sort.wff | mm0_prim_term.wn := mm0_sort.wff | mm0_prim_term.wal := mm0_sort.wff | mm0_prim_term.wceq := mm0_sort.wff | mm0_prim_term.wcel := mm0_sort.wff | mm0_prim_term.cab := mm0_sort.Class def mm0_prim_term.args : ∀ t : mm0_prim_term, Σ n, mm0_context n | mm0_prim_term.wtru := ⟨0, mm0_context.empty⟩ | mm0_prim_term.wi := ⟨0, mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.empty⟩ | mm0_prim_term.wn := ⟨0, mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.empty⟩ | mm0_prim_term.wal := ⟨1, mm0_context.rvar ([tt]) mm0_sort.wff rfl $ mm0_context.lvar $ mm0_context.empty⟩ | mm0_prim_term.wceq := ⟨0, mm0_context.rvar ([]) mm0_sort.Class rfl $ mm0_context.rvar ([]) mm0_sort.Class rfl $ mm0_context.empty⟩ | mm0_prim_term.wcel := ⟨0, mm0_context.rvar ([]) mm0_sort.Class rfl $ mm0_context.rvar ([]) mm0_sort.Class rfl $ mm0_context.empty⟩ | mm0_prim_term.cab := ⟨1, mm0_context.rvar ([tt]) mm0_sort.wff rfl $ mm0_context.lvar $ mm0_context.empty⟩ def mm0_prim_term.value' : ∀ (t : mm0_prim_term), t.args.2.value' → mm0_value t.tgt | mm0_prim_term.wtru _ := ⊤ | mm0_prim_term.wi ⟨⟨_, f₁⟩, f₂⟩ := f₁ ⟹ f₂ | mm0_prim_term.wn ⟨_, f⟩ := ∼f | mm0_prim_term.wal ⟨⟨_, x⟩, f⟩ := ∀' f.map_vars (fol.move0 x) 0 | mm0_prim_term.wceq ⟨⟨_, e₁⟩, e₂⟩ := ∀' (e₁ ⇔ e₂) | mm0_prim_term.wcel ⟨⟨_, A⟩, B⟩ := ∃' (∀' (&1 ∈' &0 ⇔ A ↑ 1) ⊓ B) | mm0_prim_term.cab ⟨⟨_, x⟩, f⟩ := f.map_vars (fol.move0 x) 0 def mm0_prim_term.value (t : mm0_prim_term) (v : t.args.2.value) : mm0_value t.tgt := t.value' v.1 inductive mm0_preterm : ∀ {γ} (Γ : mm0_context γ) {m}, mm0_context m → mm0_sort → Type | lvar {γ Γ} (i) : i < γ → @mm0_preterm γ Γ _ mm0_context.empty mm0_sort.set | rvar {γ Γ} (i) {s} : @mm0_context.sort γ Γ i = some s → mm0_preterm Γ mm0_context.empty s | prim {γ Γ} (t : mm0_prim_term) : @mm0_preterm γ Γ _ t.args.2 t.tgt | lapp {γ Γ n c s} (i) : @mm0_preterm γ Γ _ (@mm0_context.lvar n c) s → i < γ → mm0_preterm Γ c s | rapp {γ Γ n d' s' h c s} : @mm0_preterm γ Γ _ (@mm0_context.rvar n d' s' h c) s → mm0_preterm Γ mm0_context.empty s' → mm0_preterm Γ c s | defn {γ Γ n c s} : mm0_preterm c mm0_context.empty s → @mm0_preterm γ Γ n c s def mm0_term {γ} (Γ : mm0_context γ) : mm0_sort → Type := mm0_preterm Γ mm0_context.empty def mm0_preterm.weak_deps : ∀ {γ Γ m c s}, @mm0_preterm γ Γ m c s → deps | _ _ _ _ _ (@mm0_preterm.lvar _ _ i _) := deps.single i | _ _ _ _ _ (@mm0_preterm.rvar _ Γ i _ _) := Γ.rvar_deps i | _ _ _ _ _ (@mm0_preterm.prim _ _ t) := ([]) | _ _ _ _ _ (@mm0_preterm.lapp _ _ _ _ _ i f _) := f.weak_deps.union (deps.single i) | _ _ _ _ _ (@mm0_preterm.rapp _ _ _ _ _ _ _ _ f t) := f.weak_deps.union t.weak_deps | _ _ _ _ _ (@mm0_preterm.defn _ _ _ _ _ t) := ([]) def mm0_context.value'.lnth_rev : ∀ {n} {c : mm0_context n} (i : ℕ), c.value' → ℕ | _ mm0_context.empty i _ := 0 | _ (mm0_context.lvar c) 0 v := v.2 | _ (mm0_context.lvar c) (n+1) v := v.1.lnth_rev n | _ (mm0_context.rvar d s h c) n v := v.1.lnth_rev n def mm0_context.value'.lnth {n} {c : mm0_context n} (i : ℕ) (v : c.value') : ℕ := v.lnth_rev (n - i.succ) def mm0_lvar.value {n} {c : mm0_context n} (i : ℕ) (v : c.value) : ℕ := mm0_context.value'.lnth i v.1 def mm0_ovalue : option mm0_sort → Type 1 | none := punit | (some s) := mm0_value s def mm0_context.value'.rnth_rev : ∀ {n} {c : mm0_context n}, c.value' → ∀ i, mm0_ovalue (c.sort_rev i) | _ mm0_context.empty _ i := ⟨⟩ | _ (mm0_context.lvar c) v i := v.1.rnth_rev i | _ (mm0_context.rvar d s h c) v 0 := v.2 | _ (mm0_context.rvar d s h c) v (i+1) := v.1.rnth_rev i theorem mm0_context.sort_rev_eq {n} (c : mm0_context n) {i s} (h : mm0_context.sort c i = some s) : c.sort_rev (c.rsize - i.succ) = some s := begin unfold mm0_context.sort at h, rw nat.sub_eq_psub, cases c.rsize.psub i.succ, {cases h}, exact h end def mm0_context.value'.rnth {n} {c : mm0_context n} (v : c.value') {i s} (h : mm0_context.sort c i = some s) : mm0_value s := begin have := v.rnth_rev (c.rsize - i.succ), rwa c.sort_rev_eq h at this end def mm0_preterm.value' : ∀ {γ} {Γ : mm0_context γ} (V : Γ.value') {m c s}, @mm0_preterm _ Γ m c s → c.value' → mm0_value s | _ _ V _ _ _ (@mm0_preterm.lvar _ _ i h) v := ⟨V.lnth i⟩ | _ _ V _ _ s (@mm0_preterm.rvar _ _ i _ h) v := V.rnth h | _ _ V _ _ s (@mm0_preterm.prim _ _ t) v := mm0_prim_term.value' _ v | _ _ V m c s (@mm0_preterm.lapp _ _ n c' _ i f t) v := mm0_preterm.value' V f $ by exact (v, V.lnth i) | _ _ V m c s (@mm0_preterm.rapp _ _ n d' s' h c' _ f t) v := mm0_preterm.value' V f $ by exact (v, mm0_preterm.value' V t $ by split) | _ _ V m c s (@mm0_preterm.defn _ _ _ _ _ t) v := mm0_preterm.value' v t $ by split def mm0_preterm.value {γ} {Γ : mm0_context γ} (V : Γ.value) {m c s} (t : @mm0_preterm _ Γ m c s) (v : c.value) : mm0_value s := t.value' V.1 v.1 def mm0_term.value {γ} {Γ : mm0_context γ} (V : Γ.value) {s} (t : mm0_term Γ s) : mm0_value s := mm0_preterm.value V t ⟨⟨⟩, rfl⟩ section open tactic meta def name.is_numeric : name → bool | (name.mk_numeral _ name.anonymous) := tt | (name.mk_numeral _ n) := n.is_numeric | (name.mk_string _ n) := n.is_numeric | _ := ff meta def is_forall_domain : pexpr → bool | (expr.const n ([])) := n.is_numeric | _ := ff meta def ref_apply (r : ref expr) (n : name) : tactic unit := do m ← read_ref r, e ← mk_const n, m' ← mk_mvar, to_expr ```(%%e %%m') >>= unify m, write_ref r m' meta def tactic.mk_term : expr → pexpr → tactic unit | m (expr.pi x bi b t) := do if is_forall_domain b then do m1 ← mk_mvar, m2 ← mk_mvar, to_expr ```(((mm0_preterm.prim mm0_prim_term.wal).rapp %%m2).lapp %%m1) >>= unify m, tactic.mk_term m1 (expr.local_const x x bi ``(ℕ)), tactic.mk_term m2 $ (expr.lam x bi b t).subst (expr.local_const x x bi ``(ℕ)) else do m1 ← mk_mvar, m2 ← mk_mvar, to_expr ```(((mm0_preterm.prim mm0_prim_term.wi).rapp %%m2).rapp %%m1) tt ff >>= unify m, tactic.mk_term m1 b, tactic.mk_term m2 $ (expr.lam x bi b t).subst (expr.local_const x x bi ``(ℕ)) | m (expr.local_const _ (name.mk_string v name.anonymous) _ _) := if v.front = 'v' then do let i := v.mk_iterator.next.next_to_string.to_nat, e ← mk_mvar, to_expr ```(mm0_preterm.rvar %%(reflect i) %%e) >>= unify m, to_expr ```(rfl) >>= unify e else if v.front = 'x' then do let i := v.mk_iterator.next.next_to_string.to_nat, e ← mk_mvar, to_expr ```(mm0_preterm.rvar %%(reflect i) %%e) >>= unify m, to_expr ```(rfl) >>= unify e -- using_new_ref m $ λ r, do -- ref_apply r `mm0_preterm.lvar, -- iterate_exactly i $ do -- repeat $ ref_apply r `mm0_lvar.rtail, -- ref_apply r `mm0_lvar.ltail, -- repeat $ ref_apply r `mm0_lvar.rtail, -- m ← read_ref r, -- to_expr ```(mm0_lvar.head) >>= unify m else fail v | m e@(expr.app e1 e2) := match expr.erase_annotations e1 with | (expr.const `not ([])) := do m' ← mk_mvar, to_expr ```((mm0_preterm.prim mm0_prim_term.wn).rapp %%m') tt ff >>= unify m, tactic.mk_term m' e2 | e1' := trace e1'.to_raw_fmt >> failed end | m e := match expr.is_annotation e with | some (_, e') := tactic.mk_term m e' | none := trace e.to_raw_fmt >> failed end meta def tactic.interactive.mk_term : interactive.parse interactive.types.texpr → tactic unit | e := do m ← get_goals, tactic.mk_term m.head e end inductive mm0_stmt {γ} (Γ : mm0_context γ) : Type | proof : mm0_term Γ mm0_sort.wff → mm0_stmt | conv {s} : mm0_term Γ s → mm0_term Γ s → mm0_stmt def mm0_stmt.value {γ} {Γ : mm0_context γ} : mm0_stmt Γ → Type 1 | (mm0_stmt.proof t) := Π V, fol.Theory.fst zfc.ZFC ⊢ t.value V | (mm0_stmt.conv t₁ t₂) := Π V, plift $ t₁.value V = t₂.value V def mm0_thm.value {γ} {Γ : mm0_context γ} : list (mm0_term Γ mm0_sort.wff) → mm0_term Γ mm0_sort.wff → Type 1 | ([]) conc := (mm0_stmt.proof conc).value | (h :: hs) conc := (mm0_stmt.proof h).value → mm0_thm.value hs conc def mm0_subst {γ} (Γ : mm0_context γ) : ∀ {n}, mm0_context n → Type | _ mm0_context.empty := punit | _ (mm0_context.lvar c) := mm0_subst c × {i // i < γ} | _ (mm0_context.rvar d s h c) := mm0_subst c × mm0_term Γ s def mm0_subst.lnth_rev {γ Γ} : ∀ {n c}, ℕ → @mm0_subst γ Γ n c → ℕ | _ mm0_context.empty i σ := 0 | _ (mm0_context.lvar c) 0 σ := σ.2.1 | _ (mm0_context.lvar c) (n+1) σ := σ.1.lnth_rev n | _ (mm0_context.rvar d s h c) n σ := σ.1.lnth_rev n def mm0_subst.lnth {γ Γ n c} (i : ℕ) (σ : @mm0_subst γ Γ n c) : ℕ := σ.lnth_rev (n - i.succ) theorem mm0_subst.lnth_rev_lt {γ Γ} : ∀ {n c i} (σ : @mm0_subst γ Γ n c), i < n → mm0_subst.lnth_rev i σ < γ | _ (mm0_context.lvar c) 0 σ h := σ.2.2 | _ (mm0_context.lvar c) (n+1) σ h := σ.1.lnth_rev_lt (nat.lt_of_succ_lt_succ h) | _ (mm0_context.rvar _ _ _ c) n σ h := σ.1.lnth_rev_lt h theorem mm0_subst.lnth_lt {γ Γ n c i} (σ : @mm0_subst γ Γ n c) (h : i < n) : mm0_subst.lnth i σ < γ := σ.lnth_rev_lt (nat.sub_lt (lt_of_le_of_lt (zero_le _) h) (nat.succ_pos _)) def mm0_oterm {γ} (Γ : mm0_context γ) : option mm0_sort → Type | none := punit | (some s) := mm0_term Γ s def mm0_subst.rnth_rev {γ Γ} : ∀ {n c} (i : ℕ), @mm0_subst γ Γ n c → mm0_oterm Γ (c.sort_rev i) | _ mm0_context.empty i σ := ⟨⟩ | _ (mm0_context.lvar c) i σ := σ.1.rnth_rev i | _ (mm0_context.rvar d s h c) 0 σ := σ.2 | _ (mm0_context.rvar d s h c) (i+1) σ := σ.1.rnth_rev i def mm0_subst.rnth {γ Γ n c} (σ : @mm0_subst γ Γ n c) (i) {s} (h : mm0_context.sort c i = some s) : mm0_term Γ s := begin have := σ.rnth_rev (c.rsize - i.succ), rwa c.sort_rev_eq h at this end def mm0_subst.apply : ∀ {γ Γ δ Δ} (σ : @mm0_subst γ Γ δ Δ), ∀ {n c s}, @mm0_preterm _ Δ n c s → mm0_preterm Γ c s | γ Γ δ Δ σ _ _ _ (mm0_preterm.lvar i h) := mm0_preterm.lvar (σ.lnth i) (σ.lnth_lt h) | γ Γ δ Δ σ _ _ _ (mm0_preterm.rvar i h) := σ.rnth i h | γ Γ δ Δ σ _ _ _ (mm0_preterm.prim t) := mm0_preterm.prim t | γ Γ δ Δ σ _ _ _ (mm0_preterm.lapp i f h) := (σ.apply f).lapp (σ.lnth i) (σ.lnth_lt h) | γ Γ δ Δ σ _ _ _ (mm0_preterm.rapp f t) := (σ.apply f).rapp (σ.apply t) | γ Γ δ Δ σ _ _ _ (mm0_preterm.defn t) := mm0_preterm.defn t inductive mm0_axiom : Type | ax_1 | ax_2 | ax_3 | ax_mp def mm0_axiom.args : ∀ t : mm0_axiom, Σ n, mm0_context n | mm0_axiom.ax_1 := ⟨0, mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.empty⟩ | mm0_axiom.ax_2 := ⟨0, mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.empty⟩ | mm0_axiom.ax_3 := ⟨0, mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.empty⟩ | mm0_axiom.ax_mp := ⟨0, mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.rvar ([]) mm0_sort.wff rfl $ mm0_context.empty⟩ def mm0_axiom.hyps : ∀ t : mm0_axiom, list (mm0_term t.args.2 mm0_sort.wff) | mm0_axiom.ax_1 := [] | mm0_axiom.ax_2 := [] | mm0_axiom.ax_3 := [] | mm0_axiom.ax_mp := [by mk_term v0, by mk_term v0 → v1] def mm0_axiom.conc : ∀ t : mm0_axiom, mm0_term t.args.2 mm0_sort.wff | mm0_axiom.ax_1 := by mk_term v0 → v1 → v0 | mm0_axiom.ax_2 := by mk_term (v0 → v1 → v2) → (v0 → v1) → v0 → v2 | mm0_axiom.ax_3 := by mk_term (¬ v0 → ¬ v1) → v1 → v0 | mm0_axiom.ax_mp := by mk_term v1 def mm0_axiom.sound : ∀ t : mm0_axiom, mm0_thm.value t.hyps t.conc | mm0_axiom.ax_1 := λ V, fol.ax_1 | mm0_axiom.ax_2 := λ V, fol.ax_2 | mm0_axiom.ax_3 := λ V, fol.ax_3 | mm0_axiom.ax_mp := λ h₁ h₂ V, fol.prf.impE _ (h₂ V) (h₁ V) inductive mm0_preproof {γ Γ} (hyps : list (@mm0_term γ Γ mm0_sort.wff)) : list (mm0_term Γ mm0_sort.wff) → mm0_term Γ mm0_sort.wff → Type | Axiom (A : mm0_axiom) (σ : mm0_subst Γ A.args.2) : mm0_preproof (A.hyps.map σ.apply) (σ.apply A.conc) | app {h hs p} : mm0_preproof (h :: hs) p → mm0_preproof ([]) h → mm0_preproof hs p | hyp (i h) : mm0_preproof ([]) (hyps.nth_le i h) def mm0_proof {γ Γ} (hyps : list (mm0_term Γ mm0_sort.wff)) : @mm0_term γ Γ mm0_sort.wff → Type := mm0_preproof hyps ([]) def mm0_thm.weaken1 {γ Γ} : ∀ hyps {t}, (mm0_stmt.proof t).value → @mm0_thm.value γ Γ hyps t | ([]) t h := h | (l::ls) t h := λ _, mm0_thm.weaken1 ls h def mm0_thm.weaken {γ Γ} : ∀ {hs hyps t}, mm0_thm.value hs t → @mm0_thm.value γ Γ (hs ++ hyps) t | ([]) ls t h := mm0_thm.weaken1 ls h | (e::es) ls t h := λ x, mm0_thm.weaken (h x) def mm0_subst.sound {γ Γ} : ∀ {n c} (σ : @mm0_subst γ Γ n c) {hs t}, mm0_thm.value hs t → mm0_thm.value (hs.map σ.apply) (σ.apply t) := sorry def mm0_preproof.app_sound1 {γ Γ s} : ∀ {hs p}, ((@mm0_stmt.proof γ Γ s).value → @mm0_thm.value γ Γ hs p) → mm0_thm.value hs s → mm0_thm.value hs p | ([]) t f v := f v | (h::hs) t f v := λ x, mm0_preproof.app_sound1 (λ y, f y x) (v x) def mm0_preproof.app_sound {γ Γ} (hyps : list (@mm0_term γ Γ mm0_sort.wff)) : ∀ {s hs p}, ((mm0_stmt.proof s).value → mm0_thm.value (hs ++ hyps) p) → mm0_thm.value hyps s → mm0_thm.value (hs ++ hyps) p | s ([]) t f v := mm0_preproof.app_sound1 f v | s (h::hs) t f v := λ x, mm0_preproof.app_sound (λ y, f y x) v def mm0_preproof.hyp_sound {γ Γ} : ∀ hs i h, @mm0_thm.value γ Γ hs (list.nth_le hs i h) | ([]) _ h' := absurd h' (nat.not_lt_zero _) | (h::hs) 0 h' := λ x, mm0_thm.weaken1 _ x | (h::hs) (n+1) h' := λ x, mm0_preproof.hyp_sound _ _ _ def mm0_preproof.sound {γ Γ hyps} : ∀ {hs t}, @mm0_preproof γ Γ hyps hs t → mm0_thm.value (hs ++ hyps) t | _ _ (mm0_preproof.Axiom _ A σ) := mm0_thm.weaken $ σ.sound A.sound | _ _ (mm0_preproof.app P p) := mm0_preproof.app_sound _ P.sound p.sound | _ _ (mm0_preproof.hyp i h) := mm0_preproof.hyp_sound _ _ _
808195bad0065d46ef163220bb694fad0af83506
4f9ca1935adf84f1bae9c5740ec1f2ad406716fa
/src/data/real/nnreal.lean
96f855c503762387229197a24bc03f9c5a77c1c6
[ "Apache-2.0" ]
permissive
matthew-hilty/mathlib
36fd7db866365e9ee4a0ba1d6f8ad34d068cec6c
585e107f9811719832c6656f49e1263a8eef5380
refs/heads/master
1,607,100,509,178
1,578,151,707,000
1,578,151,707,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,155
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import data.real.basic order.lattice algebra.field noncomputable theory open lattice open_locale classical /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ instance : can_lift ℝ nnreal := { coe := coe, cond := λ r, r ≥ 0, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≤ r := r.2 instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_div ℝ≥0 := ⟨λa b, ⟨a.1 / b.1, div_nonneg' a.2 b.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ @[simp] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, move_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, move_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, move_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, move_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma zero_div (r : ℝ≥0) : 0 / r = 0 := nnreal.eq (zero_div _) @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := @nnreal.eq_iff r 0 instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero] } end instance : is_semiring_hom (coe : ℝ≥0 → ℝ) := by refine_struct {..}; intros; refl @[move_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := is_monoid_hom.map_pow coe r n @[move_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := eq.symm $ l.sum_hom coe @[move_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := eq.symm $ l.prod_hom coe @[move_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := eq.symm $ s.sum_hom coe @[move_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := eq.symm $ s.prod_hom coe @[move_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(s.sum f) = s.sum (λa, (f a : ℝ)) := eq.symm $ s.sum_hom coe @[move_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(s.prod f) = s.prod (λa, (f a : ℝ)) := eq.symm $ s.prod_hom coe @[move_cast] lemma smul_coe (r : ℝ≥0) (n : ℕ) : ↑(add_monoid.smul n r) = add_monoid.smul n (r:ℝ) := is_add_monoid_hom.map_smul coe r n @[simp, squash_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := is_semiring_hom.map_nat_cast coe n instance : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective (by apply_instance) @[elim_cast] protected lemma coe_le {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[elim_cast] protected lemma coe_lt {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[elim_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl @[elim_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := subtype.ext.symm protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.lattice.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.lattice.order_bot, .. nnreal.lattice.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.lattice.order_bot, .. nnreal.lattice.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_le_mul_of_nonneg_left := assume a b c, @mul_le_mul_of_nonneg_left ℝ _ a b c, mul_le_mul_of_nonneg_right := assume a b c, @mul_le_mul_of_nonneg_right ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_monoid, .. nnreal.comm_semiring } instance : canonically_ordered_comm_semiring ℝ≥0 := { zero_ne_one := assume h, @zero_ne_one ℝ _ $ congr_arg subtype.val $ h, mul_eq_zero_iff := assume a b, nnreal.eq_iff.symm.trans $ mul_eq_zero.trans $ by simp, .. nnreal.linear_ordered_semiring, .. nnreal.canonically_ordered_monoid, .. nnreal.comm_semiring } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : nnreal → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' s), begin by_cases h : s = ∅, { simp [h, set.image_empty, real.Sup_empty] }, rcases set.ne_empty_iff_exists_mem.1 h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : nnreal → ℝ) '' s), begin by_cases h : s = ∅, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (by simp [h]) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : nnreal → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice.lattice_of_decidable_linear_order, .. nnreal.lattice.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (add_monoid.smul n y : nnreal), by simp [*, smul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : nnreal) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p ≥ 0) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r ≥ 0) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact eq_of_mul_eq_mul_left (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : nnreal} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le] using h lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt protected lemma sub_lt_self {r p : nnreal} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le, ← nnreal.coe_le, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le, nnreal.coe_le, sub_eq_zero h] end lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := by rw [← nnreal.eq_iff, nnreal.coe_inv, nnreal.coe_zero, inv_eq_zero, ← nnreal.coe_zero, nnreal.eq_iff] @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] @[simp] lemma inv_one : (1:ℝ≥0)⁻¹ = 1 := nnreal.eq $ inv_one protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv' _ _ protected lemma inv_pow' {r : ℝ≥0} {n : ℕ} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq $ inv_inv' _ @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa [div_def] using half_lt_self zero_ne_one.symm end inv end nnreal
280040c58fd8e186cae1d4fbb1c01ea5937cc7a1
4727251e0cd73359b15b664c3170e5d754078599
/src/order/filter/basic.lean
748cf89a8400b475a214bab0b0de99a5c45ba79a
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
121,167
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, Jeremy Avigad -/ import control.traversable.instances import data.set.finite import order.copy import tactic.monotonicity /-! # Theory of filters on sets ## Main definitions * `filter` : filters on a set; * `at_top`, `at_bot`, `cofinite`, `principal` : specific filters; * `map`, `comap`, `prod` : operations on filters; * `tendsto` : limit with respect to filters; * `eventually` : `f.eventually p` means `{x | p x} ∈ f`; * `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`; * `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f` with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`; * `ne_bot f` : an utility class stating that `f` is a non-trivial filter. Filters on a type `X` are sets of sets of `X` satisfying three conditions. They are mostly used to abstract two related kinds of ideas: * *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions at a point or at infinity, etc... * *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough a point `x`, or for close enough pairs of points, or things happening almost everywhere in the sense of measure theory. Dually, filters can also express the idea of *things happening often*: for arbitrarily large `n`, or at a point in any neighborhood of given a point etc... In this file, we define the type `filter X` of filters on `X`, and endow it with a complete lattice structure. This structure is lifted from the lattice structure on `set (set X)` using the Galois insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to the smallest filter containing it in the other direction. We also prove `filter` is a monadic functor, with a push-forward operation `filter.map` and a pull-back operation `filter.comap` that form a Galois connections for the order on filters. Finally we describe a product operation `filter X → filter Y → filter (X × Y)`. The examples of filters appearing in the description of the two motivating ideas are: * `(at_top : filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N` * `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic) * `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces defined in topology.uniform_space.basic) * `μ.ae` : made of sets whose complement has zero measure with respect to `μ` (defined in `measure_theory.measure_space`) The general notion of limit of a map with respect to filters on the source and target types is `filter.tendsto`. It is defined in terms of the order and the push-forward operation. The predicate "happening eventually" is `filter.eventually`, and "happening often" is `filter.frequently`, whose definitions are immediate after `filter` is defined (but they come rather late in this file in order to immediately relate them to the lattice structure). For instance, anticipating on topology.basic, the statement: "if a sequence `u` converges to some `x` and `u n` belongs to a set `M` for `n` large enough then `x` is in the closure of `M`" is formalized as: `tendsto u at_top (𝓝 x) → (∀ᶠ n in at_top, u n ∈ M) → x ∈ closure M`, which is a special case of `mem_closure_of_tendsto` from topology.basic. ## Notations * `∀ᶠ x in f, p x` : `f.eventually p`; * `∃ᶠ x in f, p x` : `f.frequently p`; * `f =ᶠ[l] g` : `∀ᶠ x in l, f x = g x`; * `f ≤ᶠ[l] g` : `∀ᶠ x in l, f x ≤ g x`; * `f ×ᶠ g` : `filter.prod f g`, localized in `filter`; * `𝓟 s` : `principal s`, localized in `filter`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which we do *not* require. This gives `filter X` better formal properties, in particular a bottom element `⊥` for its lattice structure, at the cost of including the assumption `[ne_bot f]` in a number of lemmas and definitions. -/ open function set order universes u v w x y open_locale classical /-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`, is upwards-closed, and is stable under intersection. We do not forbid this collection to be all sets of `α`. -/ structure filter (α : Type*) := (sets : set (set α)) (univ_sets : set.univ ∈ sets) (sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets) (inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets) /-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/ instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩ namespace filter variables {α : Type u} {f g : filter α} {s t : set α} @[simp] protected lemma mem_mk {t : set (set α)} {h₁ h₂ h₃} : s ∈ mk t h₁ h₂ h₃ ↔ s ∈ t := iff.rfl @[simp] protected lemma mem_sets : s ∈ f.sets ↔ s ∈ f := iff.rfl instance inhabited_mem : inhabited {s : set α // s ∈ f} := ⟨⟨univ, f.univ_sets⟩⟩ lemma filter_eq : ∀ {f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g := by simp only [filter_eq_iff, ext_iff, filter.mem_sets] @[ext] protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g := filter.ext_iff.2 /-- An extensionality lemma that is useful for filters with good lemmas about `sᶜ ∈ f` (e.g., `filter.comap`, `filter.coprod`, `filter.Coprod`, `filter.cofinite`). -/ protected lemma coext (h : ∀ s, sᶜ ∈ f ↔ sᶜ ∈ g) : f = g := filter.ext $ compl_surjective.forall.2 h @[simp] lemma univ_mem : univ ∈ f := f.univ_sets lemma mem_of_superset {x y : set α} (hx : x ∈ f) (hxy : x ⊆ y) : y ∈ f := f.sets_of_superset hx hxy lemma inter_mem {s t : set α} (hs : s ∈ f) (ht : t ∈ f) : s ∩ t ∈ f := f.inter_sets hs ht @[simp] lemma inter_mem_iff {s t : set α} : s ∩ t ∈ f ↔ s ∈ f ∧ t ∈ f := ⟨λ h, ⟨mem_of_superset h (inter_subset_left s t), mem_of_superset h (inter_subset_right s t)⟩, and_imp.2 inter_mem⟩ lemma diff_mem {s t : set α} (hs : s ∈ f) (ht : tᶜ ∈ f) : s \ t ∈ f := inter_mem hs ht lemma univ_mem' (h : ∀ a, a ∈ s) : s ∈ f := mem_of_superset univ_mem (λ x _, h x) lemma mp_mem (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f := mem_of_superset (inter_mem hs h) $ λ x ⟨h₁, h₂⟩, h₂ h₁ lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f := ⟨λ hs, mp_mem hs (mem_of_superset h (λ x, iff.mp)), λ hs, mp_mem hs (mem_of_superset h (λ x, iff.mpr))⟩ @[simp] lemma bInter_mem {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (⋂ i ∈ is, s i) ∈ f ↔ ∀ i ∈ is, s i ∈ f := finite.induction_on hf (by simp) (λ i s hi _ hs, by simp [hs]) @[simp] lemma bInter_finset_mem {β : Type v} {s : β → set α} (is : finset β) : (⋂ i ∈ is, s i) ∈ f ↔ ∀ i ∈ is, s i ∈ f := bInter_mem is.finite_to_set alias bInter_finset_mem ← finset.Inter_mem_sets attribute [protected] finset.Inter_mem_sets @[simp] lemma sInter_mem {s : set (set α)} (hfin : finite s) : ⋂₀ s ∈ f ↔ ∀ U ∈ s, U ∈ f := by rw [sInter_eq_bInter, bInter_mem hfin] @[simp] lemma Inter_mem {β : Type v} {s : β → set α} [fintype β] : (⋂ i, s i) ∈ f ↔ ∀ i, s i ∈ f := by simpa using bInter_mem finite_univ lemma exists_mem_subset_iff : (∃ t ∈ f, t ⊆ s) ↔ s ∈ f := ⟨λ ⟨t, ht, ts⟩, mem_of_superset ht ts, λ hs, ⟨s, hs, subset.rfl⟩⟩ lemma monotone_mem {f : filter α} : monotone (λ s, s ∈ f) := λ s t hst h, mem_of_superset h hst lemma exists_mem_and_iff {P : set α → Prop} {Q : set α → Prop} (hP : antitone P) (hQ : antitone Q) : (∃ u ∈ f, P u) ∧ (∃ u ∈ f, Q u) ↔ (∃ u ∈ f, P u ∧ Q u) := begin split, { rintro ⟨⟨u, huf, hPu⟩, v, hvf, hQv⟩, exact ⟨u ∩ v, inter_mem huf hvf, hP (inter_subset_left _ _) hPu, hQ (inter_subset_right _ _) hQv⟩ }, { rintro ⟨u, huf, hPu, hQu⟩, exact ⟨⟨u, huf, hPu⟩, u, huf, hQu⟩ } end lemma forall_in_swap {β : Type*} {p : set α → β → Prop} : (∀ (a ∈ f) b, p a b) ↔ ∀ b (a ∈ f), p a b := set.forall_in_swap end filter namespace tactic.interactive open tactic setup_tactic_parser /-- `filter_upwards [h₁, ⋯, hₙ]` replaces a goal of the form `s ∈ f` and terms `h₁ : t₁ ∈ f, ⋯, hₙ : tₙ ∈ f` with `∀ x, x ∈ t₁ → ⋯ → x ∈ tₙ → x ∈ s`. The list is an optional parameter, `[]` being its default value. `filter_upwards [h₁, ⋯, hₙ] with a₁ a₂ ⋯ aₖ` is a short form for `{ filter_upwards [h₁, ⋯, hₙ], intros a₁ a₂ ⋯ aₖ }`. `filter_upwards [h₁, ⋯, hₙ] using e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`. Combining both shortcuts is done by writing `filter_upwards [h₁, ⋯, hₙ] with a₁ a₂ ⋯ aₖ using e`. Note that in this case, the `aᵢ` terms can be used in `e`. -/ meta def filter_upwards (s : parse types.pexpr_list?) (wth : parse with_ident_list?) (tgt : parse (tk "using" *> texpr)?) : tactic unit := do (s.get_or_else []).reverse.mmap (λ e, eapplyc `filter.mp_mem >> eapply e), eapplyc `filter.univ_mem', `[dsimp only [set.mem_set_of_eq]], let wth := wth.get_or_else [], if ¬wth.empty then intros wth else skip, match tgt with | some e := exact e | none := skip end add_tactic_doc { name := "filter_upwards", category := doc_category.tactic, decl_names := [`tactic.interactive.filter_upwards], tags := ["goal management", "lemma application"] } end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := subset_univ s, sets_of_superset := λ x y hx, subset.trans hx, inter_sets := λ x y, subset_inter } localized "notation `𝓟` := filter.principal" in filter instance : inhabited (filter α) := ⟨𝓟 ∅⟩ @[simp] lemma mem_principal {s t : set α} : s ∈ 𝓟 t ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ 𝓟 s := subset.rfl end principal open_locale filter section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t} ∈ f}, univ_sets := by simp only [mem_set_of_eq, univ_sets, ← filter.mem_sets, set_of_true], sets_of_superset := λ x y hx xy, mem_of_superset hx $ λ f h, mem_of_superset h xy, inter_sets := λ x y hx hy, mem_of_superset (inter_mem hx hy) $ λ f ⟨h₁, h₂⟩, inter_mem h₁ h₂ } @[simp] lemma mem_join {s : set α} {f : filter (filter α)} : s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λ f g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f, le_antisymm := λ a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := λ a, subset.rfl, le_trans := λ a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl /-- `generate_sets g s`: `s` is in the filter closure of `g`. -/ inductive generate_sets (g : set (set α)) : set α → Prop | basic {s : set α} : s ∈ g → generate_sets s | univ : generate_sets univ | superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t | inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t) /-- `generate g` is the largest filter containing the sets `g`. -/ def generate (g : set (set α)) : filter α := { sets := generate_sets g, univ_sets := generate_sets.univ, sets_of_superset := λ x y, generate_sets.superset, inter_sets := λ s t, generate_sets.inter } lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets := iff.intro (λ h u hu, h $ generate_sets.basic $ hu) (λ h u hu, hu.rec_on h univ_mem (λ x y _ hxy hx, mem_of_superset hx hxy) (λ x y _ _ hx hy, inter_mem hx hy)) lemma mem_generate_iff {s : set $ set α} {U : set α} : U ∈ generate s ↔ ∃ t ⊆ s, finite t ∧ ⋂₀ t ⊆ U := begin split ; intro h, { induction h with V V_in V W V_in hVW hV V W V_in W_in hV hW, { use {V}, simp [V_in] }, { use ∅, simp [subset.refl, univ] }, { rcases hV with ⟨t, hts, htfin, hinter⟩, exact ⟨t, hts, htfin, hinter.trans hVW⟩ }, { rcases hV with ⟨t, hts, htfin, htinter⟩, rcases hW with ⟨z, hzs, hzfin, hzinter⟩, refine ⟨t ∪ z, union_subset hts hzs, htfin.union hzfin, _⟩, rw sInter_union, exact inter_subset_inter htinter hzinter } }, { rcases h with ⟨t, ts, tfin, h⟩, apply generate_sets.superset _ h, revert ts, apply finite.induction_on tfin, { intro h, rw sInter_empty, exact generate_sets.univ }, { intros V r hV rfin hinter h, cases insert_subset.mp h with V_in r_sub, rw [insert_eq V r, sInter_union], apply generate_sets.inter _ (hinter r_sub), rw sInter_singleton, exact generate_sets.basic V_in } }, end /-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly `s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/ protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α := { sets := s, univ_sets := hs ▸ (univ_mem : univ ∈ generate s), sets_of_superset := λ x y, hs ▸ (mem_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s), inter_sets := λ x y, hs ▸ (inter_mem : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) } lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} : filter.mk_of_closure s hs = generate s := filter.ext $ λ u, show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl /-- Galois insertion from sets of sets into filters. -/ def gi_generate (α : Type*) : @galois_insertion (set (set α)) (filter α)ᵒᵈ _ _ filter.generate filter.sets := { gc := λ s f, sets_iff_generate, le_l_u := λ f u h, generate_sets.basic h, choice := λ s hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_rfl), choice_eq := λ s hs, mk_of_closure_sets } /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f) (b ∈ g), s = a ∩ b }, univ_sets := ⟨_, univ_mem, _, univ_mem, by simp⟩, sets_of_superset := begin rintro x y ⟨a, ha, b, hb, rfl⟩ xy, refine ⟨a ∪ y, mem_of_superset ha (subset_union_left a y), b ∪ y, mem_of_superset hb (subset_union_left b y), _⟩, rw [← inter_union_distrib_right, union_eq_self_of_subset_left xy] end, inter_sets := begin rintro x y ⟨a, ha, b, hb, rfl⟩ ⟨c, hc, d, hd, rfl⟩, refine ⟨a ∩ c, inter_mem ha hc, b ∩ d, inter_mem hb hd, _⟩, ac_refl end }⟩ lemma mem_inf_iff {f g : filter α} {s : set α} : s ∈ f ⊓ g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, s = t₁ ∩ t₂ := iff.rfl lemma mem_inf_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g := ⟨s, h, univ, univ_mem, (inter_univ s).symm⟩ lemma mem_inf_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g := ⟨univ, univ_mem, s, h, (univ_inter s).symm⟩ lemma inter_mem_inf {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g := ⟨s, hs, t, ht, rfl⟩ lemma mem_inf_of_inter {f g : filter α} {s t u : set α} (hs : s ∈ f) (ht : t ∈ g) (h : s ∩ t ⊆ u) : u ∈ f ⊓ g := mem_of_superset (inter_mem_inf hs ht) h lemma mem_inf_iff_superset {f g : filter α} {s : set α} : s ∈ f ⊓ g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ∩ t₂ ⊆ s := ⟨λ ⟨t₁, h₁, t₂, h₂, eq⟩, ⟨t₁, h₁, t₂, h₂, eq ▸ subset.rfl⟩, λ ⟨t₁, h₁, t₂, h₂, sub⟩, mem_inf_of_inter h₁ h₂ sub⟩ instance : has_top (filter α) := ⟨{ sets := {s | ∀ x, x ∈ s}, univ_sets := λ x, mem_univ x, sets_of_superset := λ x y hx hxy a, hxy (hx a), inter_sets := λ x y hx hy a, mem_inter (hx _) (hy _) }⟩ lemma mem_top_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀ x, x ∈ s) := iff.rfl @[simp] lemma mem_top {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ := by rw [mem_top_iff_forall, eq_univ_iff_forall] section complete_lattice /- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately, we want to have different definitional equalities for the lattice operations. So we define them upfront and change the lattice operations for the complete lattice instance. -/ private def original_complete_lattice : complete_lattice (filter α) := @order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice local attribute [instance] original_complete_lattice instance : complete_lattice (filter α) := original_complete_lattice.copy /- le -/ filter.partial_order.le rfl /- top -/ (filter.has_top).1 (top_unique $ λ s hs, by simp [mem_top.1 hs]) /- bot -/ _ rfl /- sup -/ _ rfl /- inf -/ (filter.has_inf).1 begin ext f g : 2, exact le_antisymm (le_inf (λ s, mem_inf_of_left) (λ s, mem_inf_of_right)) (begin rintro s ⟨a, ha, b, hb, rfl⟩, exact inter_sets _ (@inf_le_left (filter α) _ _ _ _ ha) (@inf_le_right (filter α) _ _ _ _ hb) end) end /- Sup -/ (join ∘ 𝓟) (by { ext s x, exact mem_Inter₂.symm.trans (set.ext_iff.1 (sInter_image _ _) x).symm}) /- Inf -/ _ rfl end complete_lattice /-- A filter is `ne_bot` if it is not equal to `⊥`, or equivalently the empty set does not belong to the filter. Bourbaki include this assumption in the definition of a filter but we prefer to have a `complete_lattice` structure on filter, so we use a typeclass argument in lemmas instead. -/ class ne_bot (f : filter α) : Prop := (ne' : f ≠ ⊥) lemma ne_bot_iff {f : filter α} : ne_bot f ↔ f ≠ ⊥ := ⟨λ h, h.1, λ h, ⟨h⟩⟩ lemma ne_bot.ne {f : filter α} (hf : ne_bot f) : f ≠ ⊥ := ne_bot.ne' @[simp] lemma not_ne_bot {α : Type*} {f : filter α} : ¬ f.ne_bot ↔ f = ⊥ := not_iff_comm.1 ne_bot_iff.symm lemma ne_bot.mono {f g : filter α} (hf : ne_bot f) (hg : f ≤ g) : ne_bot g := ⟨ne_bot_of_le_ne_bot hf.1 hg⟩ lemma ne_bot_of_le {f g : filter α} [hf : ne_bot f] (hg : f ≤ g) : ne_bot g := hf.mono hg @[simp] lemma sup_ne_bot {f g : filter α} : ne_bot (f ⊔ g) ↔ ne_bot f ∨ ne_bot g := by simp [ne_bot_iff, not_and_distrib] lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (gi_generate α).gc.u_inf lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂ f ∈ s, (f : filter α).sets) := (gi_generate α).gc.u_Inf lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂ i, (f i).sets) := (gi_generate α).gc.u_infi lemma generate_empty : filter.generate ∅ = (⊤ : filter α) := (gi_generate α).gc.l_bot lemma generate_univ : filter.generate univ = (⊥ : filter α) := mk_of_closure_sets.symm lemma generate_union {s t : set (set α)} : filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t := (gi_generate α).gc.l_sup lemma generate_Union {s : ι → set (set α)} : filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) := (gi_generate α).gc.l_supr @[simp] lemma mem_bot {s : set α} : s ∈ (⊥ : filter α) := trivial @[simp] lemma mem_sup {f g : filter α} {s : set α} : s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g := iff.rfl lemma union_mem_sup {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∪ t ∈ f ⊔ g := ⟨mem_of_superset hs (subset_union_left s t), mem_of_superset ht (subset_union_right s t)⟩ @[simp] lemma mem_Sup {x : set α} {s : set (filter α)} : x ∈ Sup s ↔ (∀ f ∈ s, x ∈ (f : filter α)) := iff.rfl @[simp] lemma mem_supr {x : set α} {f : ι → filter α} : x ∈ supr f ↔ (∀ i, x ∈ f i) := by simp only [← filter.mem_sets, supr_sets_eq, iff_self, mem_Inter] @[simp] lemma supr_ne_bot {f : ι → filter α} : (⨆ i, f i).ne_bot ↔ ∃ i, (f i).ne_bot := by simp [ne_bot_iff] lemma infi_eq_generate (s : ι → filter α) : infi s = generate (⋃ i, (s i).sets) := show generate _ = generate _, from congr_arg _ $ congr_arg Sup $ (range_comp _ _).symm lemma mem_infi_of_mem {f : ι → filter α} (i : ι) : ∀ {s}, s ∈ f i → s ∈ ⨅ i, f i := show (⨅ i, f i) ≤ f i, from infi_le _ _ lemma mem_infi_of_Inter {ι} {s : ι → filter α} {U : set α} {I : set ι} (I_fin : finite I) {V : I → set α} (hV : ∀ i, V i ∈ s i) (hU : (⋂ i, V i) ⊆ U) : U ∈ ⨅ i, s i := begin haveI := I_fin.fintype, refine mem_of_superset (Inter_mem.2 $ λ i, _) hU, exact mem_infi_of_mem i (hV _) end lemma mem_infi {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔ ∃ I : set ι, finite I ∧ ∃ V : I → set α, (∀ i, V i ∈ s i) ∧ U = ⋂ i, V i := begin split, { rw [infi_eq_generate, mem_generate_iff], rintro ⟨t, tsub, tfin, tinter⟩, rcases eq_finite_Union_of_finite_subset_Union tfin tsub with ⟨I, Ifin, σ, σfin, σsub, rfl⟩, rw sInter_Union at tinter, set V := λ i, U ∪ ⋂₀ σ i with hV, have V_in : ∀ i, V i ∈ s i, { rintro i, have : (⋂₀ σ i) ∈ s i, { rw sInter_mem (σfin _), apply σsub }, exact mem_of_superset this (subset_union_right _ _) }, refine ⟨I, Ifin, V, V_in, _⟩, rwa [hV, ← union_Inter, union_eq_self_of_subset_right] }, { rintro ⟨I, Ifin, V, V_in, rfl⟩, exact mem_infi_of_Inter Ifin V_in subset.rfl } end lemma mem_infi' {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔ ∃ I : set ι, finite I ∧ ∃ V : ι → set α, (∀ i, V i ∈ s i) ∧ (∀ i ∉ I, V i = univ) ∧ (U = ⋂ i ∈ I, V i) ∧ U = ⋂ i, V i := begin simp only [mem_infi, set_coe.forall', bInter_eq_Inter], refine ⟨_, λ ⟨I, If, V, hVs, _, hVU, _⟩, ⟨I, If, λ i, V i, λ i, hVs i, hVU⟩⟩, rintro ⟨I, If, V, hV, rfl⟩, refine ⟨I, If, λ i, if hi : i ∈ I then V ⟨i, hi⟩ else univ, λ i, _, λ i hi, _, _⟩, { split_ifs, exacts [hV _, univ_mem] }, { exact dif_neg hi }, { simp only [Inter_dite, bInter_eq_Inter, dif_pos (subtype.coe_prop _), subtype.coe_eta, Inter_univ, inter_univ, eq_self_iff_true, true_and] } end lemma exists_Inter_of_mem_infi {ι : Type*} {α : Type*} {f : ι → filter α} {s} (hs : s ∈ ⨅ i, f i) : ∃ t : ι → set α, (∀ i, t i ∈ f i) ∧ s = ⋂ i, t i := let ⟨I, If, V, hVs, hV', hVU, hVU'⟩ := mem_infi'.1 hs in ⟨V, hVs, hVU'⟩ lemma mem_infi_of_fintype {ι : Type*} [fintype ι] {α : Type*} {f : ι → filter α} (s) : s ∈ (⨅ i, f i) ↔ ∃ t : ι → set α, (∀ i, t i ∈ f i) ∧ s = ⋂ i, t i := begin refine ⟨exists_Inter_of_mem_infi, _⟩, rintro ⟨t, ht, rfl⟩, exact Inter_mem.2 (λ i, mem_infi_of_mem i (ht i)) end @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ 𝓟 s ↔ s ∈ f := show (∀ {t}, s ⊆ t → t ∈ f) ↔ s ∈ f, from ⟨λ h, h (subset.refl s), λ hs t ht, mem_of_superset hs ht⟩ lemma principal_mono {s t : set α} : 𝓟 s ≤ 𝓟 t ↔ s ⊆ t := by simp only [le_principal_iff, iff_self, mem_principal] @[mono] lemma monotone_principal : monotone (𝓟 : set α → filter α) := λ _ _, principal_mono.2 @[simp] lemma principal_eq_iff_eq {s t : set α} : 𝓟 s = 𝓟 t ↔ s = t := by simp only [le_antisymm_iff, le_principal_iff, mem_principal]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (𝓟 s) = Sup s := rfl @[simp] lemma principal_univ : 𝓟 (univ : set α) = ⊤ := top_unique $ by simp only [le_principal_iff, mem_top, eq_self_iff_true] @[simp] lemma principal_empty : 𝓟 (∅ : set α) = ⊥ := bot_unique $ λ s _, empty_subset _ lemma generate_eq_binfi (S : set (set α)) : generate S = ⨅ s ∈ S, 𝓟 s := eq_of_forall_le_iff $ λ f, by simp [sets_iff_generate, le_principal_iff, subset_def] /-! ### Lattice equations -/ lemma empty_mem_iff_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ := ⟨λ h, bot_unique $ λ s _, mem_of_superset h (empty_subset s), λ h, h.symm ▸ mem_bot⟩ lemma nonempty_of_mem {f : filter α} [hf : ne_bot f] {s : set α} (hs : s ∈ f) : s.nonempty := s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_mem_iff_bot.mp hf.1)) id lemma ne_bot.nonempty_of_mem {f : filter α} (hf : ne_bot f) {s : set α} (hs : s ∈ f) : s.nonempty := @nonempty_of_mem α f hf s hs @[simp] lemma empty_not_mem (f : filter α) [ne_bot f] : ¬(∅ ∈ f) := λ h, (nonempty_of_mem h).ne_empty rfl lemma nonempty_of_ne_bot (f : filter α) [ne_bot f] : nonempty α := nonempty_of_exists $ nonempty_of_mem (univ_mem : univ ∈ f) lemma compl_not_mem {f : filter α} {s : set α} [ne_bot f] (h : s ∈ f) : sᶜ ∉ f := λ hsc, (nonempty_of_mem (inter_mem h hsc)).ne_empty $ inter_compl_self s lemma filter_eq_bot_of_is_empty [is_empty α] (f : filter α) : f = ⊥ := empty_mem_iff_bot.mp $ univ_mem' is_empty_elim protected lemma disjoint_iff {f g : filter α} : disjoint f g ↔ ∃ (s ∈ f) (t ∈ g), disjoint s t := by simp only [disjoint_iff, ← empty_mem_iff_bot, mem_inf_iff, inf_eq_inter, bot_eq_empty, @eq_comm _ ∅] lemma disjoint_of_disjoint_of_mem {f g : filter α} {s t : set α} (h : disjoint s t) (hs : s ∈ f) (ht : t ∈ g) : disjoint f g := filter.disjoint_iff.mpr ⟨s, hs, t, ht, h⟩ lemma inf_eq_bot_iff {f g : filter α} : f ⊓ g = ⊥ ↔ ∃ (U ∈ f) (V ∈ g), U ∩ V = ∅ := by simpa only [disjoint_iff] using filter.disjoint_iff /-- There is exactly one filter on an empty type. --/ -- TODO[gh-6025]: make this globally an instance once safe to do so local attribute [instance] protected def unique [is_empty α] : unique (filter α) := { default := ⊥, uniq := filter_eq_bot_of_is_empty } /-- There are only two filters on a `subsingleton`: `⊥` and `⊤`. If the type is empty, then they are equal. -/ lemma eq_top_of_ne_bot [subsingleton α] (l : filter α) [ne_bot l] : l = ⊤ := begin refine top_unique (λ s hs, _), obtain rfl : s = univ, from subsingleton.eq_univ_of_nonempty (nonempty_of_mem hs), exact univ_mem end lemma forall_mem_nonempty_iff_ne_bot {f : filter α} : (∀ (s : set α), s ∈ f → s.nonempty) ↔ ne_bot f := ⟨λ h, ⟨λ hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot)⟩, @nonempty_of_mem _ _⟩ lemma nontrivial_iff_nonempty : nontrivial (filter α) ↔ nonempty α := ⟨λ ⟨⟨f, g, hfg⟩⟩, by_contra $ λ h, hfg $ by haveI : is_empty α := not_nonempty_iff.1 h; exact subsingleton.elim _ _, λ ⟨x⟩, ⟨⟨⊤, ⊥, ne_bot.ne $ forall_mem_nonempty_iff_ne_bot.1 $ λ s hs, by rwa [mem_top.1 hs, ← nonempty_iff_univ_nonempty]⟩⟩⟩ lemma eq_Inf_of_mem_iff_exists_mem {S : set (filter α)} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S := le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩) (λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs) lemma eq_infi_of_mem_iff_exists_mem {f : ι → filter α} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) : l = infi f := eq_Inf_of_mem_iff_exists_mem $ λ s, h.trans exists_range_iff.symm lemma eq_binfi_of_mem_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) : l = ⨅ i (_ : p i), f i := begin rw [infi_subtype'], apply eq_infi_of_mem_iff_exists_mem, intro s, exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩ end lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) [ne : nonempty ι] : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem⟩, sets_of_superset := by simp only [mem_Union, exists_imp_distrib]; intros x y i hx hxy; exact ⟨i, mem_of_superset hx hxy⟩, inter_sets := begin simp only [mem_Union, exists_imp_distrib], intros x y a hx b hy, rcases h a b with ⟨c, ha, hb⟩, exact ⟨c, inter_mem (ha hx) (hb hy)⟩ end } in have u = infi f, from eq_infi_of_mem_iff_exists_mem (λ s, by simp only [filter.mem_mk, mem_Union, filter.mem_sets]), congr_arg filter.sets this.symm lemma mem_infi_of_directed {f : ι → filter α} (h : directed (≥) f) [nonempty ι] (s) : s ∈ infi f ↔ ∃ i, s ∈ f i := by simp only [← filter.mem_sets, infi_sets_eq h, mem_Union] lemma mem_binfi_of_directed {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} : t ∈ (⨅ i ∈ s, f i) ↔ ∃ i ∈ s, t ∈ f i := by haveI : nonempty {x // x ∈ s} := ne.to_subtype; erw [infi_subtype', mem_infi_of_directed h.directed_coe, subtype.exists]; refl lemma binfi_sets_eq {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) : (⨅ i ∈ s, f i).sets = ⋃ i ∈ s, (f i).sets := ext $ λ t, by simp [mem_binfi_of_directed h ne] lemma infi_sets_eq_finite {ι : Type*} (f : ι → filter α) : (⨅ i, f i).sets = (⋃ t : finset ι, (⨅ i ∈ t, f i).sets) := begin rw [infi_eq_infi_finset, infi_sets_eq], exact directed_of_sup (λ s₁ s₂, binfi_mono), end lemma infi_sets_eq_finite' (f : ι → filter α) : (⨅ i, f i).sets = (⋃ t : finset (plift ι), (⨅ i ∈ t, f (plift.down i)).sets) := by { rw [← infi_sets_eq_finite, ← equiv.plift.surjective.infi_comp], refl } lemma mem_infi_finite {ι : Type*} {f : ι → filter α} (s) : s ∈ infi f ↔ ∃ t : finset ι, s ∈ ⨅ i ∈ t, f i := (set.ext_iff.1 (infi_sets_eq_finite f) s).trans mem_Union lemma mem_infi_finite' {f : ι → filter α} (s) : s ∈ infi f ↔ ∃ t : finset (plift ι), s ∈ ⨅ i ∈ t, f (plift.down i) := (set.ext_iff.1 (infi_sets_eq_finite' f) s).trans mem_Union @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter.ext $ λ x, by simp only [mem_sup, mem_join] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆ x, join (f x)) = join (⨆ x, f x) := filter.ext $ λ x, by simp only [mem_supr, mem_join] instance : distrib_lattice (filter α) := { le_sup_inf := begin intros x y z s, simp only [and_assoc, mem_inf_iff, mem_sup, exists_prop, exists_imp_distrib, and_imp], rintro hs t₁ ht₁ t₂ ht₂ rfl, exact ⟨t₁, x.sets_of_superset hs (inter_subset_left t₁ t₂), ht₁, t₂, x.sets_of_superset hs (inter_subset_right t₁ t₂), ht₂, rfl⟩ end, ..filter.complete_lattice } -- The dual version does not hold! `filter α` is not a `complete_distrib_lattice`. -/ instance : coframe (filter α) := { Inf := Inf, infi_sup_le_sup_Inf := λ f s, begin rw [Inf_eq_infi', infi_subtype'], rintro t ⟨h₁, h₂⟩, rw infi_sets_eq_finite' at h₂, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂, obtain ⟨u, hu⟩ := h₂, suffices : (⨅ i, f ⊔ ↑i) ≤ f ⊔ u.inf (λ i, ↑i.down), { exact this ⟨h₁, hu⟩ }, refine finset.induction_on u (le_sup_of_le_right le_top) _, rintro ⟨i⟩ u _ ih, rw [finset.inf_insert, sup_inf_left], exact le_inf (infi_le _ _) ih, end, ..filter.complete_lattice } lemma mem_infi_finset {s : finset α} {f : α → filter β} {t : set β} : t ∈ (⨅ a ∈ s, f a) ↔ (∃ p : α → set β, (∀ a ∈ s, p a ∈ f a) ∧ t = ⋂ a ∈ s, p a) := begin simp only [← finset.set_bInter_coe, bInter_eq_Inter, infi_subtype'], refine ⟨λ h, _, _⟩, { rcases (mem_infi_of_fintype _).1 h with ⟨p, hp, rfl⟩, refine ⟨λ a, if h : a ∈ s then p ⟨a, h⟩ else univ, λ a ha, by simpa [ha] using hp ⟨a, ha⟩, _⟩, refine Inter_congr_of_surjective id surjective_id _, rintro ⟨a, ha⟩, simp [ha] }, { rintro ⟨p, hpf, rfl⟩, exact Inter_mem.2 (λ a, mem_infi_of_mem a (hpf a a.2)) } end /-- If `f : ι → filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/ lemma infi_ne_bot_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) (hb : ∀ i, ne_bot (f i)) : ne_bot (infi f) := ⟨begin intro h, have he : ∅ ∈ (infi f), from h.symm ▸ (mem_bot : ∅ ∈ (⊥ : filter α)), obtain ⟨i, hi⟩ : ∃ i, ∅ ∈ f i, from (mem_infi_of_directed hd ∅).1 he, exact (hb i).ne (empty_mem_iff_bot.1 hi) end⟩ /-- If `f : ι → filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/ lemma infi_ne_bot_of_directed {f : ι → filter α} [hn : nonempty α] (hd : directed (≥) f) (hb : ∀ i, ne_bot (f i)) : ne_bot (infi f) := if hι : nonempty ι then @infi_ne_bot_of_directed' _ _ _ hι hd hb else ⟨λ h : infi f = ⊥, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ λ i, false.elim $ hι ⟨i⟩) end, let ⟨x⟩ := hn in this (mem_univ x)⟩ lemma infi_ne_bot_iff_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) : ne_bot (infi f) ↔ ∀ i, ne_bot (f i) := ⟨λ H i, H.mono (infi_le _ i), infi_ne_bot_of_directed' hd⟩ lemma infi_ne_bot_iff_of_directed {f : ι → filter α} [nonempty α] (hd : directed (≥) f) : ne_bot (infi f) ↔ (∀ i, ne_bot (f i)) := ⟨λ H i, H.mono (infi_le _ i), infi_ne_bot_of_directed hd⟩ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop} (uni : p univ) (ins : ∀ {i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂)) : p s := begin rw [mem_infi_finite'] at hs, simp only [← finset.inf_eq_infi] at hs, rcases hs with ⟨is, his⟩, revert s, refine finset.induction_on is _ _, { intros s hs, rwa [mem_top.1 hs] }, { rintro ⟨i⟩ js his ih s hs, rw [finset.inf_insert, mem_inf_iff] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, rfl⟩, exact ins hs₁ (ih hs₂) } end /-! #### `principal` equations -/ @[simp] lemma inf_principal {s t : set α} : 𝓟 s ⊓ 𝓟 t = 𝓟 (s ∩ t) := le_antisymm (by simp only [le_principal_iff, mem_inf_iff]; exact ⟨s, subset.rfl, t, subset.rfl, rfl⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : 𝓟 s ⊔ 𝓟 t = 𝓟 (s ∪ t) := filter.ext $ λ u, by simp only [union_subset_iff, mem_sup, mem_principal] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆ x, 𝓟 (s x)) = 𝓟 (⋃ i, s i) := filter.ext $ λ x, by simp only [mem_supr, mem_principal, Union_subset_iff] @[simp] lemma principal_eq_bot_iff {s : set α} : 𝓟 s = ⊥ ↔ s = ∅ := empty_mem_iff_bot.symm.trans $ mem_principal.trans subset_empty_iff @[simp] lemma principal_ne_bot_iff {s : set α} : ne_bot (𝓟 s) ↔ s.nonempty := ne_bot_iff.trans $ (not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty lemma is_compl_principal (s : set α) : is_compl (𝓟 s) (𝓟 sᶜ) := ⟨by simp only [inf_principal, inter_compl_self, principal_empty, le_refl], by simp only [sup_principal, union_compl_self, principal_univ, le_refl]⟩ theorem mem_inf_principal' {f : filter α} {s t : set α} : s ∈ f ⊓ 𝓟 t ↔ tᶜ ∪ s ∈ f := by simp only [← le_principal_iff, (is_compl_principal s).le_left_iff, disjoint_assoc, inf_principal, ← (is_compl_principal (t ∩ sᶜ)).le_right_iff, compl_inter, compl_compl] theorem mem_inf_principal {f : filter α} {s t : set α} : s ∈ f ⊓ 𝓟 t ↔ {x | x ∈ t → x ∈ s} ∈ f := by { simp only [mem_inf_principal', imp_iff_not_or], refl } lemma supr_inf_principal (f : ι → filter α) (s : set α) : (⨆ i, f i ⊓ 𝓟 s) = (⨆ i, f i) ⊓ 𝓟 s := by { ext, simp only [mem_supr, mem_inf_principal] } lemma inf_principal_eq_bot {f : filter α} {s : set α} : f ⊓ 𝓟 s = ⊥ ↔ sᶜ ∈ f := by { rw [← empty_mem_iff_bot, mem_inf_principal], refl } lemma mem_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ 𝓟 sᶜ = ⊥) : s ∈ f := by rwa [inf_principal_eq_bot, compl_compl] at h lemma diff_mem_inf_principal_compl {f : filter α} {s : set α} (hs : s ∈ f) (t : set α) : s \ t ∈ f ⊓ 𝓟 tᶜ := inter_mem_inf hs $ mem_principal_self tᶜ lemma principal_le_iff {s : set α} {f : filter α} : 𝓟 s ≤ f ↔ ∀ V ∈ f, s ⊆ V := begin change (∀ V, V ∈ f → V ∈ _) ↔ _, simp_rw mem_principal, end @[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) : (⨅ i ∈ s, 𝓟 (f i)) = 𝓟 (⋂ i ∈ s, f i) := begin induction s using finset.induction_on with i s hi hs, { simp }, { rw [finset.infi_insert, finset.set_bInter_insert, hs, inf_principal] }, end @[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) : (⨅ i, 𝓟 (f i)) = 𝓟 (⋂ i, f i) := by simpa using infi_principal_finset finset.univ f lemma infi_principal_finite {ι : Type w} {s : set ι} (hs : finite s) (f : ι → set α) : (⨅ i ∈ s, 𝓟 (f i)) = 𝓟 (⋂ i ∈ s, f i) := begin lift s to finset ι using hs, exact_mod_cast infi_principal_finset s f end end lattice @[mono] lemma join_mono {f₁ f₂ : filter (filter α)} (h : f₁ ≤ f₂) : join f₁ ≤ join f₂ := λ s hs, h hs /-! ### Eventually -/ /-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x` means that `p` holds true for sufficiently large `x`. -/ protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r lemma eventually_iff {f : filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ {x | P x} ∈ f := iff.rfl @[simp] lemma eventually_mem_set {s : set α} {l : filter α} : (∀ᶠ x in l, x ∈ s) ↔ s ∈ l := iff.rfl protected lemma ext' {f₁ f₂ : filter α} (h : ∀ p : α → Prop, (∀ᶠ x in f₁, p x) ↔ (∀ᶠ x in f₂, p x)) : f₁ = f₂ := filter.ext h lemma eventually.filter_mono {f₁ f₂ : filter α} (h : f₁ ≤ f₂) {p : α → Prop} (hp : ∀ᶠ x in f₂, p x) : ∀ᶠ x in f₁, p x := h hp lemma eventually_of_mem {f : filter α} {P : α → Prop} {U : set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) : ∀ᶠ x in f, P x := mem_of_superset hU h protected lemma eventually.and {p q : α → Prop} {f : filter α} : f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x := inter_mem @[simp] lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem lemma eventually_of_forall {p : α → Prop} {f : filter α} (hp : ∀ x, p x) : ∀ᶠ x in f, p x := univ_mem' hp @[simp] lemma eventually_false_iff_eq_bot {f : filter α} : (∀ᶠ x in f, false) ↔ f = ⊥ := empty_mem_iff_bot @[simp] lemma eventually_const {f : filter α} [t : ne_bot f] {p : Prop} : (∀ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simp [h]) (λ h, by simpa [h] using t.ne) lemma eventually_iff_exists_mem {p : α → Prop} {f : filter α} : (∀ᶠ x in f, p x) ↔ ∃ v ∈ f, ∀ y ∈ v, p y := exists_mem_subset_iff.symm lemma eventually.exists_mem {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) : ∃ v ∈ f, ∀ y ∈ v, p y := eventually_iff_exists_mem.1 hp lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ᶠ x in f, p x → q x) : ∀ᶠ x in f, q x := mp_mem hp hq lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ x, p x → q x) : ∀ᶠ x in f, q x := hp.mp (eventually_of_forall hq) @[simp] lemma eventually_and {p q : α → Prop} {f : filter α} : (∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) := inter_mem_iff lemma eventually.congr {f : filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x) (h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x := h'.mp (h.mono $ λ x hx, hx.mp) lemma eventually_congr {f : filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) : (∀ᶠ x in f, p x) ↔ (∀ᶠ x in f, q x) := ⟨λ hp, hp.congr h, λ hq, hq.congr $ by simpa only [iff.comm] using h⟩ @[simp] lemma eventually_all {ι} [fintype ι] {l} {p : ι → α → Prop} : (∀ᶠ x in l, ∀ i, p i x) ↔ ∀ i, ∀ᶠ x in l, p i x := by simpa only [filter.eventually, set_of_forall] using Inter_mem @[simp] lemma eventually_all_finite {ι} {I : set ι} (hI : I.finite) {l} {p : ι → α → Prop} : (∀ᶠ x in l, ∀ i ∈ I, p i x) ↔ (∀ i ∈ I, ∀ᶠ x in l, p i x) := by simpa only [filter.eventually, set_of_forall] using bInter_mem hI alias eventually_all_finite ← set.finite.eventually_all attribute [protected] set.finite.eventually_all @[simp] lemma eventually_all_finset {ι} (I : finset ι) {l} {p : ι → α → Prop} : (∀ᶠ x in l, ∀ i ∈ I, p i x) ↔ ∀ i ∈ I, ∀ᶠ x in l, p i x := I.finite_to_set.eventually_all alias eventually_all_finset ← finset.eventually_all attribute [protected] finset.eventually_all @[simp] lemma eventually_or_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p ∨ q x) ↔ (p ∨ ∀ᶠ x in f, q x) := classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h]) @[simp] lemma eventually_or_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x ∨ q) ↔ ((∀ᶠ x in f, p x) ∨ q) := by simp only [or_comm _ q, eventually_or_distrib_left] @[simp] lemma eventually_imp_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p → q x) ↔ (p → ∀ᶠ x in f, q x) := by simp only [imp_iff_not_or, eventually_or_distrib_left] @[simp] lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩ @[simp] lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) := iff.rfl @[simp] lemma eventually_sup {p : α → Prop} {f g : filter α} : (∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) := iff.rfl @[simp] lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} : (∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) := iff.rfl @[simp] lemma eventually_supr {p : α → Prop} {fs : β → filter α} : (∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) := mem_supr @[simp] lemma eventually_principal {a : set α} {p : α → Prop} : (∀ᶠ x in 𝓟 a, p x) ↔ (∀ x ∈ a, p x) := iff.rfl lemma eventually_inf {f g : filter α} {p : α → Prop} : (∀ᶠ x in f ⊓ g, p x) ↔ ∃ (s ∈ f) (t ∈ g), ∀ x ∈ s ∩ t, p x := mem_inf_iff_superset theorem eventually_inf_principal {f : filter α} {p : α → Prop} {s : set α} : (∀ᶠ x in f ⊓ 𝓟 s, p x) ↔ ∀ᶠ x in f, x ∈ s → p x := mem_inf_principal /-! ### Frequently -/ /-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x` means that there exist arbitrarily large `x` for which `p` holds true. -/ protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r lemma eventually.frequently {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ᶠ x in f, p x := compl_not_mem h lemma frequently_of_forall {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ x, p x) : ∃ᶠ x in f, p x := eventually.frequently (eventually_of_forall h) lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ᶠ x in f, p x → q x) : ∃ᶠ x in f, q x := mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h lemma frequently.filter_mono {p : α → Prop} {f g : filter α} (h : ∃ᶠ x in f, p x) (hle : f ≤ g) : ∃ᶠ x in g, p x := mt (λ h', h'.filter_mono hle) h lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ x, p x → q x) : ∃ᶠ x in f, q x := h.mp (eventually_of_forall hpq) lemma frequently.and_eventually {p q : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := begin refine mt (λ h, hq.mp $ h.mono _) hp, exact λ x hpq hq hp, hpq ⟨hp, hq⟩ end lemma eventually.and_frequently {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∃ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := by simpa only [and.comm] using hq.and_eventually hp lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x := begin by_contradiction H, replace H : ∀ᶠ x in f, ¬ p x, from eventually_of_forall (not_exists.1 H), exact hp H end lemma eventually.exists {p : α → Prop} {f : filter α} [ne_bot f] (hp : ∀ᶠ x in f, p x) : ∃ x, p x := hp.frequently.exists lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} : (∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x := ⟨λ hp q hq, (hp.and_eventually hq).exists, λ H hp, by simpa only [and_not_self, exists_false] using H hp⟩ lemma frequently_iff {f : filter α} {P : α → Prop} : (∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x := begin simp only [frequently_iff_forall_eventually_exists_and, exists_prop, and_comm (P _)], refl end @[simp] lemma not_eventually {p : α → Prop} {f : filter α} : (¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) := by simp [filter.frequently] @[simp] lemma not_frequently {p : α → Prop} {f : filter α} : (¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) := by simp only [filter.frequently, not_not] @[simp] lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ ne_bot f := by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot, ne_bot_iff] @[simp] lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp @[simp] lemma frequently_const {f : filter α} [ne_bot f] {p : Prop} : (∃ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simpa [h]) (λ h, by simp [h]) @[simp] lemma frequently_or_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in f, q x) := by simp only [filter.frequently, ← not_and_distrib, not_or_distrib, eventually_and] lemma frequently_or_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∨ q x) ↔ (p ∨ ∃ᶠ x in f, q x) := by simp lemma frequently_or_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q := by simp @[simp] lemma frequently_imp_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x → q x) ↔ ((∀ᶠ x in f, p x) → ∃ᶠ x in f, q x) := by simp [imp_iff_not_or, not_eventually, frequently_or_distrib] lemma frequently_imp_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p → q x) ↔ (p → ∃ᶠ x in f, q x) := by simp lemma frequently_imp_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x → q) ↔ ((∀ᶠ x in f, p x) → q) := by simp @[simp] lemma eventually_imp_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x → q) ↔ ((∃ᶠ x in f, p x) → q) := by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently] @[simp] lemma frequently_and_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∧ q x) ↔ (p ∧ ∃ᶠ x in f, q x) := by simp only [filter.frequently, not_and, eventually_imp_distrib_left, not_imp] @[simp] lemma frequently_and_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∧ q) ↔ ((∃ᶠ x in f, p x) ∧ q) := by simp only [and_comm _ q, frequently_and_distrib_left] @[simp] lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp @[simp] lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) := by simp [filter.frequently] @[simp] lemma frequently_principal {a : set α} {p : α → Prop} : (∃ᶠ x in 𝓟 a, p x) ↔ (∃ x ∈ a, p x) := by simp [filter.frequently, not_forall] lemma frequently_sup {p : α → Prop} {f g : filter α} : (∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) := by simp only [filter.frequently, eventually_sup, not_and_distrib] @[simp] lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} : (∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) := by simp [filter.frequently, -not_eventually, not_forall] @[simp] lemma frequently_supr {p : α → Prop} {fs : β → filter α} : (∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) := by simp [filter.frequently, -not_eventually, not_forall] /-! ### Relation “eventually equal” -/ /-- Two functions `f` and `g` are *eventually equal* along a filter `l` if the set of `x` such that `f x = g x` belongs to `l`. -/ def eventually_eq (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x = g x notation f ` =ᶠ[`:50 l:50 `] `:0 g:50 := eventually_eq l f g lemma eventually_eq.eventually {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∀ᶠ x in l, f x = g x := h lemma eventually_eq.rw {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (p : α → β → Prop) (hf : ∀ᶠ x in l, p x (f x)) : ∀ᶠ x in l, p x (g x) := hf.congr $ h.mono $ λ x hx, hx ▸ iff.rfl lemma eventually_eq_set {s t : set α} {l : filter α} : s =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ s ↔ x ∈ t := eventually_congr $ eventually_of_forall $ λ x, ⟨eq.to_iff, iff.to_eq⟩ alias eventually_eq_set ↔ filter.eventually_eq.mem_iff filter.eventually.set_eq @[simp] lemma eventually_eq_univ {s : set α} {l : filter α} : s =ᶠ[l] univ ↔ s ∈ l := by simp [eventually_eq_set] lemma eventually_eq.exists_mem {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∃ s ∈ l, eq_on f g s := h.exists_mem lemma eventually_eq_of_mem {l : filter α} {f g : α → β} {s : set α} (hs : s ∈ l) (h : eq_on f g s) : f =ᶠ[l] g := eventually_of_mem hs h lemma eventually_eq_iff_exists_mem {l : filter α} {f g : α → β} : (f =ᶠ[l] g) ↔ ∃ s ∈ l, eq_on f g s := eventually_iff_exists_mem lemma eventually_eq.filter_mono {l l' : filter α} {f g : α → β} (h₁ : f =ᶠ[l] g) (h₂ : l' ≤ l) : f =ᶠ[l'] g := h₂ h₁ @[refl] lemma eventually_eq.refl (l : filter α) (f : α → β) : f =ᶠ[l] f := eventually_of_forall $ λ x, rfl lemma eventually_eq.rfl {l : filter α} {f : α → β} : f =ᶠ[l] f := eventually_eq.refl l f @[symm] lemma eventually_eq.symm {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) : g =ᶠ[l] f := H.mono $ λ _, eq.symm @[trans] lemma eventually_eq.trans {l : filter α} {f g h : α → β} (H₁ : f =ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f =ᶠ[l] h := H₂.rw (λ x y, f x = y) H₁ lemma eventually_eq.prod_mk {l} {f f' : α → β} (hf : f =ᶠ[l] f') {g g' : α → γ} (hg : g =ᶠ[l] g') : (λ x, (f x, g x)) =ᶠ[l] (λ x, (f' x, g' x)) := hf.mp $ hg.mono $ by { intros, simp only * } lemma eventually_eq.fun_comp {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) (h : β → γ) : (h ∘ f) =ᶠ[l] (h ∘ g) := H.mono $ λ x hx, congr_arg h hx lemma eventually_eq.comp₂ {δ} {f f' : α → β} {g g' : α → γ} {l} (Hf : f =ᶠ[l] f') (h : β → γ → δ) (Hg : g =ᶠ[l] g') : (λ x, h (f x) (g x)) =ᶠ[l] (λ x, h (f' x) (g' x)) := (Hf.prod_mk Hg).fun_comp (uncurry h) @[to_additive] lemma eventually_eq.mul [has_mul β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x * f' x) =ᶠ[l] (λ x, g x * g' x)) := h.comp₂ (*) h' @[to_additive] lemma eventually_eq.inv [has_inv β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) : ((λ x, (f x)⁻¹) =ᶠ[l] (λ x, (g x)⁻¹)) := h.fun_comp has_inv.inv @[to_additive] lemma eventually_eq.div [has_div β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x / f' x) =ᶠ[l] (λ x, g x / g' x)) := h.comp₂ (/) h' @[to_additive] lemma eventually_eq.const_smul {𝕜} [has_scalar 𝕜 β] {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (c : 𝕜) : (λ x, c • f x) =ᶠ[l] (λ x, c • g x) := h.fun_comp (λ x, c • x) @[to_additive] lemma eventually_eq.smul {𝕜} [has_scalar 𝕜 β] {l : filter α} {f f' : α → 𝕜} {g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : (λ x, f x • g x) =ᶠ[l] λ x, f' x • g' x := hf.comp₂ (•) hg lemma eventually_eq.sup [has_sup β] {l : filter α} {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : (λ x, f x ⊔ g x) =ᶠ[l] λ x, f' x ⊔ g' x := hf.comp₂ (⊔) hg lemma eventually_eq.inf [has_inf β] {l : filter α} {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : (λ x, f x ⊓ g x) =ᶠ[l] λ x, f' x ⊓ g' x := hf.comp₂ (⊓) hg lemma eventually_eq.preimage {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (s : set β) : f ⁻¹' s =ᶠ[l] g ⁻¹' s := h.fun_comp s lemma eventually_eq.inter {s t s' t' : set α} {l : filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∩ s' : set α) =ᶠ[l] (t ∩ t' : set α) := h.comp₂ (∧) h' lemma eventually_eq.union {s t s' t' : set α} {l : filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∪ s' : set α) =ᶠ[l] (t ∪ t' : set α) := h.comp₂ (∨) h' lemma eventually_eq.compl {s t : set α} {l : filter α} (h : s =ᶠ[l] t) : (sᶜ : set α) =ᶠ[l] (tᶜ : set α) := h.fun_comp not lemma eventually_eq.diff {s t s' t' : set α} {l : filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s \ s' : set α) =ᶠ[l] (t \ t' : set α) := h.inter h'.compl lemma eventually_eq_empty {s : set α} {l : filter α} : s =ᶠ[l] (∅ : set α) ↔ ∀ᶠ x in l, x ∉ s := eventually_eq_set.trans $ by simp lemma inter_eventually_eq_left {s t : set α} {l : filter α} : (s ∩ t : set α) =ᶠ[l] s ↔ ∀ᶠ x in l, x ∈ s → x ∈ t := by simp only [eventually_eq_set, mem_inter_eq, and_iff_left_iff_imp] lemma inter_eventually_eq_right {s t : set α} {l : filter α} : (s ∩ t : set α) =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ t → x ∈ s := by rw [inter_comm, inter_eventually_eq_left] @[simp] lemma eventually_eq_principal {s : set α} {f g : α → β} : f =ᶠ[𝓟 s] g ↔ eq_on f g s := iff.rfl lemma eventually_eq_inf_principal_iff {F : filter α} {s : set α} {f g : α → β} : (f =ᶠ[F ⊓ 𝓟 s] g) ↔ ∀ᶠ x in F, x ∈ s → f x = g x := eventually_inf_principal lemma eventually_eq.sub_eq [add_group β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) : f - g =ᶠ[l] 0 := by simpa using (eventually_eq.sub (eventually_eq.refl l f) h).symm lemma eventually_eq_iff_sub [add_group β] {f g : α → β} {l : filter α} : f =ᶠ[l] g ↔ f - g =ᶠ[l] 0 := ⟨λ h, h.sub_eq, λ h, by simpa using h.add (eventually_eq.refl l g)⟩ section has_le variables [has_le β] {l : filter α} /-- A function `f` is eventually less than or equal to a function `g` at a filter `l`. -/ def eventually_le (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x ≤ g x notation f ` ≤ᶠ[`:50 l:50 `] `:0 g:50 := eventually_le l f g lemma eventually_le.congr {f f' g g' : α → β} (H : f ≤ᶠ[l] g) (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f' ≤ᶠ[l] g' := H.mp $ hg.mp $ hf.mono $ λ x hf hg H, by rwa [hf, hg] at H lemma eventually_le_congr {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f ≤ᶠ[l] g ↔ f' ≤ᶠ[l] g' := ⟨λ H, H.congr hf hg, λ H, H.congr hf.symm hg.symm⟩ end has_le section preorder variables [preorder β] {l : filter α} {f g h : α → β} lemma eventually_eq.le (h : f =ᶠ[l] g) : f ≤ᶠ[l] g := h.mono $ λ x, le_of_eq @[refl] lemma eventually_le.refl (l : filter α) (f : α → β) : f ≤ᶠ[l] f := eventually_eq.rfl.le lemma eventually_le.rfl : f ≤ᶠ[l] f := eventually_le.refl l f @[trans] lemma eventually_le.trans (H₁ : f ≤ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₂.mp $ H₁.mono $ λ x, le_trans @[trans] lemma eventually_eq.trans_le (H₁ : f =ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₁.le.trans H₂ @[trans] lemma eventually_le.trans_eq (H₁ : f ≤ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f ≤ᶠ[l] h := H₁.trans H₂.le end preorder lemma eventually_le.antisymm [partial_order β] {l : filter α} {f g : α → β} (h₁ : f ≤ᶠ[l] g) (h₂ : g ≤ᶠ[l] f) : f =ᶠ[l] g := h₂.mp $ h₁.mono $ λ x, le_antisymm lemma eventually_le_antisymm_iff [partial_order β] {l : filter α} {f g : α → β} : f =ᶠ[l] g ↔ f ≤ᶠ[l] g ∧ g ≤ᶠ[l] f := by simp only [eventually_eq, eventually_le, le_antisymm_iff, eventually_and] lemma eventually_le.le_iff_eq [partial_order β] {l : filter α} {f g : α → β} (h : f ≤ᶠ[l] g) : g ≤ᶠ[l] f ↔ g =ᶠ[l] f := ⟨λ h', h'.antisymm h, eventually_eq.le⟩ lemma eventually.ne_of_lt [preorder β] {l : filter α} {f g : α → β} (h : ∀ᶠ x in l, f x < g x) : ∀ᶠ x in l, f x ≠ g x := h.mono (λ x hx, hx.ne) lemma eventually.ne_top_of_lt [partial_order β] [order_top β] {l : filter α} {f g : α → β} (h : ∀ᶠ x in l, f x < g x) : ∀ᶠ x in l, f x ≠ ⊤ := h.mono (λ x hx, hx.ne_top) lemma eventually.lt_top_of_ne [partial_order β] [order_top β] {l : filter α} {f : α → β} (h : ∀ᶠ x in l, f x ≠ ⊤) : ∀ᶠ x in l, f x < ⊤ := h.mono (λ x hx, hx.lt_top) lemma eventually.lt_top_iff_ne_top [partial_order β] [order_top β] {l : filter α} {f : α → β} : (∀ᶠ x in l, f x < ⊤) ↔ ∀ᶠ x in l, f x ≠ ⊤ := ⟨eventually.ne_of_lt, eventually.lt_top_of_ne⟩ @[mono] lemma eventually_le.inter {s t s' t' : set α} {l : filter α} (h : s ≤ᶠ[l] t) (h' : s' ≤ᶠ[l] t') : (s ∩ s' : set α) ≤ᶠ[l] (t ∩ t' : set α) := h'.mp $ h.mono $ λ x, and.imp @[mono] lemma eventually_le.union {s t s' t' : set α} {l : filter α} (h : s ≤ᶠ[l] t) (h' : s' ≤ᶠ[l] t') : (s ∪ s' : set α) ≤ᶠ[l] (t ∪ t' : set α) := h'.mp $ h.mono $ λ x, or.imp @[mono] lemma eventually_le.compl {s t : set α} {l : filter α} (h : s ≤ᶠ[l] t) : (tᶜ : set α) ≤ᶠ[l] (sᶜ : set α) := h.mono $ λ x, mt @[mono] lemma eventually_le.diff {s t s' t' : set α} {l : filter α} (h : s ≤ᶠ[l] t) (h' : t' ≤ᶠ[l] s') : (s \ s' : set α) ≤ᶠ[l] (t \ t' : set α) := h.inter h'.compl lemma join_le {f : filter (filter α)} {l : filter α} (h : ∀ᶠ m in f, m ≤ l) : join f ≤ l := λ s hs, h.mono $ λ m hm, hm hs /-! ### Push-forwards, pull-backs, and the monad structure -/ section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, univ_sets := univ_mem, sets_of_superset := λ s t hs st, mem_of_superset hs $ preimage_mono st, inter_sets := λ s t hs ht, inter_mem hs ht } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (𝓟 s) = 𝓟 (set.image f s) := filter.ext $ λ a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma eventually_map {P : β → Prop} : (∀ᶠ b in map m f, P b) ↔ ∀ᶠ a in f, P (m a) := iff.rfl @[simp] lemma frequently_map {P : β → Prop} : (∃ᶠ b in map m f, P b) ↔ ∃ᶠ a in f, P (m a) := iff.rfl @[simp] lemma mem_map : t ∈ map m f ↔ m ⁻¹' t ∈ f := iff.rfl lemma mem_map' : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f := f.sets_of_superset hs $ subset_preimage_image m s lemma image_mem_map_iff (hf : injective m) : m '' s ∈ map m f ↔ s ∈ f := ⟨λ h, by rwa [← preimage_image_eq s hf], image_mem_map⟩ lemma range_mem_map : range m ∈ map m f := by { rw ←image_univ, exact image_mem_map univ_mem } lemma mem_map_iff_exists_image : t ∈ map m f ↔ (∃ s ∈ f, m '' s ⊆ t) := ⟨λ ht, ⟨m ⁻¹' t, ht, image_preimage_subset _ _⟩, λ ⟨s, hs, ht⟩, mem_of_superset (image_mem_map hs) ht⟩ @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_id' : filter.map (λ x, x) f = f := map_id @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ λ _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f /-- If functions `m₁` and `m₂` are eventually equal at a filter `f`, then they map this filter to the same filter. -/ lemma map_congr {m₁ m₂ : α → β} {f : filter α} (h : m₁ =ᶠ[f] m₂) : map m₁ f = map m₂ f := filter.ext' $ λ p, by { simp only [eventually_map], exact eventually_congr (h.mono $ λ x hx, hx ▸ iff.rfl) } end map section comap /-- The inverse map of a filter. A set `s` belongs to `filter.comap f l` if either of the following equivalent conditions hold. 1. There exists a set `t ∈ l` such that `f ⁻¹' t ⊆ s`. This is used as a definition. 2. The set `{y | ∀ x, f x = y → x ∈ s}` belongs to `l`, see `filter.mem_comap'`. 3. The set `(f '' sᶜ)ᶜ` belongs to `l`, see `filter.mem_comap_iff_compl` and `filter.compl_mem_comap`. -/ def comap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃ t ∈ f, m ⁻¹' t ⊆ s }, univ_sets := ⟨univ, univ_mem, by simp only [subset_univ, preimage_univ]⟩, sets_of_superset := λ a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', ma'a.trans ab⟩, inter_sets := λ a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ } variables {f : α → β} {l : filter β} {p : α → Prop} {s : set α} lemma mem_comap' : s ∈ comap f l ↔ {y | ∀ ⦃x⦄, f x = y → x ∈ s} ∈ l := ⟨λ ⟨t, ht, hts⟩, mem_of_superset ht $ λ y hy x hx, hts $ mem_preimage.2 $ by rwa hx, λ h, ⟨_, h, λ x hx, hx rfl⟩⟩ @[simp] lemma eventually_comap : (∀ᶠ a in comap f l, p a) ↔ ∀ᶠ b in l, ∀ a, f a = b → p a := mem_comap' @[simp] lemma frequently_comap : (∃ᶠ a in comap f l, p a) ↔ ∃ᶠ b in l, ∃ a, f a = b ∧ p a := by simp only [filter.frequently, eventually_comap, not_exists, not_and] lemma mem_comap_iff_compl : s ∈ comap f l ↔ (f '' sᶜ)ᶜ ∈ l := by simp only [mem_comap', compl_def, mem_image, mem_set_of_eq, not_exists, not_and', not_not] lemma compl_mem_comap : sᶜ ∈ comap f l ↔ (f '' s)ᶜ ∈ l := by rw [mem_comap_iff_compl, compl_compl] end comap /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the applicative instance. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) /-- The applicative sequentiation operation. This is not induced by the bind operation. -/ def seq (f : filter (α → β)) (g : filter α) : filter β := ⟨{ s | ∃ u ∈ f, ∃ t ∈ g, (∀ m ∈ u, ∀ x ∈ t, (m : α → β) x ∈ s) }, ⟨univ, univ_mem, univ, univ_mem, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩, λ s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, λ x hx y hy, hst $ h _ hx _ hy⟩, λ s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩, ⟨t₀ ∩ u₀, inter_mem ht₀ hu₀, t₁ ∩ u₁, inter_mem ht₁ hu₁, λ x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩ /-- `pure x` is the set of sets that contain `x`. It is equal to `𝓟 {x}` but with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/ instance : has_pure filter := ⟨λ (α : Type u) x, { sets := {s | x ∈ s}, inter_sets := λ s t, and.intro, sets_of_superset := λ s t hs hst, hst hs, univ_sets := trivial }⟩ instance : has_bind filter := ⟨@filter.bind⟩ instance : has_seq filter := ⟨@filter.seq⟩ instance : functor filter := { map := @filter.map } lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl @[simp] lemma mem_pure {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl @[simp] lemma eventually_pure {a : α} {p : α → Prop} : (∀ᶠ x in pure a, p x) ↔ p a := iff.rfl @[simp] lemma principal_singleton (a : α) : 𝓟 {a} = pure a := filter.ext $ λ s, by simp only [mem_pure, mem_principal, singleton_subset_iff] @[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) := rfl @[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl @[simp] lemma pure_bind (a : α) (m : α → filter β) : bind (pure a) m = m a := by simp only [has_bind.bind, bind, map_pure, join_pure] section -- this section needs to be before applicative, otherwise the wrong instance will be chosen /-- The monad structure on filters. -/ protected def monad : monad filter := { map := @filter.map } local attribute [instance] filter.monad protected lemma is_lawful_monad : is_lawful_monad filter := { id_map := λ α f, filter_eq rfl, pure_bind := λ α β, pure_bind, bind_assoc := λ α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := λ α β f x, filter.ext $ λ s, by simp only [has_bind.bind, bind, functor.map, mem_map', mem_join, mem_set_of_eq, comp, mem_pure] } end instance : applicative filter := { map := @filter.map, seq := @filter.seq } instance : alternative filter := { failure := λ α, ⊥, orelse := λ α x y, x ⊔ y } @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl /-! #### `map` and `comap` equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_comap : s ∈ comap m g ↔ ∃ t ∈ g, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g := ⟨t, ht, subset.rfl⟩ lemma eventually.comap {p : β → Prop} (hf : ∀ᶠ b in g, p b) (f : α → β) : ∀ᶠ a in comap f g, p (f a) := preimage_mem_comap hf lemma comap_id : comap id f = f := le_antisymm (λ s, preimage_mem_comap) (λ s ⟨t, ht, hst⟩, mem_of_superset ht hst) lemma comap_const_of_not_mem {x : β} (ht : t ∈ g) (hx : x ∉ t) : comap (λ y : α, x) g = ⊥ := empty_mem_iff_bot.1 $ mem_comap'.2 $ mem_of_superset ht $ λ x' hx' y h, hx $ h.symm ▸ hx' lemma comap_const_of_mem {x : β} (h : ∀ t ∈ g, x ∈ t) : comap (λ y : α, x) g = ⊤ := top_unique $ λ s hs, univ_mem' $ λ y, h _ (mem_comap'.1 hs) rfl lemma map_const [ne_bot f] {c : β} : f.map (λ x, c) = pure c := by { ext s, by_cases h : c ∈ s; simp [h] } lemma comap_comap {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f := filter.coext $ λ s, by simp only [compl_mem_comap, image_image] section comm variables {δ : Type*} /-! The variables in the following lemmas are used as in this diagram: ``` φ α → β θ ↓ ↓ ψ γ → δ ρ ``` -/ variables {φ : α → β} {θ : α → γ} {ψ : β → δ} {ρ : γ → δ} (H : ψ ∘ φ = ρ ∘ θ) include H lemma map_comm (F : filter α) : map ψ (map φ F) = map ρ (map θ F) := by rw [filter.map_map, H, ← filter.map_map] lemma comap_comm (G : filter δ) : comap φ (comap ψ G) = comap θ (comap ρ G) := by rw [filter.comap_comap, H, ← filter.comap_comap] end comm @[simp] theorem comap_principal {t : set β} : comap m (𝓟 t) = 𝓟 (m ⁻¹' t) := filter.ext $ λ s, ⟨λ ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, (preimage_mono hu).trans b, λ h, ⟨t, subset.refl t, h⟩⟩ @[simp] theorem comap_pure {b : β} : comap m (pure b) = 𝓟 (m ⁻¹' {b}) := by rw [← principal_singleton, comap_principal] lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g := ⟨λ h s ⟨t, ht, hts⟩, mem_of_superset (h ht) hts, λ h s ht, h ⟨_, ht, subset.rfl⟩⟩ lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) := λ f g, map_le_iff_le_comap @[mono] lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l @[mono] lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆ i, f i) = (⨆ i, map m (f i)) := (gc_map_comap m).l_supr @[simp] lemma map_top (f : α → β) : map f ⊤ = 𝓟 (range f) := by rw [← principal_univ, map_principal, image_univ] @[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top @[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf @[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅ i, f i) = (⨅ i, comap m (f i)) := (gc_map_comap m).u_infi lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ := by { rw [comap_top], exact le_top } lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _ lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _ @[simp] lemma comap_bot : comap m ⊥ = ⊥ := bot_unique $ λ s _, ⟨∅, by simp only [mem_bot], by simp only [empty_subset, preimage_empty]⟩ lemma disjoint_comap (h : disjoint g₁ g₂) : disjoint (comap m g₁) (comap m g₂) := by simp only [disjoint_iff, ← comap_inf, h.eq_bot, comap_bot] lemma comap_supr {ι} {f : ι → filter β} {m : α → β} : comap m (supr f) = (⨆ i, comap m (f i)) := le_antisymm (λ s hs, have ∀ i, ∃ t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap, exists_prop, mem_supr] using mem_supr.1 hs, let ⟨t, ht⟩ := classical.axiom_of_choice this in ⟨⋃ i, t i, mem_supr.2 $ λ i, (f i).sets_of_superset (ht i).1 (subset_Union _ _), begin rw [preimage_Union, Union_subset_iff], exact λ i, (ht i).2 end⟩) (supr_le $ λ i, comap_mono $ le_supr _ _) lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆ f ∈ s, comap m f) := by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true] lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ := by rw [sup_eq_supr, comap_supr, supr_bool_eq, bool.cond_tt, bool.cond_ff] lemma map_comap (f : filter β) (m : α → β) : (f.comap m).map m = f ⊓ 𝓟 (range m) := begin refine le_antisymm (le_inf map_comap_le $ le_principal_iff.2 range_mem_map) _, rintro t' ⟨t, ht, sub⟩, refine mem_inf_principal.2 (mem_of_superset ht _), rintro _ hxt ⟨x, rfl⟩, exact sub hxt end lemma map_comap_of_mem {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f := by rw [map_comap, inf_eq_left.2 (le_principal_iff.2 hf)] instance [can_lift α β] : can_lift (filter α) (filter β) := { coe := map can_lift.coe, cond := λ f, ∀ᶠ x : α in f, can_lift.cond β x, prf := λ f hf, ⟨comap can_lift.coe f, map_comap_of_mem $ hf.mono can_lift.prf⟩ } lemma comap_le_comap_iff {f g : filter β} {m : α → β} (hf : range m ∈ f) : comap m f ≤ comap m g ↔ f ≤ g := ⟨λ h, map_comap_of_mem hf ▸ (map_mono h).trans map_comap_le, λ h, comap_mono h⟩ theorem map_comap_of_surjective {f : α → β} (hf : surjective f) (l : filter β) : map f (comap f l) = l := map_comap_of_mem $ by simp only [hf.range_eq, univ_mem] lemma _root_.function.surjective.filter_map_top {f : α → β} (hf : surjective f) : map f ⊤ = ⊤ := (congr_arg _ comap_top).symm.trans $ map_comap_of_surjective hf ⊤ lemma subtype_coe_map_comap (s : set α) (f : filter α) : map (coe : s → α) (comap (coe : s → α) f) = f ⊓ 𝓟 s := by rw [map_comap, subtype.range_coe] lemma subtype_coe_map_comap_prod (s : set α) (f : filter (α × α)) : map (coe : s × s → α × α) (comap (coe : s × s → α × α) f) = f ⊓ 𝓟 (s ×ˢ s) := have (coe : s × s → α × α) = (λ x, (x.1, x.2)), by ext ⟨x, y⟩; refl, by simp [this, map_comap, ← prod_range_range_eq] lemma image_mem_of_mem_comap {f : filter α} {c : β → α} (h : range c ∈ f) {W : set β} (W_in : W ∈ comap c f) : c '' W ∈ f := begin rw ← map_comap_of_mem h, exact image_mem_map W_in end lemma image_coe_mem_of_mem_comap {f : filter α} {U : set α} (h : U ∈ f) {W : set U} (W_in : W ∈ comap (coe : U → α) f) : coe '' W ∈ f := image_mem_of_mem_comap (by simp [h]) W_in lemma comap_map {f : filter α} {m : α → β} (h : injective m) : comap m (map m f) = f := le_antisymm (λ s hs, mem_of_superset (preimage_mem_comap $ image_mem_map hs) $ by simp only [preimage_image_eq s h]) le_comap_map lemma mem_comap_iff {f : filter β} {m : α → β} (inj : injective m) (large : set.range m ∈ f) {S : set α} : S ∈ comap m f ↔ m '' S ∈ f := by rw [← image_mem_map_iff inj, map_comap_of_mem large] lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀ x ∈ s, ∀ y ∈ s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := λ t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem hsg ht)] using λ _ has ⟨_, ⟨hbs, hb⟩, h⟩, hm _ hbs _ has h ▸ hb lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀ x ∈ s, ∀ y ∈ s, m x = m y → x = y) : map m f ≤ map m g ↔ f ≤ g := iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h) lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀ x ∈ s, ∀ y ∈ s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : injective m) (h : map m f = map m g) : f = g := have comap m (map m f) = comap m (map m g), by rw h, by rwa [comap_map hm, comap_map hm] at this lemma comap_ne_bot_iff {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ ∀ t ∈ f, ∃ a, m a ∈ t := begin rw ← forall_mem_nonempty_iff_ne_bot, exact ⟨λ h t t_in, h (m ⁻¹' t) ⟨t, t_in, subset.rfl⟩, λ h s ⟨u, u_in, hu⟩, let ⟨x, hx⟩ := h u u_in in ⟨x, hu hx⟩⟩, end lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀ t ∈ f, ∃ a, m a ∈ t) : ne_bot (comap m f) := comap_ne_bot_iff.mpr hm lemma comap_ne_bot_iff_frequently {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ ∃ᶠ y in f, y ∈ range m := by simp [comap_ne_bot_iff, frequently_iff, ← exists_and_distrib_left, and.comm] lemma comap_ne_bot_iff_compl_range {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ (range m)ᶜ ∉ f := comap_ne_bot_iff_frequently lemma ne_bot.comap_of_range_mem {f : filter β} {m : α → β} (hf : ne_bot f) (hm : range m ∈ f) : ne_bot (comap m f) := comap_ne_bot_iff_frequently.2 $ eventually.frequently hm @[simp] lemma comap_fst_ne_bot_iff {f : filter α} : (f.comap (prod.fst : α × β → α)).ne_bot ↔ f.ne_bot ∧ nonempty β := begin casesI is_empty_or_nonempty β, { rw [filter_eq_bot_of_is_empty (f.comap _), ← not_iff_not]; [simp *, apply_instance] }, { simp [comap_ne_bot_iff_frequently, h] } end @[instance] lemma comap_fst_ne_bot [nonempty β] {f : filter α} [ne_bot f] : (f.comap (prod.fst : α × β → α)).ne_bot := comap_fst_ne_bot_iff.2 ⟨‹_›, ‹_›⟩ @[simp] lemma comap_snd_ne_bot_iff {f : filter β} : (f.comap (prod.snd : α × β → β)).ne_bot ↔ nonempty α ∧ f.ne_bot := begin casesI is_empty_or_nonempty α with hα hα, { rw [filter_eq_bot_of_is_empty (f.comap _), ← not_iff_not]; [simpa using hα.elim, apply_instance] }, { simp [comap_ne_bot_iff_frequently, hα] } end @[instance] lemma comap_snd_ne_bot [nonempty α] {f : filter β} [ne_bot f] : (f.comap (prod.snd : α × β → β)).ne_bot := comap_snd_ne_bot_iff.2 ⟨‹_›, ‹_›⟩ lemma comap_eval_ne_bot_iff' {ι : Type*} {α : ι → Type*} {i : ι} {f : filter (α i)} : (comap (eval i) f).ne_bot ↔ (∀ j, nonempty (α j)) ∧ ne_bot f := begin casesI is_empty_or_nonempty (Π j, α j) with H H, { rw [filter_eq_bot_of_is_empty (f.comap _), ← not_iff_not]; [skip, assumption], simpa [← classical.nonempty_pi] using H.elim }, { haveI : ∀ j, nonempty (α j), from classical.nonempty_pi.1 H, simp [comap_ne_bot_iff_frequently, *] } end @[simp] lemma comap_eval_ne_bot_iff {ι : Type*} {α : ι → Type*} [∀ j, nonempty (α j)] {i : ι} {f : filter (α i)} : (comap (eval i) f).ne_bot ↔ ne_bot f := by simp [comap_eval_ne_bot_iff', *] @[instance] lemma comap_eval_ne_bot {ι : Type*} {α : ι → Type*} [∀ j, nonempty (α j)] (i : ι) (f : filter (α i)) [ne_bot f] : (comap (eval i) f).ne_bot := comap_eval_ne_bot_iff.2 ‹_› lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) : ne_bot (comap m f ⊓ 𝓟 s) := begin refine ⟨compl_compl s ▸ mt mem_of_eq_bot _⟩, rintro ⟨t, ht, hts⟩, rcases hf.nonempty_of_mem (inter_mem hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩, exact absurd hxs (hts hxt) end lemma comap_coe_ne_bot_of_le_principal {s : set γ} {l : filter γ} [h : ne_bot l] (h' : l ≤ 𝓟 s) : ne_bot (comap (coe : s → γ) l) := h.comap_of_range_mem $ (@subtype.range_coe γ s).symm ▸ h' (mem_principal_self s) lemma ne_bot.comap_of_surj {f : filter β} {m : α → β} (hf : ne_bot f) (hm : surjective m) : ne_bot (comap m f) := hf.comap_of_range_mem $ univ_mem' hm lemma ne_bot.comap_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) : ne_bot (comap m f) := hf.comap_of_range_mem $ mem_of_superset hs (image_subset_range _ _) @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by { rw [←empty_mem_iff_bot, ←empty_mem_iff_bot], exact id }, λ h, by simp only [h, map_bot]⟩ lemma map_ne_bot_iff (f : α → β) {F : filter α} : ne_bot (map f F) ↔ ne_bot F := by simp only [ne_bot_iff, ne, map_eq_bot_iff] lemma ne_bot.map (hf : ne_bot f) (m : α → β) : ne_bot (map m f) := (map_ne_bot_iff m).2 hf instance map_ne_bot [hf : ne_bot f] : ne_bot (f.map m) := hf.map m lemma sInter_comap_sets (f : α → β) (F : filter β) : ⋂₀ (comap f F).sets = ⋂ U ∈ F, f ⁻¹' U := begin ext x, suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔ ∀ (B : set β), B ∈ F → f x ∈ B, by simp only [mem_sInter, mem_Inter, filter.mem_sets, mem_comap, this, and_imp, exists_prop, mem_preimage, exists_imp_distrib], split, { intros h U U_in, simpa only [subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in }, { intros h V U U_in f_U_V, exact f_U_V (h U U_in) }, end end map -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ λ i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) [nonempty ι] : map m (infi f) = (⨅ i, map m (f i)) := map_infi_le.antisymm (λ s (hs : preimage m s ∈ infi f), let ⟨i, hi⟩ := (mem_infi_of_directed hf _).1 hs in have (⨅ i, map m (f i)) ≤ 𝓟 s, from infi_le_of_le i $ by { simp only [le_principal_iff, mem_map], assumption }, filter.le_principal_iff.1 this) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃ i, p i) : map m (⨅ i (h : p i), f i) = (⨅ i (h : p i), map m (f i)) := begin haveI := nonempty_subtype.2 ne, simp only [infi_subtype'], exact map_infi_eq h.directed_coe end lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g := (@map_mono _ _ m).map_inf_le f g lemma map_inf {f g : filter α} {m : α → β} (h : injective m) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine map_inf_le.antisymm _, rintro t ⟨s₁, hs₁, s₂, hs₂, ht : m ⁻¹' t = s₁ ∩ s₂⟩, refine mem_inf_of_inter (image_mem_map hs₁) (image_mem_map hs₂) _, rw [image_inter h, image_subset_iff, ht] end lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g) (h : inj_on m t) : map m (f ⊓ g) = map m f ⊓ map m g := begin lift f to filter t using htf, lift g to filter t using htg, replace h : injective (m ∘ coe) := h.injective, simp only [map_map, ← map_inf subtype.coe_injective, map_inf h], end lemma disjoint_map {m : α → β} (hm : injective m) {f₁ f₂ : filter α} : disjoint (map m f₁) (map m f₂) ↔ disjoint f₁ f₂ := by simp only [disjoint_iff, ← map_inf hm, map_eq_bot_iff] lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f := le_antisymm (λ b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $ calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true] ... ⊆ preimage m b : preimage_mono h) (λ b (hb : preimage m b ∈ f), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩) lemma map_equiv_symm (e : α ≃ β) (f : filter β) : map e.symm f = comap e f := map_eq_comap_of_inverse e.symm_comp_self e.self_comp_symm lemma comap_equiv_symm (e : α ≃ β) (f : filter α) : comap e.symm f = map e f := (map_eq_comap_of_inverse e.self_comp_symm e.symm_comp_self).symm lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f := map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀ s ∈ f, m '' s ∈ g) : g ≤ f.map m := λ s hs, mem_of_superset (h _ hs) $ image_preimage_subset _ _ protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) : map f (F ⊓ comap f G) = map f F ⊓ G := begin apply le_antisymm, { calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le ... ≤ map f F ⊓ G : inf_le_inf_left (map f F) map_comap_le }, { rintro U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩, apply mem_inf_of_inter (image_mem_map V_in) Z_in, calc f '' V ∩ Z = f '' (V ∩ f ⁻¹' Z) : by rw image_inter_preimage ... ⊆ f '' (V ∩ W) : image_subset _ (inter_subset_inter_right _ ‹_›) ... = f '' (f ⁻¹' U) : by rw h ... ⊆ U : image_preimage_subset f U } end protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) : map f (comap f G ⊓ F) = G ⊓ map f F := by simp only [filter.push_pull, inf_comm] section applicative lemma singleton_mem_pure {a : α} : {a} ∈ (pure a : filter α) := mem_singleton a lemma pure_injective : injective (pure : α → filter α) := λ a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl instance pure_ne_bot {α : Type u} {a : α} : ne_bot (pure a) := ⟨mt empty_mem_iff_bot.2 $ not_mem_empty a⟩ @[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f := ⟨λ h, h singleton_mem_pure, λ h s hs, mem_of_superset h $ singleton_subset_iff.2 hs⟩ lemma mem_seq_def {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃ u ∈ f, ∃ t ∈ g, ∀ x ∈ u, ∀ y ∈ t, (x : α → β) y ∈ s) := iff.rfl lemma mem_seq_iff {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃ u ∈ f, ∃ t ∈ g, set.seq u t ⊆ s) := by simp only [mem_seq_def, seq_subset, exists_prop, iff_self] lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} : s ∈ (f.map m).seq g ↔ (∃ t u, t ∈ g ∧ u ∈ f ∧ ∀ x ∈ u, ∀ y ∈ t, m x y ∈ s) := iff.intro (λ ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, λ a, hts _⟩) (λ ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, λ f ⟨a, has, eq⟩, eq ▸ hts _ has⟩) lemma seq_mem_seq {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α} (hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g := ⟨s, hs, t, ht, λ f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩ lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β} (hh : ∀ t ∈ f, ∀ u ∈ g, set.seq t u ∈ h) : h ≤ seq f g := λ s ⟨t, ht, u, hu, hs⟩, mem_of_superset (hh _ ht _ hu) $ λ b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha @[mono] lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ := le_seq $ λ s hs t ht, seq_mem_seq (hf hs) (hg ht) @[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g := begin refine le_antisymm (le_map $ λ s hs, _) (le_seq $ λ s hs t ht, _), { rw ← singleton_seq, apply seq_mem_seq _ hs, exact singleton_mem_pure }, { refine sets_of_superset (map g f) (image_mem_map ht) _, rintro b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ } end @[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λ g : α → β, g a) f := begin refine le_antisymm (le_map $ λ s hs, _) (le_seq $ λ s hs t ht, _), { rw ← seq_singleton, exact seq_mem_seq hs singleton_mem_pure }, { refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _, rintro b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ } end @[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) : seq h (seq g x) = seq (seq (map (∘) h) g) x := begin refine le_antisymm (le_seq $ λ s hs t ht, _) (le_seq $ λ s hs t ht, _), { rcases mem_seq_iff.1 hs with ⟨u, hu, v, hv, hs⟩, rcases mem_map_iff_exists_image.1 hu with ⟨w, hw, hu⟩, refine mem_of_superset _ (set.seq_mono ((set.seq_mono hu subset.rfl).trans hs) subset.rfl), rw ← set.seq_seq, exact seq_mem_seq hw (seq_mem_seq hv ht) }, { rcases mem_seq_iff.1 ht with ⟨u, hu, v, hv, ht⟩, refine mem_of_superset _ (set.seq_mono subset.rfl ht), rw set.seq_seq, exact seq_mem_seq (seq_mem_seq (image_mem_map hs) hu) hv } end lemma prod_map_seq_comm (f : filter α) (g : filter β) : (map prod.mk f).seq g = seq (map (λ b a, (a, b)) g) f := begin refine le_antisymm (le_seq $ λ s hs t ht, _) (le_seq $ λ s hs t ht, _), { rcases mem_map_iff_exists_image.1 hs with ⟨u, hu, hs⟩, refine mem_of_superset _ (set.seq_mono hs subset.rfl), rw ← set.prod_image_seq_comm, exact seq_mem_seq (image_mem_map ht) hu }, { rcases mem_map_iff_exists_image.1 hs with ⟨u, hu, hs⟩, refine mem_of_superset _ (set.seq_mono hs subset.rfl), rw set.prod_image_seq_comm, exact seq_mem_seq (image_mem_map ht) hu } end instance : is_lawful_functor (filter : Type u → Type u) := { id_map := λ α f, map_id, comp_map := λ α β γ f g a, map_map.symm } instance : is_lawful_applicative (filter : Type u → Type u) := { pure_seq_eq_map := λ α β, pure_seq_eq_map, map_pure := λ α β, map_pure, seq_pure := λ α β, seq_pure, seq_assoc := λ α β γ, seq_assoc } instance : is_comm_applicative (filter : Type u → Type u) := ⟨λ α β f g, prod_map_seq_comm f g⟩ lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) : f <*> g = seq f g := rfl end applicative /-! #### `bind` equations -/ section bind @[simp] lemma eventually_bind {f : filter α} {m : α → filter β} {p : β → Prop} : (∀ᶠ y in bind f m, p y) ↔ ∀ᶠ x in f, ∀ᶠ y in m x, p y := iff.rfl @[simp] lemma eventually_eq_bind {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} : (g₁ =ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ =ᶠ[m x] g₂ := iff.rfl @[simp] lemma eventually_le_bind [has_le γ] {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} : (g₁ ≤ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ ≤ᶠ[m x] g₂ := iff.rfl lemma mem_bind' {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ {a | s ∈ m a} ∈ f := iff.rfl @[simp] lemma mem_bind {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ ∃ t ∈ f, ∀ x ∈ t, s ∈ m x := calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : iff.rfl ... ↔ (∃ t ∈ f, t ⊆ {a | s ∈ m a}) : exists_mem_subset_iff.symm ... ↔ (∃ t ∈ f, ∀ x ∈ t, s ∈ m x) : iff.rfl lemma bind_le {f : filter α} {g : α → filter β} {l : filter β} (h : ∀ᶠ x in f, g x ≤ l) : f.bind g ≤ l := join_le $ eventually_map.2 h @[mono] lemma bind_mono {f₁ f₂ : filter α} {g₁ g₂ : α → filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ᶠ[f₁] g₂) : bind f₁ g₁ ≤ bind f₂ g₂ := begin refine le_trans (λ s hs, _) (join_mono $ map_mono hf), simp only [mem_join, mem_bind', mem_map] at hs ⊢, filter_upwards [hg, hs] with _ hx hs using hx hs, end lemma bind_inf_principal {f : filter α} {g : α → filter β} {s : set β} : f.bind (λ x, g x ⊓ 𝓟 s) = (f.bind g) ⊓ 𝓟 s := filter.ext $ λ s, by simp only [mem_bind, mem_inf_principal] lemma sup_bind {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp only [bind, sup_join, map_sup, eq_self_iff_true] lemma principal_bind {s : set α} {f : α → filter β} : (bind (𝓟 s) f) = (⨆ x ∈ s, f x) := show join (map f (𝓟 s)) = (⨆ x ∈ s, f x), by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true] end bind section list_traverse /- This is a separate section in order to open `list`, but mostly because of universe equality requirements in `traverse` -/ open list lemma sequence_mono : ∀ (as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs | [] [] forall₂.nil := le_rfl | (a :: as) (b :: bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs) variables {α' β' γ' : Type u} {f : β' → filter α'} {s : γ' → set α'} lemma mem_traverse : ∀ (fs : list β') (us : list γ'), forall₂ (λ b c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs | [] [] forall₂.nil := mem_pure.2 $ mem_singleton _ | (f :: fs) (u :: us) (forall₂.cons h hs) := seq_mem_seq (image_mem_map h) (mem_traverse fs us hs) lemma mem_traverse_iff (fs : list β') (t : set (list α')) : t ∈ traverse f fs ↔ (∃ us : list (set α'), forall₂ (λ b (s : set α'), s ∈ f b) fs us ∧ sequence us ⊆ t) := begin split, { induction fs generalizing t, case nil { simp only [sequence, mem_pure, imp_self, forall₂_nil_left_iff, exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] }, case cons : b fs ih t { intro ht, rcases mem_seq_iff.1 ht with ⟨u, hu, v, hv, ht⟩, rcases mem_map_iff_exists_image.1 hu with ⟨w, hw, hwu⟩, rcases ih v hv with ⟨us, hus, hu⟩, exact ⟨w :: us, forall₂.cons hw hus, (set.seq_mono hwu hu).trans ht⟩ } }, { rintro ⟨us, hus, hs⟩, exact mem_of_superset (mem_traverse _ _ hus) hs } end end list_traverse /-! ### Limits -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl lemma tendsto_iff_eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ ⦃p : β → Prop⦄, (∀ᶠ y in l₂, p y) → ∀ᶠ x in l₁, p (f x) := iff.rfl lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) : ∀ᶠ x in l₁, p (f x) := hf h lemma tendsto.frequently {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∃ᶠ x in l₁, p (f x)) : ∃ᶠ y in l₂, p y := mt hf.eventually h lemma tendsto.frequently_map {l₁ : filter α} {l₂ : filter β} {p : α → Prop} {q : β → Prop} (f : α → β) (c : filter.tendsto f l₁ l₂) (w : ∀ x, p x → q (f x)) (h : ∃ᶠ x in l₁, p x) : ∃ᶠ y in l₂, q y := c.frequently (h.mono w) @[simp] lemma tendsto_bot {f : α → β} {l : filter β} : tendsto f ⊥ l := by simp [tendsto] @[simp] lemma tendsto_top {f : α → β} {l : filter α} : tendsto f l ⊤ := le_top lemma le_map_of_right_inverse {mab : α → β} {mba : β → α} {f : filter α} {g : filter β} (h₁ : mab ∘ mba =ᶠ[g] id) (h₂ : tendsto mba g f) : g ≤ map mab f := by { rw [← @map_id _ g, ← map_congr h₁, ← map_map], exact map_mono h₂ } lemma tendsto_of_is_empty [is_empty α] {f : α → β} {la : filter α} {lb : filter β} : tendsto f la lb := by simp only [filter_eq_bot_of_is_empty la, tendsto_bot] lemma eventually_eq_of_left_inv_of_right_inv {f : α → β} {g₁ g₂ : β → α} {fa : filter α} {fb : filter β} (hleft : ∀ᶠ x in fa, g₁ (f x) = x) (hright : ∀ᶠ y in fb, f (g₂ y) = y) (htendsto : tendsto g₂ fb fa) : g₁ =ᶠ[fb] g₂ := (htendsto.eventually hleft).mp $ hright.mono $ λ y hr hl, (congr_arg g₁ hr.symm).trans hl lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f := map_le_iff_le_comap alias tendsto_iff_comap ↔ filter.tendsto.le_comap _ protected lemma tendsto.disjoint {f : α → β} {la₁ la₂ : filter α} {lb₁ lb₂ : filter β} (h₁ : tendsto f la₁ lb₁) (hd : disjoint lb₁ lb₂) (h₂ : tendsto f la₂ lb₂) : disjoint la₁ la₂ := (disjoint_comap hd).mono h₁.le_comap h₂.le_comap lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := by rw [tendsto, tendsto, map_congr hl] lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ := (tendsto_congr' hl).1 h theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := tendsto_congr' (univ_mem' h) theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ := (tendsto_congr h).1 lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp only [tendsto, map_id, forall_true_iff] {contextual := tt} lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto.mono_left {f : α → β} {x y : filter α} {z : filter β} (hx : tendsto f x z) (h : y ≤ x) : tendsto f y z := (map_mono h).trans hx lemma tendsto.mono_right {f : α → β} {x : filter α} {y z : filter β} (hy : tendsto f x y) (hz : y ≤ z) : tendsto f x z := le_trans hy hz lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) [hx : ne_bot x] : ne_bot y := (hx.map _).mono h lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] @[simp] lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} : tendsto f (map g x) y ↔ tendsto (f ∘ g) x y := by { rw [tendsto, map_map], refl } lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x := map_comap_le @[simp] lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c := ⟨λ h, tendsto_comap.comp h, λ h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩ lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α} (h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g := by { rw [tendsto, ← map_compose], simp only [(∘), map_comap_of_mem h, tendsto] } lemma tendsto.of_tendsto_comp {f : α → β} {g : β → γ} {a : filter α} {b : filter β} {c : filter γ} (hfg : tendsto (g ∘ f) a c) (hg : comap g c ≤ b) : tendsto f a b := begin rw tendsto_iff_comap at hfg ⊢, calc a ≤ comap (g ∘ f) c : hfg ... ≤ comap f b : by simpa [comap_comap] using comap_mono hg end lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f := begin refine ((comap_mono $ map_le_iff_le_comap.1 hψ).trans _).antisymm (map_le_iff_le_comap.1 hφ), rw [comap_comap, eq, comap_id], exact le_rfl end lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g := begin refine le_antisymm hφ (le_trans _ (map_mono hψ)), rw [map_map, eq, map_id], exact le_rfl end lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp only [tendsto, le_inf_iff, iff_self] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β} (h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) := tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩ @[simp] lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅ i, y i) ↔ ∀ i, tendsto f x (y i) := by simp only [tendsto, iff_self, le_infi_iff] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) (hi : tendsto f (x i) y) : tendsto f (⨅ i, x i) y := hi.mono_left $ infi_le _ _ @[simp] lemma tendsto_sup {f : α → β} {x₁ x₂ : filter α} {y : filter β} : tendsto f (x₁ ⊔ x₂) y ↔ tendsto f x₁ y ∧ tendsto f x₂ y := by simp only [tendsto, map_sup, sup_le_iff] lemma tendsto.sup {f : α → β} {x₁ x₂ : filter α} {y : filter β} : tendsto f x₁ y → tendsto f x₂ y → tendsto f (x₁ ⊔ x₂) y := λ h₁ h₂, tendsto_sup.mpr ⟨ h₁, h₂ ⟩ @[simp] lemma tendsto_supr {f : α → β} {x : ι → filter α} {y : filter β} : tendsto f (⨆ i, x i) y ↔ ∀ i, tendsto f (x i) y := by simp only [tendsto, map_supr, supr_le_iff] @[simp] lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} : tendsto f l (𝓟 s) ↔ ∀ᶠ a in l, f a ∈ s := by simp only [tendsto, le_principal_iff, mem_map', filter.eventually] @[simp] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (𝓟 s) (𝓟 t) ↔ ∀ a ∈ s, f a ∈ t := by simp only [tendsto_principal, eventually_principal] @[simp] lemma tendsto_pure {f : α → β} {a : filter α} {b : β} : tendsto f a (pure b) ↔ ∀ᶠ x in a, f x = b := by simp only [tendsto, le_pure_iff, mem_map', mem_singleton_iff, filter.eventually] lemma tendsto_pure_pure (f : α → β) (a : α) : tendsto f (pure a) (pure (f a)) := tendsto_pure.2 rfl lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λ x, b) a (pure b) := tendsto_pure.2 $ univ_mem' $ λ _, rfl lemma pure_le_iff {a : α} {l : filter α} : pure a ≤ l ↔ ∀ s ∈ l, a ∈ s := iff.rfl lemma tendsto_pure_left {f : α → β} {a : α} {l : filter β} : tendsto f (pure a) l ↔ ∀ s ∈ l, f a ∈ s := iff.rfl @[simp] lemma map_inf_principal_preimage {f : α → β} {s : set β} {l : filter α} : map f (l ⊓ 𝓟 (f ⁻¹' s)) = map f l ⊓ 𝓟 s := filter.ext $ λ t, by simp only [mem_map', mem_inf_principal, mem_set_of_eq, mem_preimage] /-- If two filters are disjoint, then a function cannot tend to both of them along a non-trivial filter. -/ lemma tendsto.not_tendsto {f : α → β} {a : filter α} {b₁ b₂ : filter β} (hf : tendsto f a b₁) [ne_bot a] (hb : disjoint b₁ b₂) : ¬ tendsto f a b₂ := λ hf', (tendsto_inf.2 ⟨hf, hf'⟩).ne_bot.ne hb.eq_bot lemma tendsto.if {l₁ : filter α} {l₂ : filter β} {f g : α → β} {p : α → Prop} [∀ x, decidable (p x)] (h₀ : tendsto f (l₁ ⊓ 𝓟 {x | p x}) l₂) (h₁ : tendsto g (l₁ ⊓ 𝓟 { x | ¬ p x }) l₂) : tendsto (λ x, if p x then f x else g x) l₁ l₂ := begin simp only [tendsto_def, mem_inf_principal] at *, intros s hs, filter_upwards [h₀ s hs, h₁ s hs], simp only [mem_preimage], intros x hp₀ hp₁, split_ifs, exacts [hp₀ h, hp₁ h], end lemma tendsto.piecewise {l₁ : filter α} {l₂ : filter β} {f g : α → β} {s : set α} [∀ x, decidable (x ∈ s)] (h₀ : tendsto f (l₁ ⊓ 𝓟 s) l₂) (h₁ : tendsto g (l₁ ⊓ 𝓟 sᶜ) l₂) : tendsto (piecewise s f g) l₁ l₂ := h₀.if h₁ /-! ### Products of filters -/ section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x ← seq, y ← top, return (x, y)} hence: s ∈ F ↔ ∃ n, [n..∞] × univ ⊆ s G := do {y ← top, x ← seq, return (x, y)} hence: s ∈ G ↔ ∀ i:ℕ, ∃ n, [n..∞] × {i} ⊆ s Now ⋃ i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd localized "infix ` ×ᶠ `:60 := filter.prod" in filter lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : s ×ˢ t ∈ f ×ᶠ g := inter_mem_inf (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ×ˢ t₂ ⊆ s) := begin simp only [filter.prod], split, { rintro ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, rfl⟩, exact ⟨s₁, hs₁, s₂, hs₂, λ p ⟨h, h'⟩, ⟨hts₁ h, hts₂ h'⟩⟩ }, { rintro ⟨t₁, ht₁, t₂, ht₂, h⟩, exact mem_inf_of_inter (preimage_mem_comap ht₁) (preimage_mem_comap ht₂) h } end @[simp] lemma prod_mem_prod_iff {s : set α} {t : set β} {f : filter α} {g : filter β} [f.ne_bot] [g.ne_bot] : s ×ˢ t ∈ f ×ᶠ g ↔ s ∈ f ∧ t ∈ g := ⟨λ h, let ⟨s', hs', t', ht', H⟩ := mem_prod_iff.1 h in (prod_subset_prod_iff.1 H).elim (λ ⟨hs's, ht't⟩, ⟨mem_of_superset hs' hs's, mem_of_superset ht' ht't⟩) (λ h, h.elim (λ hs'e, absurd hs'e (nonempty_of_mem hs').ne_empty) (λ ht'e, absurd ht'e (nonempty_of_mem ht').ne_empty)), λ h, prod_mem_prod h.1 h.2⟩ lemma mem_prod_principal {f : filter α} {s : set (α × β)} {t : set β}: s ∈ f ×ᶠ 𝓟 t ↔ {a | ∀ b ∈ t, (a, b) ∈ s} ∈ f := begin rw [← @exists_mem_subset_iff _ f, mem_prod_iff], refine exists₂_congr (λ u u_in, ⟨_, λ h, ⟨t, mem_principal_self t, _⟩⟩), { rintros ⟨v, v_in, hv⟩ a a_in b b_in, exact hv (mk_mem_prod a_in $ v_in b_in) }, { rintro ⟨x, y⟩ ⟨hx, hy⟩, exact h hx y hy } end lemma mem_prod_top {f : filter α} {s : set (α × β)} : s ∈ f ×ᶠ (⊤ : filter β) ↔ {a | ∀ b, (a, b) ∈ s} ∈ f := begin rw [← principal_univ, mem_prod_principal], simp only [mem_univ, forall_true_left] end lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) : comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) := by erw [comap_inf, filter.comap_comap, filter.comap_comap] lemma prod_top {f : filter α} : f ×ᶠ (⊤ : filter β) = f.comap prod.fst := by rw [filter.prod, comap_top, inf_top_eq] lemma sup_prod (f₁ f₂ : filter α) (g : filter β) : (f₁ ⊔ f₂) ×ᶠ g = (f₁ ×ᶠ g) ⊔ (f₂ ×ᶠ g) := by rw [filter.prod, comap_sup, inf_sup_right, ← filter.prod, ← filter.prod] lemma prod_sup (f : filter α) (g₁ g₂ : filter β) : f ×ᶠ (g₁ ⊔ g₂) = (f ×ᶠ g₁) ⊔ (f ×ᶠ g₂) := by rw [filter.prod, comap_sup, inf_sup_left, ← filter.prod, ← filter.prod] lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} : (∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x) (pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λ x, (m₁ x, m₂ x)) f (g ×ᶠ h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 := tendsto_fst.eventually h lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 := tendsto_snd.eventually h lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x) {lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) : ∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl lb).and (hb.prod_inr la) lemma eventually_eq.prod_map {δ} {la : filter α} {fa ga : α → γ} (ha : fa =ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb =ᶠ[lb] gb) : prod.map fa fb =ᶠ[la ×ᶠ lb] prod.map ga gb := (eventually.prod_mk ha hb).mono $ λ x h, prod.ext h.1 h.2 lemma eventually_le.prod_map {δ} [has_le γ] [has_le δ] {la : filter α} {fa ga : α → γ} (ha : fa ≤ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb ≤ᶠ[lb] gb) : prod.map fa fb ≤ᶠ[la ×ᶠ lb] prod.map ga gb := eventually.prod_mk ha hb lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop} (h : ∀ᶠ x in la ×ᶠ lb, p x) : ∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) := begin rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩, exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb) end lemma prod_infi_left [nonempty ι] {f : ι → filter α} {g : filter β}: (⨅ i, f i) ×ᶠ g = (⨅ i, (f i) ×ᶠ g) := by { rw [filter.prod, comap_infi, infi_inf], simp only [filter.prod, eq_self_iff_true] } lemma prod_infi_right [nonempty ι] {f : filter α} {g : ι → filter β} : f ×ᶠ (⨅ i, g i) = (⨅ i, f ×ᶠ (g i)) := by { rw [filter.prod, comap_infi, inf_infi], simp only [filter.prod, eq_self_iff_true] } @[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : (comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λ p : β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf] lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) := by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : f ×ᶠ g = map (λ p : β×α, (p.2, p.1)) (g ×ᶠ f) := by { rw [prod_comm', ← map_swap_eq_comap_swap], refl } lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : (map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := le_antisymm (λ s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc (m₁ '' s₁) ×ˢ (m₂ '' s₂) = (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) '' s₁ ×ˢ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto.comp le_rfl tendsto_fst).prod_mk (tendsto.comp le_rfl tendsto_snd)) lemma prod_map_map_eq' {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} (f : α₁ → α₂) (g : β₁ → β₂) (F : filter α₁) (G : filter β₁) : (map f F) ×ᶠ (map g G) = map (prod.map f g) (F ×ᶠ G) := prod_map_map_eq lemma le_prod_map_fst_snd {f : filter (α × β)} : f ≤ map prod.fst f ×ᶠ map prod.snd f := le_inf le_comap_map le_comap_map lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) := begin erw [tendsto, ← prod_map_map_eq], exact filter.prod_mono hf hg, end protected lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f ×ᶠ g) = (f.map (λ a b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], intro s, split, exact λ ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, λ x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact λ ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, λ ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f ×ᶠ g = (f.map prod.mk).seq g := have h : _ := f.map_prod id g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : (f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm] @[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : (𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (s ×ˢ t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma pure_prod {a : α} {f : filter β} : pure a ×ᶠ f = map (prod.mk a) f := by rw [prod_eq, map_pure, pure_seq_eq_map] lemma map_pure_prod (f : α → β → γ) (a : α) (B : filter β) : filter.map (function.uncurry f) (pure a ×ᶠ B) = filter.map (f a) B := by { rw filter.pure_prod, refl } @[simp] lemma prod_pure {f : filter α} {b : β} : f ×ᶠ pure b = map (λ a, (a, b)) f := by rw [prod_eq, seq_pure, map_map] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) := by simp lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { intro h, rcases mem_prod_iff.1 (empty_mem_iff_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_mem_iff_bot.1 (s_eq ▸ hs) }, { right, exact empty_mem_iff_bot.1 (t_eq ▸ ht) } }, { rintro (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) := by simp only [ne_bot_iff, ne, prod_eq_bot, not_or_distrib] lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) : ne_bot (f ×ᶠ g) := prod_ne_bot.2 ⟨hf, hg⟩ instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] : ne_bot (f ×ᶠ g) := hf.prod hg lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (x ×ᶠ y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] lemma tendsto_prod_iff' {f : filter α} {g : filter β} {g' : filter γ} {s : α → β × γ} : tendsto s f (g ×ᶠ g') ↔ tendsto (λ n, (s n).1) f g ∧ tendsto (λ n, (s n).2) f g' := by { unfold filter.prod, simp only [tendsto_inf, tendsto_comap_iff, iff_self] } end prod /-! ### Coproducts of filters -/ section coprod variables {f : filter α} {g : filter β} /-- Coproduct of filters. -/ protected def coprod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊔ g.comap prod.snd lemma mem_coprod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f.coprod g ↔ ((∃ t₁ ∈ f, prod.fst ⁻¹' t₁ ⊆ s) ∧ (∃ t₂ ∈ g, prod.snd ⁻¹' t₂ ⊆ s)) := by simp [filter.coprod] @[simp] lemma bot_coprod (l : filter β) : (⊥ : filter α).coprod l = comap prod.snd l := by simp [filter.coprod] @[simp] lemma coprod_bot (l : filter α) : l.coprod (⊥ : filter β) = comap prod.fst l := by simp [filter.coprod] lemma bot_coprod_bot : (⊥ : filter α).coprod (⊥ : filter β) = ⊥ := by simp lemma compl_mem_coprod {s : set (α × β)} {la : filter α} {lb : filter β} : sᶜ ∈ la.coprod lb ↔ (prod.fst '' s)ᶜ ∈ la ∧ (prod.snd '' s)ᶜ ∈ lb := by simp only [filter.coprod, mem_sup, compl_mem_comap] @[mono] lemma coprod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.coprod g₁ ≤ f₂.coprod g₂ := sup_le_sup (comap_mono hf) (comap_mono hg) lemma coprod_ne_bot_iff : (f.coprod g).ne_bot ↔ f.ne_bot ∧ nonempty β ∨ nonempty α ∧ g.ne_bot := by simp [filter.coprod] @[instance] lemma coprod_ne_bot_left [ne_bot f] [nonempty β] : (f.coprod g).ne_bot := coprod_ne_bot_iff.2 (or.inl ⟨‹_›, ‹_›⟩) @[instance] lemma coprod_ne_bot_right [ne_bot g] [nonempty α] : (f.coprod g).ne_bot := coprod_ne_bot_iff.2 (or.inr ⟨‹_›, ‹_›⟩) lemma principal_coprod_principal (s : set α) (t : set β) : (𝓟 s).coprod (𝓟 t) = 𝓟 (sᶜ ×ˢ tᶜ)ᶜ := by rw [filter.coprod, comap_principal, comap_principal, sup_principal, set.prod_eq, compl_inter, preimage_compl, preimage_compl, compl_compl, compl_compl] -- this inequality can be strict; see `map_const_principal_coprod_map_id_principal` and -- `map_prod_map_const_id_principal_coprod_principal` below. lemma map_prod_map_coprod_le {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : map (prod.map m₁ m₂) (f₁.coprod f₂) ≤ (map m₁ f₁).coprod (map m₂ f₂) := begin intros s, simp only [mem_map, mem_coprod_iff], rintro ⟨⟨u₁, hu₁, h₁⟩, u₂, hu₂, h₂⟩, refine ⟨⟨m₁ ⁻¹' u₁, hu₁, λ _ hx, h₁ _⟩, ⟨m₂ ⁻¹' u₂, hu₂, λ _ hx, h₂ _⟩⟩; convert hx end /-- Characterization of the coproduct of the `filter.map`s of two principal filters `𝓟 {a}` and `𝓟 {i}`, the first under the constant function `λ a, b` and the second under the identity function. Together with the next lemma, `map_prod_map_const_id_principal_coprod_principal`, this provides an example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/ lemma map_const_principal_coprod_map_id_principal {α β ι : Type*} (a : α) (b : β) (i : ι) : (map (λ _ : α, b) (𝓟 {a})).coprod (map id (𝓟 {i})) = 𝓟 (({b} : set β) ×ˢ (univ : set ι) ∪ (univ : set β) ×ˢ ({i} : set ι)) := by simp only [map_principal, filter.coprod, comap_principal, sup_principal, image_singleton, image_id, prod_univ, univ_prod] /-- Characterization of the `filter.map` of the coproduct of two principal filters `𝓟 {a}` and `𝓟 {i}`, under the `prod.map` of two functions, respectively the constant function `λ a, b` and the identity function. Together with the previous lemma, `map_const_principal_coprod_map_id_principal`, this provides an example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/ lemma map_prod_map_const_id_principal_coprod_principal {α β ι : Type*} (a : α) (b : β) (i : ι) : map (prod.map (λ _ : α, b) id) ((𝓟 {a}).coprod (𝓟 {i})) = 𝓟 (({b} : set β) ×ˢ (univ : set ι)) := begin rw [principal_coprod_principal, map_principal], congr, ext ⟨b', i'⟩, split, { rintro ⟨⟨a'', i''⟩, h₁, h₂, h₃⟩, simp }, { rintro ⟨h₁, h₂⟩, use (a, i'), simpa using h₁.symm } end lemma tendsto.prod_map_coprod {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a.coprod b) (c.coprod d) := map_prod_map_coprod_le.trans (coprod_mono hf hg) end coprod end filter open_locale filter lemma set.eq_on.eventually_eq {α β} {s : set α} {f g : α → β} (h : eq_on f g s) : f =ᶠ[𝓟 s] g := h lemma set.eq_on.eventually_eq_of_mem {α β} {s : set α} {l : filter α} {f g : α → β} (h : eq_on f g s) (hl : s ∈ l) : f =ᶠ[l] g := h.eventually_eq.filter_mono $ filter.le_principal_iff.2 hl lemma set.subset.eventually_le {α} {l : filter α} {s t : set α} (h : s ⊆ t) : s ≤ᶠ[l] t := filter.eventually_of_forall h lemma set.maps_to.tendsto {α β} {s : set α} {t : set β} {f : α → β} (h : maps_to f s t) : filter.tendsto f (𝓟 s) (𝓟 t) := filter.tendsto_principal_principal.2 h
c03175c4e21e44f818ea0f155d10529de4212579
c777c32c8e484e195053731103c5e52af26a25d1
/src/set_theory/ordinal/principal.lean
61774bc56030ae224fd06ca702bc1ddbbf4a95ab
[ "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
16,575
lean
/- Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta Hernández Palacios -/ import set_theory.ordinal.fixed_point /-! ### Principal ordinals > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define principal or indecomposable ordinals, and we prove the standard properties about them. ### Main definitions and results * `principal`: A principal or indecomposable ordinal under some binary operation. We include 0 and any other typically excluded edge cases for simplicity. * `unbounded_principal`: Principal ordinals are unbounded. * `principal_add_iff_zero_or_omega_opow`: The main characterization theorem for additive principal ordinals. * `principal_mul_iff_le_two_or_omega_opow_opow`: The main characterization theorem for multiplicative principal ordinals. ### Todo * Prove that exponential principal ordinals are 0, 1, 2, ω, or epsilon numbers, i.e. fixed points of `λ x, ω ^ x`. -/ universe u noncomputable theory open order namespace ordinal local infixr (name := ordinal.pow) ^ := @pow ordinal ordinal ordinal.has_pow /-! ### Principal ordinals -/ /-- An ordinal `o` is said to be principal or indecomposable under an operation when the set of ordinals less than it is closed under that operation. In standard mathematical usage, this term is almost exclusively used for additive and multiplicative principal ordinals. For simplicity, we break usual convention and regard 0 as principal. -/ def principal (op : ordinal → ordinal → ordinal) (o : ordinal) : Prop := ∀ ⦃a b⦄, a < o → b < o → op a b < o theorem principal_iff_principal_swap {op : ordinal → ordinal → ordinal} {o : ordinal} : principal op o ↔ principal (function.swap op) o := by split; exact λ h a b ha hb, h hb ha theorem principal_zero {op : ordinal → ordinal → ordinal} : principal op 0 := λ a _ h, (ordinal.not_lt_zero a h).elim @[simp] theorem principal_one_iff {op : ordinal → ordinal → ordinal} : principal op 1 ↔ op 0 0 = 0 := begin refine ⟨λ h, _, λ h a b ha hb, _⟩, { rwa ←lt_one_iff_zero, exact h zero_lt_one zero_lt_one }, { rwa [lt_one_iff_zero, ha, hb] at * } end theorem principal.iterate_lt {op : ordinal → ordinal → ordinal} {a o : ordinal} (hao : a < o) (ho : principal op o) (n : ℕ) : (op a)^[n] a < o := begin induction n with n hn, { rwa function.iterate_zero }, { rw function.iterate_succ', exact ho hao hn } end theorem op_eq_self_of_principal {op : ordinal → ordinal → ordinal} {a o : ordinal.{u}} (hao : a < o) (H : is_normal (op a)) (ho : principal op o) (ho' : is_limit o) : op a o = o := begin refine le_antisymm _ (H.self_le _), rw [←is_normal.bsup_eq.{u u} H ho', bsup_le_iff], exact λ b hbo, (ho hao hbo).le end theorem nfp_le_of_principal {op : ordinal → ordinal → ordinal} {a o : ordinal} (hao : a < o) (ho : principal op o) : nfp (op a) a ≤ o := nfp_le $ λ n, (ho.iterate_lt hao n).le /-! ### Principal ordinals are unbounded -/ /-- The least strict upper bound of `op` applied to all pairs of ordinals less than `o`. This is essentially a two-argument version of `ordinal.blsub`. -/ def blsub₂ (op : ordinal → ordinal → ordinal) (o : ordinal) : ordinal := lsub (λ x : o.out.α × o.out.α, op (typein (<) x.1) (typein (<) x.2)) theorem lt_blsub₂ (op : ordinal → ordinal → ordinal) {o : ordinal} {a b : ordinal} (ha : a < o) (hb : b < o) : op a b < blsub₂ op o := begin convert lt_lsub _ (prod.mk (enum (<) a (by rwa type_lt)) (enum (<) b (by rwa type_lt))), simp only [typein_enum] end theorem principal_nfp_blsub₂ (op : ordinal → ordinal → ordinal) (o : ordinal) : principal op (nfp (blsub₂.{u u} op) o) := λ a b ha hb, begin rw lt_nfp at *, cases ha with m hm, cases hb with n hn, cases le_total ((blsub₂.{u u} op)^[m] o) ((blsub₂.{u u} op)^[n] o) with h h, { use n + 1, rw function.iterate_succ', exact lt_blsub₂ op (hm.trans_le h) hn }, { use m + 1, rw function.iterate_succ', exact lt_blsub₂ op hm (hn.trans_le h) }, end theorem unbounded_principal (op : ordinal → ordinal → ordinal) : set.unbounded (<) {o | principal op o} := λ o, ⟨_, principal_nfp_blsub₂ op o, (le_nfp _ o).not_lt⟩ /-! #### Additive principal ordinals -/ theorem principal_add_one : principal (+) 1 := principal_one_iff.2 $ zero_add 0 theorem principal_add_of_le_one {o : ordinal} (ho : o ≤ 1) : principal (+) o := begin rcases le_one_iff.1 ho with rfl | rfl, { exact principal_zero }, { exact principal_add_one } end theorem principal_add_is_limit {o : ordinal} (ho₁ : 1 < o) (ho : principal (+) o) : o.is_limit := begin refine ⟨λ ho₀, _, λ a hao, _⟩, { rw ho₀ at ho₁, exact not_lt_of_gt zero_lt_one ho₁ }, { cases eq_or_ne a 0 with ha ha, { rw [ha, succ_zero], exact ho₁ }, { refine lt_of_le_of_lt _ (ho hao hao), rwa [←add_one_eq_succ, add_le_add_iff_left, one_le_iff_ne_zero] } } end theorem principal_add_iff_add_left_eq_self {o : ordinal} : principal (+) o ↔ ∀ a < o, a + o = o := begin refine ⟨λ ho a hao, _, λ h a b hao hbo, _⟩, { cases lt_or_le 1 o with ho₁ ho₁, { exact op_eq_self_of_principal hao (add_is_normal a) ho (principal_add_is_limit ho₁ ho) }, { rcases le_one_iff.1 ho₁ with rfl | rfl, { exact (ordinal.not_lt_zero a hao).elim }, { rw lt_one_iff_zero at hao, rw [hao, zero_add] }}}, { rw ←h a hao, exact (add_is_normal a).strict_mono hbo } end theorem exists_lt_add_of_not_principal_add {a} (ha : ¬ principal (+) a) : ∃ (b c) (hb : b < a) (hc : c < a), b + c = a := begin unfold principal at ha, push_neg at ha, rcases ha with ⟨b, c, hb, hc, H⟩, refine ⟨b, _, hb, lt_of_le_of_ne (sub_le_self a b) (λ hab, _), ordinal.add_sub_cancel_of_le hb.le⟩, rw [←sub_le, hab] at H, exact H.not_lt hc end theorem principal_add_iff_add_lt_ne_self {a} : principal (+) a ↔ ∀ ⦃b c⦄, b < a → c < a → b + c ≠ a := ⟨λ ha b c hb hc, (ha hb hc).ne, λ H, begin by_contra' ha, rcases exists_lt_add_of_not_principal_add ha with ⟨b, c, hb, hc, rfl⟩, exact (H hb hc).irrefl end⟩ theorem add_omega {a : ordinal} (h : a < omega) : a + omega = omega := begin rcases lt_omega.1 h with ⟨n, rfl⟩, clear h, induction n with n IH, { rw [nat.cast_zero, zero_add] }, { rwa [nat.cast_succ, add_assoc, one_add_of_omega_le (le_refl _)] } end theorem principal_add_omega : principal (+) omega := principal_add_iff_add_left_eq_self.2 (λ a, add_omega) theorem add_omega_opow {a b : ordinal} (h : a < omega ^ b) : a + omega ^ b = omega ^ b := begin refine le_antisymm _ (le_add_left _ _), revert h, refine limit_rec_on b (λ h, _) (λ b _ h, _) (λ b l IH h, _), { rw [opow_zero, ← succ_zero, lt_succ_iff, ordinal.le_zero] at h, rw [h, zero_add] }, { rw opow_succ at h, rcases (lt_mul_of_limit omega_is_limit).1 h with ⟨x, xo, ax⟩, refine le_trans (add_le_add_right (le_of_lt ax) _) _, rw [opow_succ, ← mul_add, add_omega xo] }, { rcases (lt_opow_of_limit omega_ne_zero l).1 h with ⟨x, xb, ax⟩, exact (((add_is_normal a).trans (opow_is_normal one_lt_omega)).limit_le l).2 (λ y yb, (add_le_add_left (opow_le_opow_right omega_pos (le_max_right _ _)) _).trans (le_trans (IH _ (max_lt xb yb) (ax.trans_le $ opow_le_opow_right omega_pos (le_max_left _ _))) (opow_le_opow_right omega_pos $ le_of_lt $ max_lt xb yb))) } end theorem principal_add_omega_opow (o : ordinal) : principal (+) (omega ^ o) := principal_add_iff_add_left_eq_self.2 (λ a, add_omega_opow) /-- The main characterization theorem for additive principal ordinals. -/ theorem principal_add_iff_zero_or_omega_opow {o : ordinal} : principal (+) o ↔ o = 0 ∨ ∃ a, o = omega ^ a := begin rcases eq_or_ne o 0 with rfl | ho, { simp only [principal_zero, or.inl] }, { rw [principal_add_iff_add_left_eq_self], simp only [ho, false_or], refine ⟨λ H, ⟨_, ((lt_or_eq_of_le (opow_log_le_self _ ho)) .resolve_left $ λ h, _).symm⟩, λ ⟨b, e⟩, e.symm ▸ λ a, add_omega_opow⟩, have := H _ h, have := lt_opow_succ_log_self one_lt_omega o, rw [opow_succ, lt_mul_of_limit omega_is_limit] at this, rcases this with ⟨a, ao, h'⟩, rcases lt_omega.1 ao with ⟨n, rfl⟩, clear ao, revert h', apply not_lt_of_le, suffices e : omega ^ log omega o * ↑n + o = o, { simpa only [e] using le_add_right (omega ^ log omega o * ↑n) o }, induction n with n IH, {simp only [nat.cast_zero, mul_zero, zero_add]}, simp only [nat.cast_succ, mul_add_one, add_assoc, this, IH] } end theorem opow_principal_add_of_principal_add {a} (ha : principal (+) a) (b : ordinal) : principal (+) (a ^ b) := begin rcases principal_add_iff_zero_or_omega_opow.1 ha with rfl | ⟨c, rfl⟩, { rcases eq_or_ne b 0 with rfl | hb, { rw opow_zero, exact principal_add_one }, { rwa zero_opow hb } }, { rw ←opow_mul, exact principal_add_omega_opow _ } end theorem add_absorp {a b c : ordinal} (h₁ : a < omega ^ b) (h₂ : omega ^ b ≤ c) : a + c = c := by rw [← ordinal.add_sub_cancel_of_le h₂, ← add_assoc, add_omega_opow h₁] theorem mul_principal_add_is_principal_add (a : ordinal.{u}) {b : ordinal.{u}} (hb₁ : b ≠ 1) (hb : principal (+) b) : principal (+) (a * b) := begin rcases eq_zero_or_pos a with rfl | ha, { rw zero_mul, exact principal_zero }, { rcases eq_zero_or_pos b with rfl | hb₁', { rw mul_zero, exact principal_zero }, { rw [← succ_le_iff, succ_zero] at hb₁', intros c d hc hd, rw lt_mul_of_limit (principal_add_is_limit (lt_of_le_of_ne hb₁' hb₁.symm) hb) at *, { rcases hc with ⟨x, hx, hx'⟩, rcases hd with ⟨y, hy, hy'⟩, use [x + y, hb hx hy], rw mul_add, exact left.add_lt_add hx' hy' }, assumption' } } end /-! #### Multiplicative principal ordinals -/ theorem principal_mul_one : principal (*) 1 := by { rw principal_one_iff, exact zero_mul _ } theorem principal_mul_two : principal (*) 2 := λ a b ha hb, begin have h₂ : succ (1 : ordinal) = 2 := rfl, rw [←h₂, lt_succ_iff] at *, convert mul_le_mul' ha hb, exact (mul_one 1).symm end theorem principal_mul_of_le_two {o : ordinal} (ho : o ≤ 2) : principal (*) o := begin rcases lt_or_eq_of_le ho with ho | rfl, { have h₂ : succ (1 : ordinal) = 2 := rfl, rw [←h₂, lt_succ_iff] at ho, rcases lt_or_eq_of_le ho with ho | rfl, { rw lt_one_iff_zero.1 ho, exact principal_zero }, { exact principal_mul_one } }, { exact principal_mul_two } end theorem principal_add_of_principal_mul {o : ordinal} (ho : principal (*) o) (ho₂ : o ≠ 2) : principal (+) o := begin cases lt_or_gt_of_ne ho₂ with ho₁ ho₂, { change o < succ 1 at ho₁, rw lt_succ_iff at ho₁, exact principal_add_of_le_one ho₁ }, { refine λ a b hao hbo, lt_of_le_of_lt _ (ho (max_lt hao hbo) ho₂), rw mul_two, exact add_le_add (le_max_left a b) (le_max_right a b) } end theorem principal_mul_is_limit {o : ordinal.{u}} (ho₂ : 2 < o) (ho : principal (*) o) : o.is_limit := principal_add_is_limit ((lt_succ 1).trans ho₂) (principal_add_of_principal_mul ho (ne_of_gt ho₂)) theorem principal_mul_iff_mul_left_eq {o : ordinal} : principal (*) o ↔ ∀ a, 0 < a → a < o → a * o = o := begin refine ⟨λ h a ha₀ hao, _, λ h a b hao hbo, _⟩, { cases le_or_gt o 2 with ho ho, { convert one_mul o, apply le_antisymm, { have : a < succ 1 := hao.trans_le ho, rwa lt_succ_iff at this }, { rwa [←succ_le_iff, succ_zero] at ha₀ } }, { exact op_eq_self_of_principal hao (mul_is_normal ha₀) h (principal_mul_is_limit ho h) } }, { rcases eq_or_ne a 0 with rfl | ha, { rwa zero_mul }, rw ←ordinal.pos_iff_ne_zero at ha, rw ←h a ha hao, exact (mul_is_normal ha).strict_mono hbo } end theorem principal_mul_omega : principal (*) omega := λ a b ha hb, match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by { rw [← nat_cast_mul], apply nat_lt_omega } end theorem mul_omega {a : ordinal} (a0 : 0 < a) (ha : a < omega) : a * omega = omega := principal_mul_iff_mul_left_eq.1 (principal_mul_omega) a a0 ha theorem mul_lt_omega_opow {a b c : ordinal} (c0 : 0 < c) (ha : a < omega ^ c) (hb : b < omega) : a * b < omega ^ c := begin rcases zero_or_succ_or_limit c with rfl|⟨c,rfl⟩|l, { exact (lt_irrefl _).elim c0 }, { rw opow_succ at ha, rcases ((mul_is_normal $ opow_pos _ omega_pos).limit_lt omega_is_limit).1 ha with ⟨n, hn, an⟩, apply (mul_le_mul_right' (le_of_lt an) _).trans_lt, rw [opow_succ, mul_assoc, mul_lt_mul_iff_left (opow_pos _ omega_pos)], exact principal_mul_omega hn hb }, { rcases ((opow_is_normal one_lt_omega).limit_lt l).1 ha with ⟨x, hx, ax⟩, refine (mul_le_mul' (le_of_lt ax) (le_of_lt hb)).trans_lt _, rw [← opow_succ, opow_lt_opow_iff_right one_lt_omega], exact l.2 _ hx } end theorem mul_omega_opow_opow {a b : ordinal} (a0 : 0 < a) (h : a < omega ^ omega ^ b) : a * omega ^ omega ^ b = omega ^ omega ^ b := begin by_cases b0 : b = 0, {rw [b0, opow_zero, opow_one] at h ⊢, exact mul_omega a0 h}, refine le_antisymm _ (by simpa only [one_mul] using mul_le_mul_right' (one_le_iff_pos.2 a0) (omega ^ omega ^ b)), rcases (lt_opow_of_limit omega_ne_zero (opow_is_limit_left omega_is_limit b0)).1 h with ⟨x, xb, ax⟩, apply (mul_le_mul_right' (le_of_lt ax) _).trans, rw [← opow_add, add_omega_opow xb] end theorem principal_mul_omega_opow_opow (o : ordinal) : principal (*) (omega ^ omega ^ o) := principal_mul_iff_mul_left_eq.2 (λ a, mul_omega_opow_opow) theorem principal_add_of_principal_mul_opow {o b : ordinal} (hb : 1 < b) (ho : principal (*) (b ^ o)) : principal (+) o := λ x y hx hy, begin have := ho ((opow_lt_opow_iff_right hb).2 hx) ((opow_lt_opow_iff_right hb).2 hy), rwa [←opow_add, opow_lt_opow_iff_right hb] at this end /-- The main characterization theorem for multiplicative principal ordinals. -/ theorem principal_mul_iff_le_two_or_omega_opow_opow {o : ordinal} : principal (*) o ↔ o ≤ 2 ∨ ∃ a, o = omega ^ omega ^ a := begin refine ⟨λ ho, _, _⟩, { cases le_or_lt o 2 with ho₂ ho₂, { exact or.inl ho₂ }, rcases principal_add_iff_zero_or_omega_opow.1 (principal_add_of_principal_mul ho ho₂.ne') with rfl | ⟨a, rfl⟩, { exact (ordinal.not_lt_zero 2 ho₂).elim }, rcases principal_add_iff_zero_or_omega_opow.1 (principal_add_of_principal_mul_opow one_lt_omega ho) with rfl | ⟨b, rfl⟩, { rw opow_zero at ho₂, exact ((lt_succ 1).not_le ho₂.le).elim }, exact or.inr ⟨b, rfl⟩ }, { rintro (ho₂ | ⟨a, rfl⟩), { exact principal_mul_of_le_two ho₂ }, { exact principal_mul_omega_opow_opow a } } end theorem mul_omega_dvd {a : ordinal} (a0 : 0 < a) (ha : a < omega) : ∀ {b}, omega ∣ b → a * b = b | _ ⟨b, rfl⟩ := by rw [← mul_assoc, mul_omega a0 ha] theorem mul_eq_opow_log_succ {a b : ordinal.{u}} (ha : a ≠ 0) (hb : principal (*) b) (hb₂ : 2 < b) : a * b = b ^ succ (log b a) := begin apply le_antisymm, { have hbl := principal_mul_is_limit hb₂ hb, rw [←is_normal.bsup_eq.{u u} (mul_is_normal (ordinal.pos_iff_ne_zero.2 ha)) hbl, bsup_le_iff], intros c hcb, have hb₁ : 1 < b := (lt_succ 1).trans hb₂, have hbo₀ : b ^ b.log a ≠ 0 := ordinal.pos_iff_ne_zero.1 (opow_pos _ (zero_lt_one.trans hb₁)), apply le_trans (mul_le_mul_right' (le_of_lt (lt_mul_succ_div a hbo₀)) c), rw [mul_assoc, opow_succ], refine mul_le_mul_left' (le_of_lt (hb (hbl.2 _ _) hcb)) _, rw [div_lt hbo₀, ←opow_succ], exact lt_opow_succ_log_self hb₁ _ }, { rw opow_succ, exact mul_le_mul_right' (opow_log_le_self b ha) b } end /-! #### Exponential principal ordinals -/ theorem principal_opow_omega : principal (^) omega := λ a b ha hb, match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by { simp_rw ←nat_cast_opow, apply nat_lt_omega } end theorem opow_omega {a : ordinal} (a1 : 1 < a) (h : a < omega) : a ^ omega = omega := le_antisymm ((opow_le_of_limit (one_le_iff_ne_zero.1 $ le_of_lt a1) omega_is_limit).2 (λ b hb, (principal_opow_omega h hb).le)) (right_le_opow _ a1) end ordinal
daf9f8871b8cf8ef65dcdff017d38eeb591f5aee
f3849be5d845a1cb97680f0bbbe03b85518312f0
/old_library/init/funext.lean
b5e7563d9f33b429ad642de5a24908d46f1b1ac1
[ "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
2,227
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Extensional equality for functions, and a proof of function extensionality from quotients. -/ prelude import init.quot init.logic namespace function universe variables u v variables {A : Type u} {B : A → Type v} protected definition equiv (f₁ f₂ : Π x : A, B x) : Prop := ∀ x, f₁ x = f₂ x local infix `~` := function.equiv protected theorem equiv.refl (f : Π x : A, B x) : f ~ f := take x, rfl protected theorem equiv.symm {f₁ f₂ : Π x: A, B x} : f₁ ~ f₂ → f₂ ~ f₁ := λ H x, eq.symm (H x) protected theorem equiv.trans {f₁ f₂ f₃ : Π x: A, B x} : f₁ ~ f₂ → f₂ ~ f₃ → f₁ ~ f₃ := λ H₁ H₂ x, eq.trans (H₁ x) (H₂ x) protected theorem equiv.is_equivalence (A : Type u) (B : A → Type v) : equivalence (@function.equiv A B) := mk_equivalence (@function.equiv A B) (@equiv.refl A B) (@equiv.symm A B) (@equiv.trans A B) end function section open quot universe variables u v variables {A : Type u} {B : A → Type v} attribute [instance] private definition fun_setoid (A : Type u) (B : A → Type v) : setoid (Π x : A, B x) := setoid.mk (@function.equiv A B) (function.equiv.is_equivalence A B) private definition extfun (A : Type u) (B : A → Type v) : Type (imax u v) := quot (fun_setoid A B) private definition fun_to_extfun (f : Π x : A, B x) : extfun A B := ⟦f⟧ private definition extfun_app (f : extfun A B) : Π x : A, B x := take x, quot.lift_on f (λ f : Π x : A, B x, f x) (λ f₁ f₂ H, H x) theorem funext {f₁ f₂ : Π x : A, B x} : (∀ x, f₁ x = f₂ x) → f₁ = f₂ := assume H, calc f₁ = extfun_app ⟦f₁⟧ : rfl ... = extfun_app ⟦f₂⟧ : @sound _ _ f₁ f₂ H ▸ rfl ... = f₂ : rfl end attribute funext [intro!] local infix `~` := function.equiv attribute [instance] definition {u v} subsingleton_pi {A : Type u} {B : A → Type v} (H : ∀ a, subsingleton (B a)) : subsingleton (Π a, B a) := ⟨λ f₁ f₂, funext (λ a, subsingleton.elim (f₁ a) (f₂ a))⟩
3d215252222c2386aa31ce2a27d7be9f077b751e
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Lean/Parser/Do.lean
32afe9ecd699afeddc6c26096c0cc95613c55079
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,483
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.Parser.Term namespace Lean namespace Parser builtin_initialize registerBuiltinParserAttribute `builtinDoElemParser `doElem builtin_initialize registerBuiltinDynamicParserAttribute `doElemParser `doElem @[inline] def doElemParser (rbp : Nat := 0) : Parser := categoryParser `doElem rbp namespace Term def leftArrow : Parser := unicodeSymbol " ← " " <- " @[builtinTermParser] def liftMethod := parser!:0 leftArrow >> termParser def doSeqIndent := parser! many1Indent $ group (doElemParser >> optional "; ") def doSeqBracketed := parser! "{" >> withoutPosition (many1 (group (doElemParser >> optional "; "))) >> "}" def doSeq := doSeqBracketed <|> doSeqIndent def notFollowedByRedefinedTermToken := notFollowedBy ("if" <|> "match" <|> "let" <|> "have" <|> "do" <|> "dbgTrace!" <|> "assert!" <|> "for" <|> "unless" <|> "return" <|> "try") "token at 'do' element" @[builtinDoElemParser] def doLet := parser! "let " >> optional "mut " >> letDecl @[builtinDoElemParser] def doLetRec := parser! group ("let " >> nonReservedSymbol "rec ") >> letRecDecls def doIdDecl := parser! «try» (ident >> optType >> leftArrow) >> doElemParser def doPatDecl := parser! «try» (termParser >> leftArrow) >> doElemParser >> optional (" | " >> doElemParser) @[builtinDoElemParser] def doLetArrow := parser! "let " >> optional "mut " >> (doIdDecl <|> doPatDecl) @[builtinDoElemParser] def doReassign := parser! notFollowedByRedefinedTermToken >> (letIdDecl <|> letPatDecl) @[builtinDoElemParser] def doReassignArrow := parser! notFollowedByRedefinedTermToken >> (doIdDecl <|> doPatDecl) @[builtinDoElemParser] def doHave := parser! "have " >> Term.haveDecl /- In `do` blocks, we support `if` without an `else`. Thus, we use indentation to prevent examples such as ``` if c_1 then if c_2 then action_1 else action_2 ``` from being parsed as ``` if c_1 then { if c_2 then { action_1 } else { action_2 } } ``` We also have special support for `else if` because we don't want to write ``` if c_1 then action_1 else if c_2 then action_2 else action_3 ``` -/ def elseIf := «try» (group (withPosition (" else " >> checkLineEq >> " if "))) @[builtinDoElemParser] def doIf := parser! withPosition $ "if " >> optIdent >> termParser >> " then " >> doSeq >> many (checkColGe "'else if' in 'do' must be indented" >> group (elseIf >> optIdent >> termParser >> " then " >> doSeq)) >> optional (checkColGe "'else' in 'do' must be indented" >> " else " >> doSeq) @[builtinDoElemParser] def doUnless := parser! "unless " >> withForbidden "do" termParser >> "do " >> doSeq @[builtinDoElemParser] def doFor := parser! "for " >> termParser >> " in " >> withForbidden "do" termParser >> "do " >> doSeq /- `match`-expression where the right-hand-side of alternatives is a `doSeq` instead of a `term` -/ def doMatchAlt : Parser := parser! sepBy1 termParser ", " >> darrow >> doSeq def doMatchAlts : Parser := parser! withPosition $ (optional "| ") >> sepBy1 doMatchAlt (checkColGe "alternatives must be indented" >> "| ") @[builtinDoElemParser] def doMatch := parser!:leadPrec "match " >> sepBy1 matchDiscr ", " >> optType >> " with " >> doMatchAlts def doCatch := parser! «try» ("catch " >> binderIdent) >> optional (" : " >> termParser) >> darrow >> doSeq def doCatchMatch := parser! "catch " >> doMatchAlts def doFinally := parser! "finally " >> doSeq @[builtinDoElemParser] def doTry := parser! "try " >> doSeq >> many (doCatch <|> doCatchMatch) >> optional doFinally @[builtinDoElemParser] def doBreak := parser! "break" @[builtinDoElemParser] def doContinue := parser! "continue" @[builtinDoElemParser] def doReturn := parser!:leadPrec withPosition ("return " >> optional (checkLineEq >> termParser)) @[builtinDoElemParser] def doDbgTrace := parser!:leadPrec "dbgTrace! " >> ((interpolatedStr termParser) <|> termParser) @[builtinDoElemParser] def doAssert := parser!:leadPrec "assert! " >> termParser /- We use `notFollowedBy` to avoid counterintuitive behavior. For example, the `if`-term parser doesn't enforce indentation restrictions, but we don't want it to be used when `doIf` fails. Note that parser priorities would not solve this problem since the `doIf` parser is failing while the `if` parser is succeeding. -/ @[builtinDoElemParser] def doExpr := parser! notFollowedByRedefinedTermToken >> termParser @[builtinDoElemParser] def doNested := parser! "do " >> doSeq @[builtinTermParser] def «do» := parser!:maxPrec "do " >> doSeq @[builtinTermParser] def doElem.quot : Parser := parser! "`(doElem|" >> toggleInsideQuot doElemParser >> ")" /- macros for using `unless`, `for`, `try`, `return` as terms. They expand into `do unless ...`, `do for ...`, `do try ...`, and `do return ...` -/ @[builtinTermParser] def termUnless := parser! "unless " >> withForbidden "do" termParser >> "do " >> doSeq @[builtinTermParser] def termFor := parser! "for " >> termParser >> " in " >> withForbidden "do" termParser >> "do " >> doSeq @[builtinTermParser] def termTry := parser! "try " >> doSeq >> many (doCatch <|> doCatchMatch) >> optional doFinally @[builtinTermParser] def termReturn := parser!:leadPrec withPosition ("return " >> optional (checkLineEq >> termParser)) end Term end Parser end Lean
ae5c2470734a94a965085df85f17de831006eb85
2a8b9c9fb34993732e24b7934050207f28675948
/lua/tests/fixtures/example-lean4-project/Test/Test1.lean
6877d754bb3da8335e1d4916195d43f5dd06e150
[ "MIT" ]
permissive
jcommelin/lean.nvim
471b8c2c16f47e76dfc36dc85d1e9cf6938b58e6
b10a525f65cd2fda1c2e629d4b68f241caf67d64
refs/heads/main
1,691,387,304,436
1,633,447,198,000
1,633,447,198,000
413,898,327
0
0
MIT
1,633,451,737,000
1,633,451,737,000
null
UTF-8
Lean
false
false
20
lean
def test : Nat := 5
98ae692ddb74b742045638c9b1a821c4e9a45784
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/witt_vector/basic.lean
2067ecdfe286813e95b42743cdbe6f712e29abdc
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
9,183
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import data.mv_polynomial.counit import data.mv_polynomial.invertible import ring_theory.witt_vector.defs /-! # Witt vectors This file verifies that the ring operations on `witt_vector p R` satisfy the axioms of a commutative ring. ## Main definitions * `witt_vector.map`: lifts a ring homomorphism `R →+* S` to a ring homomorphism `𝕎 R →+* 𝕎 S`. * `witt_vector.ghost_component n x`: evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`, producing a value in `R`. This is a ring homomorphism. * `witt_vector.ghost_map`: a ring homomorphism `𝕎 R →+* (ℕ → R)`, obtained by packaging all the ghost components together. If `p` is invertible in `R`, then the ghost map is an equivalence, which we use to define the ring operations on `𝕎 R`. * `witt_vector.comm_ring`: the ring structure induced by the ghost components. ## Notation We use notation `𝕎 R`, entered `\bbW`, for the Witt vectors over `R`. ## Implementation details As we prove that the ghost components respect the ring operations, we face a number of repetitive proofs. To avoid duplicating code we factor these proofs into a custom tactic, only slightly more powerful than a tactic macro. This tactic is not particularly useful outside of its applications in this file. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ noncomputable theory open mv_polynomial function open_locale big_operators variables {p : ℕ} {R S T : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] [comm_ring T] variables {α : Type*} {β : Type*} local notation `𝕎` := witt_vector p -- type as `\bbW` open_locale witt namespace witt_vector /-- `f : α → β` induces a map from `𝕎 α` to `𝕎 β` by applying `f` componentwise. If `f` is a ring homomorphism, then so is `f`, see `witt_vector.map f`. -/ def map_fun (f : α → β) : 𝕎 α → 𝕎 β := λ x, mk _ (f ∘ x.coeff) namespace map_fun lemma injective (f : α → β) (hf : injective f) : injective (map_fun f : 𝕎 α → 𝕎 β) := λ x y h, ext $ λ n, hf (congr_arg (λ x, coeff x n) h : _) lemma surjective (f : α → β) (hf : surjective f) : surjective (map_fun f : 𝕎 α → 𝕎 β) := λ x, ⟨mk _ (λ n, classical.some $ hf $ x.coeff n), by { ext n, dsimp [map_fun], rw classical.some_spec (hf (x.coeff n)) }⟩ variables (f : R →+* S) (x y : 𝕎 R) /-- Auxiliary tactic for showing that `map_fun` respects the ring operations. -/ meta def map_fun_tac : tactic unit := `[ext n, show f (aeval _ _) = aeval _ _, rw map_aeval, apply eval₂_hom_congr (ring_hom.ext_int _ _) _ rfl, ext ⟨i, k⟩, fin_cases i; refl] include hp /- We do not tag these lemmas as `@[simp]` because they will be bundled in `map` later on. -/ lemma zero : map_fun f (0 : 𝕎 R) = 0 := by map_fun_tac lemma one : map_fun f (1 : 𝕎 R) = 1 := by map_fun_tac lemma add : map_fun f (x + y) = map_fun f x + map_fun f y := by map_fun_tac lemma sub : map_fun f (x - y) = map_fun f x - map_fun f y := by map_fun_tac lemma mul : map_fun f (x * y) = map_fun f x * map_fun f y := by map_fun_tac lemma neg : map_fun f (-x) = -map_fun f x := by map_fun_tac end map_fun end witt_vector section tactic setup_tactic_parser open tactic /-- An auxiliary tactic for proving that `ghost_fun` respects the ring operations. -/ meta def tactic.interactive.ghost_fun_tac (φ fn : parse parser.pexpr) : tactic unit := do fn ← to_expr ```(%%fn : fin _ → ℕ → R), `(fin %%k → _ → _) ← infer_type fn, `[ext n], `[dunfold witt_vector.has_zero witt_zero witt_vector.has_one witt_one witt_vector.has_neg witt_neg witt_vector.has_mul witt_mul witt_vector.has_sub witt_sub witt_vector.has_add witt_add ], to_expr ```(congr_fun (congr_arg (@peval R _ %%k) (witt_structure_int_prop p %%φ n)) %%fn) >>= note `this none, `[simpa [ghost_fun, aeval_rename, aeval_bind₁, (∘), uncurry, peval, eval] using this] end tactic namespace witt_vector /-- Evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`, producing a value in `R`. This function will be bundled as the ring homomorphism `witt_vector.ghost_map` once the ring structure is available, but we rely on it to set up the ring structure in the first place. -/ private def ghost_fun : 𝕎 R → (ℕ → R) := λ x n, aeval x.coeff (W_ ℤ n) section ghost_fun include hp /- The following lemmas are not `@[simp]` because they will be bundled in `ghost_map` later on. -/ variables (x y : 𝕎 R) omit hp local attribute [simp] lemma matrix_vec_empty_coeff {R} (i j) : @coeff p R (matrix.vec_empty i) j = (matrix.vec_empty i : ℕ → R) j := by rcases i with ⟨_ | _ | _ | _ | i_val, ⟨⟩⟩ include hp private lemma ghost_fun_zero : ghost_fun (0 : 𝕎 R) = 0 := by ghost_fun_tac 0 ![] private lemma ghost_fun_one : ghost_fun (1 : 𝕎 R) = 1 := by ghost_fun_tac 1 ![] private lemma ghost_fun_add : ghost_fun (x + y) = ghost_fun x + ghost_fun y := by ghost_fun_tac (X 0 + X 1) ![x.coeff, y.coeff] private lemma ghost_fun_sub : ghost_fun (x - y) = ghost_fun x - ghost_fun y := by ghost_fun_tac (X 0 - X 1) ![x.coeff, y.coeff] private lemma ghost_fun_mul : ghost_fun (x * y) = ghost_fun x * ghost_fun y := by ghost_fun_tac (X 0 * X 1) ![x.coeff, y.coeff] private lemma ghost_fun_neg : ghost_fun (-x) = - ghost_fun x := by ghost_fun_tac (-X 0) ![x.coeff] end ghost_fun variables (p) (R) /-- The bijection between `𝕎 R` and `ℕ → R`, under the assumption that `p` is invertible in `R`. In `witt_vector.ghost_equiv` we upgrade this to an isomorphism of rings. -/ private def ghost_equiv' [invertible (p : R)] : 𝕎 R ≃ (ℕ → R) := { to_fun := ghost_fun, inv_fun := λ x, mk p $ λ n, aeval x (X_in_terms_of_W p R n), left_inv := begin intro x, ext n, have := bind₁_witt_polynomial_X_in_terms_of_W p R n, apply_fun (aeval x.coeff) at this, simpa only [aeval_bind₁, aeval_X, ghost_fun, aeval_witt_polynomial] end, right_inv := begin intro x, ext n, have := bind₁_X_in_terms_of_W_witt_polynomial p R n, apply_fun (aeval x) at this, simpa only [aeval_bind₁, aeval_X, ghost_fun, aeval_witt_polynomial] end } include hp local attribute [instance] private def comm_ring_aux₁ : comm_ring (𝕎 (mv_polynomial R ℚ)) := (ghost_equiv' p (mv_polynomial R ℚ)).injective.comm_ring_sub (ghost_fun) ghost_fun_zero ghost_fun_one ghost_fun_add ghost_fun_mul ghost_fun_neg ghost_fun_sub local attribute [instance] private def comm_ring_aux₂ : comm_ring (𝕎 (mv_polynomial R ℤ)) := (map_fun.injective _ $ map_injective (int.cast_ring_hom ℚ) int.cast_injective).comm_ring_sub _ (map_fun.zero _) (map_fun.one _) (map_fun.add _) (map_fun.mul _) (map_fun.neg _) (map_fun.sub _) /-- The commutative ring structure on `𝕎 R`. -/ instance : comm_ring (𝕎 R) := (map_fun.surjective _ $ counit_surjective _).comm_ring_sub (map_fun $ mv_polynomial.counit _) (map_fun.zero _) (map_fun.one _) (map_fun.add _) (map_fun.mul _) (map_fun.neg _) (map_fun.sub _) variables {p R} /-- `witt_vector.map f` is the ring homomorphism `𝕎 R →+* 𝕎 S` naturally induced by a ring homomorphism `f : R →+* S`. It acts coefficientwise. -/ def map (f : R →+* S) : 𝕎 R →+* 𝕎 S := { to_fun := map_fun f, map_zero' := map_fun.zero f, map_one' := map_fun.one f, map_add' := map_fun.add f, map_mul' := map_fun.mul f } lemma map_injective (f : R →+* S) (hf : injective f) : injective (map f : 𝕎 R → 𝕎 S) := map_fun.injective f hf lemma map_surjective (f : R →+* S) (hf : surjective f) : surjective (map f : 𝕎 R → 𝕎 S) := map_fun.surjective f hf @[simp] lemma map_coeff (f : R →+* S) (x : 𝕎 R) (n : ℕ) : (map f x).coeff n = f (x.coeff n) := rfl /-- `witt_vector.ghost_map` is a ring homomorphism that maps each Witt vector to the sequence of its ghost components. -/ def ghost_map : 𝕎 R →+* ℕ → R := { to_fun := ghost_fun, map_zero' := ghost_fun_zero, map_one' := ghost_fun_one, map_add' := ghost_fun_add, map_mul' := ghost_fun_mul } /-- Evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`, producing a value in `R`. -/ def ghost_component (n : ℕ) : 𝕎 R →+* R := (ring_hom.apply _ n).comp ghost_map lemma ghost_component_apply (n : ℕ) (x : 𝕎 R) : ghost_component n x = aeval x.coeff (W_ ℤ n) := rfl @[simp] lemma ghost_map_apply (x : 𝕎 R) (n : ℕ) : ghost_map x n = ghost_component n x := rfl variables (p R) [invertible (p : R)] /-- `witt_vector.ghost_map` is a ring isomorphism when `p` is invertible in `R`. -/ def ghost_equiv : 𝕎 R ≃+* (ℕ → R) := { .. (ghost_map : 𝕎 R →+* (ℕ → R)), .. (ghost_equiv' p R) } @[simp] lemma ghost_equiv_coe : (ghost_equiv p R : 𝕎 R →+* (ℕ → R)) = ghost_map := rfl lemma ghost_map.bijective_of_invertible : function.bijective (ghost_map : 𝕎 R → ℕ → R) := (ghost_equiv p R).bijective end witt_vector
653a20c1cd651a1aadc2b2c497a40c4b0f7be0a0
9d2e3d5a2e2342a283affd97eead310c3b528a24
/src/hints/thursday/afternoon/category_theory/exercise3/hint2.lean
5e22fcdabdb9761c8ec48e6a7c3c80e59575d8ad
[]
permissive
Vtec234/lftcm2020
ad2610ab614beefe44acc5622bb4a7fff9a5ea46
bbbd4c8162f8c2ef602300ab8fdeca231886375d
refs/heads/master
1,668,808,098,623
1,594,989,081,000
1,594,990,079,000
280,423,039
0
0
MIT
1,594,990,209,000
1,594,990,209,000
null
UTF-8
Lean
false
false
652
lean
import for_mathlib.category_theory -- This imports some simp lemmas that I realised belong in mathlib while writing this exercise. open category_theory variables {C : Type*} [category C] variables {D : Type*} [category D] lemma equiv_reflects_mono {X Y : C} (f : X ⟶ Y) (e : C ≌ D) (hef : mono (e.functor.map f)) : mono f := begin split, intros Z g h w, -- Let's think about the maths here. -- We're trying to prove an equation between morphisms in `C`, -- but the only thing we know, namely `hef`, lives over in `D`. -- So lets use the injectivity of an equivalence at the level of morphisms: apply e.functor.map_injective, end
79cdfacb52b9911de4c028021c8edb26d7c39961
93366ecea09eebeeb0b320567c6f71715434c3f0
/src/bum/auxiliary.lean
78464bb08029d7f8876981656b7c28fe2ed3de20
[ "LicenseRef-scancode-mit-taylor-variant", "LicenseRef-scancode-warranty-disclaimer" ]
permissive
o89/bum
9c1286cedb878c1e14c618e81b33d365f62c8a8a
18ffb2b46932ba767c50758a93cc99846ef1b46a
refs/heads/master
1,670,171,693,018
1,669,086,393,000
1,669,086,393,000
205,409,352
8
6
null
null
null
null
UTF-8
Lean
false
false
1,377
lean
open System (FilePath) @[extern 2 "lean_io_chdir"] constant changeDir (s : @& String) : IO UInt32 @[extern 2 "lean_io_remove"] constant IO.remove (s : @& String) : IO UInt32 @[extern 2 "lean_get_last_write_time"] constant IO.getLastWriteTime (path : @& String) : IO UInt64 @[extern 3 "lean_io_set_env"] constant IO.setEnv (name val : @& String) : IO UInt32 abbrev List.joinPath : List String → String := String.intercalate System.FilePath.pathSeparator.toString def List.space : List String → String := String.intercalate " " def IO.chdir (path : FilePath) : IO Unit := do let exitv ← changeDir (toString path) unless (exitv = 0) do throw (IO.Error.userError s!"cannot chdir to {path}") def sequence {α : Type} {m : Type → Type} [Monad m] : List (m α) → m (List α) | (hd :: tl) => do let hd' ← hd; let tl' ← sequence tl; pure (hd' :: tl') | [] => pure [] -- ??? instance Monad.HAndThen {α β : Type} {m : Type → Type} [Monad m] : HAndThen (m α) (m β) (m β) := ⟨λ a b => a >>= λ _ => b ()⟩ def uniqAux {α β : Type} [BEq β] (f : α → β) : List α → List α → List α | buff, [] => buff | buff, hd :: tl => if List.elem (f hd) (List.map f buff) then uniqAux f buff tl else uniqAux f (hd :: buff) tl def List.uniq {α β : Type} [BEq β] (f : α → β) : List α → List α := List.reverse ∘ uniqAux f []
cb47af27a0a9213ccd53b57873fac9720e0d5068
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/field_theory/perfect_closure.lean
f247b2e20259ffbb8a746a82c8650d5453994011
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,647
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.char_p.basic import data.equiv.ring import algebra.group_with_zero.power import algebra.iterate_hom /-! # The perfect closure of a field -/ universes u v open function section defs variables (K : Type u) [field K] (p : ℕ) [fact p.prime] [char_p K p] /-- A perfect field is a field of characteristic p that has p-th root. -/ class perfect_field (K : Type u) [field K] (p : ℕ) [fact p.prime] [char_p K p] : Type u := (pth_root' : K → K) (frobenius_pth_root' : ∀ x, frobenius K p (pth_root' x) = x) /-- Frobenius automorphism of a perfect field. -/ def frobenius_equiv [perfect_field K p] : K ≃+* K := { inv_fun := perfect_field.pth_root' p, left_inv := λ x, frobenius_inj K p $ perfect_field.frobenius_pth_root' _, right_inv := perfect_field.frobenius_pth_root', .. frobenius K p } /-- `p`-th root of a number in a `perfect_field` as a `ring_hom`. -/ def pth_root [perfect_field K p] : K →+* K := (frobenius_equiv K p).symm.to_ring_hom end defs section variables {K : Type u} [field K] {L : Type v} [field L] (f : K →* L) (g : K →+* L) {p : ℕ} [fact p.prime] [char_p K p] [perfect_field K p] [char_p L p] [perfect_field L p] @[simp] lemma coe_frobenius_equiv : ⇑(frobenius_equiv K p) = frobenius K p := rfl @[simp] lemma coe_frobenius_equiv_symm : ⇑(frobenius_equiv K p).symm = pth_root K p := rfl @[simp] theorem frobenius_pth_root (x : K) : frobenius K p (pth_root K p x) = x := (frobenius_equiv K p).apply_symm_apply x @[simp] theorem pth_root_frobenius (x : K) : pth_root K p (frobenius K p x) = x := (frobenius_equiv K p).symm_apply_apply x theorem left_inverse_pth_root_frobenius : left_inverse (pth_root K p) (frobenius K p) := pth_root_frobenius theorem eq_pth_root_iff {x y : K} : x = pth_root K p y ↔ frobenius K p x = y := (frobenius_equiv K p).to_equiv.eq_symm_apply theorem pth_root_eq_iff {x y : K} : pth_root K p x = y ↔ x = frobenius K p y := (frobenius_equiv K p).to_equiv.symm_apply_eq theorem monoid_hom.map_pth_root (x : K) : f (pth_root K p x) = pth_root L p (f x) := eq_pth_root_iff.2 $ by rw [← f.map_frobenius, frobenius_pth_root] theorem monoid_hom.map_iterate_pth_root (x : K) (n : ℕ) : f (pth_root K p^[n] x) = (pth_root L p^[n] (f x)) := semiconj.iterate_right f.map_pth_root n x theorem ring_hom.map_pth_root (x : K) : g (pth_root K p x) = pth_root L p (g x) := g.to_monoid_hom.map_pth_root x theorem ring_hom.map_iterate_pth_root (x : K) (n : ℕ) : g (pth_root K p^[n] x) = (pth_root L p^[n] (g x)) := g.to_monoid_hom.map_iterate_pth_root x n end section variables (K : Type u) [comm_ring K] (p : ℕ) [fact p.prime] [char_p K p] /-- `perfect_closure K p` is the quotient by this relation. -/ @[mk_iff] inductive perfect_closure.r : (ℕ × K) → (ℕ × K) → Prop | intro : ∀ n x, perfect_closure.r (n, x) (n+1, frobenius K p x) /-- The perfect closure is the smallest extension that makes frobenius surjective. -/ def perfect_closure : Type u := quot (perfect_closure.r K p) end namespace perfect_closure variables (K : Type u) section ring variables [comm_ring K] (p : ℕ) [fact p.prime] [char_p K p] /-- Constructor for `perfect_closure`. -/ def mk (x : ℕ × K) : perfect_closure K p := quot.mk (r K p) x @[simp] lemma quot_mk_eq_mk (x : ℕ × K) : (quot.mk (r K p) x : perfect_closure K p) = mk K p x := rfl variables {K p} /-- Lift a function `ℕ × K → L` to a function on `perfect_closure K p`. -/ @[elab_as_eliminator] def lift_on {L : Type*} (x : perfect_closure K p) (f : ℕ × K → L) (hf : ∀ x y, r K p x y → f x = f y) : L := quot.lift_on x f hf @[simp] lemma lift_on_mk {L : Sort*} (f : ℕ × K → L) (hf : ∀ x y, r K p x y → f x = f y) (x : ℕ × K) : (mk K p x).lift_on f hf = f x := rfl @[elab_as_eliminator] lemma induction_on (x : perfect_closure K p) {q : perfect_closure K p → Prop} (h : ∀ x, q (mk K p x)) : q x := quot.induction_on x h variables (K p) private lemma mul_aux_left (x1 x2 y : ℕ × K) (H : r K p x1 x2) : mk K p (x1.1 + y.1, ((frobenius K p)^[y.1] x1.2) * ((frobenius K p)^[x1.1] y.2)) = mk K p (x2.1 + y.1, ((frobenius K p)^[y.1] x2.2) * ((frobenius K p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro n x := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_mul, nat.succ_add]; apply r.intro end private lemma mul_aux_right (x y1 y2 : ℕ × K) (H : r K p y1 y2) : mk K p (x.1 + y1.1, ((frobenius K p)^[y1.1] x.2) * ((frobenius K p)^[x.1] y1.2)) = mk K p (x.1 + y2.1, ((frobenius K p)^[y2.1] x.2) * ((frobenius K p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro n y := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_mul]; apply r.intro end instance : has_mul (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, quot.lift (λ y:ℕ×K, mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) * ((frobenius K p)^[x.1] y.2))) (mul_aux_right K p x)) (λ x1 x2 (H : r K p x1 x2), funext $ λ e, quot.induction_on e $ λ y, mul_aux_left K p x1 x2 y H)⟩ @[simp] lemma mk_mul_mk (x y : ℕ × K) : mk K p x * mk K p y = mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) * ((frobenius K p)^[x.1] y.2)) := rfl instance : comm_monoid (perfect_closure K p) := { mul_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, mul_assoc, ring_hom.iterate_map_mul, ← iterate_add_apply, add_comm, add_left_comm], one := mk K p (0, 1), one_mul := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_one, iterate_zero_apply, one_mul, zero_add]), mul_one := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_one, iterate_zero_apply, mul_one, add_zero]), mul_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm, mul_comm])), .. (infer_instance : has_mul (perfect_closure K p)) } lemma one_def : (1 : perfect_closure K p) = mk K p (0, 1) := rfl instance : inhabited (perfect_closure K p) := ⟨1⟩ private lemma add_aux_left (x1 x2 y : ℕ × K) (H : r K p x1 x2) : mk K p (x1.1 + y.1, ((frobenius K p)^[y.1] x1.2) + ((frobenius K p)^[x1.1] y.2)) = mk K p (x2.1 + y.1, ((frobenius K p)^[y.1] x2.2) + ((frobenius K p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro n x := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_add, nat.succ_add]; apply r.intro end private lemma add_aux_right (x y1 y2 : ℕ × K) (H : r K p y1 y2) : mk K p (x.1 + y1.1, ((frobenius K p)^[y1.1] x.2) + ((frobenius K p)^[x.1] y1.2)) = mk K p (x.1 + y2.1, ((frobenius K p)^[y2.1] x.2) + ((frobenius K p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro n y := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_add]; apply r.intro end instance : has_add (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, quot.lift (λ y:ℕ×K, mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) + ((frobenius K p)^[x.1] y.2))) (add_aux_right K p x)) (λ x1 x2 (H : r K p x1 x2), funext $ λ e, quot.induction_on e $ λ y, add_aux_left K p x1 x2 y H)⟩ @[simp] lemma mk_add_mk (x y : ℕ × K) : mk K p x + mk K p y = mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) + ((frobenius K p)^[x.1] y.2)) := rfl instance : has_neg (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, mk K p (x.1, -x.2)) (λ x y (H : r K p x y), match x, y, H with | _, _, r.intro n x := quot.sound $ by rw ← frobenius_neg; apply r.intro end)⟩ @[simp] lemma neg_mk (x : ℕ × K) : - mk K p x = mk K p (x.1, -x.2) := rfl instance : has_zero (perfect_closure K p) := ⟨mk K p (0, 0)⟩ lemma zero_def : (0 : perfect_closure K p) = mk K p (0, 0) := rfl theorem mk_zero (n : ℕ) : mk K p (n, 0) = 0 := by induction n with n ih; [refl, rw ← ih]; symmetry; apply quot.sound; have := r.intro n (0:K); rwa [frobenius_zero K p] at this theorem r.sound (m n : ℕ) (x y : K) (H : frobenius K p^[m] x = y) : mk K p (n, x) = mk K p (m + n, y) := by subst H; induction m with m ih; [simp only [zero_add, iterate_zero_apply], rw [ih, nat.succ_add, iterate_succ']]; apply quot.sound; apply r.intro instance : comm_ring (perfect_closure K p) := { add_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, ring_hom.iterate_map_add, ← iterate_add_apply, add_comm, add_left_comm], zero := 0, zero_add := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_zero, iterate_zero_apply, zero_add]), add_zero := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_zero, iterate_zero_apply, add_zero]), add_left_neg := λ e, quot.induction_on e (λ ⟨n, x⟩, by simp only [quot_mk_eq_mk, neg_mk, mk_add_mk, ring_hom.iterate_map_neg, add_left_neg, mk_zero]), add_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm])), left_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm, add_left_comm]; apply r.sound; simp only [ring_hom.iterate_map_mul, ring_hom.iterate_map_add, ← iterate_add_apply, mul_add, add_comm, add_left_comm], right_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm _ s, add_left_comm _ s]; apply r.sound; simp only [ring_hom.iterate_map_mul, ring_hom.iterate_map_add, ← iterate_add_apply, add_mul, add_comm, add_left_comm], .. (infer_instance : has_add (perfect_closure K p)), .. (infer_instance : has_neg (perfect_closure K p)), .. (infer_instance : comm_monoid (perfect_closure K p)) } theorem eq_iff' (x y : ℕ × K) : mk K p x = mk K p y ↔ ∃ z, (frobenius K p^[y.1 + z] x.2) = (frobenius K p^[x.1 + z] y.2) := begin split, { intro H, replace H := quot.exact _ H, induction H, case eqv_gen.rel : x y H { cases H with n x, exact ⟨0, rfl⟩ }, case eqv_gen.refl : H { exact ⟨0, rfl⟩ }, case eqv_gen.symm : x y H ih { cases ih with w ih, exact ⟨w, ih.symm⟩ }, case eqv_gen.trans : x y z H1 H2 ih1 ih2 { cases ih1 with z1 ih1, cases ih2 with z2 ih2, existsi z2+(y.1+z1), rw [← add_assoc, iterate_add_apply, ih1], rw [← iterate_add_apply, add_comm, iterate_add_apply, ih2], rw [← iterate_add_apply], simp only [add_comm, add_left_comm] } }, intro H, cases x with m x, cases y with n y, cases H with z H, dsimp only at H, rw [r.sound K p (n+z) m x _ rfl, r.sound K p (m+z) n y _ rfl, H], rw [add_assoc, add_comm, add_comm z] end theorem nat_cast (n x : ℕ) : (x : perfect_closure K p) = mk K p (n, x) := begin induction n with n ih, { induction x with x ih, {refl}, rw [nat.cast_succ, nat.cast_succ, ih], refl }, rw ih, apply quot.sound, conv {congr, skip, skip, rw ← frobenius_nat_cast K p x}, apply r.intro end theorem int_cast (x : ℤ) : (x : perfect_closure K p) = mk K p (0, x) := by induction x; simp only [int.cast_of_nat, int.cast_neg_succ_of_nat, nat_cast K p 0]; refl theorem nat_cast_eq_iff (x y : ℕ) : (x : perfect_closure K p) = y ↔ (x : K) = y := begin split; intro H, { rw [nat_cast K p 0, nat_cast K p 0, eq_iff'] at H, cases H with z H, simpa only [zero_add, iterate_fixed (frobenius_nat_cast K p _)] using H }, rw [nat_cast K p 0, nat_cast K p 0, H] end instance : char_p (perfect_closure K p) p := begin constructor, intro x, rw ← char_p.cast_eq_zero_iff K, rw [← nat.cast_zero, nat_cast_eq_iff, nat.cast_zero] end theorem frobenius_mk (x : ℕ × K) : (frobenius (perfect_closure K p) p : perfect_closure K p → perfect_closure K p) (mk K p x) = mk _ _ (x.1, x.2^p) := begin simp only [frobenius_def], cases x with n x, dsimp only, suffices : ∀ p':ℕ, mk K p (n, x) ^ p' = mk K p (n, x ^ p'), { apply this }, intro p, induction p with p ih, case nat.zero { apply r.sound, rw [(frobenius _ _).iterate_map_one, pow_zero] }, case nat.succ { rw [pow_succ, ih], symmetry, apply r.sound, simp only [pow_succ, (frobenius _ _).iterate_map_mul] } end /-- Embedding of `K` into `perfect_closure K p` -/ def of : K →+* perfect_closure K p := { to_fun := λ x, mk _ _ (0, x), map_one' := rfl, map_mul' := λ x y, rfl, map_zero' := rfl, map_add' := λ x y, rfl } lemma of_apply (x : K) : of K p x = mk _ _ (0, x) := rfl end ring theorem eq_iff [integral_domain K] (p : ℕ) [fact p.prime] [char_p K p] (x y : ℕ × K) : quot.mk (r K p) x = quot.mk (r K p) y ↔ (frobenius K p^[y.1] x.2) = (frobenius K p^[x.1] y.2) := (eq_iff' K p x y).trans ⟨λ ⟨z, H⟩, (frobenius_inj K p).iterate z $ by simpa only [add_comm, iterate_add] using H, λ H, ⟨0, H⟩⟩ section field variables [field K] (p : ℕ) [fact p.prime] [char_p K p] instance : has_inv (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, quot.mk (r K p) (x.1, x.2⁻¹)) (λ x y (H : r K p x y), match x, y, H with | _, _, r.intro n x := quot.sound $ by { simp only [frobenius_def], rw ← inv_pow', apply r.intro } end)⟩ instance : field (perfect_closure K p) := { exists_pair_ne := ⟨0, 1, λ H, zero_ne_one ((eq_iff _ _ _ _).1 H)⟩, mul_inv_cancel := λ e, induction_on e $ λ ⟨m, x⟩ H, have _ := mt (eq_iff _ _ _ _).2 H, (eq_iff _ _ _ _).2 (by simp only [(frobenius _ _).iterate_map_one, (frobenius K p).iterate_map_zero, iterate_zero_apply, ← (frobenius _ p).iterate_map_mul] at this ⊢; rw [mul_inv_cancel this, (frobenius _ _).iterate_map_one]), inv_zero := congr_arg (quot.mk (r K p)) (by rw [inv_zero]), .. (infer_instance : has_inv (perfect_closure K p)), .. (infer_instance : comm_ring (perfect_closure K p)) } instance : perfect_field (perfect_closure K p) p := { pth_root' := λ e, lift_on e (λ x, mk K p (x.1 + 1, x.2)) (λ x y H, match x, y, H with | _, _, r.intro n x := quot.sound (r.intro _ _) end), frobenius_pth_root' := λ e, induction_on e (λ ⟨n, x⟩, by { simp only [lift_on_mk, frobenius_mk], exact (quot.sound $ r.intro _ _).symm }) } theorem eq_pth_root (x : ℕ × K) : mk K p x = (pth_root (perfect_closure K p) p^[x.1] (of K p x.2)) := begin rcases x with ⟨m, x⟩, induction m with m ih, {refl}, rw [iterate_succ_apply', ← ih]; refl end /-- Given a field `K` of characteristic `p` and a perfect field `L` of the same characteristic, any homomorphism `K →+* L` can be lifted to `perfect_closure K p`. -/ def lift (L : Type v) [field L] [char_p L p] [perfect_field L p] : (K →+* L) ≃ (perfect_closure K p →+* L) := begin have := left_inverse_pth_root_frobenius.iterate, refine_struct { .. }, field to_fun { intro f, refine_struct { .. }, field to_fun { refine λ e, lift_on e (λ x, pth_root L p^[x.1] (f x.2)) _, rintro a b ⟨n⟩, simp only [f.map_frobenius, iterate_succ_apply, pth_root_frobenius] }, field map_one' { exact f.map_one }, field map_zero' { exact f.map_zero }, field map_mul' { rintro ⟨x⟩ ⟨y⟩, simp only [quot_mk_eq_mk, lift_on_mk, mk_mul_mk, ring_hom.map_iterate_frobenius, ring_hom.iterate_map_mul, ring_hom.map_mul], rw [iterate_add_apply, this _ _, add_comm, iterate_add_apply, this _ _] }, field map_add' { rintro ⟨x⟩ ⟨y⟩, simp only [quot_mk_eq_mk, lift_on_mk, mk_add_mk, ring_hom.map_iterate_frobenius, ring_hom.iterate_map_add, ring_hom.map_add], rw [iterate_add_apply, this _ _, add_comm x.1, iterate_add_apply, this _ _] } }, field inv_fun { exact λ f, f.comp (of K p) }, field left_inv { intro f, ext x, refl }, field right_inv { intro f, ext ⟨x⟩, simp only [ring_hom.coe_mk, quot_mk_eq_mk, ring_hom.comp_apply, lift_on_mk], rw [eq_pth_root, ring_hom.map_iterate_pth_root] } end end field end perfect_closure
bc4bea87a888f7e00dd38f53ab5e6dc20b0c563b
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/ring_theory/noetherian.lean
9ad8a180eaaf521e7e86a455a1dc9a76bac25aa0
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
21,255
lean
/- Copyright (c) 2018 Mario Carneiro and Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kevin Buzzard -/ import data.equiv.algebra import linear_algebra.finsupp import ring_theory.ideal_operations import ring_theory.subring import linear_algebra.basis open set lattice namespace submodule variables {α : Type*} {β : Type*} [ring α] [add_comm_group β] [module α β] def fg (s : submodule α β) : Prop := ∃ t : finset β, submodule.span α ↑t = s theorem fg_def {s : submodule α β} : s.fg ↔ ∃ t : set β, finite t ∧ span α t = s := ⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, h⟩, begin rintro ⟨t', h, rfl⟩, rcases finite.exists_finset_coe h with ⟨t, rfl⟩, exact ⟨t, rfl⟩ end⟩ /-- Nakayama's Lemma. Atiyah-Macdonald 2.5, Eisenbud 4.7, Matsumura 2.2, Stacks 00DV -/ theorem exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul {R : Type*} [comm_ring R] {M : Type*} [add_comm_group M] [module R M] (I : ideal R) (N : submodule R M) (hn : N.fg) (hin : N ≤ I • N) : ∃ r : R, r - 1 ∈ I ∧ ∀ n ∈ N, r • n = (0 : M) := begin rw fg_def at hn, rcases hn with ⟨s, hfs, hs⟩, have : ∃ r : R, r - 1 ∈ I ∧ N ≤ (I • span R s).comap (linear_map.lsmul R M r) ∧ s ⊆ N, { refine ⟨1, _, _, _⟩, { rw sub_self, exact I.zero_mem }, { rw [hs], intros n hn, rw [mem_coe, mem_comap], change (1:R) • n ∈ I • N, rw one_smul, exact hin hn }, { rw [← span_le, hs], exact le_refl N } }, clear hin hs, revert this, refine set.finite.dinduction_on hfs (λ H, _) (λ i s his hfs ih H, _), { rcases H with ⟨r, hr1, hrn, hs⟩, refine ⟨r, hr1, λ n hn, _⟩, specialize hrn hn, rwa [mem_coe, mem_comap, span_empty, smul_bot, mem_bot] at hrn }, apply ih, rcases H with ⟨r, hr1, hrn, hs⟩, rw [← set.singleton_union, span_union, smul_sup] at hrn, rw [set.insert_subset] at hs, have : ∃ c : R, c - 1 ∈ I ∧ c • i ∈ I • span R s, { specialize hrn hs.1, rw [mem_coe, mem_comap, mem_sup] at hrn, rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • i at hyz, rw mem_smul_span_singleton at hy, rcases hy with ⟨c, hci, rfl⟩, use r-c, split, { rw [sub_right_comm], exact I.sub_mem hr1 hci }, { rw [sub_smul, ← hyz, add_sub_cancel'], exact hz } }, rcases this with ⟨c, hc1, hci⟩, refine ⟨c * r, _, _, hs.2⟩, { rw [← ideal.quotient.eq, ideal.quotient.mk_one] at hr1 hc1 ⊢, rw [ideal.quotient.mk_mul, hc1, hr1, mul_one] }, { intros n hn, specialize hrn hn, rw [mem_coe, mem_comap, mem_sup] at hrn, rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • n at hyz, rw mem_smul_span_singleton at hy, rcases hy with ⟨d, hdi, rfl⟩, change _ • _ ∈ I • span R s, rw [mul_smul, ← hyz, smul_add, smul_smul, mul_comm, mul_smul], exact add_mem _ (smul_mem _ _ hci) (smul_mem _ _ hz) } end theorem fg_bot : (⊥ : submodule α β).fg := ⟨∅, by rw [finset.coe_empty, span_empty]⟩ theorem fg_sup {s₁ s₂ : submodule α β} (hs₁ : s₁.fg) (hs₂ : s₂.fg) : (s₁ ⊔ s₂).fg := let ⟨t₁, ht₁⟩ := fg_def.1 hs₁, ⟨t₂, ht₂⟩ := fg_def.1 hs₂ in fg_def.2 ⟨t₁ ∪ t₂, finite_union ht₁.1 ht₂.1, by rw [span_union, ht₁.2, ht₂.2]⟩ variables {γ : Type*} [add_comm_group γ] [module α γ] variables {f : β →ₗ[α] γ} theorem fg_map {s : submodule α β} (hs : s.fg) : (s.map f).fg := let ⟨t, ht⟩ := fg_def.1 hs in fg_def.2 ⟨f '' t, finite_image _ ht.1, by rw [span_image, ht.2]⟩ theorem fg_prod {sb : submodule α β} {sc : submodule α γ} (hsb : sb.fg) (hsc : sc.fg) : (sb.prod sc).fg := let ⟨tb, htb⟩ := fg_def.1 hsb, ⟨tc, htc⟩ := fg_def.1 hsc in fg_def.2 ⟨prod.inl '' tb ∪ prod.inr '' tc, finite_union (finite_image _ htb.1) (finite_image _ htc.1), by rw [linear_map.span_inl_union_inr, htb.2, htc.2]⟩ variable (f) /-- If 0 → M' → M → M'' → 0 is exact and M' and M'' are finitely generated then so is M. -/ theorem fg_of_fg_map_of_fg_inf_ker {s : submodule α β} (hs1 : (s.map f).fg) (hs2 : (s ⊓ f.ker).fg) : s.fg := begin haveI := classical.dec_eq α, haveI := classical.dec_eq β, haveI := classical.dec_eq γ, cases hs1 with t1 ht1, cases hs2 with t2 ht2, have : ∀ y ∈ t1, ∃ x ∈ s, f x = y, { intros y hy, have : y ∈ map f s, { rw ← ht1, exact subset_span hy }, rcases mem_map.1 this with ⟨x, hx1, hx2⟩, exact ⟨x, hx1, hx2⟩ }, have : ∃ g : γ → β, ∀ y ∈ t1, g y ∈ s ∧ f (g y) = y, { choose g hg1 hg2, existsi λ y, if H : y ∈ t1 then g y H else 0, intros y H, split, { simp only [dif_pos H], apply hg1 }, { simp only [dif_pos H], apply hg2 } }, cases this with g hg, clear this, existsi t1.image g ∪ t2, rw [finset.coe_union, span_union, finset.coe_image], apply le_antisymm, { refine sup_le (span_le.2 $ image_subset_iff.2 _) (span_le.2 _), { intros y hy, exact (hg y hy).1 }, { intros x hx, have := subset_span hx, rw ht2 at this, exact this.1 } }, intros x hx, have : f x ∈ map f s, { rw mem_map, exact ⟨x, hx, rfl⟩ }, rw [← ht1,← set.image_id ↑t1, finsupp.mem_span_iff_total] at this, rcases this with ⟨l, hl1, hl2⟩, refine mem_sup.2 ⟨(finsupp.total β β α id).to_fun ((finsupp.lmap_domain α α g : (γ →₀ α) → β →₀ α) l), _, x - finsupp.total β β α id ((finsupp.lmap_domain α α g : (γ →₀ α) → β →₀ α) l), _, add_sub_cancel'_right _ _⟩, { rw [← set.image_id (g '' ↑t1), finsupp.mem_span_iff_total], refine ⟨_, _, rfl⟩, haveI : inhabited γ := ⟨0⟩, rw [← finsupp.lmap_domain_supported _ _ g, mem_map], refine ⟨l, hl1, _⟩, refl, }, rw [ht2, mem_inf], split, { apply s.sub_mem hx, rw [finsupp.total_apply, finsupp.lmap_domain_apply, finsupp.sum_map_domain_index], refine s.sum_mem _, { intros y hy, exact s.smul_mem _ (hg y (hl1 hy)).1 }, { exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } }, { rw [linear_map.mem_ker, f.map_sub, ← hl2], rw [finsupp.total_apply, finsupp.total_apply, finsupp.lmap_domain_apply], rw [finsupp.sum_map_domain_index, finsupp.sum, finsupp.sum, f.map_sum], rw sub_eq_zero, refine finset.sum_congr rfl (λ y hy, _), unfold id, rw [f.map_smul, (hg y (hl1 hy)).2], { exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } } end end submodule class is_noetherian (α β) [ring α] [add_comm_group β] [module α β] : Prop := (noetherian : ∀ (s : submodule α β), s.fg) section variables {α : Type*} {β : Type*} {γ : Type*} variables [ring α] [add_comm_group β] [add_comm_group γ] variables [module α β] [module α γ] open is_noetherian include α theorem is_noetherian_submodule {N : submodule α β} : is_noetherian α N ↔ ∀ s : submodule α β, s ≤ N → s.fg := ⟨λ ⟨hn⟩, λ s hs, have s ≤ N.subtype.range, from (N.range_subtype).symm ▸ hs, linear_map.map_comap_eq_self this ▸ submodule.fg_map (hn _), λ h, ⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker N.subtype (h _ $ submodule.map_subtype_le _ _) $ by rw [submodule.ker_subtype, inf_bot_eq]; exact submodule.fg_bot⟩⟩ theorem is_noetherian_submodule_left {N : submodule α β} : is_noetherian α N ↔ ∀ s : submodule α β, (N ⊓ s).fg := is_noetherian_submodule.trans ⟨λ H s, H _ inf_le_left, λ H s hs, (inf_of_le_right hs) ▸ H _⟩ theorem is_noetherian_submodule_right {N : submodule α β} : is_noetherian α N ↔ ∀ s : submodule α β, (s ⊓ N).fg := is_noetherian_submodule.trans ⟨λ H s, H _ inf_le_right, λ H s hs, (inf_of_le_left hs) ▸ H _⟩ variable (β) theorem is_noetherian_of_surjective (f : β →ₗ[α] γ) (hf : f.range = ⊤) [is_noetherian α β] : is_noetherian α γ := ⟨λ s, have (s.comap f).map f = s, from linear_map.map_comap_eq_self $ hf.symm ▸ le_top, this ▸ submodule.fg_map $ noetherian _⟩ variable {β} theorem is_noetherian_of_linear_equiv (f : β ≃ₗ[α] γ) [is_noetherian α β] : is_noetherian α γ := is_noetherian_of_surjective _ f.to_linear_map f.range instance is_noetherian_prod [is_noetherian α β] [is_noetherian α γ] : is_noetherian α (β × γ) := ⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker (linear_map.snd α β γ) (noetherian _) $ have s ⊓ linear_map.ker (linear_map.snd α β γ) ≤ linear_map.range (linear_map.inl α β γ), from λ x ⟨hx1, hx2⟩, ⟨x.1, trivial, prod.ext rfl $ eq.symm $ linear_map.mem_ker.1 hx2⟩, linear_map.map_comap_eq_self this ▸ submodule.fg_map (noetherian _)⟩ instance is_noetherian_pi {α ι : Type*} {β : ι → Type*} [ring α] [Π i, add_comm_group (β i)] [Π i, module α (β i)] [fintype ι] [∀ i, is_noetherian α (β i)] : is_noetherian α (Π i, β i) := begin haveI := classical.dec_eq ι, suffices : ∀ s : finset ι, is_noetherian α (Π i : (↑s : set ι), β i), { letI := this finset.univ, refine @is_noetherian_of_linear_equiv _ _ _ _ _ _ _ _ ⟨_, _, _, _, _, _⟩ (this finset.univ), { exact λ f i, f ⟨i, finset.mem_univ _⟩ }, { intros, ext, refl }, { intros, ext, refl }, { exact λ f i, f i.1 }, { intro, ext i, cases i, refl }, { intro, ext i, refl } }, intro s, induction s using finset.induction with a s has ih, { split, intro s, convert submodule.fg_bot, apply eq_bot_iff.2, intros x hx, refine (submodule.mem_bot α).2 _, ext i, cases i.2 }, refine @is_noetherian_of_linear_equiv _ _ _ _ _ _ _ _ ⟨_, _, _, _, _, _⟩ (@is_noetherian_prod _ (β a) _ _ _ _ _ _ _ ih), { exact λ f i, or.by_cases (finset.mem_insert.1 i.2) (λ h : i.1 = a, show β i.1, from (eq.rec_on h.symm f.1)) (λ h : i.1 ∈ s, show β i.1, from f.2 ⟨i.1, h⟩) }, { intros f g, ext i, unfold or.by_cases, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { change _ = _ + _, simp only [dif_pos], refl }, { change _ = _ + _, have : ¬i = a, { rintro rfl, exact has h }, simp only [dif_neg this, dif_pos h], refl } }, { intros c f, ext i, unfold or.by_cases, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { change _ = c • _, simp only [dif_pos], refl }, { change _ = c • _, have : ¬i = a, { rintro rfl, exact has h }, simp only [dif_neg this, dif_pos h], refl } }, { exact λ f, (f ⟨a, finset.mem_insert_self _ _⟩, λ i, f ⟨i.1, finset.mem_insert_of_mem i.2⟩) }, { intro f, apply prod.ext, { simp only [or.by_cases, dif_pos] }, { ext i, cases i with i his, have : ¬i = a, { rintro rfl, exact has his }, dsimp only [or.by_cases], change i ∈ s at his, rw [dif_neg this, dif_pos his] } }, { intro f, ext i, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { simp only [or.by_cases, dif_pos], refl }, { have : ¬i = a, { rintro rfl, exact has h }, simp only [or.by_cases, dif_neg this, dif_pos h], refl } } end end open is_noetherian submodule function theorem is_noetherian_iff_well_founded {α β} [ring α] [add_comm_group β] [module α β] : is_noetherian α β ↔ well_founded ((>) : submodule α β → submodule α β → Prop) := ⟨λ h, begin apply order_embedding.well_founded_iff_no_descending_seq.2, swap, { apply is_strict_order.swap }, rintro ⟨⟨N, hN⟩⟩, let M := ⨆ n, N n, resetI, rcases submodule.fg_def.1 (noetherian M) with ⟨t, h₁, h₂⟩, have hN' : ∀ {a b}, a ≤ b → N a ≤ N b := λ a b, (strict_mono.le_iff_le (λ _ _, hN.1)).2, have : t ⊆ ⋃ i, (N i : set β), { rw [← submodule.Union_coe_of_directed _ N _], { show t ⊆ M, rw ← h₂, apply submodule.subset_span }, { apply_instance }, { exact λ i j, ⟨max i j, hN' (le_max_left _ _), hN' (le_max_right _ _)⟩ } }, simp [subset_def] at this, choose f hf using show ∀ x : t, ∃ (i : ℕ), x.1 ∈ N i, { simpa }, cases h₁ with h₁, let A := finset.sup (@finset.univ t h₁) f, have : M ≤ N A, { rw ← h₂, apply submodule.span_le.2, exact λ x h, hN' (finset.le_sup (@finset.mem_univ t h₁ _)) (hf ⟨x, h⟩) }, exact not_le_of_lt (hN.1 (nat.lt_succ_self A)) (le_trans (le_supr _ _) this) end, begin assume h, split, assume N, suffices : ∀ M ≤ N, ∃ s, finite s ∧ M ⊔ submodule.span α s = N, { rcases this ⊥ bot_le with ⟨s, hs, e⟩, exact submodule.fg_def.2 ⟨s, hs, by simpa using e⟩ }, refine λ M, h.induction M _, intros M IH MN, letI := classical.dec, by_cases h : ∀ x, x ∈ N → x ∈ M, { cases le_antisymm MN h, exact ⟨∅, by simp⟩ }, { simp [not_forall] at h, rcases h with ⟨x, h, h₂⟩, have : ¬M ⊔ submodule.span α {x} ≤ M, { intro hn, apply h₂, have := le_trans le_sup_right hn, exact submodule.span_le.1 this (mem_singleton x) }, rcases IH (M ⊔ submodule.span α {x}) ⟨@le_sup_left _ _ M _, this⟩ (sup_le MN (submodule.span_le.2 (by simpa))) with ⟨s, hs, hs₂⟩, refine ⟨insert x s, finite_insert _ hs, _⟩, rw [← hs₂, sup_assoc, ← submodule.span_union], simp } end⟩ lemma well_founded_submodule_gt (α β) [ring α] [add_comm_group β] [module α β] : ∀ [is_noetherian α β], well_founded ((>) : submodule α β → submodule α β → Prop) := is_noetherian_iff_well_founded.mp lemma finite_of_linear_independent {α β} [nonzero_comm_ring α] [add_comm_group β] [module α β] [is_noetherian α β] {s : set β} (hs : linear_independent α (subtype.val : s → β)) : s.finite := begin refine classical.by_contradiction (λ hf, order_embedding.well_founded_iff_no_descending_seq.1 (well_founded_submodule_gt α β) ⟨_⟩), have f : ℕ ↪ s, from @infinite.nat_embedding s ⟨λ f, hf ⟨f⟩⟩, have : ∀ n, (subtype.val ∘ f) '' {m | m ≤ n} ⊆ s, { rintros n x ⟨y, hy₁, hy₂⟩, subst hy₂, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span α ((subtype.val ∘ f) '' {m | m ≤ a}) ≤ span α ((subtype.val ∘ f) '' {m | m ≤ b}), { assume a b, rw [span_le_span_iff (@zero_ne_one α _) hs (this a) (this b), set.image_subset_image_iff (injective_comp subtype.val_injective f.inj'), set.subset_def], exact ⟨λ hab x (hxa : x ≤ a), le_trans hxa hab, λ hx, hx a (le_refl a)⟩ }, exact ⟨⟨λ n, span α ((subtype.val ∘ f) '' {m | m ≤ n}), λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩, by dsimp [gt]; simp only [lt_iff_le_not_le, (this _ _).symm]; tauto⟩ end @[class] def is_noetherian_ring (α) [ring α] : Prop := is_noetherian α α instance is_noetherian_ring.to_is_noetherian {α : Type*} [ring α] : ∀ [is_noetherian_ring α], is_noetherian α α := id instance ring.is_noetherian_of_fintype (R M) [fintype M] [ring R] [add_comm_group M] [module R M] : is_noetherian R M := by letI := classical.dec; exact ⟨assume s, ⟨to_finset s, by rw [finset.coe_to_finset', submodule.span_eq]⟩⟩ theorem ring.is_noetherian_of_zero_eq_one {R} [ring R] (h01 : (0 : R) = 1) : is_noetherian_ring R := by haveI := subsingleton_of_zero_eq_one R h01; haveI := fintype.of_subsingleton (0:R); exact ring.is_noetherian_of_fintype _ _ theorem is_noetherian_of_submodule_of_noetherian (R M) [ring R] [add_comm_group M] [module R M] (N : submodule R M) (h : is_noetherian R M) : is_noetherian R N := begin rw is_noetherian_iff_well_founded at h ⊢, convert order_embedding.well_founded (order_embedding.rsymm (submodule.map_subtype.lt_order_embedding N)) h end theorem is_noetherian_of_quotient_of_noetherian (R) [ring R] (M) [add_comm_group M] [module R M] (N : submodule R M) (h : is_noetherian R M) : is_noetherian R N.quotient := begin rw is_noetherian_iff_well_founded at h ⊢, convert order_embedding.well_founded (order_embedding.rsymm (submodule.comap_mkq.lt_order_embedding N)) h end theorem is_noetherian_of_fg_of_noetherian {R M} [ring R] [add_comm_group M] [module R M] (N : submodule R M) [is_noetherian_ring R] (hN : N.fg) : is_noetherian R N := let ⟨s, hs⟩ := hN in begin haveI := classical.dec_eq M, haveI := classical.dec_eq R, letI : is_noetherian R R := by apply_instance, have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx, refine @@is_noetherian_of_surjective ((↑s : set M) → R) _ _ _ (pi.module _) _ _ _ is_noetherian_pi, { fapply linear_map.mk, { exact λ f, ⟨s.attach.sum (λ i, f i • i.1), N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ }, { intros f g, apply subtype.eq, change s.attach.sum (λ i, (f i + g i) • _) = _, simp only [add_smul, finset.sum_add_distrib], refl }, { intros c f, apply subtype.eq, change s.attach.sum (λ i, (c • f i) • _) = _, simp only [smul_eq_mul, mul_smul], exact finset.sum_hom _ } }, rw linear_map.range_eq_top, rintro ⟨n, hn⟩, change n ∈ N at hn, rw [← hs, ← set.image_id ↑s, finsupp.mem_span_iff_total] at hn, rcases hn with ⟨l, hl1, hl2⟩, refine ⟨λ x, l x.1, subtype.eq _⟩, change s.attach.sum (λ i, l i.1 • i.1) = n, rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2, finsupp.total_apply, finsupp.sum, eq_comm], refine finset.sum_subset hl1 (λ x _ hx, _), rw [finsupp.not_mem_support_iff.1 hx, zero_smul] end theorem is_noetherian_ring_of_surjective (R) [comm_ring R] (S) [comm_ring S] (f : R → S) [is_ring_hom f] (hf : function.surjective f) [H : is_noetherian_ring R] : is_noetherian_ring S := begin unfold is_noetherian_ring at H ⊢, rw is_noetherian_iff_well_founded at H ⊢, convert order_embedding.well_founded (order_embedding.rsymm (ideal.lt_order_embedding_of_surjective f hf)) H end instance is_noetherian_ring_range {R} [comm_ring R] {S} [comm_ring S] (f : R → S) [is_ring_hom f] [is_noetherian_ring R] : is_noetherian_ring (set.range f) := @is_noetherian_ring_of_surjective R _ (set.range f) _ (λ x, ⟨f x, x, rfl⟩) (⟨subtype.eq (is_ring_hom.map_one f), λ _ _, subtype.eq (is_ring_hom.map_mul f), λ _ _, subtype.eq (is_ring_hom.map_add f)⟩) (λ ⟨x, y, hy⟩, ⟨y, subtype.eq hy⟩) _ theorem is_noetherian_ring_of_ring_equiv (R) [comm_ring R] {S} [comm_ring S] (f : R ≃+* S) [is_noetherian_ring R] : is_noetherian_ring S := is_noetherian_ring_of_surjective R S f.1 f.to_equiv.surjective namespace is_noetherian_ring variables {α : Type*} [integral_domain α] [is_noetherian_ring α] open associates nat local attribute [elab_as_eliminator] well_founded.fix lemma well_founded_dvd_not_unit : well_founded (λ a b : α, a ≠ 0 ∧ ∃ x, ¬is_unit x ∧ b = a * x ) := by simp only [ideal.span_singleton_lt_span_singleton.symm]; exact inv_image.wf (λ a, ideal.span ({a} : set α)) (well_founded_submodule_gt _ _) lemma exists_irreducible_factor {a : α} (ha : ¬ is_unit a) (ha0 : a ≠ 0) : ∃ i, irreducible i ∧ i ∣ a := (irreducible_or_factor a ha).elim (λ hai, ⟨a, hai, dvd_refl _⟩) (well_founded.fix well_founded_dvd_not_unit (λ a ih ha ha0 ⟨x, y, hx, hy, hxy⟩, have hx0 : x ≠ 0, from λ hx0, ha0 (by rw [← hxy, hx0, zero_mul]), (irreducible_or_factor x hx).elim (λ hxi, ⟨x, hxi, hxy ▸ by simp⟩) (λ hxf, let ⟨i, hi⟩ := ih x ⟨hx0, y, hy, hxy.symm⟩ hx hx0 hxf in ⟨i, hi.1, dvd.trans hi.2 (hxy ▸ by simp)⟩)) a ha ha0) @[elab_as_eliminator] lemma irreducible_induction_on {P : α → Prop} (a : α) (h0 : P 0) (hu : ∀ u : α, is_unit u → P u) (hi : ∀ a i : α, a ≠ 0 → irreducible i → P a → P (i * a)) : P a := by haveI := classical.dec; exact well_founded.fix well_founded_dvd_not_unit (λ a ih, if ha0 : a = 0 then ha0.symm ▸ h0 else if hau : is_unit a then hu a hau else let ⟨i, hii, ⟨b, hb⟩⟩ := exists_irreducible_factor hau ha0 in have hb0 : b ≠ 0, from λ hb0, by simp * at *, hb.symm ▸ hi _ _ hb0 hii (ih _ ⟨hb0, i, hii.1, by rw [hb, mul_comm]⟩)) a lemma exists_factors (a : α) : a ≠ 0 → ∃f:multiset α, (∀b∈f, irreducible b) ∧ associated a f.prod := is_noetherian_ring.irreducible_induction_on a (λ h, (h rfl).elim) (λ u hu _, ⟨0, by simp [associated_one_iff_is_unit, hu]⟩) (λ a i ha0 hii ih hia0, let ⟨s, hs⟩ := ih ha0 in ⟨i::s, ⟨by clear _let_match; finish, by rw multiset.prod_cons; exact associated_mul_mul (by refl) hs.2⟩⟩) end is_noetherian_ring namespace submodule variables {R : Type*} {A : Type*} [comm_ring R] [ring A] [algebra R A] variables (M N : submodule R A) local attribute [instance] set.pointwise_mul_semiring theorem fg_mul (hm : M.fg) (hn : N.fg) : (M * N).fg := let ⟨m, hfm, hm⟩ := fg_def.1 hm, ⟨n, hfn, hn⟩ := fg_def.1 hn in fg_def.2 ⟨m * n, set.pointwise_mul_finite hfm hfn, span_mul_span R m n ▸ hm ▸ hn ▸ rfl⟩ lemma fg_pow (h : M.fg) (n : ℕ) : (M^n).fg := nat.rec_on n (⟨finset.singleton 1, by simp [one_eq_span]⟩) (λ n ih, by simpa [pow_succ] using fg_mul _ _ h ih) end submodule
96c51d5a5389eeabdb2bdb7454cdf3008071003b
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/ring_theory/polynomial/scale_roots.lean
00dc95b0543b5d1d2525117b9db622ce7470dd31
[ "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
5,332
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Devon Tuma -/ import ring_theory.polynomial.basic import ring_theory.non_zero_divisors /-! # Scaling the roots of a polynomial This file defines `scale_roots p s` for a polynomial `p` in one variable and a ring element `s` to be the polynomial with root `r * s` for each root `r` of `p` and proves some basic results about it. -/ section scale_roots variables {A K R S : Type*} [comm_ring A] [integral_domain A] [field K] [comm_ring R] [comm_ring S] variables {M : submonoid A} open polynomial open_locale big_operators /-- `scale_roots p s` is a polynomial with root `r * s` for each root `r` of `p`. -/ noncomputable def scale_roots (p : polynomial R) (s : R) : polynomial R := ∑ i in p.support, monomial i (p.coeff i * s ^ (p.nat_degree - i)) @[simp] lemma coeff_scale_roots (p : polynomial R) (s : R) (i : ℕ) : (scale_roots p s).coeff i = coeff p i * s ^ (p.nat_degree - i) := by simp [scale_roots, coeff_monomial] {contextual := tt} lemma coeff_scale_roots_nat_degree (p : polynomial R) (s : R) : (scale_roots p s).coeff p.nat_degree = p.leading_coeff := by rw [leading_coeff, coeff_scale_roots, nat.sub_self, pow_zero, mul_one] @[simp] lemma zero_scale_roots (s : R) : scale_roots 0 s = 0 := by { ext, simp } lemma scale_roots_ne_zero {p : polynomial R} (hp : p ≠ 0) (s : R) : scale_roots p s ≠ 0 := begin intro h, have : p.coeff p.nat_degree ≠ 0 := mt leading_coeff_eq_zero.mp hp, have : (scale_roots p s).coeff p.nat_degree = 0 := congr_fun (congr_arg (coeff : polynomial R → ℕ → R) h) p.nat_degree, rw [coeff_scale_roots_nat_degree] at this, contradiction end lemma support_scale_roots_le (p : polynomial R) (s : R) : (scale_roots p s).support ≤ p.support := by { intro, simpa using left_ne_zero_of_mul } lemma support_scale_roots_eq (p : polynomial R) {s : R} (hs : s ∈ non_zero_divisors R) : (scale_roots p s).support = p.support := le_antisymm (support_scale_roots_le p s) begin intro i, simp only [coeff_scale_roots, polynomial.mem_support_iff], intros p_ne_zero ps_zero, have := ((non_zero_divisors R).pow_mem hs (p.nat_degree - i)) _ ps_zero, contradiction end @[simp] lemma degree_scale_roots (p : polynomial R) {s : R} : degree (scale_roots p s) = degree p := begin haveI := classical.prop_decidable, by_cases hp : p = 0, { rw [hp, zero_scale_roots] }, have := scale_roots_ne_zero hp s, refine le_antisymm (finset.sup_mono (support_scale_roots_le p s)) (degree_le_degree _), rw coeff_scale_roots_nat_degree, intro h, have := leading_coeff_eq_zero.mp h, contradiction, end @[simp] lemma nat_degree_scale_roots (p : polynomial R) (s : R) : nat_degree (scale_roots p s) = nat_degree p := by simp only [nat_degree, degree_scale_roots] lemma monic_scale_roots_iff {p : polynomial R} (s : R) : monic (scale_roots p s) ↔ monic p := by simp only [monic, leading_coeff, nat_degree_scale_roots, coeff_scale_roots_nat_degree] lemma scale_roots_eval₂_eq_zero {p : polynomial S} (f : S →+* R) {r : R} {s : S} (hr : eval₂ f r p = 0) : eval₂ f (f s * r) (scale_roots p s) = 0 := calc eval₂ f (f s * r) (scale_roots p s) = (scale_roots p s).support.sum (λ i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) : by simp [eval₂_eq_sum, sum_def] ... = p.support.sum (λ i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) : finset.sum_subset (support_scale_roots_le p s) (λ i hi hi', let this : coeff p i * s ^ (p.nat_degree - i) = 0 := by simpa using hi' in by simp [this]) ... = p.support.sum (λ (i : ℕ), f (p.coeff i) * f s ^ (p.nat_degree - i + i) * r ^ i) : finset.sum_congr rfl (λ i hi, by simp_rw [f.map_mul, f.map_pow, pow_add, mul_pow, mul_assoc]) ... = p.support.sum (λ (i : ℕ), f s ^ p.nat_degree * (f (p.coeff i) * r ^ i)) : finset.sum_congr rfl (λ i hi, by { rw [mul_assoc, mul_left_comm, nat.sub_add_cancel], exact le_nat_degree_of_ne_zero (polynomial.mem_support_iff.mp hi) }) ... = f s ^ p.nat_degree * p.support.sum (λ (i : ℕ), (f (p.coeff i) * r ^ i)) : finset.mul_sum.symm ... = f s ^ p.nat_degree * eval₂ f r p : by { simp [eval₂_eq_sum, sum_def] } ... = 0 : by rw [hr, _root_.mul_zero] lemma scale_roots_aeval_eq_zero [algebra S R] {p : polynomial S} {r : R} {s : S} (hr : aeval r p = 0) : aeval (algebra_map S R s * r) (scale_roots p s) = 0 := scale_roots_eval₂_eq_zero (algebra_map S R) hr lemma scale_roots_eval₂_eq_zero_of_eval₂_div_eq_zero {p : polynomial A} {f : A →+* K} (hf : function.injective f) {r s : A} (hr : eval₂ f (f r / f s) p = 0) (hs : s ∈ non_zero_divisors A) : eval₂ f (f r) (scale_roots p s) = 0 := begin convert scale_roots_eval₂_eq_zero f hr, rw [←mul_div_assoc, mul_comm, mul_div_cancel], exact f.map_ne_zero_of_mem_non_zero_divisors hf hs end lemma scale_roots_aeval_eq_zero_of_aeval_div_eq_zero [algebra A K] (inj : function.injective (algebra_map A K)) {p : polynomial A} {r s : A} (hr : aeval (algebra_map A K r / algebra_map A K s) p = 0) (hs : s ∈ non_zero_divisors A) : aeval (algebra_map A K r) (scale_roots p s) = 0 := scale_roots_eval₂_eq_zero_of_eval₂_div_eq_zero inj hr hs end scale_roots
754fc85f65fc85d35d166d0c1b5e45acebf5ff5e
63abd62053d479eae5abf4951554e1064a4c45b4
/src/ring_theory/prime.lean
ca448c7ebe0fcea5de8cf54e7e203986d4b38272
[ "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
2,684
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.associated import algebra.big_operators.basic /-! # Prime elements in rings This file contains lemmas about prime elements of commutative rings. -/ variables {R : Type*} [integral_domain R] open finset open_locale big_operators /-- If `x * y = a * ∏ i in s, p i` where `p i` is always prime, then `x` and `y` can both be written as a divisor of `a` multiplied by a product over a subset of `s` -/ lemma mul_eq_mul_prime_prod {α : Type*} [decidable_eq α] {x y a : R} {s : finset α} {p : α → R} (hp : ∀ i ∈ s, prime (p i)) (hx : x * y = a * ∏ i in s, p i) : ∃ (t u : finset α) (b c : R), t ∪ u = s ∧ disjoint t u ∧ a = b * c ∧ x = b * ∏ i in t, p i ∧ y = c * ∏ i in u, p i := begin induction s using finset.induction with i s his ih generalizing x y a, { exact ⟨∅, ∅, x, y, by simp [hx]⟩ }, { rw [prod_insert his, ← mul_assoc] at hx, have hpi : prime (p i), { exact hp i (mem_insert_self _ _) }, rcases ih (λ i hi, hp i (mem_insert_of_mem hi)) hx with ⟨t, u, b, c, htus, htu, hbc, rfl, rfl⟩, have hit : i ∉ t, from λ hit, his (htus ▸ mem_union_left _ hit), have hiu : i ∉ u, from λ hiu, his (htus ▸ mem_union_right _ hiu), obtain ⟨d, rfl⟩ | ⟨d, rfl⟩ : p i ∣ b ∨ p i ∣ c, from hpi.div_or_div ⟨a, by rw [← hbc, mul_comm]⟩, { rw [mul_assoc, mul_comm a, mul_right_inj' hpi.ne_zero] at hbc, exact ⟨insert i t, u, d, c, by rw [insert_union, htus], disjoint_insert_left.2 ⟨hiu, htu⟩, by simp [hbc, prod_insert hit, mul_assoc, mul_comm, mul_left_comm]⟩ }, { rw [← mul_assoc, mul_right_comm b, mul_left_inj' hpi.ne_zero] at hbc, exact ⟨t, insert i u, b, d, by rw [union_insert, htus], disjoint_insert_right.2 ⟨hit, htu⟩, by simp [← hbc, prod_insert hiu, mul_assoc, mul_comm, mul_left_comm]⟩ } } end /-- If ` x * y = a * p ^ n` where `p` is prime, then `x` and `y` can both be written as the product of a power of `p` and a divisor of `a`. -/ lemma mul_eq_mul_prime_pow {x y a p : R} {n : ℕ} (hp : prime p) (hx : x * y = a * p ^ n) : ∃ (i j : ℕ) (b c : R), i + j = n ∧ a = b * c ∧ x = b * p ^ i ∧ y = c * p ^ j := begin rcases mul_eq_mul_prime_prod (λ _ _, hp) (show x * y = a * (range n).prod (λ _, p), by simpa) with ⟨t, u, b, c, htus, htu, rfl, rfl, rfl⟩, exact ⟨t.card, u.card, b, c, by rw [← card_disjoint_union htu, htus, card_range], by simp⟩, end
a8a37090a1e0cac7893093efcb0e153c1e73ecbf
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/series/L01defs.lean
09f98f65fdd2548b8194fdbd0808461273025dd1
[ "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
179
lean
import data.real.basic import game.limits.bdd_monos_converge --import topology.algebra.infinite_sum namespace xena --hide notation `|` x `|` := abs x -- hide end xena --hide
9152c16cb91bc0130a1fc4a6dc33a508ccd054d6
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/simp7b.lean
29280a2c0124ab3d6504da72640097960f1e9c0f
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
263
lean
variables a b c d e f p: Bool rewrite_set simple add_rewrite and_assoc and_comm and_left_comm : simple (* local t = parse_lean("((f ∨ p) ∧ (e ∨ p)) ∧ ((p ∨ d) ∧ c) ∧ (b ∧ a)") local t2 = simplify(t, "simple") print(t) print("====>") print(t2) *)
cca9da0b49dd09b0a913ab0d151e2178a3b788cc
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/integral/circle_integral.lean
9cd190c8bdbcd22fed27fad57e494b6c17bedbdf
[ "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
30,659
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.integral.interval_integral import analysis.normed_space.pointwise import analysis.special_functions.non_integrable import analysis.analytic.basic /-! # Integral over a circle in `ℂ` In this file we define `∮ z in C(c, R), f z` to be the integral $\oint_{|z-c|=|R|} f(z)\,dz$ and prove some properties of this integral. We give definition and prove most lemmas for a function `f : ℂ → E`, where `E` is a complex Banach space. For this reason, some lemmas use, e.g., `(z - c)⁻¹ • f z` instead of `f z / (z - c)`. ## Main definitions * `circle_map c R`: the exponential map $θ ↦ c + R e^{θi}$; * `circle_integrable f c R`: a function `f : ℂ → E` is integrable on the circle with center `c` and radius `R` if `f ∘ circle_map c R` is integrable on `[0, 2π]`; * `circle_integral f c R`: the integral $\oint_{|z-c|=|R|} f(z)\,dz$, defined as $\int_{0}^{2π}(c + Re^{θ i})' f(c+Re^{θ i})\,dθ$; * `cauchy_power_series f c R`: the power series that is equal to $\sum_{n=0}^{\infty} \oint_{|z-c|=R} \left(\frac{w-c}{z - c}\right)^n \frac{1}{z-c}f(z)\,dz$ at `w - c`. The coefficients of this power series depend only on `f ∘ circle_map c R`, and the power series converges to `f w` if `f` is differentiable on the closed ball `metric.closed_ball c R` and `w` belongs to the corresponding open ball. ## Main statements * `has_fpower_series_on_cauchy_integral`: for any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `metric.ball c R`; * `circle_integral.integral_sub_zpow_of_undef`, `circle_integral.integral_sub_zpow_of_ne`, and `circle_integral.integral_sub_inv_of_mem_ball`: formulas for `∮ z in C(c, R), (z - w) ^ n`, `n : ℤ`. These lemmas cover the following cases: - `circle_integral.integral_sub_zpow_of_undef`, `n < 0` and `|w - c| = |R|`: in this case the function is not integrable, so the integral is equal to its default value (zero); - `circle_integral.integral_sub_zpow_of_ne`, `n ≠ -1`: in the cases not covered by the previous lemma, we have `(z - w) ^ n = ((z - w) ^ (n + 1) / (n + 1))'`, thus the integral equals zero; - `circle_integral.integral_sub_inv_of_mem_ball`, `n = -1`, `|w - c| < R`: in this case the integral is equal to `2πi`. The case `n = -1`, `|w -c| > R` is not covered by these lemmas. While it is possible to construct an explicit primitive, it is easier to apply Cauchy theorem, so we postpone the proof till we have this theorem (see #10000). ## Notation - `∮ z in C(c, R), f z`: notation for the integral $\oint_{|z-c|=|R|} f(z)\,dz$, defined as $\int_{0}^{2π}(c + Re^{θ i})' f(c+Re^{θ i})\,dθ$. ## Tags integral, circle, Cauchy integral -/ variables {E : Type*} [normed_add_comm_group E] noncomputable theory open_locale real nnreal interval pointwise topological_space open complex measure_theory topological_space metric function set filter asymptotics /-! ### `circle_map`, a parametrization of a circle -/ /-- The exponential map $θ ↦ c + R e^{θi}$. The range of this map is the circle in `ℂ` with center `c` and radius `|R|`. -/ def circle_map (c : ℂ) (R : ℝ) : ℝ → ℂ := λ θ, c + R * exp (θ * I) /-- `circle_map` is `2π`-periodic. -/ lemma periodic_circle_map (c : ℂ) (R : ℝ) : periodic (circle_map c R) (2 * π) := λ θ, by simp [circle_map, add_mul, exp_periodic _] lemma set.countable.preimage_circle_map {s : set ℂ} (hs : s.countable) (c : ℂ) {R : ℝ} (hR : R ≠ 0) : (circle_map c R ⁻¹' s).countable := show (coe ⁻¹' ((* I) ⁻¹' (exp ⁻¹' ((*) R ⁻¹' ((+) c ⁻¹' s))))).countable, from (((hs.preimage (add_right_injective _)).preimage $ mul_right_injective₀ $ of_real_ne_zero.2 hR).preimage_cexp.preimage $ mul_left_injective₀ I_ne_zero).preimage of_real_injective @[simp] lemma circle_map_sub_center (c : ℂ) (R : ℝ) (θ : ℝ) : circle_map c R θ - c = circle_map 0 R θ := by simp [circle_map] lemma circle_map_zero (R θ : ℝ) : circle_map 0 R θ = R * exp (θ * I) := zero_add _ @[simp] lemma abs_circle_map_zero (R : ℝ) (θ : ℝ) : abs (circle_map 0 R θ) = |R| := by simp [circle_map] lemma circle_map_mem_sphere' (c : ℂ) (R : ℝ) (θ : ℝ) : circle_map c R θ ∈ sphere c (|R|) := by simp lemma circle_map_mem_sphere (c : ℂ) {R : ℝ} (hR : 0 ≤ R) (θ : ℝ) : circle_map c R θ ∈ sphere c R := by simpa only [_root_.abs_of_nonneg hR] using circle_map_mem_sphere' c R θ lemma circle_map_mem_closed_ball (c : ℂ) {R : ℝ} (hR : 0 ≤ R) (θ : ℝ) : circle_map c R θ ∈ closed_ball c R := sphere_subset_closed_ball (circle_map_mem_sphere c hR θ) lemma circle_map_not_mem_ball (c : ℂ) (R : ℝ) (θ : ℝ) : circle_map c R θ ∉ ball c R := by simp [dist_eq, le_abs_self] lemma circle_map_ne_mem_ball {c : ℂ} {R : ℝ} {w : ℂ} (hw : w ∈ ball c R) (θ : ℝ) : circle_map c R θ ≠ w := (ne_of_mem_of_not_mem hw (circle_map_not_mem_ball _ _ _)).symm /-- The range of `circle_map c R` is the circle with center `c` and radius `|R|`. -/ @[simp] lemma range_circle_map (c : ℂ) (R : ℝ) : range (circle_map c R) = sphere c (|R|) := calc range (circle_map c R) = c +ᵥ R • range (λ θ : ℝ, exp (θ * I)) : by simp only [← image_vadd, ← image_smul, ← range_comp, vadd_eq_add, circle_map, (∘), real_smul] ... = sphere c (|R|) : by simp [smul_sphere R (0 : ℂ) zero_le_one] /-- The image of `(0, 2π]` under `circle_map c R` is the circle with center `c` and radius `|R|`. -/ @[simp] lemma image_circle_map_Ioc (c : ℂ) (R : ℝ) : circle_map c R '' Ioc 0 (2 * π) = sphere c (|R|) := by rw [← range_circle_map, ← (periodic_circle_map c R).image_Ioc real.two_pi_pos 0, zero_add] @[simp] lemma circle_map_eq_center_iff {c : ℂ} {R : ℝ} {θ : ℝ} : circle_map c R θ = c ↔ R = 0 := by simp [circle_map, exp_ne_zero] @[simp] lemma circle_map_zero_radius (c : ℂ) : circle_map c 0 = const ℝ c := funext $ λ θ, circle_map_eq_center_iff.2 rfl lemma circle_map_ne_center {c : ℂ} {R : ℝ} (hR : R ≠ 0) {θ : ℝ} : circle_map c R θ ≠ c := mt circle_map_eq_center_iff.1 hR lemma has_deriv_at_circle_map (c : ℂ) (R : ℝ) (θ : ℝ) : has_deriv_at (circle_map c R) (circle_map 0 R θ * I) θ := by simpa only [mul_assoc, one_mul, of_real_clm_apply, circle_map, of_real_one, zero_add] using ((of_real_clm.has_deriv_at.mul_const I).cexp.const_mul (R : ℂ)).const_add c /- TODO: prove `cont_diff ℝ (circle_map c R)`. This needs a version of `cont_diff.mul` for multiplication in a normed algebra over the base field. -/ lemma differentiable_circle_map (c : ℂ) (R : ℝ) : differentiable ℝ (circle_map c R) := λ θ, (has_deriv_at_circle_map c R θ).differentiable_at @[continuity] lemma continuous_circle_map (c : ℂ) (R : ℝ) : continuous (circle_map c R) := (differentiable_circle_map c R).continuous @[measurability] lemma measurable_circle_map (c : ℂ) (R : ℝ) : measurable (circle_map c R) := (continuous_circle_map c R).measurable @[simp] lemma deriv_circle_map (c : ℂ) (R : ℝ) (θ : ℝ) : deriv (circle_map c R) θ = circle_map 0 R θ * I := (has_deriv_at_circle_map _ _ _).deriv lemma deriv_circle_map_eq_zero_iff {c : ℂ} {R : ℝ} {θ : ℝ} : deriv (circle_map c R) θ = 0 ↔ R = 0 := by simp [I_ne_zero] lemma deriv_circle_map_ne_zero {c : ℂ} {R : ℝ} {θ : ℝ} (hR : R ≠ 0) : deriv (circle_map c R) θ ≠ 0 := mt deriv_circle_map_eq_zero_iff.1 hR lemma lipschitz_with_circle_map (c : ℂ) (R : ℝ) : lipschitz_with R.nnabs (circle_map c R) := lipschitz_with_of_nnnorm_deriv_le (differentiable_circle_map _ _) $ λ θ, nnreal.coe_le_coe.1 $ by simp lemma continuous_circle_map_inv {R : ℝ} {z w : ℂ} (hw : w ∈ ball z R) : continuous (λ θ, (circle_map z R θ - w)⁻¹) := begin have : ∀ θ, circle_map z R θ - w ≠ 0, { simp_rw sub_ne_zero, exact λ θ, circle_map_ne_mem_ball hw θ, }, continuity, end /-! ### Integrability of a function on a circle -/ /-- We say that a function `f : ℂ → E` is integrable on the circle with center `c` and radius `R` if the function `f ∘ circle_map c R` is integrable on `[0, 2π]`. Note that the actual function used in the definition of `circle_integral` is `(deriv (circle_map c R) θ) • f (circle_map c R θ)`. Integrability of this function is equivalent to integrability of `f ∘ circle_map c R` whenever `R ≠ 0`. -/ def circle_integrable (f : ℂ → E) (c : ℂ) (R : ℝ) : Prop := interval_integrable (λ θ : ℝ, f (circle_map c R θ)) volume 0 (2 * π) @[simp] lemma circle_integrable_const (a : E) (c : ℂ) (R : ℝ) : circle_integrable (λ _, a) c R := interval_integrable_const namespace circle_integrable variables {f g : ℂ → E} {c : ℂ} {R : ℝ} lemma add (hf : circle_integrable f c R) (hg : circle_integrable g c R) : circle_integrable (f + g) c R := hf.add hg lemma neg (hf : circle_integrable f c R) : circle_integrable (-f) c R := hf.neg /-- The function we actually integrate over `[0, 2π]` in the definition of `circle_integral` is integrable. -/ lemma out [normed_space ℂ E] (hf : circle_integrable f c R) : interval_integrable (λ θ : ℝ, deriv (circle_map c R) θ • f (circle_map c R θ)) volume 0 (2 * π) := begin simp only [circle_integrable, deriv_circle_map, interval_integrable_iff] at *, refine (hf.norm.const_mul (|R|)).mono' _ _, { exact ((continuous_circle_map _ _).ae_strongly_measurable.mul_const I).smul hf.ae_strongly_measurable }, { simp [norm_smul] } end end circle_integrable @[simp] lemma circle_integrable_zero_radius {f : ℂ → E} {c : ℂ} : circle_integrable f c 0 := by simp [circle_integrable] lemma circle_integrable_iff [normed_space ℂ E] {f : ℂ → E} {c : ℂ} (R : ℝ) : circle_integrable f c R ↔ interval_integrable (λ θ : ℝ, deriv (circle_map c R) θ • f (circle_map c R θ)) volume 0 (2 * π) := begin by_cases h₀ : R = 0, { simp [h₀], }, refine ⟨λ h, h.out, λ h, _⟩, simp only [circle_integrable, interval_integrable_iff, deriv_circle_map] at h ⊢, refine (h.norm.const_mul (|R|⁻¹)).mono' _ _, { have H : ∀ {θ}, circle_map 0 R θ * I ≠ 0 := λ θ, by simp [h₀, I_ne_zero], simpa only [inv_smul_smul₀ H] using (((continuous_circle_map 0 R).ae_strongly_measurable).mul_const I).ae_measurable .inv.ae_strongly_measurable.smul h.ae_strongly_measurable }, { simp [norm_smul, h₀] }, end lemma continuous_on.circle_integrable' {f : ℂ → E} {c : ℂ} {R : ℝ} (hf : continuous_on f (sphere c (|R|))) : circle_integrable f c R := (hf.comp_continuous (continuous_circle_map _ _) (circle_map_mem_sphere' _ _)).interval_integrable _ _ lemma continuous_on.circle_integrable {f : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (hf : continuous_on f (sphere c R)) : circle_integrable f c R := continuous_on.circle_integrable' $ (_root_.abs_of_nonneg hR).symm ▸ hf /-- The function `λ z, (z - w) ^ n`, `n : ℤ`, is circle integrable on the circle with center `c` and radius `|R|` if and only if `R = 0` or `0 ≤ n`, or `w` does not belong to this circle. -/ @[simp] lemma circle_integrable_sub_zpow_iff {c w : ℂ} {R : ℝ} {n : ℤ} : circle_integrable (λ z, (z - w) ^ n) c R ↔ R = 0 ∨ 0 ≤ n ∨ w ∉ sphere c (|R|) := begin split, { intro h, contrapose! h, rcases h with ⟨hR, hn, hw⟩, simp only [circle_integrable_iff R, deriv_circle_map], rw ← image_circle_map_Ioc at hw, rcases hw with ⟨θ, hθ, rfl⟩, replace hθ : θ ∈ [0, 2 * π], from Icc_subset_interval (Ioc_subset_Icc_self hθ), refine not_interval_integrable_of_sub_inv_is_O_punctured _ real.two_pi_pos.ne hθ, set f : ℝ → ℂ := λ θ', circle_map c R θ' - circle_map c R θ, have : ∀ᶠ θ' in 𝓝[≠] θ, f θ' ∈ ball (0 : ℂ) 1 \ {0}, { suffices : ∀ᶠ z in 𝓝[≠] (circle_map c R θ), z - circle_map c R θ ∈ ball (0 : ℂ) 1 \ {0}, from ((differentiable_circle_map c R θ).has_deriv_at.tendsto_punctured_nhds (deriv_circle_map_ne_zero hR)).eventually this, filter_upwards [self_mem_nhds_within, mem_nhds_within_of_mem_nhds (ball_mem_nhds _ zero_lt_one)], simp only [dist_eq, sub_eq_zero, mem_compl_iff, mem_singleton_iff, mem_ball, mem_diff, mem_ball_zero_iff, norm_eq_abs, not_false_iff, and_self, implies_true_iff] {contextual := tt} }, refine ((((has_deriv_at_circle_map c R θ).is_O_sub).mono inf_le_left).inv_rev (this.mono (λ θ' h₁ h₂, absurd h₂ h₁.2))).trans _, refine is_O.of_bound (|R|)⁻¹ (this.mono $ λ θ' hθ', _), set x := abs (f θ'), suffices : x⁻¹ ≤ x ^ n, by simpa only [inv_mul_cancel_left₀, abs_eq_zero.not.2 hR, norm_eq_abs, map_inv₀, algebra.id.smul_eq_mul, map_mul, abs_circle_map_zero, abs_I, mul_one, abs_zpow, ne.def, not_false_iff] using this, have : x ∈ Ioo (0 : ℝ) 1, by simpa [and.comm, x] using hθ', rw ← zpow_neg_one, refine (zpow_strict_anti this.1 this.2).le_iff_le.2 (int.lt_add_one_iff.1 _), exact hn }, { rintro (rfl|H), exacts [circle_integrable_zero_radius, ((continuous_on_id.sub continuous_on_const).zpow₀ _ $ λ z hz, H.symm.imp_left $ λ hw, sub_ne_zero.2 $ ne_of_mem_of_not_mem hz hw).circle_integrable'] }, end @[simp] lemma circle_integrable_sub_inv_iff {c w : ℂ} {R : ℝ} : circle_integrable (λ z, (z - w)⁻¹) c R ↔ R = 0 ∨ w ∉ sphere c (|R|) := by { simp only [← zpow_neg_one, circle_integrable_sub_zpow_iff], norm_num } variables [normed_space ℂ E] [complete_space E] /-- Definition for $\oint_{|z-c|=R} f(z)\,dz$. -/ def circle_integral (f : ℂ → E) (c : ℂ) (R : ℝ) : E := ∫ (θ : ℝ) in 0..2 * π, deriv (circle_map c R) θ • f (circle_map c R θ) notation `∮` binders ` in ` `C(` c `, ` R `)` `, ` r:(scoped:60 f, circle_integral f c R) := r lemma circle_integral_def_Icc (f : ℂ → E) (c : ℂ) (R : ℝ) : ∮ z in C(c, R), f z = ∫ θ in Icc 0 (2 * π), deriv (circle_map c R) θ • f (circle_map c R θ) := by simp only [circle_integral, interval_integral.integral_of_le real.two_pi_pos.le, measure.restrict_congr_set Ioc_ae_eq_Icc] namespace circle_integral @[simp] lemma integral_radius_zero (f : ℂ → E) (c : ℂ) : ∮ z in C(c, 0), f z = 0 := by simp [circle_integral] lemma integral_congr {f g : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (h : eq_on f g (sphere c R)) : ∮ z in C(c, R), f z = ∮ z in C(c, R), g z := interval_integral.integral_congr $ λ θ hθ, by simp only [h (circle_map_mem_sphere _ hR _)] lemma integral_sub_inv_smul_sub_smul (f : ℂ → E) (c w : ℂ) (R : ℝ) : ∮ z in C(c, R), (z - w)⁻¹ • (z - w) • f z = ∮ z in C(c, R), f z := begin rcases eq_or_ne R 0 with rfl|hR, { simp only [integral_radius_zero] }, have : (circle_map c R ⁻¹' {w}).countable, from (countable_singleton _).preimage_circle_map c hR, refine interval_integral.integral_congr_ae ((this.ae_not_mem _).mono $ λ θ hθ hθ', _), change circle_map c R θ ≠ w at hθ, simp only [inv_smul_smul₀ (sub_ne_zero.2 $ hθ)] end lemma integral_undef {f : ℂ → E} {c : ℂ} {R : ℝ} (hf : ¬circle_integrable f c R) : ∮ z in C(c, R), f z = 0 := interval_integral.integral_undef (mt (circle_integrable_iff R).mpr hf) lemma integral_sub {f g : ℂ → E} {c : ℂ} {R : ℝ} (hf : circle_integrable f c R) (hg : circle_integrable g c R) : ∮ z in C(c, R), f z - g z = (∮ z in C(c, R), f z) - ∮ z in C(c, R), g z := by simp only [circle_integral, smul_sub, interval_integral.integral_sub hf.out hg.out] lemma norm_integral_le_of_norm_le_const' {f : ℂ → E} {c : ℂ} {R C : ℝ} (hf : ∀ z ∈ sphere c (|R|), ‖f z‖ ≤ C) : ‖∮ z in C(c, R), f z‖ ≤ 2 * π * |R| * C := calc ‖∮ z in C(c, R), f z‖ ≤ |R| * C * |2 * π - 0| : interval_integral.norm_integral_le_of_norm_le_const $ λ θ _, (calc ‖deriv (circle_map c R) θ • f (circle_map c R θ)‖ = |R| * ‖f (circle_map c R θ)‖ : by simp [norm_smul] ... ≤ |R| * C : mul_le_mul_of_nonneg_left (hf _ $ circle_map_mem_sphere' _ _ _) (_root_.abs_nonneg _)) ... = 2 * π * |R| * C : by { rw [sub_zero, _root_.abs_of_pos real.two_pi_pos], ac_refl } lemma norm_integral_le_of_norm_le_const {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 ≤ R) (hf : ∀ z ∈ sphere c R, ‖f z‖ ≤ C) : ‖∮ z in C(c, R), f z‖ ≤ 2 * π * R * C := have |R| = R, from _root_.abs_of_nonneg hR, calc ‖∮ z in C(c, R), f z‖ ≤ 2 * π * |R| * C : norm_integral_le_of_norm_le_const' $ by rwa this ... = 2 * π * R * C : by rw this lemma norm_two_pi_I_inv_smul_integral_le_of_norm_le_const {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 ≤ R) (hf : ∀ z ∈ sphere c R, ‖f z‖ ≤ C) : ‖(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), f z‖ ≤ R * C := begin have : ‖(2 * π * I : ℂ)⁻¹‖ = (2 * π)⁻¹, by simp [real.pi_pos.le], rw [norm_smul, this, ← div_eq_inv_mul, div_le_iff real.two_pi_pos, mul_comm (R * C), ← mul_assoc], exact norm_integral_le_of_norm_le_const hR hf end /-- If `f` is continuous on the circle `|z - c| = R`, `R > 0`, the `‖f z‖` is less than or equal to `C : ℝ` on this circle, and this norm is strictly less than `C` at some point `z` of the circle, then `‖∮ z in C(c, R), f z‖ < 2 * π * R * C`. -/ lemma norm_integral_lt_of_norm_le_const_of_lt {f : ℂ → E} {c : ℂ} {R C : ℝ} (hR : 0 < R) (hc : continuous_on f (sphere c R)) (hf : ∀ z ∈ sphere c R, ‖f z‖ ≤ C) (hlt : ∃ z ∈ sphere c R, ‖f z‖ < C) : ‖∮ z in C(c, R), f z‖ < 2 * π * R * C := begin rw [← _root_.abs_of_pos hR, ← image_circle_map_Ioc] at hlt, rcases hlt with ⟨_, ⟨θ₀, hmem, rfl⟩, hlt⟩, calc ‖∮ z in C(c, R), f z‖ ≤ ∫ θ in 0..2 * π, ‖deriv (circle_map c R) θ • f (circle_map c R θ)‖ : interval_integral.norm_integral_le_integral_norm real.two_pi_pos.le ... < ∫ θ in 0..2 * π, R * C : begin simp only [norm_smul, deriv_circle_map, norm_eq_abs, map_mul, abs_I, mul_one, abs_circle_map_zero, abs_of_pos hR], refine interval_integral.integral_lt_integral_of_continuous_on_of_le_of_exists_lt real.two_pi_pos _ continuous_on_const (λ θ hθ, _) ⟨θ₀, Ioc_subset_Icc_self hmem, _⟩, { exact continuous_on_const.mul (hc.comp (continuous_circle_map _ _).continuous_on (λ θ hθ, circle_map_mem_sphere _ hR.le _)).norm }, { exact mul_le_mul_of_nonneg_left (hf _ $ circle_map_mem_sphere _ hR.le _) hR.le }, { exact (mul_lt_mul_left hR).2 hlt } end ... = 2 * π * R * C : by simp [mul_assoc] end @[simp] lemma integral_smul {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] [smul_comm_class 𝕜 ℂ E] (a : 𝕜) (f : ℂ → E) (c : ℂ) (R : ℝ) : ∮ z in C(c, R), a • f z = a • ∮ z in C(c, R), f z := by simp only [circle_integral, ← smul_comm a, interval_integral.integral_smul] @[simp] lemma integral_smul_const (f : ℂ → ℂ) (a : E) (c : ℂ) (R : ℝ) : ∮ z in C(c, R), (f z • a) = (∮ z in C(c, R), f z) • a := by simp only [circle_integral, interval_integral.integral_smul_const, ← smul_assoc] @[simp] lemma integral_const_mul (a : ℂ) (f : ℂ → ℂ) (c : ℂ) (R : ℝ) : ∮ z in C(c, R), a * f z = a * ∮ z in C(c, R), f z := integral_smul a f c R @[simp] lemma integral_sub_center_inv (c : ℂ) {R : ℝ} (hR : R ≠ 0) : ∮ z in C(c, R), (z - c)⁻¹ = 2 * π * I := by simp [circle_integral, ← div_eq_mul_inv, mul_div_cancel_left _ (circle_map_ne_center hR)] /-- If `f' : ℂ → E` is a derivative of a complex differentiable function on the circle `metric.sphere c |R|`, then `∮ z in C(c, R), f' z = 0`. -/ lemma integral_eq_zero_of_has_deriv_within_at' {f f' : ℂ → E} {c : ℂ} {R : ℝ} (h : ∀ z ∈ sphere c (|R|), has_deriv_within_at f (f' z) (sphere c (|R|)) z) : ∮ z in C(c, R), f' z = 0 := begin by_cases hi : circle_integrable f' c R, { rw ← sub_eq_zero.2 ((periodic_circle_map c R).comp f).eq, refine interval_integral.integral_eq_sub_of_has_deriv_at (λ θ hθ, _) hi.out, exact (h _ (circle_map_mem_sphere' _ _ _)).scomp_has_deriv_at θ (differentiable_circle_map _ _ _).has_deriv_at (circle_map_mem_sphere' _ _) }, { exact integral_undef hi } end /-- If `f' : ℂ → E` is a derivative of a complex differentiable function on the circle `metric.sphere c R`, then `∮ z in C(c, R), f' z = 0`. -/ lemma integral_eq_zero_of_has_deriv_within_at {f f' : ℂ → E} {c : ℂ} {R : ℝ} (hR : 0 ≤ R) (h : ∀ z ∈ sphere c R, has_deriv_within_at f (f' z) (sphere c R) z) : ∮ z in C(c, R), f' z = 0 := integral_eq_zero_of_has_deriv_within_at' $ (_root_.abs_of_nonneg hR).symm.subst h /-- If `n < 0` and `|w - c| = |R|`, then `(z - w) ^ n` is not circle integrable on the circle with center `c` and radius `(|R|)`, so the integral `∮ z in C(c, R), (z - w) ^ n` is equal to zero. -/ lemma integral_sub_zpow_of_undef {n : ℤ} {c w : ℂ} {R : ℝ} (hn : n < 0) (hw : w ∈ sphere c (|R|)) : ∮ z in C(c, R), (z - w) ^ n = 0 := begin rcases eq_or_ne R 0 with rfl|h0, { apply integral_radius_zero }, apply integral_undef, simp [circle_integrable_sub_zpow_iff, *] end /-- If `n ≠ -1` is an integer number, then the integral of `(z - w) ^ n` over the circle equals zero. -/ lemma integral_sub_zpow_of_ne {n : ℤ} (hn : n ≠ -1) (c w : ℂ) (R : ℝ) : ∮ z in C(c, R), (z - w) ^ n = 0 := begin rcases em (w ∈ sphere c (|R|) ∧ n < -1) with ⟨hw, hn⟩|H, { exact integral_sub_zpow_of_undef (hn.trans dec_trivial) hw }, push_neg at H, have hd : ∀ z, (z ≠ w ∨ -1 ≤ n) → has_deriv_at (λ z, (z - w) ^ (n + 1) / (n + 1)) ((z - w) ^ n) z, { intros z hne, convert ((has_deriv_at_zpow (n + 1) _ (hne.imp _ _)).comp z ((has_deriv_at_id z).sub_const w)).div_const _ using 1, { have hn' : (n + 1 : ℂ) ≠ 0, by rwa [ne, ← eq_neg_iff_add_eq_zero, ← int.cast_one, ← int.cast_neg, int.cast_inj], simp [mul_assoc, mul_div_cancel_left _ hn'] }, exacts [sub_ne_zero.2, neg_le_iff_add_nonneg.1] }, refine integral_eq_zero_of_has_deriv_within_at' (λ z hz, (hd z _).has_deriv_within_at), exact (ne_or_eq z w).imp_right (λ h, H $ h ▸ hz) end end circle_integral /-- The power series that is equal to $\sum_{n=0}^{\infty} \oint_{|z-c|=R} \left(\frac{w-c}{z - c}\right)^n \frac{1}{z-c}f(z)\,dz$ at `w - c`. The coefficients of this power series depend only on `f ∘ circle_map c R`, and the power series converges to `f w` if `f` is differentiable on the closed ball `metric.closed_ball c R` and `w` belongs to the corresponding open ball. For any circle integrable function `f`, this power series converges to the Cauchy integral for `f`. -/ def cauchy_power_series (f : ℂ → E) (c : ℂ) (R : ℝ) : formal_multilinear_series ℂ ℂ E := λ n, continuous_multilinear_map.mk_pi_field ℂ _ $ (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - c)⁻¹ ^ n • (z - c)⁻¹ • f z lemma cauchy_power_series_apply (f : ℂ → E) (c : ℂ) (R : ℝ) (n : ℕ) (w : ℂ) : cauchy_power_series f c R n (λ _, w) = (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (w / (z - c)) ^ n • (z - c)⁻¹ • f z := by simp only [cauchy_power_series, continuous_multilinear_map.mk_pi_field_apply, fin.prod_const, div_eq_mul_inv, mul_pow, mul_smul, circle_integral.integral_smul, ← smul_comm (w ^ n)] lemma norm_cauchy_power_series_le (f : ℂ → E) (c : ℂ) (R : ℝ) (n : ℕ) : ‖cauchy_power_series f c R n‖ ≤ (2 * π)⁻¹ * (∫ θ : ℝ in 0..2*π, ‖f (circle_map c R θ)‖) * (|R|⁻¹) ^ n := calc ‖cauchy_power_series f c R n‖ = (2 * π)⁻¹ * ‖∮ z in C(c, R), (z - c)⁻¹ ^ n • (z - c)⁻¹ • f z‖ : by simp [cauchy_power_series, norm_smul, real.pi_pos.le] ... ≤ (2 * π)⁻¹ * ∫ θ in 0..2*π, ‖deriv (circle_map c R) θ • (circle_map c R θ - c)⁻¹ ^ n • (circle_map c R θ - c)⁻¹ • f (circle_map c R θ)‖ : mul_le_mul_of_nonneg_left (interval_integral.norm_integral_le_integral_norm real.two_pi_pos.le) (by simp [real.pi_pos.le]) ... = (2 * π)⁻¹ * (|R|⁻¹ ^ n * (|R| * (|R|⁻¹ * ∫ (x : ℝ) in 0..2 * π, ‖f (circle_map c R x)‖))) : by simp [norm_smul, mul_left_comm (|R|)] ... ≤ (2 * π)⁻¹ * (∫ θ : ℝ in 0..2*π, ‖f (circle_map c R θ)‖) * |R|⁻¹ ^ n : begin rcases eq_or_ne R 0 with rfl|hR, { cases n; simp [-mul_inv_rev, real.two_pi_pos] }, { rw [mul_inv_cancel_left₀, mul_assoc, mul_comm (|R|⁻¹ ^ n)], rwa [ne.def, _root_.abs_eq_zero] } end lemma le_radius_cauchy_power_series (f : ℂ → E) (c : ℂ) (R : ℝ≥0) : ↑R ≤ (cauchy_power_series f c R).radius := begin refine (cauchy_power_series f c R).le_radius_of_bound ((2 * π)⁻¹ * (∫ θ : ℝ in 0..2*π, ‖f (circle_map c R θ)‖)) (λ n, _), refine (mul_le_mul_of_nonneg_right (norm_cauchy_power_series_le _ _ _ _) (pow_nonneg R.coe_nonneg _)).trans _, rw [_root_.abs_of_nonneg R.coe_nonneg], cases eq_or_ne (R ^ n : ℝ) 0 with hR hR, { rw [hR, mul_zero], exact mul_nonneg (inv_nonneg.2 real.two_pi_pos.le) (interval_integral.integral_nonneg real.two_pi_pos.le (λ _ _, norm_nonneg _)) }, { rw [inv_pow, inv_mul_cancel_right₀ hR] } end /-- For any circle integrable function `f`, the power series `cauchy_power_series f c R` multiplied by `2πI` converges to the integral `∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `metric.ball c R`. -/ lemma has_sum_two_pi_I_cauchy_power_series_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ} (hf : circle_integrable f c R) (hw : abs w < R) : has_sum (λ n : ℕ, ∮ z in C(c, R), (w / (z - c)) ^ n • (z - c)⁻¹ • f z) (∮ z in C(c, R), (z - (c + w))⁻¹ • f z) := begin have hR : 0 < R := (complex.abs.nonneg w).trans_lt hw, have hwR : abs w / R ∈ Ico (0 : ℝ) 1, from ⟨div_nonneg (complex.abs.nonneg w) hR.le, (div_lt_one hR).2 hw⟩, refine interval_integral.has_sum_integral_of_dominated_convergence (λ n θ, ‖f (circle_map c R θ)‖ * (abs w / R) ^ n) (λ n, _) (λ n, _) _ _ _, { simp only [deriv_circle_map], apply_rules [ae_strongly_measurable.smul, hf.def.1]; { apply measurable.ae_strongly_measurable, measurability } }, { simp [norm_smul, abs_of_pos hR, mul_left_comm R, mul_inv_cancel_left₀ hR.ne', mul_comm (‖_‖)] }, { exact eventually_of_forall (λ _ _, (summable_geometric_of_lt_1 hwR.1 hwR.2).mul_left _) }, { simpa only [tsum_mul_left, tsum_geometric_of_lt_1 hwR.1 hwR.2] using hf.norm.mul_continuous_on continuous_on_const }, { refine eventually_of_forall (λ θ hθ, has_sum.const_smul _), simp only [smul_smul], refine has_sum.smul_const _, have : ‖w / (circle_map c R θ - c)‖ < 1, by simpa [abs_of_pos hR] using hwR.2, convert (has_sum_geometric_of_norm_lt_1 this).mul_right _, simp [← sub_sub, ← mul_inv, sub_mul, div_mul_cancel _ (circle_map_ne_center hR.ne')] } end /-- For any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `metric.ball c R`. -/ lemma has_sum_cauchy_power_series_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ} (hf : circle_integrable f c R) (hw : abs w < R) : has_sum (λ n, cauchy_power_series f c R n (λ _, w)) ((2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - (c + w))⁻¹ • f z) := begin simp only [cauchy_power_series_apply], exact (has_sum_two_pi_I_cauchy_power_series_integral hf hw).const_smul end /-- For any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `metric.ball c R`. -/ lemma sum_cauchy_power_series_eq_integral {f : ℂ → E} {c : ℂ} {R : ℝ} {w : ℂ} (hf : circle_integrable f c R) (hw : abs w < R) : (cauchy_power_series f c R).sum w = ((2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - (c + w))⁻¹ • f z) := (has_sum_cauchy_power_series_integral hf hw).tsum_eq /-- For any circle integrable function `f`, the power series `cauchy_power_series f c R`, `R > 0`, converges to the Cauchy integral `(2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z` on the open disc `metric.ball c R`. -/ lemma has_fpower_series_on_cauchy_integral {f : ℂ → E} {c : ℂ} {R : ℝ≥0} (hf : circle_integrable f c R) (hR : 0 < R) : has_fpower_series_on_ball (λ w, (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), (z - w)⁻¹ • f z) (cauchy_power_series f c R) c R := { r_le := le_radius_cauchy_power_series _ _ _, r_pos := ennreal.coe_pos.2 hR, has_sum := λ y hy, begin refine has_sum_cauchy_power_series_integral hf _, rw [← norm_eq_abs, ← coe_nnnorm, nnreal.coe_lt_coe, ← ennreal.coe_lt_coe], exact mem_emetric_ball_zero_iff.1 hy end } namespace circle_integral /-- Integral $\oint_{|z-c|=R} \frac{dz}{z-w}=2πi$ whenever $|w-c|<R$. -/ lemma integral_sub_inv_of_mem_ball {c w : ℂ} {R : ℝ} (hw : w ∈ ball c R) : ∮ z in C(c, R), (z - w)⁻¹ = 2 * π * I := begin have hR : 0 < R := dist_nonneg.trans_lt hw, suffices H : has_sum (λ n : ℕ, ∮ z in C(c, R), ((w - c) / (z - c)) ^ n * (z - c)⁻¹) (2 * π * I), { have A : circle_integrable (λ _, (1 : ℂ)) c R, from continuous_on_const.circle_integrable', refine (H.unique _).symm, simpa only [smul_eq_mul, mul_one, add_sub_cancel'_right] using has_sum_two_pi_I_cauchy_power_series_integral A hw }, have H : ∀ n : ℕ, n ≠ 0 → ∮ z in C(c, R), (z - c) ^ (-n - 1 : ℤ) = 0, { refine λ n hn, integral_sub_zpow_of_ne _ _ _ _, simpa }, have : ∮ z in C(c, R), ((w - c) / (z - c)) ^ 0 * (z - c)⁻¹ = 2 * π * I, by simp [hR.ne'], refine this ▸ has_sum_single _ (λ n hn, _), simp only [div_eq_mul_inv, mul_pow, integral_const_mul, mul_assoc], rw [(integral_congr hR.le (λ z hz, _)).trans (H n hn), mul_zero], rw [← pow_succ', ← zpow_coe_nat, inv_zpow, ← zpow_neg, int.coe_nat_succ, neg_add, sub_eq_add_neg _ (1 : ℤ)] end end circle_integral
93a236e2a9a9edd18f0d904a219f210167b8e5bd
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/group_theory/congruence.lean
fdc393c614ad6f452b3d19c20703723ce4cdfb33
[ "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
42,709
lean
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import data.setoid.basic import algebra.group.pi import algebra.group.prod import data.equiv.mul_add import group_theory.submonoid.operations /-! # Congruence relations This file defines congruence relations: equivalence relations that preserve a binary operation, which in this case is multiplication or addition. The principal definition is a `structure` extending a `setoid` (an equivalence relation), and the inductive definition of the smallest congruence relation containing a binary relation is also given (see `con_gen`). The file also proves basic properties of the quotient of a type by a congruence relation, and the complete lattice of congruence relations on a type. We then establish an order-preserving bijection between the set of congruence relations containing a congruence relation `c` and the set of congruence relations on the quotient by `c`. The second half of the file concerns congruence relations on monoids, in which case the quotient by the congruence relation is also a monoid. There are results about the universal property of quotients of monoids, and the isomorphism theorems for monoids. ## Implementation notes The inductive definition of a congruence relation could be a nested inductive type, defined using the equivalence closure of a binary relation `eqv_gen`, but the recursor generated does not work. A nested inductive definition could conceivably shorten proofs, because they would allow invocation of the corresponding lemmas about `eqv_gen`. The lemmas `refl`, `symm` and `trans` are not tagged with `@[refl]`, `@[symm]`, and `@[trans]` respectively as these tags do not work on a structure coerced to a binary relation. There is a coercion from elements of a type to the element's equivalence class under a congruence relation. A congruence relation on a monoid `M` can be thought of as a submonoid of `M × M` for which membership is an equivalence relation, but whilst this fact is established in the file, it is not used, since this perspective adds more layers of definitional unfolding. ## Tags congruence, congruence relation, quotient, quotient by congruence relation, monoid, quotient monoid, isomorphism theorems -/ variables (M : Type*) {N : Type*} {P : Type*} set_option old_structure_cmd true open function setoid /-- A congruence relation on a type with an addition is an equivalence relation which preserves addition. -/ structure add_con [has_add M] extends setoid M := (add' : ∀ {w x y z}, r w x → r y z → r (w + y) (x + z)) /-- A congruence relation on a type with a multiplication is an equivalence relation which preserves multiplication. -/ @[to_additive add_con] structure con [has_mul M] extends setoid M := (mul' : ∀ {w x y z}, r w x → r y z → r (w * y) (x * z)) variables {M} /-- The inductively defined smallest additive congruence relation containing a given binary relation. -/ inductive add_con_gen.rel [has_add M] (r : M → M → Prop) : M → M → Prop | of : Π x y, r x y → add_con_gen.rel x y | refl : Π x, add_con_gen.rel x x | symm : Π x y, add_con_gen.rel x y → add_con_gen.rel y x | trans : Π x y z, add_con_gen.rel x y → add_con_gen.rel y z → add_con_gen.rel x z | add : Π w x y z, add_con_gen.rel w x → add_con_gen.rel y z → add_con_gen.rel (w + y) (x + z) /-- The inductively defined smallest multiplicative congruence relation containing a given binary relation. -/ @[to_additive add_con_gen.rel] inductive con_gen.rel [has_mul M] (r : M → M → Prop) : M → M → Prop | of : Π x y, r x y → con_gen.rel x y | refl : Π x, con_gen.rel x x | symm : Π x y, con_gen.rel x y → con_gen.rel y x | trans : Π x y z, con_gen.rel x y → con_gen.rel y z → con_gen.rel x z | mul : Π w x y z, con_gen.rel w x → con_gen.rel y z → con_gen.rel (w * y) (x * z) /-- The inductively defined smallest multiplicative congruence relation containing a given binary relation. -/ @[to_additive add_con_gen "The inductively defined smallest additive congruence relation containing a given binary relation."] def con_gen [has_mul M] (r : M → M → Prop) : con M := ⟨con_gen.rel r, ⟨con_gen.rel.refl, con_gen.rel.symm, con_gen.rel.trans⟩, con_gen.rel.mul⟩ namespace con section variables [has_mul M] [has_mul N] [has_mul P] (c : con M) @[to_additive] instance : inhabited (con M) := ⟨con_gen empty_relation⟩ /-- A coercion from a congruence relation to its underlying binary relation. -/ @[to_additive "A coercion from an additive congruence relation to its underlying binary relation."] instance : has_coe_to_fun (con M) := ⟨_, λ c, λ x y, c.r x y⟩ @[simp, to_additive] lemma rel_eq_coe (c : con M) : c.r = c := rfl /-- Congruence relations are reflexive. -/ @[to_additive "Additive congruence relations are reflexive."] protected lemma refl (x) : c x x := c.2.1 x /-- Congruence relations are symmetric. -/ @[to_additive "Additive congruence relations are symmetric."] protected lemma symm : ∀ {x y}, c x y → c y x := λ _ _ h, c.2.2.1 h /-- Congruence relations are transitive. -/ @[to_additive "Additive congruence relations are transitive."] protected lemma trans : ∀ {x y z}, c x y → c y z → c x z := λ _ _ _ h, c.2.2.2 h /-- Multiplicative congruence relations preserve multiplication. -/ @[to_additive "Additive congruence relations preserve addition."] protected lemma mul : ∀ {w x y z}, c w x → c y z → c (w * y) (x * z) := λ _ _ _ _ h1 h2, c.3 h1 h2 /-- Given a type `M` with a multiplication, a congruence relation `c` on `M`, and elements of `M` `x, y`, `(x, y) ∈ M × M` iff `x` is related to `y` by `c`. -/ @[to_additive "Given a type `M` with an addition, `x, y ∈ M`, and an additive congruence relation `c` on `M`, `(x, y) ∈ M × M` iff `x` is related to `y` by `c`."] instance : has_mem (M × M) (con M) := ⟨λ x c, c x.1 x.2⟩ variables {c} /-- The map sending a congruence relation to its underlying binary relation is injective. -/ @[to_additive "The map sending an additive congruence relation to its underlying binary relation is injective."] lemma ext' {c d : con M} (H : c.r = d.r) : c = d := by cases c; cases d; simpa using H /-- Extensionality rule for congruence relations. -/ @[ext, to_additive "Extensionality rule for additive congruence relations."] lemma ext {c d : con M} (H : ∀ x y, c x y ↔ d x y) : c = d := ext' $ by ext; apply H attribute [ext] add_con.ext /-- The map sending a congruence relation to its underlying equivalence relation is injective. -/ @[to_additive "The map sending an additive congruence relation to its underlying equivalence relation is injective."] lemma to_setoid_inj {c d : con M} (H : c.to_setoid = d.to_setoid) : c = d := ext $ ext_iff.1 H /-- Iff version of extensionality rule for congruence relations. -/ @[to_additive "Iff version of extensionality rule for additive congruence relations."] lemma ext_iff {c d : con M} : (∀ x y, c x y ↔ d x y) ↔ c = d := ⟨ext, λ h _ _, h ▸ iff.rfl⟩ /-- Two congruence relations are equal iff their underlying binary relations are equal. -/ @[to_additive "Two additive congruence relations are equal iff their underlying binary relations are equal."] lemma ext'_iff {c d : con M} : c.r = d.r ↔ c = d := ⟨ext', λ h, h ▸ rfl⟩ /-- The kernel of a multiplication-preserving function as a congruence relation. -/ @[to_additive "The kernel of an addition-preserving function as an additive congruence relation."] def mul_ker (f : M → P) (h : ∀ x y, f (x * y) = f x * f y) : con M := { r := λ x y, f x = f y, iseqv := ⟨λ _, rfl, λ _ _, eq.symm, λ _ _ _, eq.trans⟩, mul' := λ _ _ _ _ h1 h2, by rw [h, h1, h2, h] } /-- Given types with multiplications `M, N`, the product of two congruence relations `c` on `M` and `d` on `N`: `(x₁, x₂), (y₁, y₂) ∈ M × N` are related by `c.prod d` iff `x₁` is related to `y₁` by `c` and `x₂` is related to `y₂` by `d`. -/ @[to_additive prod "Given types with additions `M, N`, the product of two congruence relations `c` on `M` and `d` on `N`: `(x₁, x₂), (y₁, y₂) ∈ M × N` are related by `c.prod d` iff `x₁` is related to `y₁` by `c` and `x₂` is related to `y₂` by `d`."] protected def prod (c : con M) (d : con N) : con (M × N) := { mul' := λ _ _ _ _ h1 h2, ⟨c.mul h1.1 h2.1, d.mul h1.2 h2.2⟩, ..c.to_setoid.prod d.to_setoid } /-- The product of an indexed collection of congruence relations. -/ @[to_additive "The product of an indexed collection of additive congruence relations."] def pi {ι : Type*} {f : ι → Type*} [Π i, has_mul (f i)] (C : Π i, con (f i)) : con (Π i, f i) := { mul' := λ _ _ _ _ h1 h2 i, (C i).mul (h1 i) (h2 i), ..@pi_setoid _ _ $ λ i, (C i).to_setoid } variables (c) @[simp, to_additive] lemma coe_eq : c.to_setoid.r = c := rfl -- Quotients /-- Defining the quotient by a congruence relation of a type with a multiplication. -/ @[to_additive "Defining the quotient by an additive congruence relation of a type with an addition."] protected def quotient := quotient $ c.to_setoid /-- Coercion from a type with a multiplication to its quotient by a congruence relation. See Note [use has_coe_t]. -/ @[to_additive "Coercion from a type with an addition to its quotient by an additive congruence relation", priority 0] instance : has_coe_t M c.quotient := ⟨@quotient.mk _ c.to_setoid⟩ /-- The quotient of a type with decidable equality by a congruence relation also has decidable equality. -/ @[to_additive "The quotient of a type with decidable equality by an additive congruence relation also has decidable equality."] instance [d : ∀ a b, decidable (c a b)] : decidable_eq c.quotient := @quotient.decidable_eq M c.to_setoid d /-- The function on the quotient by a congruence relation `c` induced by a function that is constant on `c`'s equivalence classes. -/ @[elab_as_eliminator, to_additive "The function on the quotient by a congruence relation `c` induced by a function that is constant on `c`'s equivalence classes."] protected def lift_on {β} {c : con M} (q : c.quotient) (f : M → β) (h : ∀ a b, c a b → f a = f b) : β := quotient.lift_on' q f h /-- The binary function on the quotient by a congruence relation `c` induced by a binary function that is constant on `c`'s equivalence classes. -/ @[elab_as_eliminator, to_additive "The binary function on the quotient by a congruence relation `c` induced by a binary function that is constant on `c`'s equivalence classes."] protected def lift_on₂ {β} {c : con M} (q r : c.quotient) (f : M → M → β) (h : ∀ a₁ a₂ b₁ b₂, c a₁ b₁ → c a₂ b₂ → f a₁ a₂ = f b₁ b₂) : β := quotient.lift_on₂' q r f h variables {c} /-- The inductive principle used to prove propositions about the elements of a quotient by a congruence relation. -/ @[elab_as_eliminator, to_additive "The inductive principle used to prove propositions about the elements of a quotient by an additive congruence relation."] protected lemma induction_on {C : c.quotient → Prop} (q : c.quotient) (H : ∀ x : M, C x) : C q := quotient.induction_on' q H /-- A version of `con.induction_on` for predicates which take two arguments. -/ @[elab_as_eliminator, to_additive "A version of `add_con.induction_on` for predicates which take two arguments."] protected lemma induction_on₂ {d : con N} {C : c.quotient → d.quotient → Prop} (p : c.quotient) (q : d.quotient) (H : ∀ (x : M) (y : N), C x y) : C p q := quotient.induction_on₂' p q H variables (c) /-- Two elements are related by a congruence relation `c` iff they are represented by the same element of the quotient by `c`. -/ @[simp, to_additive "Two elements are related by an additive congruence relation `c` iff they are represented by the same element of the quotient by `c`."] protected lemma eq {a b : M} : (a : c.quotient) = b ↔ c a b := quotient.eq' /-- The multiplication induced on the quotient by a congruence relation on a type with a multiplication. -/ @[to_additive "The addition induced on the quotient by an additive congruence relation on a type with an addition."] instance has_mul : has_mul c.quotient := ⟨λ x y, quotient.lift_on₂' x y (λ w z, ((w * z : M) : c.quotient)) $ λ _ _ _ _ h1 h2, c.eq.2 $ c.mul h1 h2⟩ /-- The kernel of the quotient map induced by a congruence relation `c` equals `c`. -/ @[simp, to_additive "The kernel of the quotient map induced by an additive congruence relation `c` equals `c`."] lemma mul_ker_mk_eq : mul_ker (coe : M → c.quotient) (λ x y, rfl) = c := ext $ λ x y, quotient.eq' variables {c} /-- The coercion to the quotient of a congruence relation commutes with multiplication (by definition). -/ @[simp, to_additive "The coercion to the quotient of an additive congruence relation commutes with addition (by definition)."] lemma coe_mul (x y : M) : (↑(x * y) : c.quotient) = ↑x * ↑y := rfl /-- Definition of the function on the quotient by a congruence relation `c` induced by a function that is constant on `c`'s equivalence classes. -/ @[simp, to_additive "Definition of the function on the quotient by an additive congruence relation `c` induced by a function that is constant on `c`'s equivalence classes."] protected lemma lift_on_beta {β} (c : con M) (f : M → β) (h : ∀ a b, c a b → f a = f b) (x : M) : con.lift_on (x : c.quotient) f h = f x := rfl /-- Makes an isomorphism of quotients by two congruence relations, given that the relations are equal. -/ @[to_additive "Makes an additive isomorphism of quotients by two additive congruence relations, given that the relations are equal."] protected def congr {c d : con M} (h : c = d) : c.quotient ≃* d.quotient := { map_mul' := λ x y, by rcases x; rcases y; refl, ..quotient.congr (equiv.refl M) $ by apply ext_iff.2 h } -- The complete lattice of congruence relations on a type /-- For congruence relations `c, d` on a type `M` with a multiplication, `c ≤ d` iff `∀ x y ∈ M`, `x` is related to `y` by `d` if `x` is related to `y` by `c`. -/ @[to_additive "For additive congruence relations `c, d` on a type `M` with an addition, `c ≤ d` iff `∀ x y ∈ M`, `x` is related to `y` by `d` if `x` is related to `y` by `c`."] instance : has_le (con M) := ⟨λ c d, ∀ ⦃x y⦄, c x y → d x y⟩ /-- Definition of `≤` for congruence relations. -/ @[to_additive "Definition of `≤` for additive congruence relations."] theorem le_def {c d : con M} : c ≤ d ↔ ∀ {x y}, c x y → d x y := iff.rfl /-- The infimum of a set of congruence relations on a given type with a multiplication. -/ @[to_additive "The infimum of a set of additive congruence relations on a given type with an addition."] instance : has_Inf (con M) := ⟨λ S, ⟨λ x y, ∀ c : con M, c ∈ S → c x y, ⟨λ x c hc, c.refl x, λ _ _ h c hc, c.symm $ h c hc, λ _ _ _ h1 h2 c hc, c.trans (h1 c hc) $ h2 c hc⟩, λ _ _ _ _ h1 h2 c hc, c.mul (h1 c hc) $ h2 c hc⟩⟩ /-- The infimum of a set of congruence relations is the same as the infimum of the set's image under the map to the underlying equivalence relation. -/ @[to_additive "The infimum of a set of additive congruence relations is the same as the infimum of the set's image under the map to the underlying equivalence relation."] lemma Inf_to_setoid (S : set (con M)) : (Inf S).to_setoid = Inf (to_setoid '' S) := setoid.ext' $ λ x y, ⟨λ h r ⟨c, hS, hr⟩, by rw ←hr; exact h c hS, λ h c hS, h c.to_setoid ⟨c, hS, rfl⟩⟩ /-- The infimum of a set of congruence relations is the same as the infimum of the set's image under the map to the underlying binary relation. -/ @[to_additive "The infimum of a set of additive congruence relations is the same as the infimum of the set's image under the map to the underlying binary relation."] lemma Inf_def (S : set (con M)) : (Inf S).r = Inf (r '' S) := by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl } @[to_additive] instance : partial_order (con M) := { le := (≤), lt := λ c d, c ≤ d ∧ ¬d ≤ c, le_refl := λ c _ _, id, le_trans := λ c1 c2 c3 h1 h2 x y h, h2 $ h1 h, lt_iff_le_not_le := λ _ _, iff.rfl, le_antisymm := λ c d hc hd, ext $ λ x y, ⟨λ h, hc h, λ h, hd h⟩ } /-- The complete lattice of congruence relations on a given type with a multiplication. -/ @[to_additive "The complete lattice of additive congruence relations on a given type with an addition."] instance : complete_lattice (con M) := { inf := λ c d, ⟨(c.to_setoid ⊓ d.to_setoid).1, (c.to_setoid ⊓ d.to_setoid).2, λ _ _ _ _ h1 h2, ⟨c.mul h1.1 h2.1, d.mul h1.2 h2.2⟩⟩, inf_le_left := λ _ _ _ _ h, h.1, inf_le_right := λ _ _ _ _ h, h.2, le_inf := λ _ _ _ hb hc _ _ h, ⟨hb h, hc h⟩, top := { mul' := by tauto, ..setoid.complete_lattice.top}, le_top := λ _ _ _ h, trivial, bot := { mul' := λ _ _ _ _ h1 h2, h1 ▸ h2 ▸ rfl, ..setoid.complete_lattice.bot}, bot_le := λ c x y h, h ▸ c.refl x, .. complete_lattice_of_Inf (con M) $ assume s, ⟨λ r hr x y h, (h : ∀ r ∈ s, (r : con M) x y) r hr, λ r hr x y h r' hr', hr hr' h⟩ } /-- The infimum of two congruence relations equals the infimum of the underlying binary operations. -/ @[to_additive "The infimum of two additive congruence relations equals the infimum of the underlying binary operations."] lemma inf_def {c d : con M} : (c ⊓ d).r = c.r ⊓ d.r := rfl /-- Definition of the infimum of two congruence relations. -/ @[to_additive "Definition of the infimum of two additive congruence relations."] theorem inf_iff_and {c d : con M} {x y} : (c ⊓ d) x y ↔ c x y ∧ d x y := iff.rfl /-- The inductively defined smallest congruence relation containing a binary relation `r` equals the infimum of the set of congruence relations containing `r`. -/ @[to_additive add_con_gen_eq "The inductively defined smallest additive congruence relation containing a binary relation `r` equals the infimum of the set of additive congruence relations containing `r`."] theorem con_gen_eq (r : M → M → Prop) : con_gen r = Inf {s : con M | ∀ x y, r x y → s x y} := le_antisymm (λ x y H, con_gen.rel.rec_on H (λ _ _ h _ hs, hs _ _ h) (con.refl _) (λ _ _ _, con.symm _) (λ _ _ _ _ _, con.trans _) $ λ w x y z _ _ h1 h2 c hc, c.mul (h1 c hc) $ h2 c hc) (Inf_le (λ _ _, con_gen.rel.of _ _)) /-- The smallest congruence relation containing a binary relation `r` is contained in any congruence relation containing `r`. -/ @[to_additive add_con_gen_le "The smallest additive congruence relation containing a binary relation `r` is contained in any additive congruence relation containing `r`."] theorem con_gen_le {r : M → M → Prop} {c : con M} (h : ∀ x y, r x y → c.r x y) : con_gen r ≤ c := by rw con_gen_eq; exact Inf_le h /-- Given binary relations `r, s` with `r` contained in `s`, the smallest congruence relation containing `s` contains the smallest congruence relation containing `r`. -/ @[to_additive add_con_gen_mono "Given binary relations `r, s` with `r` contained in `s`, the smallest additive congruence relation containing `s` contains the smallest additive congruence relation containing `r`."] theorem con_gen_mono {r s : M → M → Prop} (h : ∀ x y, r x y → s x y) : con_gen r ≤ con_gen s := con_gen_le $ λ x y hr, con_gen.rel.of _ _ $ h x y hr /-- Congruence relations equal the smallest congruence relation in which they are contained. -/ @[simp, to_additive add_con_gen_of_add_con "Additive congruence relations equal the smallest additive congruence relation in which they are contained."] lemma con_gen_of_con (c : con M) : con_gen c = c := le_antisymm (by rw con_gen_eq; exact Inf_le (λ _ _, id)) con_gen.rel.of /-- The map sending a binary relation to the smallest congruence relation in which it is contained is idempotent. -/ @[simp, to_additive add_con_gen_idem "The map sending a binary relation to the smallest additive congruence relation in which it is contained is idempotent."] lemma con_gen_idem (r : M → M → Prop) : con_gen (con_gen r) = con_gen r := con_gen_of_con _ /-- The supremum of congruence relations `c, d` equals the smallest congruence relation containing the binary relation '`x` is related to `y` by `c` or `d`'. -/ @[to_additive sup_eq_add_con_gen "The supremum of additive congruence relations `c, d` equals the smallest additive congruence relation containing the binary relation '`x` is related to `y` by `c` or `d`'."] lemma sup_eq_con_gen (c d : con M) : c ⊔ d = con_gen (λ x y, c x y ∨ d x y) := begin rw con_gen_eq, apply congr_arg Inf, simp only [le_def, or_imp_distrib, ← forall_and_distrib] end /-- The supremum of two congruence relations equals the smallest congruence relation containing the supremum of the underlying binary operations. -/ @[to_additive "The supremum of two additive congruence relations equals the smallest additive congruence relation containing the supremum of the underlying binary operations."] lemma sup_def {c d : con M} : c ⊔ d = con_gen (c.r ⊔ d.r) := by rw sup_eq_con_gen; refl /-- The supremum of a set of congruence relations `S` equals the smallest congruence relation containing the binary relation 'there exists `c ∈ S` such that `x` is related to `y` by `c`'. -/ @[to_additive Sup_eq_add_con_gen "The supremum of a set of additive congruence relations `S` equals the smallest additive congruence relation containing the binary relation 'there exists `c ∈ S` such that `x` is related to `y` by `c`'."] lemma Sup_eq_con_gen (S : set (con M)) : Sup S = con_gen (λ x y, ∃ c : con M, c ∈ S ∧ c x y) := begin rw con_gen_eq, apply congr_arg Inf, ext, exact ⟨λ h _ _ ⟨r, hr⟩, h hr.1 hr.2, λ h r hS _ _ hr, h _ _ ⟨r, hS, hr⟩⟩, end /-- The supremum of a set of congruence relations is the same as the smallest congruence relation containing the supremum of the set's image under the map to the underlying binary relation. -/ @[to_additive "The supremum of a set of additive congruence relations is the same as the smallest additive congruence relation containing the supremum of the set's image under the map to the underlying binary relation."] lemma Sup_def {S : set (con M)} : Sup S = con_gen (Sup (r '' S)) := begin rw [Sup_eq_con_gen, Sup_image], congr' with x y, simp only [Sup_image, supr_apply, supr_Prop_eq, exists_prop, rel_eq_coe] end variables (M) /-- There is a Galois insertion of congruence relations on a type with a multiplication `M` into binary relations on `M`. -/ @[to_additive "There is a Galois insertion of additive congruence relations on a type with an addition `M` into binary relations on `M`."] protected def gi : @galois_insertion (M → M → Prop) (con M) _ _ con_gen r := { choice := λ r h, con_gen r, gc := λ r c, ⟨λ H _ _ h, H $ con_gen.rel.of _ _ h, λ H, con_gen_of_con c ▸ con_gen_mono H⟩, le_l_u := λ x, (con_gen_of_con x).symm ▸ le_refl x, choice_eq := λ _ _, rfl } variables {M} (c) /-- Given a function `f`, the smallest congruence relation containing the binary relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by a congruence relation `c`.' -/ @[to_additive "Given a function `f`, the smallest additive congruence relation containing the binary relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by an additive congruence relation `c`.'"] def map_gen (f : M → N) : con N := con_gen $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ c a b /-- Given a surjective multiplicative-preserving function `f` whose kernel is contained in a congruence relation `c`, the congruence relation on `f`'s codomain defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `c`.' -/ @[to_additive "Given a surjective addition-preserving function `f` whose kernel is contained in an additive congruence relation `c`, the additive congruence relation on `f`'s codomain defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `c`.'"] def map_of_surjective (f : M → N) (H : ∀ x y, f (x * y) = f x * f y) (h : mul_ker f H ≤ c) (hf : surjective f) : con N := { mul' := λ w x y z ⟨a, b, hw, hx, h1⟩ ⟨p, q, hy, hz, h2⟩, ⟨a * p, b * q, by rw [H, hw, hy], by rw [H, hx, hz], c.mul h1 h2⟩, ..c.to_setoid.map_of_surjective f h hf } /-- A specialization of 'the smallest congruence relation containing a congruence relation `c` equals `c`'. -/ @[to_additive "A specialization of 'the smallest additive congruence relation containing an additive congruence relation `c` equals `c`'."] lemma map_of_surjective_eq_map_gen {c : con M} {f : M → N} (H : ∀ x y, f (x * y) = f x * f y) (h : mul_ker f H ≤ c) (hf : surjective f) : c.map_gen f = c.map_of_surjective f H h hf := by rw ←con_gen_of_con (c.map_of_surjective f H h hf); refl /-- Given types with multiplications `M, N` and a congruence relation `c` on `N`, a multiplication-preserving map `f : M → N` induces a congruence relation on `f`'s domain defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `c`.' -/ @[to_additive "Given types with additions `M, N` and an additive congruence relation `c` on `N`, an addition-preserving map `f : M → N` induces an additive congruence relation on `f`'s domain defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `c`.' "] def comap (f : M → N) (H : ∀ x y, f (x * y) = f x * f y) (c : con N) : con M := { mul' := λ w x y z h1 h2, show c (f (w * y)) (f (x * z)), by rw [H, H]; exact c.mul h1 h2, ..c.to_setoid.comap f } section open quotient /-- Given a congruence relation `c` on a type `M` with a multiplication, the order-preserving bijection between the set of congruence relations containing `c` and the congruence relations on the quotient of `M` by `c`. -/ @[to_additive "Given an additive congruence relation `c` on a type `M` with an addition, the order-preserving bijection between the set of additive congruence relations containing `c` and the additive congruence relations on the quotient of `M` by `c`."] def correspondence : {d // c ≤ d} ≃o (con c.quotient) := { to_fun := λ d, d.1.map_of_surjective coe _ (by rw mul_ker_mk_eq; exact d.2) $ @exists_rep _ c.to_setoid, inv_fun := λ d, ⟨comap (coe : M → c.quotient) (λ x y, rfl) d, λ _ _ h, show d _ _, by rw c.eq.2 h; exact d.refl _ ⟩, left_inv := λ d, subtype.ext_iff_val.2 $ ext $ λ _ _, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in d.1.trans (d.1.symm $ d.2 $ c.eq.1 hx) $ d.1.trans H $ d.2 $ c.eq.1 hy, λ h, ⟨_, _, rfl, rfl, h⟩⟩, right_inv := λ d, let Hm : mul_ker (coe : M → c.quotient) (λ x y, rfl) ≤ comap (coe : M → c.quotient) (λ x y, rfl) d := λ x y h, show d _ _, by rw mul_ker_mk_eq at h; exact c.eq.2 h ▸ d.refl _ in ext $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H, con.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩, map_rel_iff' := λ s t, ⟨λ h _ _ hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩, λ h _ _ hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨_, _, rfl, rfl, hs⟩ in t.1.trans (t.1.symm $ t.2 $ eq_rel.1 hx) $ t.1.trans ht $ t.2 $ eq_rel.1 hy⟩ } end end -- Monoids variables {M} [monoid M] [monoid N] [monoid P] (c : con M) /-- The quotient of a monoid by a congruence relation is a monoid. -/ @[to_additive "The quotient of an `add_monoid` by an additive congruence relation is an `add_monoid`."] instance monoid : monoid c.quotient := { one := ((1 : M) : c.quotient), mul := (*), mul_assoc := λ x y z, quotient.induction_on₃' x y z $ λ _ _ _, congr_arg coe $ mul_assoc _ _ _, mul_one := λ x, quotient.induction_on' x $ λ _, congr_arg coe $ mul_one _, one_mul := λ x, quotient.induction_on' x $ λ _, congr_arg coe $ one_mul _ } /-- The quotient of a `comm_monoid` by a congruence relation is a `comm_monoid`. -/ @[to_additive "The quotient of an `add_comm_monoid` by an additive congruence relation is an `add_comm_monoid`."] instance comm_monoid {α : Type*} [comm_monoid α] (c : con α) : comm_monoid c.quotient := { mul_comm := λ x y, con.induction_on₂ x y $ λ w z, by rw [←coe_mul, ←coe_mul, mul_comm], ..c.monoid} variables {c} /-- The 1 of the quotient of a monoid by a congruence relation is the equivalence class of the monoid's 1. -/ @[simp, to_additive "The 0 of the quotient of an `add_monoid` by an additive congruence relation is the equivalence class of the `add_monoid`'s 0."] lemma coe_one : ((1 : M) : c.quotient) = 1 := rfl variables (M c) /-- The submonoid of `M × M` defined by a congruence relation on a monoid `M`. -/ @[to_additive "The `add_submonoid` of `M × M` defined by an additive congruence relation on an `add_monoid` `M`."] protected def submonoid : submonoid (M × M) := { carrier := { x | c x.1 x.2 }, one_mem' := c.iseqv.1 1, mul_mem' := λ _ _, c.mul } variables {M c} /-- The congruence relation on a monoid `M` from a submonoid of `M × M` for which membership is an equivalence relation. -/ @[to_additive "The additive congruence relation on an `add_monoid` `M` from an `add_submonoid` of `M × M` for which membership is an equivalence relation."] def of_submonoid (N : submonoid (M × M)) (H : equivalence (λ x y, (x, y) ∈ N)) : con M := { r := λ x y, (x, y) ∈ N, iseqv := H, mul' := λ _ _ _ _, N.mul_mem } /-- Coercion from a congruence relation `c` on a monoid `M` to the submonoid of `M × M` whose elements are `(x, y)` such that `x` is related to `y` by `c`. -/ @[to_additive "Coercion from a congruence relation `c` on an `add_monoid` `M` to the `add_submonoid` of `M × M` whose elements are `(x, y)` such that `x` is related to `y` by `c`."] instance to_submonoid : has_coe (con M) (submonoid (M × M)) := ⟨λ c, c.submonoid M⟩ @[to_additive] lemma mem_coe {c : con M} {x y} : (x, y) ∈ (↑c : submonoid (M × M)) ↔ (x, y) ∈ c := iff.rfl @[to_additive] theorem to_submonoid_inj (c d : con M) (H : (c : submonoid (M × M)) = d) : c = d := ext $ λ x y, show (x, y) ∈ (c : submonoid (M × M)) ↔ (x, y) ∈ ↑d, by rw H @[to_additive] lemma le_iff {c d : con M} : c ≤ d ↔ (c : submonoid (M × M)) ≤ d := ⟨λ h x H, h H, λ h x y hc, h $ show (x, y) ∈ c, from hc⟩ /-- The kernel of a monoid homomorphism as a congruence relation. -/ @[to_additive "The kernel of an `add_monoid` homomorphism as an additive congruence relation."] def ker (f : M →* P) : con M := mul_ker f f.3 /-- The definition of the congruence relation defined by a monoid homomorphism's kernel. -/ @[to_additive "The definition of the additive congruence relation defined by an `add_monoid` homomorphism's kernel."] lemma ker_rel (f : M →* P) {x y} : ker f x y ↔ f x = f y := iff.rfl /-- There exists an element of the quotient of a monoid by a congruence relation (namely 1). -/ @[to_additive "There exists an element of the quotient of an `add_monoid` by a congruence relation (namely 0)."] instance quotient.inhabited : inhabited c.quotient := ⟨((1 : M) : c.quotient)⟩ variables (c) /-- The natural homomorphism from a monoid to its quotient by a congruence relation. -/ @[to_additive "The natural homomorphism from an `add_monoid` to its quotient by an additive congruence relation."] def mk' : M →* c.quotient := ⟨coe, rfl, λ _ _, rfl⟩ variables (x y : M) /-- The kernel of the natural homomorphism from a monoid to its quotient by a congruence relation `c` equals `c`. -/ @[simp, to_additive "The kernel of the natural homomorphism from an `add_monoid` to its quotient by an additive congruence relation `c` equals `c`."] lemma mk'_ker : ker c.mk' = c := ext $ λ _ _, c.eq variables {c} /-- The natural homomorphism from a monoid to its quotient by a congruence relation is surjective. -/ @[to_additive "The natural homomorphism from an `add_monoid` to its quotient by a congruence relation is surjective."] lemma mk'_surjective : surjective c.mk' := λ x, by rcases x; exact ⟨x, rfl⟩ @[simp, to_additive] lemma comp_mk'_apply (g : c.quotient →* P) {x} : g.comp c.mk' x = g x := rfl /-- The elements related to `x ∈ M`, `M` a monoid, by the kernel of a monoid homomorphism are those in the preimage of `f(x)` under `f`. -/ @[to_additive "The elements related to `x ∈ M`, `M` an `add_monoid`, by the kernel of an `add_monoid` homomorphism are those in the preimage of `f(x)` under `f`. "] lemma ker_apply_eq_preimage {f : M →* P} (x) : (ker f) x = f ⁻¹' {f x} := set.ext $ λ x, ⟨λ h, set.mem_preimage.2 $ set.mem_singleton_iff.2 h.symm, λ h, (set.mem_singleton_iff.1 $ set.mem_preimage.1 h).symm⟩ /-- Given a monoid homomorphism `f : N → M` and a congruence relation `c` on `M`, the congruence relation induced on `N` by `f` equals the kernel of `c`'s quotient homomorphism composed with `f`. -/ @[to_additive "Given an `add_monoid` homomorphism `f : N → M` and an additive congruence relation `c` on `M`, the additive congruence relation induced on `N` by `f` equals the kernel of `c`'s quotient homomorphism composed with `f`."] lemma comap_eq {f : N →* M} : comap f f.map_mul c = ker (c.mk'.comp f) := ext $ λ x y, show c _ _ ↔ c.mk' _ = c.mk' _, by rw ←c.eq; refl variables (c) (f : M →* P) /-- The homomorphism on the quotient of a monoid by a congruence relation `c` induced by a homomorphism constant on `c`'s equivalence classes. -/ @[to_additive "The homomorphism on the quotient of an `add_monoid` by an additive congruence relation `c` induced by a homomorphism constant on `c`'s equivalence classes."] def lift (H : c ≤ ker f) : c.quotient →* P := { to_fun := λ x, con.lift_on x f $ λ _ _ h, H h, map_one' := by rw ←f.map_one; refl, map_mul' := λ x y, con.induction_on₂ x y $ λ m n, f.map_mul m n ▸ rfl } variables {c f} /-- The diagram describing the universal property for quotients of monoids commutes. -/ @[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s commutes."] lemma lift_mk' (H : c ≤ ker f) (x) : c.lift f H (c.mk' x) = f x := rfl /-- The diagram describing the universal property for quotients of monoids commutes. -/ @[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s commutes."] lemma lift_coe (H : c ≤ ker f) (x : M) : c.lift f H x = f x := rfl /-- The diagram describing the universal property for quotients of monoids commutes. -/ @[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s commutes."] theorem lift_comp_mk' (H : c ≤ ker f) : (c.lift f H).comp c.mk' = f := by ext; refl /-- Given a homomorphism `f` from the quotient of a monoid by a congruence relation, `f` equals the homomorphism on the quotient induced by `f` composed with the natural map from the monoid to the quotient. -/ @[simp, to_additive "Given a homomorphism `f` from the quotient of an `add_monoid` by an additive congruence relation, `f` equals the homomorphism on the quotient induced by `f` composed with the natural map from the `add_monoid` to the quotient."] lemma lift_apply_mk' (f : c.quotient →* P) : c.lift (f.comp c.mk') (λ x y h, show f ↑x = f ↑y, by rw c.eq.2 h) = f := by ext; rcases x; refl /-- Homomorphisms on the quotient of a monoid by a congruence relation are equal if they are equal on elements that are coercions from the monoid. -/ @[to_additive "Homomorphisms on the quotient of an `add_monoid` by an additive congruence relation are equal if they are equal on elements that are coercions from the `add_monoid`."] lemma lift_funext (f g : c.quotient →* P) (h : ∀ a : M, f a = g a) : f = g := begin rw [←lift_apply_mk' f, ←lift_apply_mk' g], congr' 1, exact monoid_hom.ext_iff.2 h, end /-- The uniqueness part of the universal property for quotients of monoids. -/ @[to_additive "The uniqueness part of the universal property for quotients of `add_monoid`s."] theorem lift_unique (H : c ≤ ker f) (g : c.quotient →* P) (Hg : g.comp c.mk' = f) : g = c.lift f H := lift_funext g (c.lift f H) $ λ x, by rw [lift_coe H, ←comp_mk'_apply, Hg] /-- Given a congruence relation `c` on a monoid and a homomorphism `f` constant on `c`'s equivalence classes, `f` has the same image as the homomorphism that `f` induces on the quotient. -/ @[to_additive "Given an additive congruence relation `c` on an `add_monoid` and a homomorphism `f` constant on `c`'s equivalence classes, `f` has the same image as the homomorphism that `f` induces on the quotient."] theorem lift_range (H : c ≤ ker f) : (c.lift f H).mrange = f.mrange := submonoid.ext $ λ x, ⟨λ ⟨y, hy⟩, by revert hy; rcases y; exact λ hy, ⟨y, hy.1, by rw [hy.2.symm, ←lift_coe H]; refl⟩, λ ⟨y, hy⟩, ⟨↑y, hy.1, by rw ←hy.2; refl⟩⟩ /-- Surjective monoid homomorphisms constant on a congruence relation `c`'s equivalence classes induce a surjective homomorphism on `c`'s quotient. -/ @[to_additive "Surjective `add_monoid` homomorphisms constant on an additive congruence relation `c`'s equivalence classes induce a surjective homomorphism on `c`'s quotient."] lemma lift_surjective_of_surjective (h : c ≤ ker f) (hf : surjective f) : surjective (c.lift f h) := λ y, exists.elim (hf y) $ λ w hw, ⟨w, (lift_mk' h w).symm ▸ hw⟩ variables (c f) /-- Given a monoid homomorphism `f` from `M` to `P`, the kernel of `f` is the unique congruence relation on `M` whose induced map from the quotient of `M` to `P` is injective. -/ @[to_additive "Given an `add_monoid` homomorphism `f` from `M` to `P`, the kernel of `f` is the unique additive congruence relation on `M` whose induced map from the quotient of `M` to `P` is injective."] lemma ker_eq_lift_of_injective (H : c ≤ ker f) (h : injective (c.lift f H)) : ker f = c := to_setoid_inj $ ker_eq_lift_of_injective f H h variables {c} /-- The homomorphism induced on the quotient of a monoid by the kernel of a monoid homomorphism. -/ @[to_additive "The homomorphism induced on the quotient of an `add_monoid` by the kernel of an `add_monoid` homomorphism."] def ker_lift : (ker f).quotient →* P := (ker f).lift f $ λ _ _, id variables {f} /-- The diagram described by the universal property for quotients of monoids, when the congruence relation is the kernel of the homomorphism, commutes. -/ @[simp, to_additive "The diagram described by the universal property for quotients of `add_monoid`s, when the additive congruence relation is the kernel of the homomorphism, commutes."] lemma ker_lift_mk (x : M) : ker_lift f x = f x := rfl /-- Given a monoid homomorphism `f`, the induced homomorphism on the quotient by `f`'s kernel has the same image as `f`. -/ @[simp, to_additive "Given an `add_monoid` homomorphism `f`, the induced homomorphism on the quotient by `f`'s kernel has the same image as `f`."] lemma ker_lift_range_eq : (ker_lift f).mrange = f.mrange := lift_range $ λ _ _, id /-- A monoid homomorphism `f` induces an injective homomorphism on the quotient by `f`'s kernel. -/ @[to_additive "An `add_monoid` homomorphism `f` induces an injective homomorphism on the quotient by `f`'s kernel."] lemma ker_lift_injective (f : M →* P) : injective (ker_lift f) := λ x y, quotient.induction_on₂' x y $ λ _ _, (ker f).eq.2 /-- Given congruence relations `c, d` on a monoid such that `d` contains `c`, `d`'s quotient map induces a homomorphism from the quotient by `c` to the quotient by `d`. -/ @[to_additive "Given additive congruence relations `c, d` on an `add_monoid` such that `d` contains `c`, `d`'s quotient map induces a homomorphism from the quotient by `c` to the quotient by `d`."] def map (c d : con M) (h : c ≤ d) : c.quotient →* d.quotient := c.lift d.mk' $ λ x y hc, show (ker d.mk') x y, from (mk'_ker d).symm ▸ h hc /-- Given congruence relations `c, d` on a monoid such that `d` contains `c`, the definition of the homomorphism from the quotient by `c` to the quotient by `d` induced by `d`'s quotient map. -/ @[to_additive "Given additive congruence relations `c, d` on an `add_monoid` such that `d` contains `c`, the definition of the homomorphism from the quotient by `c` to the quotient by `d` induced by `d`'s quotient map."] lemma map_apply {c d : con M} (h : c ≤ d) (x) : c.map d h x = c.lift d.mk' (λ x y hc, d.eq.2 $ h hc) x := rfl variables (c) /-- The first isomorphism theorem for monoids. -/ @[to_additive "The first isomorphism theorem for `add_monoid`s."] noncomputable def quotient_ker_equiv_range (f : M →* P) : (ker f).quotient ≃* f.mrange := { map_mul' := monoid_hom.map_mul _, ..equiv.of_bijective ((@mul_equiv.to_monoid_hom (ker_lift f).mrange _ _ _ $ mul_equiv.submonoid_congr ker_lift_range_eq).comp (ker_lift f).mrange_restrict) $ (equiv.bijective _).comp ⟨λ x y h, ker_lift_injective f $ by rcases x; rcases y; injections, λ ⟨w, z, hzm, hz⟩, ⟨z, by rcases hz; rcases _x; refl⟩⟩ } /-- The first isomorphism theorem for monoids in the case of a surjective homomorphism. -/ @[to_additive "The first isomorphism theorem for `add_monoid`s in the case of a surjective homomorphism."] noncomputable def quotient_ker_equiv_of_surjective (f : M →* P) (hf : surjective f) : (ker f).quotient ≃* P := { map_mul' := monoid_hom.map_mul _, ..equiv.of_bijective (ker_lift f) ⟨ker_lift_injective f, lift_surjective_of_surjective (le_refl _) hf⟩ } /-- The second isomorphism theorem for monoids. -/ @[to_additive "The second isomorphism theorem for `add_monoid`s."] noncomputable def comap_quotient_equiv (f : N →* M) : (comap f f.map_mul c).quotient ≃* (c.mk'.comp f).mrange := (con.congr comap_eq).trans $ quotient_ker_equiv_range $ c.mk'.comp f /-- The third isomorphism theorem for monoids. -/ @[to_additive "The third isomorphism theorem for `add_monoid`s."] def quotient_quotient_equiv_quotient (c d : con M) (h : c ≤ d) : (ker (c.map d h)).quotient ≃* d.quotient := { map_mul' := λ x y, con.induction_on₂ x y $ λ w z, con.induction_on₂ w z $ λ a b, show _ = d.mk' a * d.mk' b, by rw ←d.mk'.map_mul; refl, ..quotient_quotient_equiv_quotient c.to_setoid d.to_setoid h } end con
ec320ff6b3efe271152c851f73a3a10a9fa19cfa
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/doc_commands.lean
d03a2a214e221c40d5be33aa8fefdc54dd064de1
[ "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
161
lean
import tactic.doc_commands open tactic namespace bar def foo := 5 /-- ok -/ add_decl_doc foo run_cmd do ds ← doc_string ``foo, guard $ ds = "ok" end bar
44e086e478c58d8aa14c4985e00aa3f40c2f8352
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/polynomial/div.lean
8cc64300f878fb835ab2dbc1d46e06f13306803d
[ "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,910
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.monic import ring_theory.euclidean_domain import ring_theory.multiplicity /-! # Division of univariate polynomials The main defs are `div_by_monic` and `mod_by_monic`. The compatibility between these is given by `mod_by_monic_add_div`. We also define `root_multiplicity`. -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section semiring variables [semiring R] {p q : polynomial R} section /-- The coercion turning a `polynomial` into the function which reports the coefficient of a given monomial `X^n` -/ -- TODO we would like to completely remove this, but this requires fixing some proofs def coeff_coe_to_fun : has_coe_to_fun (polynomial R) := finsupp.has_coe_to_fun local attribute [instance] coeff_coe_to_fun lemma apply_eq_coeff : p n = coeff p n := rfl end /-- `div_X p` return a polynomial `q` such that `q * X + C (p.coeff 0) = p`. It can be used in a semiring where the usual division algorithm is not possible -/ def div_X (p : polynomial R) : polynomial R := { to_fun := λ n, p.coeff (n + 1), support := ⟨(p.support.filter (> 0)).1.map (λ n, n - 1), multiset.nodup_map_on begin simp only [finset.mem_def.symm, finset.mem_erase, finset.mem_filter], assume x hx y hy hxy, rwa [← @add_right_cancel_iff _ _ 1, nat.sub_add_cancel hx.2, nat.sub_add_cancel hy.2] at hxy end (p.support.filter (> 0)).2⟩, mem_support_to_fun := λ n, suffices (∃ (a : ℕ), (¬coeff p a = 0 ∧ a > 0) ∧ a - 1 = n) ↔ ¬coeff p (n + 1) = 0, by simpa [finset.mem_def.symm], ⟨λ ⟨a, ha⟩, by rw [← ha.2, nat.sub_add_cancel ha.1.2]; exact ha.1.1, λ h, ⟨n + 1, ⟨h, nat.succ_pos _⟩, nat.succ_sub_one _⟩⟩ } lemma div_X_mul_X_add (p : polynomial R) : div_X p * X + C (p.coeff 0) = p := ext $ λ n, nat.cases_on n (by simp) (by simp [coeff_C, nat.succ_ne_zero, coeff_mul_X, div_X]) @[simp] lemma div_X_C (a : R) : div_X (C a) = 0 := ext $ λ n, by cases n; simp [div_X, coeff_C]; simp [coeff] lemma div_X_eq_zero_iff : div_X p = 0 ↔ p = C (p.coeff 0) := ⟨λ h, by simpa [eq_comm, h] using div_X_mul_X_add p, λ h, by rw [h, div_X_C]⟩ lemma div_X_add : div_X (p + q) = div_X p + div_X q := ext $ by simp [div_X] lemma degree_div_X_lt (hp0 : p ≠ 0) : (div_X p).degree < p.degree := by haveI := nonzero.of_polynomial_ne hp0; exact calc (div_X p).degree < (div_X p * X + C (p.coeff 0)).degree : if h : degree p ≤ 0 then begin have h' : C (p.coeff 0) ≠ 0, by rwa [← eq_C_of_degree_le_zero h], rw [eq_C_of_degree_le_zero h, div_X_C, degree_zero, zero_mul, zero_add], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 $ by simp [h'])), end else have hXp0 : div_X p ≠ 0, by simpa [div_X_eq_zero_iff, -not_le, degree_le_zero_iff] using h, have leading_coeff (div_X p) * leading_coeff X ≠ 0, by simpa, have degree (C (p.coeff 0)) < degree (div_X p * X), from calc degree (C (p.coeff 0)) ≤ 0 : degree_C_le ... < 1 : dec_trivial ... = degree (X : polynomial R) : degree_X.symm ... ≤ degree (div_X p * X) : by rw [← zero_add (degree X), degree_mul' this]; exact add_le_add (by rw [zero_le_degree_iff, ne.def, div_X_eq_zero_iff]; exact λ h0, h (h0.symm ▸ degree_C_le)) (le_refl _), by rw [add_comm, degree_add_eq_of_degree_lt this]; exact degree_lt_degree_mul_X hXp0 ... = p.degree : by rw div_X_mul_X_add /-- An induction principle for polynomials, valued in Sort* instead of Prop. -/ @[elab_as_eliminator] noncomputable def rec_on_horner {M : polynomial R → Sort*} : Π (p : polynomial R), M 0 → (Π p a, coeff p 0 = 0 → a ≠ 0 → M p → M (p + C a)) → (Π p, p ≠ 0 → M p → M (p * X)) → M p | p := λ M0 MC MX, if hp : p = 0 then eq.rec_on hp.symm M0 else have wf : degree (div_X p) < degree p, from degree_div_X_lt hp, by rw [← div_X_mul_X_add p] at *; exact if hcp0 : coeff p 0 = 0 then by rw [hcp0, C_0, add_zero]; exact MX _ (λ h : div_X p = 0, by simpa [h, hcp0] using hp) (rec_on_horner _ M0 MC MX) else MC _ _ (coeff_mul_X_zero _) hcp0 (if hpX0 : div_X p = 0 then show M (div_X p * X), by rw [hpX0, zero_mul]; exact M0 else MX (div_X p) hpX0 (rec_on_horner _ M0 MC MX)) using_well_founded {dec_tac := tactic.assumption} @[elab_as_eliminator] lemma degree_pos_induction_on {P : polynomial R → Prop} (p : polynomial R) (h0 : 0 < degree p) (hC : ∀ {a}, a ≠ 0 → P (C a * X)) (hX : ∀ {p}, 0 < degree p → P p → P (p * X)) (hadd : ∀ {p} {a}, 0 < degree p → P p → P (p + C a)) : P p := rec_on_horner p (λ h, by rw degree_zero at h; exact absurd h dec_trivial) (λ p a _ _ ih h0, have 0 < degree p, from lt_of_not_ge (λ h, (not_lt_of_ge degree_C_le) $ by rwa [eq_C_of_degree_le_zero h, ← C_add] at h0), hadd this (ih this)) (λ p _ ih h0', if h0 : 0 < degree p then hX h0 (ih h0) else by rw [eq_C_of_degree_le_zero (le_of_not_gt h0)] at *; exact hC (λ h : coeff p 0 = 0, by simpa [h, nat.not_lt_zero] using h0')) h0 end semiring section comm_semiring variables [comm_semiring R] theorem X_dvd_iff {α : Type u} [comm_semiring α] {f : polynomial α} : X ∣ f ↔ f.coeff 0 = 0 := ⟨λ ⟨g, hfg⟩, by rw [hfg, mul_comm, coeff_mul_X_zero], λ hf, ⟨f.div_X, by rw [mul_comm, ← add_zero (f.div_X * X), ← C_0, ← hf, div_X_mul_X_add]⟩⟩ end comm_semiring section comm_semiring variables [comm_semiring R] {p q : polynomial R} lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot ℕ) < degree p) (hmp : monic p) (hq : q ≠ 0) : multiplicity.finite p q := have zn0 : (0 : R) ≠ 1, from λ h, by haveI := subsingleton_of_zero_eq_one h; exact hq (subsingleton.elim _ _), ⟨nat_degree q, λ ⟨r, hr⟩, have hp0 : p ≠ 0, from λ hp0, by simp [hp0] at hp; contradiction, have hr0 : r ≠ 0, from λ hr0, by simp * at *, have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1, by simp [show _ = _, from hmp], have hpn0' : leading_coeff p ^ (nat_degree q + 1) ≠ 0, from hpn1.symm ▸ zn0.symm, have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r ≠ 0, by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1, one_pow, one_mul, ne.def, hr0]; simp, have hpn0 : p ^ (nat_degree q + 1) ≠ 0, from mt leading_coeff_eq_zero.2 $ by rw [leading_coeff_pow' hpn0', show _ = _, from hmp, one_pow]; exact zn0.symm, have hnp : 0 < nat_degree p, by rw [← with_bot.coe_lt_coe, ← degree_eq_nat_degree hp0]; exact hp, begin have := congr_arg nat_degree hr, rw [nat_degree_mul' hpnr0, nat_degree_pow' hpn0', add_mul, add_assoc] at this, exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right (nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this end⟩ end comm_semiring section ring variables [ring R] {p q : polynomial R} lemma div_wf_lemma (h : degree q ≤ degree p ∧ p ≠ 0) (hq : monic q) : degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p := have hp : leading_coeff p ≠ 0 := mt leading_coeff_eq_zero.1 h.2, have hpq : leading_coeff (C (leading_coeff p) * X ^ (nat_degree p - nat_degree q)) * leading_coeff q ≠ 0, by rwa [leading_coeff_monomial, monic.def.1 hq, mul_one], if h0 : p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q = 0 then h0.symm ▸ (lt_of_not_ge $ mt le_bot_iff.1 (mt degree_eq_bot.1 h.2)) else have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic h.2 hq, have hlt : nat_degree q ≤ nat_degree p := with_bot.coe_le_coe.1 (by rw [← degree_eq_nat_degree h.2, ← degree_eq_nat_degree hq0]; exact h.1), degree_sub_lt (by rw [degree_mul' hpq, degree_monomial _ hp, degree_eq_nat_degree h.2, degree_eq_nat_degree hq0, ← with_bot.coe_add, nat.sub_add_cancel hlt]) h.2 (by rw [leading_coeff_mul' hpq, leading_coeff_monomial, monic.def.1 hq, mul_one]) /-- See `div_by_monic`. -/ noncomputable def div_mod_by_monic_aux : Π (p : polynomial R) {q : polynomial R}, monic q → polynomial R × polynomial R | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in have wf : _ := div_wf_lemma h hq, let dm := div_mod_by_monic_aux (p - z * q) hq in ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ using_well_founded {dec_tac := tactic.assumption} /-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/ def div_by_monic (p q : polynomial R) : polynomial R := if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0 /-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/ def mod_by_monic (p q : polynomial R) : polynomial R := if hq : monic q then (div_mod_by_monic_aux p hq).2 else p infixl ` /ₘ ` : 70 := div_by_monic infixl ` %ₘ ` : 70 := mod_by_monic lemma degree_mod_by_monic_lt : ∀ (p : polynomial R) {q : polynomial R} (hq : monic q) (hq0 : q ≠ 0), degree (p %ₘ q) < degree q | p := λ q hq hq0, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma ⟨h.1, h.2⟩ hq, have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %ₘ q) < degree q := degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq hq0, begin unfold mod_by_monic at this ⊢, unfold div_mod_by_monic_aux, rw dif_pos hq at this ⊢, rw if_pos h, exact this end else or.cases_on (not_and_distrib.1 h) begin unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h], exact lt_of_not_ge, end begin assume hp, unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, not_not.1 hp], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 hq0)), end using_well_founded {dec_tac := tactic.assumption} @[simp] lemma zero_mod_by_monic (p : polynomial R) : 0 %ₘ p = 0 := begin unfold mod_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma zero_div_by_monic (p : polynomial R) : 0 /ₘ p = 0 := begin unfold div_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma mod_by_monic_zero (p : polynomial R) : p %ₘ 0 = p := if h : monic (0 : polynomial R) then (subsingleton_of_monic_zero h).1 _ _ else by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h @[simp] lemma div_by_monic_zero (p : polynomial R) : p /ₘ 0 = 0 := if h : monic (0 : polynomial R) then (subsingleton_of_monic_zero h).1 _ _ else by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h lemma div_by_monic_eq_of_not_monic (p : polynomial R) (hq : ¬monic q) : p /ₘ q = 0 := dif_neg hq lemma mod_by_monic_eq_of_not_monic (p : polynomial R) (hq : ¬monic q) : p %ₘ q = p := dif_neg hq lemma mod_by_monic_eq_self_iff (hq : monic q) (hq0 : q ≠ 0) : p %ₘ q = p ↔ degree p < degree q := ⟨λ h, h ▸ degree_mod_by_monic_lt _ hq hq0, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ theorem degree_mod_by_monic_le (p : polynomial R) {q : polynomial R} (hq : monic q) : degree (p %ₘ q) ≤ degree q := decidable.by_cases (assume H : q = 0, by rw [monic, H, leading_coeff_zero] at hq; have : (0:polynomial R) = 1 := (by rw [← C_0, ← C_1, hq]); exact le_of_eq (congr_arg _ $ eq_of_zero_eq_one this (p %ₘ q) q)) (assume H : q ≠ 0, le_of_lt $ degree_mod_by_monic_lt _ hq H) end ring section comm_ring variables [comm_ring R] {p q : polynomial R} lemma mod_by_monic_eq_sub_mul_div : ∀ (p : polynomial R) {q : polynomial R} (hq : monic q), p %ₘ q = p - q * (p /ₘ q) | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma h hq, have ih : _ := mod_by_monic_eq_sub_mul_div (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_pos h], rw [mod_by_monic, dif_pos hq] at ih, refine ih.trans _, unfold div_by_monic, rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm] end else begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_add_div (p : polynomial R) {q : polynomial R} (hq : monic q) : p %ₘ q + q * (p /ₘ q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq) lemma div_by_monic_eq_zero_iff (hq : monic q) (hq0 : q ≠ 0) : p /ₘ q = 0 ↔ degree p < degree q := ⟨λ h, by have := mod_by_monic_add_div p hq; rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq hq0] at this, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma degree_add_div_by_monic (hq : monic q) (h : degree q ≤ degree p) : degree q + degree (p /ₘ q) = degree p := if hq0 : q = 0 then have ∀ (p : polynomial R), p = 0, from λ p, (@subsingleton_of_monic_zero R _ (hq0 ▸ hq)).1 _ _, by rw [this (p /ₘ q), this p, this q]; refl else have hdiv0 : p /ₘ q ≠ 0 := by rwa [(≠), div_by_monic_eq_zero_iff hq hq0, not_lt], have hlc : leading_coeff q * leading_coeff (p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul, (≠), leading_coeff_eq_zero], have hmod : degree (p %ₘ q) < degree (q * (p /ₘ q)) := calc degree (p %ₘ q) < degree q : degree_mod_by_monic_lt _ hq hq0 ... ≤ _ : by rw [degree_mul' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree hdiv0, ← with_bot.coe_add, with_bot.coe_le_coe]; exact nat.le_add_right _ _, calc degree q + degree (p /ₘ q) = degree (q * (p /ₘ q)) : eq.symm (degree_mul' hlc) ... = degree (p %ₘ q + q * (p /ₘ q)) : (degree_add_eq_of_degree_lt hmod).symm ... = _ : congr_arg _ (mod_by_monic_add_div _ hq) lemma degree_div_by_monic_le (p q : polynomial R) : degree (p /ₘ q) ≤ degree p := if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl] else if hq : monic q then have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic hp0 hq, if h : degree q ≤ degree p then by rw [← degree_add_div_by_monic hq h, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 (not_lt.2 h))]; exact with_bot.coe_le_coe.2 (nat.le_add_left _ _) else by unfold div_by_monic div_mod_by_monic_aux; simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le] else (div_by_monic_eq_of_not_monic p hq).symm ▸ bot_le lemma degree_div_by_monic_lt (p : polynomial R) {q : polynomial R} (hq : monic q) (hp0 : p ≠ 0) (h0q : 0 < degree q) : degree (p /ₘ q) < degree p := have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic hp0 hq, if hpq : degree p < degree q then begin rw [(div_by_monic_eq_zero_iff hq hq0).2 hpq, degree_eq_nat_degree hp0], exact with_bot.bot_lt_some _ end else begin rw [← degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 hpq)], exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left (with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq0) ▸ h0q)) end theorem nat_degree_div_by_monic {R : Type u} [comm_ring R] (f : polynomial R) {g : polynomial R} (hg : g.monic) : nat_degree (f /ₘ g) = nat_degree f - nat_degree g := begin by_cases h01 : (0 : R) = 1, { haveI := subsingleton_of_zero_eq_one h01, rw [subsingleton.elim (f /ₘ g) 0, subsingleton.elim f 0, subsingleton.elim g 0, nat_degree_zero] }, haveI : nontrivial R := ⟨⟨0, 1, h01⟩⟩, by_cases hfg : f /ₘ g = 0, { rw [hfg, nat_degree_zero], rw div_by_monic_eq_zero_iff hg hg.ne_zero at hfg, rw nat.sub_eq_zero_of_le (nat_degree_le_nat_degree $ le_of_lt hfg) }, have hgf := hfg, rw div_by_monic_eq_zero_iff hg hg.ne_zero at hgf, push_neg at hgf, have := degree_add_div_by_monic hg hgf, have hf : f ≠ 0, { intro hf, apply hfg, rw [hf, zero_div_by_monic] }, rw [degree_eq_nat_degree hf, degree_eq_nat_degree hg.ne_zero, degree_eq_nat_degree hfg, ← with_bot.coe_add, with_bot.coe_eq_coe] at this, rw [← this, nat.add_sub_cancel_left] end lemma div_mod_by_monic_unique {f g} (q r : polynomial R) (hg : monic g) (h : r + g * q = f ∧ degree r < degree g) : f /ₘ g = q ∧ f %ₘ g = r := if hg0 : g = 0 then by split; exact (subsingleton_of_monic_zero (hg0 ▸ hg : monic (0 : polynomial R))).1 _ _ else have h₁ : r - f %ₘ g = -g * (q - f /ₘ g), from eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)]; simp [mul_add, mul_comm, sub_eq_add_neg, add_comm, add_left_comm, add_assoc]), have h₂ : degree (r - f %ₘ g) = degree (g * (q - f /ₘ g)), by simp [h₁], have h₄ : degree (r - f %ₘ g) < degree g, from calc degree (r - f %ₘ g) ≤ max (degree r) (degree (-(f %ₘ g))) : degree_add_le _ _ ... < degree g : max_lt_iff.2 ⟨h.2, by rw degree_neg; exact degree_mod_by_monic_lt _ hg hg0⟩, have h₅ : q - (f /ₘ g) = 0, from by_contradiction (λ hqf, not_le_of_gt h₄ $ calc degree g ≤ degree g + degree (q - f /ₘ g) : by erw [degree_eq_nat_degree hg0, degree_eq_nat_degree hqf, with_bot.coe_le_coe]; exact nat.le_add_right _ _ ... = degree (r - f %ₘ g) : by rw [h₂, degree_mul']; simpa [monic.def.1 hg]), ⟨eq.symm $ eq_of_sub_eq_zero h₅, eq.symm $ eq_of_sub_eq_zero $ by simpa [h₅] using h₁⟩ lemma map_mod_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f ∧ (p %ₘ q).map f = p.map f %ₘ q.map f := if h01 : (0 : S) = 1 then by haveI := subsingleton_of_zero_eq_one h01; exact ⟨subsingleton.elim _ _, subsingleton.elim _ _⟩ else have h01R : (0 : R) ≠ 1, from mt (congr_arg f) (by rwa [is_semiring_hom.map_one f, is_semiring_hom.map_zero f]), have map f p /ₘ map f q = map f (p /ₘ q) ∧ map f p %ₘ map f q = map f (p %ₘ q), from (div_mod_by_monic_unique ((p /ₘ q).map f) _ (monic_map f hq) ⟨eq.symm $ by rw [← map_mul, ← map_add, mod_by_monic_add_div _ hq], calc _ ≤ degree (p %ₘ q) : degree_map_le _ ... < degree q : degree_mod_by_monic_lt _ hq $ (ne_zero_of_monic_of_zero_ne_one hq h01R) ... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _ (by rw [monic.def.1 hq, is_semiring_hom.map_one f]; exact ne.symm h01)⟩), ⟨this.1.symm, this.2.symm⟩ lemma map_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f := (map_mod_div_by_monic f hq).1 lemma map_mod_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p %ₘ q).map f = p.map f %ₘ q.map f := (map_mod_div_by_monic f hq).2 lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %ₘ q = 0 ↔ q ∣ p := ⟨λ h, by rw [← mod_by_monic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, λ h, if hq0 : q = 0 then by rw hq0 at hq; exact (subsingleton_of_monic_zero hq).1 _ _ else let ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h in by_contradiction (λ hpq0, have hmod : p %ₘ q = q * (r - p /ₘ q) := by rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, ← hr], have degree (q * (r - p /ₘ q)) < degree q := hmod ▸ degree_mod_by_monic_lt _ hq hq0, have hrpq0 : leading_coeff (r - p /ₘ q) ≠ 0 := λ h, hpq0 $ leading_coeff_eq_zero.1 (by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]), have hlc : leading_coeff q * leading_coeff (r - p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul], by rw [degree_mul' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this; exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this))⟩ theorem map_dvd_map [comm_ring S] (f : R →+* S) (hf : function.injective f) {x y : polynomial R} (hx : x.monic) : x.map f ∣ y.map f ↔ x ∣ y := begin rw [← dvd_iff_mod_by_monic_eq_zero hx, ← dvd_iff_mod_by_monic_eq_zero (monic_map f hx), ← map_mod_by_monic f hx], exact ⟨λ H, map_injective f hf $ by rw [H, map_zero], λ H, by rw [H, map_zero]⟩ end @[simp] lemma mod_by_monic_one (p : polynomial R) : p %ₘ 1 = 0 := (dvd_iff_mod_by_monic_eq_zero (by convert monic_one)).2 (one_dvd _) @[simp] lemma div_by_monic_one (p : polynomial R) : p /ₘ 1 = p := by conv_rhs { rw [← mod_by_monic_add_div p monic_one] }; simp @[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : polynomial R) (a : R) : p %ₘ (X - C a) = C (p.eval a) := if h0 : (0 : R) = 1 then by letI := subsingleton_of_zero_eq_one h0; exact subsingleton.elim _ _ else by haveI : nontrivial R := nontrivial_of_ne 0 1 h0; exact have h : (p %ₘ (X - C a)).eval a = p.eval a := by rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero], have degree (p %ₘ (X - C a)) < 1 := degree_X_sub_C a ▸ degree_mod_by_monic_lt p (monic_X_sub_C a) ((degree_X_sub_C a).symm ▸ ne_zero_of_monic (monic_X_sub_C _)), have degree (p %ₘ (X - C a)) ≤ 0 := begin cases (degree (p %ₘ (X - C a))), { exact bot_le }, { exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) } end, begin rw [eq_C_of_degree_le_zero this, eval_C] at h, rw [eq_C_of_degree_le_zero this, h] end lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /ₘ (X - C a)) = p ↔ is_root p a := ⟨λ h, by rw [← h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], λ h : p.eval a = 0, by conv {to_rhs, rw ← mod_by_monic_add_div p (monic_X_sub_C a)}; rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ lemma dvd_iff_is_root : (X - C a) ∣ p ↔ is_root p a := ⟨λ h, by rwa [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, λ h, ⟨(p /ₘ (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 h⟩⟩ lemma mod_by_monic_X (p : polynomial R) : p %ₘ X = C (p.eval 0) := by rw [← mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero] section multiplicity /-- An algorithm for deciding polynomial divisibility. The algorithm is "compute `p %ₘ q` and compare to `0`". ` See `polynomial.mod_by_monic` for the algorithm that computes `%ₘ`. -/ def decidable_dvd_monic (p : polynomial R) (hq : monic q) : decidable (q ∣ p) := decidable_of_iff (p %ₘ q = 0) (dvd_iff_mod_by_monic_eq_zero hq) open_locale classical lemma multiplicity_X_sub_C_finite (a : R) (h0 : p ≠ 0) : multiplicity.finite (X - C a) p := multiplicity_finite_of_degree_pos_of_monic (have (0 : R) ≠ 1, from (λ h, by haveI := subsingleton_of_zero_eq_one h; exact h0 (subsingleton.elim _ _)), by haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩; rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) h0 /-- The largest power of `X - C a` which divides `p`. This is computable via the divisibility algorithm `decidable_dvd_monic`. -/ def root_multiplicity (a : R) (p : polynomial R) : ℕ := if h0 : p = 0 then 0 else let I : decidable_pred (λ n : ℕ, ¬(X - C a) ^ (n + 1) ∣ p) := λ n, @not.decidable _ (decidable_dvd_monic p (monic_pow (monic_X_sub_C a) (n + 1))) in by exactI nat.find (multiplicity_X_sub_C_finite a h0) lemma root_multiplicity_eq_multiplicity (p : polynomial R) (a : R) : root_multiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, root_multiplicity, roption.dom]; congr; funext; congr lemma pow_root_multiplicity_dvd (p : polynomial R) (a : R) : (X - C a) ^ root_multiplicity a p ∣ p := if h : p = 0 then by simp [h] else by rw [root_multiplicity_eq_multiplicity, dif_neg h]; exact multiplicity.pow_multiplicity_dvd _ lemma div_by_monic_mul_pow_root_multiplicity_eq (p : polynomial R) (a : R) : p /ₘ ((X - C a) ^ root_multiplicity a p) * (X - C a) ^ root_multiplicity a p = p := have monic ((X - C a) ^ root_multiplicity a p), from monic_pow (monic_X_sub_C _) _, by conv_rhs { rw [← mod_by_monic_add_div p this, (dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] }; simp [mul_comm] lemma eval_div_by_monic_pow_root_multiplicity_ne_zero {p : polynomial R} (a : R) (hp : p ≠ 0) : (p /ₘ ((X - C a) ^ root_multiplicity a p)).eval a ≠ 0 := begin haveI : nontrivial R := nonzero.of_polynomial_ne hp, rw [ne.def, ← is_root.def, ← dvd_iff_is_root], rintros ⟨q, hq⟩, have := div_by_monic_mul_pow_root_multiplicity_eq p a, rw [mul_comm, hq, ← mul_assoc, ← pow_succ', root_multiplicity_eq_multiplicity, dif_neg hp] at this, exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : with_bot ℕ) < degree (X - C a), by rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) hp) (nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) end end multiplicity end comm_ring end polynomial
93aa0ffd1276169df3ea737facdb2e0902628d88
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/pi_instances.lean
3ab20ab678d387dde01c3c930ed5d18053492af0
[ "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
2,072
lean
/- Copyright (c) 2018 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import tactic.solve_by_elim order.basic /-! # `pi_instance` Automation for creating instances of mathematical structures for pi types -/ namespace tactic open interactive interactive.types lean.parser expr open functor has_seq list nat tactic.interactive meta def derive_field : tactic unit := do b ← target >>= is_prop, field ← get_current_field, if b then do field ← get_current_field, vs ← introv [] <|> pure [], hs ← intros <|> pure [], resetI, x ← get_unused_name, try (() <$ ext1 [rcases_patt.one x] <|> () <$ intro x), x' ← try_core (get_local x), applyc field, hs.mmap (λ h, try $ () <$ (to_expr ``(congr_fun %%h %%(x'.iget)) >>= apply) <|> () <$ apply (h x'.iget) <|> () <$ (to_expr ``(set.mem_image_of_mem _ %%h) >>= apply) <|> () <$ (solve_by_elim) ), return () else focus1 $ do field ← get_current_field, e ← mk_const field, expl_arity ← get_expl_arity e, xs ← (iota expl_arity).mmap $ λ _, intro1, x ← intro1, applyc field, xs.mmap' (λ h, try $ () <$ (apply (h x) <|> apply h) <|> refine ``(set.image ($ %%x) %%h)) <|> fail "args", return () /-- `pi_instance` constructs an instance of `my_class (Π i : I, f i)` where we know `Π i, my_class (f i)`. If an order relation is required, it defaults to `pi.partial_order`. Any field of the instance that `pi_instance` cannot construct is left untouched and generated as a new goal. -/ meta def pi_instance : tactic unit := refine_struct ``( { ..pi.partial_order, .. } ); propagate_tags (try (derive_field ; done)) run_cmd add_interactive [`pi_instance] add_tactic_doc { name := "pi_instance", category := doc_category.tactic, decl_names := [`tactic.interactive.pi_instance], tags := ["type class"] } end tactic
2d613a910b8cc1f7d2fe4ff7e58210bfca09db9c
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/algebra/lie/semisimple.lean
d91ce454a00c130d05d2cc3b5cd162bc5219d2fb
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,544
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.solvable /-! # Semisimple Lie algebras The famous Cartan-Dynkin-Killing classification of semisimple Lie algebras renders them one of the most important classes of Lie algebras. In this file we define simple and semisimple Lie algebras and prove some basic related results. ## Main definitions * `lie_module.is_irreducible` * `lie_algebra.is_simple` * `lie_algebra.is_semisimple` * `lie_algebra.is_semisimple_iff_no_solvable_ideals` * `lie_algebra.is_semisimple_iff_no_abelian_ideals` * `lie_algebra.abelian_radical_iff_solvable_is_abelian` ## Tags lie algebra, radical, simple, semisimple -/ universes u v w w₁ w₂ /-- A Lie module is irreducible if it is zero or its only non-trivial Lie submodule is itself. -/ class lie_module.is_irreducible (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] : Prop := (irreducible : ∀ (N : lie_submodule R L M), N ≠ ⊥ → N = ⊤) namespace lie_algebra variables (R : Type u) (L : Type v) variables [comm_ring R] [lie_ring L] [lie_algebra R L] set_option old_structure_cmd true /-- A Lie algebra is simple if it is irreducible as a Lie module over itself via the adjoint action, and it is non-Abelian. -/ class is_simple extends lie_module.is_irreducible R L L : Prop := (non_abelian : ¬is_lie_abelian L) /-- A semisimple Lie algebra is one with trivial radical. Note that the label 'semisimple' is apparently not universally agreed [upon](https://mathoverflow.net/questions/149391/on-radicals-of-a-lie-algebra#comment383669_149391) for general coefficients. We are following [Seligman, page 15](seligman1967) and using the label for the weakest of the various properties which are all equivalent over a field of characteristic zero. -/ class is_semisimple : Prop := (semisimple : radical R L = ⊥) lemma is_semisimple_iff_no_solvable_ideals : is_semisimple R L ↔ ∀ (I : lie_ideal R L), is_solvable R I → I = ⊥ := ⟨λ h, Sup_eq_bot.mp h.semisimple, λ h, ⟨Sup_eq_bot.mpr h⟩⟩ lemma is_semisimple_iff_no_abelian_ideals : is_semisimple R L ↔ ∀ (I : lie_ideal R L), is_lie_abelian I → I = ⊥ := begin rw is_semisimple_iff_no_solvable_ideals, split; intros h₁ I h₂, { haveI : is_lie_abelian I := h₂, apply h₁, exact lie_algebra.of_abelian_is_solvable R I, }, { haveI : is_solvable R I := h₂, rw ← abelian_of_solvable_ideal_eq_bot_iff, apply h₁, exact abelian_derived_abelian_of_ideal I, }, end @[simp] lemma center_eq_bot_of_semisimple [h : is_semisimple R L] : center R L = ⊥ := by { rw is_semisimple_iff_no_abelian_ideals at h, apply h, apply_instance, } /-- A simple Lie algebra is semisimple. -/ @[priority 100] instance is_semisimple_of_is_simple [h : is_simple R L] : is_semisimple R L := begin rw is_semisimple_iff_no_abelian_ideals, intros I hI, tactic.unfreeze_local_instances, obtain ⟨h₁, h₂⟩ := h, by_contradiction contra, rw [h₁ I contra, lie_abelian_iff_equiv_lie_abelian lie_ideal.top_equiv_self] at hI, exact h₂ hI, end /-- A semisimple Abelian Lie algebra is trivial. -/ lemma subsingleton_of_semisimple_lie_abelian [is_semisimple R L] [h : is_lie_abelian L] : subsingleton L := begin rw [is_lie_abelian_iff_center_eq_top R L, center_eq_bot_of_semisimple] at h, exact (lie_submodule.subsingleton_iff R L L).mp (subsingleton_of_bot_eq_top h), end lemma abelian_radical_of_semisimple [is_semisimple R L] : is_lie_abelian (radical R L) := by { rw is_semisimple.semisimple, exact is_lie_abelian_bot R L, } /-- The two properties shown to be equivalent here are possible definitions for a Lie algebra to be reductive. Note that there is absolutely [no agreement](https://mathoverflow.net/questions/284713/) on what the label 'reductive' should mean when the coefficients are not a field of characteristic zero. -/ lemma abelian_radical_iff_solvable_is_abelian [is_noetherian R L] : is_lie_abelian (radical R L) ↔ ∀ (I : lie_ideal R L), is_solvable R I → is_lie_abelian I := begin split, { rintros h₁ I h₂, rw lie_ideal.solvable_iff_le_radical at h₂, exact (lie_ideal.hom_of_le_injective h₂).is_lie_abelian h₁, }, { intros h, apply h, apply_instance, }, end lemma ad_ker_eq_bot_of_semisimple [is_semisimple R L] : (ad R L).ker = ⊥ := by simp end lie_algebra
ba956daded82690cc38d36f482ca5ed851bae9f1
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/data/int/basic.lean
d9c0d85c7bf0b4b8eb2e99835e8eee1c7c93a802
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
51,594
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ import data.nat.basic data.list.basic algebra.char_zero algebra.order_functions open nat namespace int instance : inhabited ℤ := ⟨int.zero⟩ @[simp] lemma default_eq_zero : default ℤ = 0 := rfl meta instance : has_to_format ℤ := ⟨λ z, int.rec_on z (λ k, ↑k) (λ k, "-("++↑k++"+1)")⟩ meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ attribute [simp] int.of_nat_eq_coe @[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl @[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl @[simp, elim_cast] theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n @[simp, elim_cast] theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n @[simp, elim_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] @[simp] theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 := not_congr coe_nat_eq_zero lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (nat.zero_le _) lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n) @[simp, elim_cast] theorem coe_nat_abs (n : ℕ) : abs (n : ℤ) = n := abs_of_nonneg (coe_nat_nonneg n) /- succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : ℤ) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : ℤ) := a - 1 theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : ℤ) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : ℤ) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : ℤ) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b := @add_le_add_iff_right _ _ a b 1 theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b := le_sub_iff_add_le @[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop} (i : ℤ) (hz : p 0) (hp : ∀i : ℕ, p i → p (i + 1)) (hn : ∀i : ℕ, p (-i) → p (-i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : ∀n:ℕ, p (- n), { intro n, induction n, { simp [hz] }, { have := hn _ n_ih, simpa } }, exact this (i + 1) } end protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) : C b → (∀ k, b ≤ k → C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z := λ H0 Hs Hp, begin rw ←sub_add_cancel z b, induction (z - b), { induction a with n ih, { rwa [of_nat_zero, zero_add] }, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc], exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih }, { induction a with n ih, { rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub], exact Hp _ (le_refl _) H0 }, { rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub], exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } } end /- nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := begin have : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b), { refine (λ a b : ℕ, sub_nat_nat_elim a b.succ (λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ_inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp only [(*), int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs] @[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a := by rw [← int.coe_nat_mul, nat_abs_mul_self] theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq] lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 := λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h @[simp] lemma nat_abs_eq_zero {a : ℤ} : a.nat_abs = 0 ↔ a = 0 := ⟨int.eq_zero_of_nat_abs_eq_zero, λ h, h.symm ▸ rfl⟩ /- / -/ @[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl @[simp, move_cast] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : 0 < b) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end @[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b) | (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl | (m : ℕ) (n+1:ℕ) := rfl | 0 -[1+ n] := rfl | (m+1:ℕ) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : ℤ} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end @[simp] protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0 | 0 := rfl | (n+1:ℕ) := rfl | -[1+ n] := rfl @[simp] protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0 | 0 := rfl | (n+1:ℕ) := rfl | -[1+ n] := rfl @[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a | 0 := rfl | (n+1:ℕ) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 := match b, abs b, abs_eq_nat_abs b, H2 with | (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_inj $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) : (a + b * c) / c = a / c + b := have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from λ k n a, match a with | (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ = n - (m / k.succ + 1 : ℕ), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : ℕ) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have ∀ {a b c : ℤ}, 0 < c → (a + b * c) / c = a / c + b, from λ a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : ℕ) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] @[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /- mod -/ theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl @[simp] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : 0 < b) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b | (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : ℤ) : a % (abs b) = a % b := abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _) @[simp] theorem zero_mod (b : ℤ) : 0 % b = 0 := congr_arg of_nat $ nat.zero_mod _ @[simp] theorem mod_zero : ∀ (a : ℤ), a % 0 = a | (m : ℕ) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ @[simp] theorem mod_one : ∀ (a : ℤ), a % 1 = 0 | (m : ℕ) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → 0 ≤ a % b | (m : ℕ) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : 0 < b) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < abs b := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos_of_ne_zero H) theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm end theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a | (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := rfl | (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : ℤ} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] @[simp] theorem mod_self {a : ℤ} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} @[simp] theorem mod_mod_of_dvd (n : int) {m k : int} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end /- properties of / and % -/ @[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b / (a * c) = b / c := suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, λ m k b, match b, k with | (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≤ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (c : ℤ) (H : 0 < b) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : 0 < b) : a < (a / b + 1) * b := by rw [add_mul, one_mul, mul_comm]; apply lt_add_of_sub_left_lt; rw [← mod_def]; apply mod_lt_of_pos _ H theorem abs_div_le_abs : ∀ (a b : ℤ), abs (a / b) ≤ abs a := suffices ∀ (a : ℤ) (n : ℕ), abs (a / n) ≤ abs a, from λ a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : ℕ), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : ℤ} (b : ℤ) (Ha : 0 ≤ a) : a / b ≤ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial, have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : ℕ) := λ _ _, or.inl rfl | (1 : ℕ) := λ _ _, or.inr rfl | (k + 2 : ℕ) := λ h _, absurd h dec_trivial | -[1+ a] := λ _ h₁, absurd h₁ dec_trivial end /- dvd -/ @[elim_cast] theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (λm0, by simp [m0] at ae; simp [ae, m0]) (λm0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem dvd_antisymm {a b : ℤ} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ b → b ∣ a → a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd_iff_dvd, ← e]) theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg_iff_dvd, ← e]) instance decidable_dvd : @decidable_rel ℤ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma add_div_of_dvd {a b c : ℤ} : c ∣ a → c ∣ b → (a + b) / c = a / c + b / c := begin intros h1 h2, by_cases h3 : c = 0, { rw [h3, zero_dvd_iff] at *, rw [h1, h2, h3], refl }, { apply eq_of_mul_eq_mul_right h3, rw add_mul, repeat {rw [int.div_mul_cancel]}; try {apply dvd_add}; assumption } end theorem div_sign : ∀ a b, a / sign b = a * sign b | a (n+1:ℕ) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:ℕ) (n+1:ℕ) := rfl | (m+1:ℕ) -[1+ n] := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt eq_zero_of_abs_eq_zero az) (sign_mul_abs _).symm).symm theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i | (n+1:ℕ) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ theorem le_of_dvd {a b : ℤ} (bpos : 0 < b) (H : a ∣ b) : a ≤ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : ℤ} (H : 0 ≤ a) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : 0 ≤ a) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : 0 ≤ b) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z | (int.of_nat _) haz := int.coe_nat_dvd.2 haz | -[1+k] haz := begin change ↑a ∣ -(k+1 : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, exact haz end lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs | (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz) | -[1+k] haz := have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz, int.coe_nat_dvd.1 haz' lemma pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) : ↑(p ^ m) ∣ k := begin induction k, { apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1 hdiv }, { change -[1+k] with -(↑(k+1) : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1, apply dvd_of_dvd_neg, exact hdiv } end lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk /- / and ordering -/ protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : ℤ} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : 0 < c) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b := le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : ℤ} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : ℤ} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : 0 < c) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : 0 < c) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : 0 < c) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) : a ≤ c * b := by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H1 : b ∣ a) (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0) (hbc : b ∣ c) (h : b * a = c * d) : a = c / b * d := begin cases hbc with k hk, subst hk, rw int.mul_div_cancel_left, rw mul_assoc at h, apply _root_.eq_of_mul_eq_mul_left _ h, repeat {assumption} end theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ} (h : n.succ ≤ m) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /- to_nat -/ theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0 | (n : ℕ) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] lemma to_nat_sub_of_le (a b : ℤ) (h : b ≤ a) : (to_nat (a + -b) : ℤ) = a + - b := int.to_nat_of_nonneg (sub_nonneg_of_le h) @[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl theorem le_to_nat (a : ℤ) : a ≤ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le {a : ℤ} {n : ℕ} : to_nat a ≤ n ↔ a ≤ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) @[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a := le_iff_le_iff_lt_iff_lt.1 to_nat_le theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b := ⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end, λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩ theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b := (to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h def to_nat' : ℤ → option ℕ | (n : ℕ) := some n | -[1+ n] := none theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n | (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm | -[1+ m] n := by split; intro h; cases h /- units -/ @[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 := units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹, by rw [← nat_abs_mul, units.mul_inv]; refl, by rw [← nat_abs_mul, units.inv_mul]; refl⟩ theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 := by simpa [units.ext_iff, units_nat_abs] using nat_abs_eq u lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) /- bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl @[simp] lemma bodd_two : bodd 2 = ff := rfl @[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd); intros; simp [bodd, -of_nat_eq_coe] @[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n := by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe] @[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, bodd, -of_nat_eq_coe] @[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; unfold has_mul.mul; simp [int.mul, bodd, -of_nat_eq_coe] theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : ℕ) := by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : ∀ n, div2 n = n / 2 | (n : ℕ) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add], cases b, all_goals {exact dec_trivial} end @[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (λ a b, a && bnot b) n m = nat.bitwise (λ a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]}, apply congr_arg (λ f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : ℕ) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : ℕ) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k | (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (λ i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (λ i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k | (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n) | (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n) | (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (nat.pos_pow_of_pos _ dec_trivial) end lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0 | (n : ℕ) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ /- Least upper bound property for integers -/ theorem exists_least_of_bdd {P : ℤ → Prop} [HP : decidable_pred P] (Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → b ≤ z) (Hinh : ∃ z : ℤ, P z) : ∃ lb : ℤ, P lb ∧ (∀ z : ℤ, P z → lb ≤ z) := let ⟨b, Hb⟩ := Hbdd in have EX : ∃ n : ℕ, P (b + n), from let ⟨elt, Helt⟩ := Hinh in match elt, le.dest (Hb _ Helt), Helt with | ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩ end, ⟨b + (nat.find EX : ℤ), nat.find_spec EX, λ z h, match z, le.dest (Hb _ h), h with | ._, ⟨n, rfl⟩, h := add_le_add_left (int.coe_nat_le.2 $ nat.find_min' _ h) _ end⟩ theorem exists_greatest_of_bdd {P : ℤ → Prop} [HP : decidable_pred P] (Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → z ≤ b) (Hinh : ∃ z : ℤ, P z) : ∃ ub : ℤ, P ub ∧ (∀ z : ℤ, P z → z ≤ ub) := have Hbdd' : ∃ (b : ℤ), ∀ (z : ℤ), P (-z) → b ≤ z, from let ⟨b, Hb⟩ := Hbdd in ⟨-b, λ z h, neg_le.1 (Hb _ h)⟩, have Hinh' : ∃ z : ℤ, P (-z), from let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩, let ⟨lb, Plb, al⟩ := exists_least_of_bdd Hbdd' Hinh' in ⟨-lb, Plb, λ z h, le_neg.1 $ al _ $ by rwa neg_neg⟩ /- cast (injection into groups with one) -/ @[simp] theorem nat_cast_eq_coe_nat : ∀ n, @coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n = @coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ int.has_coe)) n | 0 := rfl | (n+1) := congr_arg (+(1:ℤ)) (nat_cast_eq_coe_nat n) section cast variables {α : Type*} section variables [has_neg α] [has_zero α] [has_one α] [has_add α] /-- Canonical homomorphism from the integers to any ring(-like) structure `α` -/ protected def cast : ℤ → α | (n : ℕ) := n | -[1+ n] := -(n+1) @[priority 10] instance cast_coe : has_coe ℤ α := ⟨int.cast⟩ @[simp, squash_cast] theorem cast_zero : ((0 : ℤ) : α) = 0 := rfl @[simp] theorem cast_of_nat (n : ℕ) : (of_nat n : α) = n := rfl @[simp, squash_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n := rfl @[simp] theorem cast_coe_nat' (n : ℕ) : (@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n : α) = n := by simp @[simp, move_cast] theorem cast_neg_succ_of_nat (n : ℕ) : (-[1+ n] : α) = -(n + 1) := rfl end @[simp, squash_cast] theorem cast_one [add_monoid α] [has_one α] [has_neg α] : ((1 : ℤ) : α) = 1 := nat.cast_one @[simp, move_cast] theorem cast_sub_nat_nat [add_group α] [has_one α] (m n) : ((int.sub_nat_nat m n : ℤ) : α) = m - n := begin unfold sub_nat_nat, cases e : n - m, { simp [sub_nat_nat, e, nat.le_of_sub_eq_zero e] }, { rw [sub_nat_nat, cast_neg_succ_of_nat, ← nat.cast_succ, ← e, nat.cast_sub $ _root_.le_of_lt $ nat.lt_of_sub_eq_succ e, neg_sub] }, end @[simp, move_cast] theorem cast_neg_of_nat [add_group α] [has_one α] : ∀ n, ((neg_of_nat n : ℤ) : α) = -n | 0 := neg_zero.symm | (n+1) := rfl @[simp, move_cast] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : ℤ) : α) = m + n | (m : ℕ) (n : ℕ) := nat.cast_add _ _ | (m : ℕ) -[1+ n] := cast_sub_nat_nat _ _ | -[1+ m] (n : ℕ) := (cast_sub_nat_nat _ _).trans $ sub_eq_of_eq_add $ show (n:α) = -(m+1) + n + (m+1), by rw [add_assoc, ← cast_succ, ← nat.cast_add, add_comm, nat.cast_add, cast_succ, neg_add_cancel_left] | -[1+ m] -[1+ n] := show -((m + n + 1 + 1 : ℕ) : α) = -(m + 1) + -(n + 1), by rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add]; apply congr_arg (λ x:ℕ, -(x:α)); simp @[simp, move_cast] theorem cast_neg [add_group α] [has_one α] : ∀ n, ((-n : ℤ) : α) = -n | (n : ℕ) := cast_neg_of_nat _ | -[1+ n] := (neg_neg _).symm @[move_cast] theorem cast_sub [add_group α] [has_one α] (m n) : ((m - n : ℤ) : α) = m - n := by simp @[simp] theorem cast_eq_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) = 0 ↔ n = 0 := ⟨λ h, begin cases n, { exact congr_arg coe (nat.cast_eq_zero.1 h) }, { rw [cast_neg_succ_of_nat, neg_eq_zero, ← cast_succ, nat.cast_eq_zero] at h, contradiction } end, λ h, by rw [h, cast_zero]⟩ @[simp, elim_cast] theorem cast_inj [add_group α] [has_one α] [char_zero α] {m n : ℤ} : (m : α) = n ↔ m = n := by rw [← sub_eq_zero, ← cast_sub, cast_eq_zero, sub_eq_zero] theorem cast_injective [add_group α] [has_one α] [char_zero α] : function.injective (coe : ℤ → α) | m n := cast_inj.1 @[simp] theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 := not_congr cast_eq_zero @[simp, move_cast] theorem cast_mul [ring α] : ∀ m n, ((m * n : ℤ) : α) = m * n | (m : ℕ) (n : ℕ) := nat.cast_mul _ _ | (m : ℕ) -[1+ n] := (cast_neg_of_nat _).trans $ show (-(m * (n + 1) : ℕ) : α) = m * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_mul_neg] | -[1+ m] (n : ℕ) := (cast_neg_of_nat _).trans $ show (-((m + 1) * n : ℕ) : α) = -(m + 1) * n, by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_neg_mul] | -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : ℕ) : α) = -(m + 1) * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg] instance cast.is_ring_hom [ring α] : is_ring_hom (int.cast : ℤ → α) := ⟨cast_one, cast_mul, cast_add⟩ instance coe.is_ring_hom [ring α] : is_ring_hom (coe : ℤ → α) := cast.is_ring_hom theorem mul_cast_comm [ring α] (a : α) (n : ℤ) : a * n = n * a := by cases n; simp [nat.mul_cast_comm, left_distrib, right_distrib, *] @[simp, squash_cast, move_cast] theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n := by {unfold bit0, simp} @[simp, squash_cast, move_cast] theorem coe_nat_bit1 (n : ℕ) : (↑(bit1 n) : ℤ) = bit1 ↑n := by {unfold bit1, unfold bit0, simp} @[simp, squash_cast, move_cast] theorem cast_bit0 [ring α] (n : ℤ) : ((bit0 n : ℤ) : α) = bit0 n := cast_add _ _ @[simp, squash_cast, move_cast] theorem cast_bit1 [ring α] (n : ℤ) : ((bit1 n : ℤ) : α) = bit1 n := by rw [bit1, cast_add, cast_one, cast_bit0]; refl lemma cast_two [ring α] : ((2 : ℤ) : α) = 2 := by simp theorem cast_nonneg [linear_ordered_ring α] : ∀ {n : ℤ}, (0 : α) ≤ n ↔ 0 ≤ n | (n : ℕ) := by simp | -[1+ n] := by simpa [not_le_of_gt (neg_succ_lt_zero n)] using show -(n:α) < 1, from lt_of_le_of_lt (by simp) zero_lt_one @[simp, elim_cast] theorem cast_le [linear_ordered_ring α] {m n : ℤ} : (m : α) ≤ n ↔ m ≤ n := by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg] @[simp, elim_cast] theorem cast_lt [linear_ordered_ring α] {m n : ℤ} : (m : α) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le α _ n m) @[simp] theorem cast_nonpos [linear_ordered_ring α] {n : ℤ} : (n : α) ≤ 0 ↔ n ≤ 0 := by rw [← cast_zero, cast_le] @[simp] theorem cast_pos [linear_ordered_ring α] {n : ℤ} : (0 : α) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] @[simp] theorem cast_lt_zero [linear_ordered_ring α] {n : ℤ} : (n : α) < 0 ↔ n < 0 := by rw [← cast_zero, cast_lt] theorem eq_cast [add_group α] [has_one α] (f : ℤ → α) (H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) (n : ℤ) : f n = n := begin have H : ∀ (n : ℕ), f n = n := nat.eq_cast' (λ n, f n) H1 (λ x y, Hadd x y), cases n, {apply H}, apply eq_neg_of_add_eq_zero, rw [← nat.cast_zero, ← H 0, int.coe_nat_zero, ← show -[1+ n] + (↑n + 1) = 0, from neg_add_self (↑n+1), Hadd, show f (n+1) = n+1, from H (n+1)] end lemma eq_cast' [ring α] (f : ℤ → α) [is_ring_hom f] : f = int.cast := funext $ int.eq_cast f (is_ring_hom.map_one f) (λ _ _, is_ring_hom.map_add f) @[simp, squash_cast] theorem cast_id (n : ℤ) : ↑n = n := (eq_cast id rfl (λ _ _, rfl) n).symm @[simp, move_cast] theorem cast_min [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [h, min] @[simp, move_cast] theorem cast_max [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [h, max] @[simp, move_cast] theorem cast_abs [decidable_linear_ordered_comm_ring α] {q : ℤ} : ((abs q : ℤ) : α) = abs q := by simp [abs] end cast section decidable def range (m n : ℤ) : list ℤ := (list.range (to_nat (n-m))).map $ λ r, m+r theorem mem_range_iff {m n r : ℤ} : r ∈ range m n ↔ m ≤ r ∧ r < n := ⟨λ H, let ⟨s, h1, h2⟩ := list.mem_map.1 H in h2 ▸ ⟨le_add_of_nonneg_right trivial, add_lt_of_lt_sub_left $ match n-m, h1 with | (k:ℕ), h1 := by rwa [list.mem_range, to_nat_coe_nat, ← coe_nat_lt] at h1 end⟩, λ ⟨h1, h2⟩, list.mem_map.2 ⟨to_nat (r-m), list.mem_range.2 $ by rw [← coe_nat_lt, to_nat_of_nonneg (sub_nonneg_of_le h1), to_nat_of_nonneg (sub_nonneg_of_le (le_of_lt (lt_of_le_of_lt h1 h2)))]; exact sub_lt_sub_right h2 _, show m + _ = _, by rw [to_nat_of_nonneg (sub_nonneg_of_le h1), add_sub_cancel'_right]⟩⟩ instance decidable_le_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r < n → P r) := decidable_of_iff (∀ r ∈ range m n, P r) $ by simp only [mem_range_iff, and_imp] instance decidable_le_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r ≤ n → P r) := decidable_of_iff (∀ r ∈ range m (n+1), P r) $ by simp only [mem_range_iff, and_imp, lt_add_one_iff] instance decidable_lt_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r < n → P r) := int.decidable_le_lt P _ _ instance decidable_lt_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r ≤ n → P r) := int.decidable_le_le P _ _ end decidable end int section ring_hom variables {α : Type*} {β : Type*} [ring α] [ring β] lemma is_ring_hom.map_int_cast (f : α → β) [is_ring_hom f] (n : ℤ) : f n = n := int.eq_cast (λ n : ℤ, f n) (by simp [is_ring_hom.map_one f]) (by simp [is_ring_hom.map_add f]) _ lemma ring_hom.map_int_cast (f : α →+* β) (n : ℤ) : f n = n := is_ring_hom.map_int_cast _ _ end ring_hom
1a90cac0f96fea99c2c2d337410ea49c2c147857
4bddde0d06fbd53be6f23d7f5899998e8f63410b
/src/tactic/iconfig/command.lean
313a7def5e8eee9b6fe0a8ae787a76c6f085eb4f
[]
no_license
khoek/libiconfig
4816290a5862af14b07683b3d2663e8e62832ef4
6f55c50bc5d852d26ee5ee4c5b52b2cda2a852e5
refs/heads/master
1,586,109,683,212
1,559,567,916,000
1,559,567,916,000
157,085,466
0
1
null
1,559,567,917,000
1,541,945,134,000
Lean
UTF-8
Lean
false
false
1,358
lean
import .tactic open iconfig open tactic lean lean.parser open interactive interactive.types @[user_command] meta def iconfig_mk_cmd (_ : decl_meta_info) (_ : parse (tk "iconfig_mk")) : lean.parser unit := do n ← ident, of_tactic' $ iconfig.env_mk n private meta def eat_members (cfgn : name) : lean.parser unit := (do n ← lean.parser.ident, lean.parser.tk ":", val ← lean.parser.ident, e ← mk_const $ (`iparam).append val, e ← eval_expr (name → name → lean.parser unit) e, e cfgn n, optional $ lean.parser.tk ",", eat_members) <|> return () @[user_command] meta def iconfig_add_cmd (_ : decl_meta_info) (_ : parse (tk "iconfig_add")) : lean.parser unit := do cfgn ← lean.parser.ident, assert_config_exists cfgn, lean.parser.tk "[", eat_members cfgn, lean.parser.tk "]" @[user_command] meta def iconfig_add_struct_cmd (_ : decl_meta_info) (_ : parse (tk "iconfig_add_struct")) : lean.parser unit := do cfgn ← lean.parser.ident, assert_config_exists cfgn, struct ← lean.parser.ident, struct ← resolve_constant struct, iconfig.env_add_struct cfgn struct reserve prefix iconfig:max @[user_notation] meta def iconfig_not (_ : parse (tk "iconfig")) : lean.parser pexpr := do n ← lean.parser.ident, let e := ((`iconfig).append n).append `interactive.itactic, of_tactic' $ resolve_name e
82a98c1dcaaba044c3a8433f8ef45ef41ddde279
649957717d58c43b5d8d200da34bf374293fe739
/src/data/finsupp.lean
78aa4f34eea119ea12924be5abb6fa3168f02e47
[ "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
58,345
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 Type of functions with finite support. Functions with finite support provide the basis for the following concrete instances: * ℕ →₀ α: Polynomials (where α is a ring) * (σ →₀ ℕ) →₀ α: Multivariate Polynomials (again α is a ring, and σ are variable names) * α →₀ ℕ: Multisets * α →₀ ℤ: Abelian groups freely generated by α * β →₀ α: Linear combinations over β where α is the scalar ring Most of the theory assumes that the range is a commutative monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. A general advice is to not use α →₀ β directly, as the type class setup might not be fitting. The best is to define a copy and select the instances best suited. -/ import data.finset data.set.finite algebra.big_operators algebra.module open finset variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Type*} {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} /-- `finsupp α β`, denoted `α →₀ β`, is the type of functions `f : α → β` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (β : Type*) [has_zero β] := (support : finset α) (to_fun : α → β) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp section basic variable [has_zero β] instance : has_coe_to_fun (α →₀ β) := ⟨λ_, α → β, finsupp.to_fun⟩ instance : has_zero (α →₀ β) := ⟨⟨∅, (λ_, 0), λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma zero_apply {a : α} : (0 : α →₀ β) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ β).support = ∅ := rfl instance : inhabited (α →₀ β) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ β} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun lemma not_mem_support_iff {f : α →₀ β} {a} : a ∉ f.support ↔ f a = 0 := by haveI := classical.dec; exact not_iff_comm.1 mem_support_iff.symm @[extensionality] lemma ext : ∀{f g : α →₀ β}, (∀a, f a = g a) → f = g | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin have : f = g, { funext a, exact h a }, subst this, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end lemma ext_iff {f g : α →₀ β} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ @[simp] lemma support_eq_empty [decidable_eq β] {f : α →₀ β} : f.support = ∅ ↔ f = 0 := ⟨assume h, ext $ assume a, by_contradiction $ λ H, (finset.ext.1 h a).1 $ mem_support_iff.2 H, by rintro rfl; refl⟩ instance [decidable_eq α] [decidable_eq β] : decidable_eq (α →₀ β) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ⟨assume ⟨h₁, h₂⟩, ext $ assume a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, by rwa [mem_support_iff, not_not] at h, have hg : g a = 0, by rwa [h₁, mem_support_iff, not_not] at h, by rw [hf, hg], by rintro rfl; exact ⟨rfl, λ _ _, rfl⟩⟩ lemma finite_supp (f : α →₀ β) : set.finite {a | f a ≠ 0} := ⟨set.fintype_of_finset f.support (λ _, mem_support_iff)⟩ lemma support_subset_iff {s : set α} {f : α →₀ β} [decidable_eq α] : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, @not_imp_comm _ _ (classical.dec _) (classical.dec _)) def equiv_fun_on_fintype [fintype α] [decidable_eq β]: (α →₀ β) ≃ (α → β) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ end basic section single variables [decidable_eq α] [decidable_eq β] [has_zero β] {a a' : α} {b : β} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : β) : α →₀ β := ⟨if b = 0 then ∅ else finset.singleton a, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply : (single a b : α →₀ β) a' = if a = a' then b else 0 := rfl @[simp] lemma single_eq_same : (single a b : α →₀ β) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ β) a' = 0 := if_neg h @[simp] lemma single_zero : (single a 0 : α →₀ β) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma injective_single (a : α) : function.injective (single a : β → α →₀ β) := assume b₁ b₂ eq, have (single a b₁ : α →₀ β) a = (single a b₂ : α →₀ β) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : β) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (injective_single a₂).eq_iff] at eq }, { rw [finsupp.ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end lemma single_swap {α β : Type*} [decidable_eq α] [decidable_eq β] [has_zero β] (a₁ a₂ : α) (b : β) : (single a₁ b : α → β) a₂ = (single a₂ b : α → β) a₁ := by simp [single_apply]; ac_refl lemma unique_single [unique α] (x : α →₀ β) : x = single (default α) (x (default α)) := by ext i; simp [unique.eq_default i] end single section on_finset variables [decidable_eq β] [has_zero β] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the set `s`. The function needs to be 0 outside of `s`. Use this when the set needs filtered anyway, otherwise often better set representation is available. -/ def on_finset (s : finset α) (f : α → β) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ β := ⟨s.filter (λa, f a ≠ 0), f, assume a, classical.by_cases (assume h : f a = 0, by rw mem_filter; exact ⟨and.right, λ H, (H h).elim⟩) (assume h : f a ≠ 0, by rw mem_filter; simp only [iff_true_intro h, hf a h, true_and])⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → β} {hf a} : (on_finset s f hf : α →₀ β) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → β} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ end on_finset section map_range variables [has_zero β₁] [has_zero β₂] [decidable_eq β₂] /-- The composition of `f : β₁ → β₂` and `g : α →₀ β₁` is `map_range f hf g : α →₀ β₂`, well defined when `f 0 = 0`. -/ def map_range (f : β₁ → β₂) (hf : f 0 = 0) (g : α →₀ β₁) : α →₀ β₂ := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : β₁ → β₂} {hf : f 0 = 0} : map_range f hf (0 : α →₀ β₁) = 0 := finsupp.ext $ λ a, by simp [hf] lemma support_map_range {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset variables [decidable_eq α] [decidable_eq β₁] @[simp] lemma map_range_single {f : β₁ → β₂} {hf : f 0 = 0} {a : α} {b : β₁} : map_range f hf (single a b) = single a (f b) := finsupp.ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range section emb_domain variables [has_zero β] [decidable_eq α₂] /-- Given `f : α₁ ↪ α₂` and `v : α₁ →₀ β`, `emb_domain f v : α₂ →₀ β` is the finitely supported function whose value at `f a : α₂` is `v a`. For a `b : α₂` outside the range of `f` it is zero. -/ def emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : α₂ →₀ β := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.inj hb) }, { assume a₂, split_ifs, { simp [h], rw [← finsupp.not_mem_support_iff, classical.not_not], apply finset.choose_mem }, { simp [h] } } end lemma support_emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : (emb_domain f v).support = v.support.map f := rfl lemma emb_domain_zero (f : α₁ ↪ α₂) : (emb_domain f 0 : α₂ →₀ β) = 0 := rfl lemma emb_domain_apply (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₁) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α₁ → β) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (finsupp.not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_inj {f : α₁ ↪ α₂} {l₁ l₂ : α₁ →₀ β} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := ⟨λ h, finsupp.ext $ λ a, by simpa [emb_domain_apply] using finsupp.ext_iff.1 h (f a), λ h, by rw h⟩ lemma emb_domain_map_range {β₁ β₂ : Type*} [has_zero β₁] [has_zero β₂] [decidable_eq β₁] [decidable_eq β₂] (f : α₁ ↪ α₂) (g : β₁ → β₂) (p : α₁ →₀ β₁) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, classical, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single [decidable_eq α₁] [decidable_eq α₂] [decidable_eq β] (l : α₁ →₀ β) (f : α₁ ↪ α₂) (a : α₂) (b : β) (hb : b ≠ 0) (h : l.emb_domain f = finsupp.single a b) : ∃ x, l = finsupp.single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = finset.singleton a, by rw [←finsupp.support_emb_domain, h, finsupp.support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp [h_map_support], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← finsupp.emb_domain_apply f l, h], by_cases h_cases : c = d, { simp [h_cases.symm, hc₂] }, { rw [finsupp.single_apply, finsupp.single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.inj (hc₂.trans hfd)) } }, { exact hc₂ } end end emb_domain section zip_with variables [has_zero β] [has_zero β₁] [has_zero β₂] [decidable_eq α] [decidable_eq β] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and well defined when `f 0 0 = 0`. -/ def zip_with (f : β₁ → β₂ → β) (hf : f 0 0 = 0) (g₁ : α →₀ β₁) (g₂ : α →₀ β₂) : (α →₀ β) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin haveI := classical.dec_eq β₁, simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := support_on_finset_subset end zip_with section erase variables [decidable_eq α] [decidable_eq β] def erase [has_zero β] (a : α) (f : α →₀ β) : α →₀ β := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase [has_zero β] {a : α} {f : α →₀ β} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same [has_zero β] {a : α} {f : α →₀ β} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne [has_zero β] {a a' : α} {f : α →₀ β} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h end erase -- [to_additive finsupp.sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := f.support.sum (λa, g a (f a)) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive finsupp.sum] def prod [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := f.support.prod (λa, g a (f a)) attribute [to_additive finsupp.sum.equations._eqn_1] finsupp.prod.equations._eqn_1 @[to_additive finsupp.sum_map_range_index] lemma prod_map_range_index [has_zero β₁] [has_zero β₂] [comm_monoid γ] [decidable_eq β₂] {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {h : α → β₂ → γ} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[to_additive finsupp.sum_zero_index] lemma prod_zero_index [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} : (0 : α →₀ β).prod h = 1 := rfl section decidable variables [decidable_eq α] [decidable_eq β] section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma nat_sub_apply {g₁ g₂ : α →₀ ℕ} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl end nat_sub section add_monoid variables [add_monoid β] @[to_additive finsupp.sum_single_index] lemma prod_single_index [comm_monoid γ] {a : α} {b : β} {h : α → β → γ} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := begin by_cases h : b = 0, { simp only [h, h_zero, single_zero]; refl }, { simp only [finsupp.prod, support_single_ne_zero h, insert_empty_eq_singleton, prod_singleton, single_eq_same] } end instance : has_add (α →₀ β) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ g₂ : α →₀ β} {a : α} : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add {g₁ g₂ : α →₀ β} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq {g₁ g₂ : α →₀ β} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : β} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (α →₀ β) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } instance (a : α) : is_add_monoid_hom (λ g : α →₀ β, g a) := { map_add := λ _ _, add_apply, map_zero := zero_apply } lemma single_add_erase {a : α} {f : α →₀ β} : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single {a : α} {f : α →₀ β} : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[elab_as_eliminator] protected theorem induction {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma map_range_add [decidable_eq β₁] [decidable_eq β₂] [add_monoid β₁] [add_monoid β₂] {f : β₁ → β₂} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ β₁) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := finsupp.ext $ λ a, by simp [hf'] end add_monoid instance [add_comm_monoid β] : add_comm_monoid (α →₀ β) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group β] : add_group (α →₀ β) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } lemma single_multiset_sum [add_comm_monoid β] [decidable_eq α] [decidable_eq β] (s : multiset β) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid β] [decidable_eq α] [decidable_eq β] (s : finset γ) (f : γ → β) (a : α) : single a (s.sum f) = s.sum (λb, single a (f b)) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero γ] [add_comm_monoid β] [decidable_eq α] [decidable_eq β] (s : δ →₀ γ) (f : δ → γ → β) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive finsupp.sum_neg_index] lemma prod_neg_index [add_group β] [comm_monoid γ] {g : α →₀ β} {h : α → β → γ} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group β] {f : α →₀ β} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) instance [add_comm_group β] : add_comm_group (α →₀ β) := { add_comm := add_comm, ..finsupp.add_group } @[simp] lemma sum_apply [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {a₂ : α} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (finset.sum_hom (λf : α →₀ β, f a₂)).symm lemma support_sum [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → (α →₀ β)} : (f.sum g).support ⊆ f.support.bind (λa, (g a (f a)).support) := have ∀a₁ : α, f.sum (λ (a : α₁) (b : β₁), (g a b) a₁) ≠ 0 → (∃ (a : α₁), f a ≠ 0 ∧ ¬ (g a (f a)) a₁ = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply, exists_prop] using this @[simp] lemma sum_zero [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} : f.sum (λa b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b + h₂ a b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h : α → β → γ} : f.sum (λa b, - h a b) = - f.sum h := finset.sum_hom (@has_neg.neg γ _) @[simp] lemma sum_sub [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := by rw [sub_eq_add_neg, ←sum_neg, ←sum_add]; refl @[simp] lemma sum_single [add_comm_monoid β] (f : α →₀ β) : f.sum single = f := have ∀a:α, f.sum (λa' b, ite (a' = a) b 0) = ({a} : finset α).sum (λa', ite (a' = a) (f a') 0), begin intro a, by_cases h : a ∈ f.support, { have : (finset.singleton a : finset α) ⊆ f.support, { simpa only [finset.subset_iff, mem_singleton, forall_eq] }, refine (finset.sum_subset this (λ _ _ H, _)).symm, exact if_neg (mt mem_singleton.2 H) }, { transitivity (f.support.sum (λa, (0 : β))), { refine (finset.sum_congr rfl $ λ a' ha', if_neg _), rintro rfl, exact h ha' }, { rw [sum_const_zero, insert_empty_eq_singleton, sum_singleton, if_pos rfl, not_mem_support_iff.1 h] } } end, ext $ assume a, by simp only [sum_apply, single_apply, this, insert_empty_eq_singleton, sum_singleton, if_pos] @[to_additive finsupp.sum_add_index] lemma prod_add_index [add_comm_monoid β] [comm_monoid γ] {f g : α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : (f.support ∪ g.support).prod (λa, h a (f a)) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, have g_eq : (f.support ∪ g.support).prod (λa, h a (g a)) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, calc (f + g).support.prod (λa, h a ((f + g) a)) = (f.support ∪ g.support).prod (λa, h a ((f + g) a)) : finset.prod_subset support_add $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero] ... = (f.support ∪ g.support).prod (λa, h a (f a)) * (f.support ∪ g.support).prod (λa, h a (g a)) : by simp only [add_apply, h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := have h_zero : ∀a, h a 0 = 0, from assume a, have h a (0 - 0) = h a 0 - h a 0, from h_sub a 0 0, by simpa only [sub_self] using this, have h_neg : ∀a b, h a (- b) = - h a b, from assume a b, have h a (0 - b) = h a 0 - h a b, from h_sub a 0 b, by simpa only [h_zero, zero_sub] using this, have h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ + h a b₂, from assume a b₁ b₂, have h a (b₁ - (- b₂)) = h a b₁ - h a (- b₂), from h_sub a b₁ (-b₂), by simpa only [h_neg, sub_neg_eq_add] using this, calc (f - g).sum h = (f + - g).sum h : rfl ... = f.sum h + - g.sum h : by simp only [sum_add_index h_zero h_add, sum_neg_index h_zero, h_neg, sum_neg] ... = f.sum h - g.sum h : rfl @[to_additive finsupp.sum_finset_sum_index] lemma prod_finset_sum_index [add_comm_monoid β] [comm_monoid γ] [decidable_eq ι] {s : finset ι} {g : ι → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : s.prod (λi, (g i).prod h) = (s.sum g).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive finsupp.sum_sum_index] lemma prod_sum_index [decidable_eq α₁] [add_comm_monoid β₁] [add_comm_monoid β] [comm_monoid γ] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [decidable_eq α] [decidable_eq β] [add_comm_monoid β] [add_comm_monoid γ] (f : multiset (α →₀ β)) (h : α → β → γ) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : β), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ β, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma multiset_map_sum [has_zero β] {f : α →₀ β} {m : γ → δ} {h : α → β → multiset γ} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (finset.sum_hom _).symm lemma multiset_sum_sum [has_zero β] [add_comm_monoid γ] {f : α →₀ β} {h : α → β → multiset γ} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (finset.sum_hom multiset.sum).symm section map_range variables [decidable_eq β₁] [decidable_eq β₂] [add_comm_monoid β₁] [add_comm_monoid β₂] (f : β₁ → β₂) [hf : is_add_monoid_hom f] instance is_add_monoid_hom_map_range : is_add_monoid_hom (map_range f hf.map_zero : (α →₀ β₁) → (α →₀ β₂)) := { map_zero := map_range_zero, map_add := λ a b, map_range_add hf.map_add _ _ } lemma map_range_multiset_sum (m : multiset (α →₀ β₁)) : map_range f hf.map_zero m.sum = (m.map $ λx, map_range f hf.map_zero x).sum := (m.sum_hom (map_range f hf.map_zero)).symm lemma map_range_finset_sum {ι : Type*} [decidable_eq ι] (s : finset ι) (g : ι → (α →₀ β₁)) : map_range f hf.map_zero (s.sum g) = s.sum (λx, map_range f hf.map_zero (g x)) := by rw [finset.sum.equations._eqn_1, map_range_multiset_sum, multiset.map_map]; refl end map_range section map_domain variables [decidable_eq α₁] [decidable_eq α₂] [add_comm_monoid β] {v v₁ v₂ : α →₀ β} /-- Given `f : α₁ → α₂` and `v : α₁ →₀ β`, `map_domain f v : α₂ →₀ β` is the finitely supported function whose value at `a : α₂` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α₁ → α₂) (v : α₁ →₀ β) : α₂ →₀ β := v.sum $ λa, single (f a) lemma map_domain_apply {f : α₁ → α₂} (hf : function.injective f) (x : α₁ →₀ β) (a : α₁) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { simp only [(∉), (≠), not_not, mem_support_iff], assume h, rw [h, single_zero], refl } end lemma map_domain_notin_range {f : α₁ → α₂} (x : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → α₁} {g : α₁ → α₂} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end lemma map_domain_single {f : α → α₁} {a : α} {b : β} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → α₂} : map_domain f 0 = (0 : α₂ →₀ β) := sum_zero_index lemma map_domain_congr {f g : α → α₂} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → α₂} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_finset_sum [decidable_eq ι] {f : α → α₂} {s : finset ι} {v : ι → α →₀ β} : map_domain f (s.sum v) = s.sum (λi, map_domain f (v i)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_sum [has_zero β₁] {f : α → α₂} {s : α →₀ β₁} {v : α → β₁ → α →₀ β} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_support {f : α → α₂} {s : α →₀ β} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive finsupp.sum_map_domain_index] lemma prod_map_domain_index [comm_monoid γ] {f : α → α₂} {s : α →₀ β} {h : α₂ → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) lemma emb_domain_eq_map_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : emb_domain f v = map_domain f v := begin ext a, classical, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply (function.embedding.inj' _), emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end lemma injective_map_domain {f : α₁ → α₂} (hf : function.injective f) : function.injective (map_domain f : (α₁ →₀ β) → (α₂ →₀ β)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end end map_domain section comap_domain noncomputable def comap_domain {α₁ α₂ γ : Type*} [decidable_eq α₁] [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' l.support.to_set)) : α₁ →₀ γ := { support := l.support.preimage hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } lemma comap_domain_apply {α₁ α₂ γ : Type*} [decidable_eq α₁] [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' l.support.to_set)) (a : α₁) : comap_domain f l hf a = l (f a) := begin unfold_coes, unfold comap_domain, simp, refl end lemma sum_comap_domain {α₁ α₂ β γ : Type*} [decidable_eq α₁] [has_zero β] [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ β) (g : α₂ → β → γ) (hf : set.bij_on f (f ⁻¹' l.support.to_set) l.support.to_set): (comap_domain f l (set.inj_on_of_bij_on hf)).sum (g ∘ f) = l.sum g := begin unfold sum, haveI := classical.dec_eq α₂, simp only [comap_domain, comap_domain_apply, finset.sum_preimage f _ _ (λ (x : α₂), g x (l x))], end lemma eq_zero_of_comap_domain_eq_zero {α₁ α₂ γ : Type*} [add_comm_monoid γ] [decidable_eq α₁] [decidable_eq α₂] [decidable_eq γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.bij_on f (f ⁻¹' l.support.to_set) l.support.to_set) : comap_domain f l (set.inj_on_of_bij_on hf) = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain {α₁ α₂ γ : Type*} [add_comm_monoid γ] [decidable_eq α₁] [decidable_eq α₂] [decidable_eq γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (set.inj_on_of_injective _ hf)) = l := begin ext a, haveI := classical.dec (a ∈ set.range f), by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl (finset.mem_coe.2 (mem_support_iff.2 (λ h, h_contr h.symm)))) } end end comap_domain /-- The product of `f g : α →₀ β` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the monoid of monomial exponents.) -/ instance [has_add α] [semiring β] : has_mul (α →₀ β) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def [has_add α] [semiring β] {f g : α →₀ β} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl lemma support_mul [has_add α] [semiring β] (a b : α →₀ β) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset /-- The unit of the multiplication is `single 0 1`, i.e. the function that is 1 at 0 and zero elsewhere. -/ instance [has_zero α] [has_zero β] [has_one β] : has_one (α →₀ β) := ⟨single 0 1⟩ lemma one_def [has_zero α] [has_zero β] [has_one β] : 1 = (single 0 1 : α →₀ β) := rfl section filter section has_zero variables [has_zero β] (p : α → Prop) [decidable_pred p] (f : α →₀ β) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) [decidable_pred p] (f : α →₀ β) : α →₀ β := on_finset f.support (λa, if p a then f a else 0) $ λ a H, mem_support_iff.2 $ λ h, by rw [h, if_t_t] at H; exact H rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext.mpr $ assume a, if H : p a then by simp only [mem_support_iff, filter_apply_pos _ _ H, mem_filter, H, and_true] else by simp only [mem_support_iff, filter_apply_neg _ _ H, mem_filter, H, and_false, ne.def, ne_self_iff_false] lemma filter_zero : (0 : α →₀ β).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : β} (h : p a) : (single a b).filter p = single a b := finsupp.ext $ λ x, begin by_cases h' : p x; simp [h'], rw single_eq_of_ne, rintro rfl, exact h' h end @[simp] lemma filter_single_of_neg {a : α} {b : β} (h : ¬ p a) : (single a b).filter p = 0 := finsupp.ext $ λ x, begin by_cases h' : p x; simp [h'], rw single_eq_of_ne, rintro rfl, exact h h' end end has_zero lemma filter_pos_add_filter_neg [add_monoid β] (f : α →₀ β) (p : α → Prop) [decidable_pred p] : f.filter p + f.filter (λa, ¬ p a) = f := finsupp.ext $ assume a, if H : p a then by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_not, add_zero] else by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_false_iff, zero_add] end filter section frange variables [has_zero β] def frange (f : α →₀ β) : finset β := finset.image f f.support theorem mem_frange {f : α →₀ β} {y : β} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ β} : (0:β) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : β} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange section subtype_domain variables {α' : Type*} [has_zero δ] {p : α → Prop} [decidable_pred p] section zero variables [has_zero β] {v v' : α' →₀ β} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) [decidable_pred p] (f : α →₀ β) : (subtype p →₀ β) := ⟨f.support.subtype p, f ∘ subtype.val, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain {f : α →₀ β} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ β} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ β) = 0 := rfl @[to_additive finsupp.sum_subtype_domain_index] lemma prod_subtype_domain_index [comm_monoid γ] {v : α →₀ β} {h : α → β → γ} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a.1 b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section monoid variables [add_monoid β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_add {v v' : α →₀ β} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl instance subtype_domain.is_add_monoid_hom [add_monoid β] : is_add_monoid_hom (subtype_domain p : (α →₀ β) → subtype p →₀ β) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma filter_add {v v' : α →₀ β} : (v + v').filter p = v.filter p + v'.filter p := ext $ λ a, by by_cases p a; simp [h] instance filter.is_add_monoid_hom (p : α → Prop) [decidable_pred p] : is_add_monoid_hom (filter p : (α →₀ β) → (α →₀ β)) := { map_zero := filter_zero p, map_add := λ x y, filter_add } end monoid section comm_monoid variables [add_comm_monoid β] lemma subtype_domain_sum {s : finset γ} {h : γ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc, (h c).subtype_domain p) := eq.symm (finset.sum_hom _) lemma subtype_domain_finsupp_sum {s : γ →₀ δ} {h : γ → δ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset γ) (f : γ → α →₀ β) : (s.sum f).filter p = s.sum (λa, filter p (f a)) := (finset.sum_hom (filter p)).symm end comm_monoid section group variables [add_group β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_neg {v : α →₀ β} : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub {v v' : α →₀ β} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl end group end subtype_domain section multiset def to_multiset (f : α →₀ ℕ) : multiset α := f.sum (λa n, add_monoid.smul n {a}) lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := sum_add_index (assume a, add_monoid.zero_smul _) (assume a b₁ b₂, add_monoid.add_smul _ _ _) lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = add_monoid.smul n {a} := by rw [to_multiset, sum_single_index]; apply add_monoid.zero_smul instance is_add_monoid_hom.to_multiset : is_add_monoid_hom (to_multiset : _ → multiset α) := { map_zero := to_multiset_zero, map_add := to_multiset_add } lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.card_zero, sum_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.card_add, ih, sum_add_index, to_multiset_single, sum_single_index, multiset.card_smul, multiset.singleton_eq_singleton, multiset.card_singleton, mul_one]; intros; refl } end lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, is_add_monoid_hom.map_smul (multiset.map g)], refl } end lemma prod_to_multiset [comm_monoid α] (f : α →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_smul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end lemma to_finset_to_multiset (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_smul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint_mono support_single_subset (subset.refl _) _, rwa [finset.singleton_eq_singleton, finset.singleton_disjoint] } end @[simp] lemma count_to_multiset [decidable_eq α] (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (add_monoid.smul n {x} : multiset α).count a) : (finset.sum_hom _).symm ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_smul] ... = f.sum (λx n, n * (x :: 0 : multiset α).count a) : rfl ... = f a * (a :: 0 : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] def of_multiset [decidable_eq α] (m : multiset α) : α →₀ ℕ := on_finset m.to_finset (λa, m.count a) $ λ a H, multiset.mem_to_finset.2 $ by_contradiction (mt multiset.count_eq_zero.2 H) @[simp] lemma of_multiset_apply [decidable_eq α] (m : multiset α) (a : α) : of_multiset m a = m.count a := rfl def equiv_multiset [decidable_eq α] : (α →₀ ℕ) ≃ (multiset α) := ⟨ to_multiset, of_multiset, assume f, finsupp.ext $ λ a, by rw [of_multiset_apply, count_to_multiset], assume m, multiset.ext.2 $ λ a, by rw [count_to_multiset, of_multiset_apply] ⟩ lemma mem_support_multiset_sum [decidable_eq α] [decidable_eq β] [add_comm_monoid β] {s : multiset (α →₀ β)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ β).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [decidable_eq α] [decidable_eq β] [add_comm_monoid β] {s : finset γ} {h : γ → α →₀ β} (a : α) (ha : a ∈ (s.sum h).support) : ∃c∈s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ lemma mem_support_single [decidable_eq α] [decidable_eq β] [has_zero β] (a a' : α) (b : β) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := ⟨λ H : (a ∈ ite _ _ _), if h : b = 0 then by rw if_pos h at H; exact H.elim else ⟨by rw if_neg h at H; exact mem_singleton.1 H, h⟩, λ ⟨h1, h2⟩, show a ∈ ite _ _ _, by rw [if_neg h2]; exact mem_singleton.2 h1⟩ end multiset section curry_uncurry protected def curry [decidable_eq α] [decidable_eq β] [decidable_eq γ] [add_comm_monoid γ] (f : (α × β) →₀ γ) : α →₀ (β →₀ γ) := f.sum $ λp c, single p.1 (single p.2 c) lemma sum_curry_index [decidable_eq α] [decidable_eq β] [decidable_eq γ] [add_comm_monoid γ] [add_comm_monoid δ] (f : (α × β) →₀ γ) (g : α → β → γ → δ) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end protected def uncurry [decidable_eq α] [decidable_eq β] [decidable_eq γ] [add_comm_monoid γ] (f : α →₀ (β →₀ γ)) : (α × β) →₀ γ := f.sum $ λa g, g.sum $ λb c, single (a, b) c def finsupp_prod_equiv [add_comm_monoid γ] [decidable_eq α] [decidable_eq β] [decidable_eq γ] : ((α × β) →₀ γ) ≃ (α →₀ (β →₀ γ)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry [decidable_eq α₁] [decidable_eq α₂] [add_comm_monoid β] (f : α₁ × α₂ →₀ β) (p : α₁ → Prop) [decidable_pred p] : (f.filter (λa:α₁×α₂, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, @filter_sum _ (α₂ →₀ β) _ _ _ p _ _ f.support _], rw [support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [decidable_eq α₁] [decidable_eq α₂] [add_comm_monoid β] (f : α₁ × α₂ →₀ β) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bind_singleton, refine finset.subset.trans support_sum _, refine finset.bind_mono (assume a _, support_single_subset) end end curry_uncurry section variables [add_monoid α] [semiring β] -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : α →₀ β) : 0 * f = 0 := by simp only [mul_def, sum_zero_index] private lemma mul_zero (f : α →₀ β) : f * 0 = 0 := by simp only [mul_def, sum_zero_index, sum_zero] private lemma left_distrib (a b c : α →₀ β) : a * (b + c) = a * b + a * c := by simp only [mul_def, sum_add_index, mul_add, _root_.mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add] private lemma right_distrib (a b c : α →₀ β) : (a + b) * c = a * c + b * c := by simp only [mul_def, sum_add_index, add_mul, _root_.mul_zero, _root_.zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add] instance : semiring (α →₀ β) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, _root_.zero_mul, _root_.mul_zero, sum_zero, sum_add], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } end instance [add_comm_monoid α] [comm_semiring β] : comm_semiring (α →₀ β) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [add_comm] end, .. finsupp.semiring } instance [add_monoid α] [ring β] : ring (α →₀ β) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. finsupp.semiring } instance [add_comm_monoid α] [comm_ring β] : comm_ring (α →₀ β) := { mul_comm := mul_comm, .. finsupp.ring} lemma single_mul_single [has_add α] [semiring β] {a₁ a₂ : α} {b₁ b₂ : β}: single a₁ b₁ * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) := (sum_single_index (by simp only [_root_.zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [_root_.mul_zero, single_zero])) lemma prod_single [decidable_eq ι] [add_comm_monoid α] [comm_semiring β] {s : finset ι} {a : ι → α} {b : ι → β} : s.prod (λi, single (a i) (b i)) = single (s.sum a) (s.prod b) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] section instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : has_scalar γ (α →₀ β) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ variables (α β) @[simp] lemma smul_apply' {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {a : α} {b : γ} {v : α →₀ β} : (b • v) a = b • (v a) := rfl instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : semimodule γ (α →₀ β) := { smul := (•), smul_add := λ a x y, finsupp.ext $ λ _, smul_add _ _ _, add_smul := λ a x y, finsupp.ext $ λ _, add_smul _ _ _, one_smul := λ x, finsupp.ext $ λ _, one_smul _ _, mul_smul := λ r s x, finsupp.ext $ λ _, mul_smul _ _ _, zero_smul := λ x, finsupp.ext $ λ _, zero_smul _ _, smul_zero := λ x, finsupp.ext $ λ _, smul_zero _ } instance [ring γ] [add_comm_group β] [module γ β] : module γ (α →₀ β) := { ..finsupp.semimodule α β } instance [discrete_field γ] [add_comm_group β] [vector_space γ β] : vector_space γ (α →₀ β) := { ..finsupp.module α β } variables {α β} lemma support_smul {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {g : α →₀ β} : (b • g).support ⊆ g.support := λ a, by simp; exact mt (λ h, h.symm ▸ smul_zero _) section variables {α' : Type*} [has_zero δ] {p : α → Prop} [decidable_pred p] @[simp] lemma filter_smul {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {v : α →₀ β} : (b • v).filter p = b • v.filter p := ext $ λ a, by by_cases p a; simp [h] end lemma map_domain_smul {α'} [decidable_eq α'] {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {f : α → α'} (b : γ) (v : α →₀ β) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, {simp}, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {R:semiring γ} [add_comm_monoid β] [semimodule γ β] (c : γ) (a : α) (b : β) : c • finsupp.single a b = finsupp.single a (c • b) := ext $ λ a', by by_cases a = a'; [{subst h, simp}, simp [h]] end @[simp] lemma smul_apply [ring β] {a : α} {b : β} {v : α →₀ β} : (b • v) a = b • (v a) := rfl lemma sum_smul_index [ring β] [add_comm_monoid γ] {g : α →₀ β} {b : β} {h : α → β → γ} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 end decidable section variables [semiring β] [semiring γ] lemma sum_mul (b : γ) (s : α →₀ β) {f : α → β → γ} : (s.sum f) * b = s.sum (λ a c, (f a (s a)) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma mul_sum (b : γ) (s : α →₀ β) {f : α → β → γ} : b * (s.sum f) = s.sum (λ a c, b * (f a (s a))) := by simp only [finsupp.sum, finset.mul_sum] protected lemma eq_zero_of_zero_eq_one (zero_eq_one : (0 : β) = 1) (l : α →₀ β) : l = 0 := by ext i; simp [eq_zero_of_zero_eq_one β zero_eq_one (l i)] end def restrict_support_equiv [decidable_eq α] [decidable_eq β] [add_comm_monoid β] (s : set α) [decidable_pred (λx, x ∈ s)] : {f : α →₀ β // ↑f.support ⊆ s } ≃ (s →₀ β) := begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end protected def dom_congr [decidable_eq α₁] [decidable_eq α₂] [decidable_eq β] [add_comm_monoid β] (e : α₁ ≃ α₂) : (α₁ →₀ β) ≃ (α₂ →₀ β) := ⟨map_domain e, map_domain e.symm, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply], exact map_domain_id end, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply], exact map_domain_id end⟩ section sigma variables {αs : ι → Type*} [decidable_eq ι] [∀ i, decidable_eq (αs i)] [has_zero β] (l : (Σ i, αs i) →₀ β) noncomputable def split (i : ι) : αs i →₀ β := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end def split_support : finset ι := finset.image (sigma.fst) l.support lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin classical, rw [split_support, mem_image, ne.def, ← support_eq_empty, ← exists_mem_iff_ne_empty, split, comap_domain], simp end noncomputable def split_comp [has_zero γ] (g : Π i, (αs i →₀ β) → γ) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ γ := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw mem_split_support_iff_nonzero, haveI := classical.dec, rwa not_iff_not, exact hg _ _, end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp [finset.ext, split_support, split, comap_domain]; tauto lemma sigma_sum [add_comm_monoid γ] (f : (Σ (i : ι), αs i) → β → γ) : l.sum f = (split_support l).sum (λ (i : ι), (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b)) := by simp [sum, sigma_support, sum_sigma,split_apply] end sigma end finsupp
d50517095eb1c0a9d750404458bceec13b2bb8d4
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/is_connected.lean
a087eef022bf5998878d1029c92613fd60ccef3e
[ "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
14,272
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import data.list.chain import category_theory.punit import category_theory.groupoid /-! # Connected category Define a connected category as a _nonempty_ category for which every functor to a discrete category is isomorphic to the constant functor. NB. Some authors include the empty category as connected, we do not. We instead are interested in categories with exactly one 'connected component'. We give some equivalent definitions: - A nonempty category for which every functor to a discrete category is constant on objects. See `any_functor_const_on_obj` and `connected.of_any_functor_const_on_obj`. - A nonempty category for which every function `F` for which the presence of a morphism `f : j₁ ⟶ j₂` implies `F j₁ = F j₂` must be constant everywhere. See `constant_of_preserves_morphisms` and `connected.of_constant_of_preserves_morphisms`. - A nonempty category for which any subset of its elements containing the default and closed under morphisms is everything. See `induct_on_objects` and `connected.of_induct`. - A nonempty category for which every object is related under the reflexive transitive closure of the relation "there is a morphism in some direction from `j₁` to `j₂`". See `connected_zigzag` and `zigzag_connected`. - A nonempty category for which for any two objects there is a sequence of morphisms (some reversed) from one to the other. See `exists_zigzag'` and `connected_of_zigzag`. We also prove the result that the functor given by `(X × -)` preserves any connected limit. That is, any limit of shape `J` where `J` is a connected category is preserved by the functor `(X × -)`. This appears in `category_theory.limits.connected`. -/ universes v₁ v₂ u₁ u₂ noncomputable theory open category_theory.category open opposite namespace category_theory /-- A possibly empty category for which every functor to a discrete category is constant. -/ class is_preconnected (J : Type u₁) [category.{v₁} J] : Prop := (iso_constant : Π {α : Type u₁} (F : J ⥤ discrete α) (j : J), nonempty (F ≅ (functor.const J).obj (F.obj j))) /-- We define a connected category as a _nonempty_ category for which every functor to a discrete category is constant. NB. Some authors include the empty category as connected, we do not. We instead are interested in categories with exactly one 'connected component'. This allows us to show that the functor X ⨯ - preserves connected limits. See https://stacks.math.columbia.edu/tag/002S -/ class is_connected (J : Type u₁) [category.{v₁} J] extends is_preconnected J : Prop := [is_nonempty : nonempty J] attribute [instance, priority 100] is_connected.is_nonempty variables {J : Type u₁} [category.{v₁} J] variables {K : Type u₂} [category.{v₂} K] /-- If `J` is connected, any functor `F : J ⥤ discrete α` is isomorphic to the constant functor with value `F.obj j` (for any choice of `j`). -/ def iso_constant [is_preconnected J] {α : Type u₁} (F : J ⥤ discrete α) (j : J) : F ≅ (functor.const J).obj (F.obj j) := (is_preconnected.iso_constant F j).some /-- If J is connected, any functor to a discrete category is constant on objects. The converse is given in `is_connected.of_any_functor_const_on_obj`. -/ lemma any_functor_const_on_obj [is_preconnected J] {α : Type u₁} (F : J ⥤ discrete α) (j j' : J) : F.obj j = F.obj j' := ((iso_constant F j').hom.app j).down.1 /-- If any functor to a discrete category is constant on objects, J is connected. The converse of `any_functor_const_on_obj`. -/ lemma is_connected.of_any_functor_const_on_obj [nonempty J] (h : ∀ {α : Type u₁} (F : J ⥤ discrete α), ∀ (j j' : J), F.obj j = F.obj j') : is_connected J := { iso_constant := λ α F j', ⟨nat_iso.of_components (λ j, eq_to_iso (h F j j')) (λ _ _ _, subsingleton.elim _ _)⟩ } /-- If `J` is connected, then given any function `F` such that the presence of a morphism `j₁ ⟶ j₂` implies `F j₁ = F j₂`, we have that `F` is constant. This can be thought of as a local-to-global property. The converse is shown in `is_connected.of_constant_of_preserves_morphisms` -/ lemma constant_of_preserves_morphisms [is_preconnected J] {α : Type u₁} (F : J → α) (h : ∀ (j₁ j₂ : J) (f : j₁ ⟶ j₂), F j₁ = F j₂) (j j' : J) : F j = F j' := any_functor_const_on_obj { obj := F, map := λ _ _ f, eq_to_hom (h _ _ f) } j j' /-- `J` is connected if: given any function `F : J → α` which is constant for any `j₁, j₂` for which there is a morphism `j₁ ⟶ j₂`, then `F` is constant. This can be thought of as a local-to-global property. The converse of `constant_of_preserves_morphisms`. -/ lemma is_connected.of_constant_of_preserves_morphisms [nonempty J] (h : ∀ {α : Type u₁} (F : J → α), (∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), F j₁ = F j₂) → (∀ j j' : J, F j = F j')) : is_connected J := is_connected.of_any_functor_const_on_obj (λ _ F, h F.obj (λ _ _ f, (F.map f).down.1)) /-- An inductive-like property for the objects of a connected category. If the set `p` is nonempty, and `p` is closed under morphisms of `J`, then `p` contains all of `J`. The converse is given in `is_connected.of_induct`. -/ lemma induct_on_objects [is_preconnected J] (p : set J) {j₀ : J} (h0 : j₀ ∈ p) (h1 : ∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), j₁ ∈ p ↔ j₂ ∈ p) (j : J) : j ∈ p := begin injection (constant_of_preserves_morphisms (λ k, ulift.up (k ∈ p)) (λ j₁ j₂ f, _) j j₀) with i, rwa i, dsimp, exact congr_arg ulift.up (propext (h1 f)), end /-- If any maximal connected component containing some element j₀ of J is all of J, then J is connected. The converse of `induct_on_objects`. -/ lemma is_connected.of_induct [nonempty J] {j₀ : J} (h : ∀ (p : set J), j₀ ∈ p → (∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), j₁ ∈ p ↔ j₂ ∈ p) → ∀ (j : J), j ∈ p) : is_connected J := is_connected.of_constant_of_preserves_morphisms (λ α F a, begin have w := h {j | F j = F j₀} rfl (λ _ _ f, by simp [a f]), dsimp at w, intros j j', rw [w j, w j'], end) /-- Another induction principle for `is_preconnected J`: given a type family `Z : J → Sort*` and a rule for transporting in *both* directions along a morphism in `J`, we can transport an `x : Z j₀` to a point in `Z j` for any `j`. -/ lemma is_preconnected_induction [is_preconnected J] (Z : J → Sort*) (h₁ : Π {j₁ j₂ : J} (f : j₁ ⟶ j₂), Z j₁ → Z j₂) (h₂ : Π {j₁ j₂ : J} (f : j₁ ⟶ j₂), Z j₂ → Z j₁) {j₀ : J} (x : Z j₀) (j : J) : nonempty (Z j) := (induct_on_objects {j | nonempty (Z j)} ⟨x⟩ (λ j₁ j₂ f, ⟨by { rintro ⟨y⟩, exact ⟨h₁ f y⟩, }, by { rintro ⟨y⟩, exact ⟨h₂ f y⟩, }⟩) j : _) /-- If `J` and `K` are equivalent, then if `J` is preconnected then `K` is as well. -/ lemma is_preconnected_of_equivalent {K : Type u₁} [category.{v₂} K] [is_preconnected J] (e : J ≌ K) : is_preconnected K := { iso_constant := λ α F k, ⟨ calc F ≅ e.inverse ⋙ e.functor ⋙ F : (e.inv_fun_id_assoc F).symm ... ≅ e.inverse ⋙ (functor.const J).obj ((e.functor ⋙ F).obj (e.inverse.obj k)) : iso_whisker_left e.inverse (iso_constant (e.functor ⋙ F) (e.inverse.obj k)) ... ≅ e.inverse ⋙ (functor.const J).obj (F.obj k) : iso_whisker_left _ ((F ⋙ functor.const J).map_iso (e.counit_iso.app k)) ... ≅ (functor.const K).obj (F.obj k) : nat_iso.of_components (λ X, iso.refl _) (by simp), ⟩ } /-- If `J` and `K` are equivalent, then if `J` is connected then `K` is as well. -/ lemma is_connected_of_equivalent {K : Type u₁} [category.{v₂} K] (e : J ≌ K) [is_connected J] : is_connected K := { is_nonempty := nonempty.map e.functor.obj (by apply_instance), to_is_preconnected := is_preconnected_of_equivalent e } /-- If `J` is preconnected, then `Jᵒᵖ` is preconnected as well. -/ instance is_preconnected_op [is_preconnected J] : is_preconnected Jᵒᵖ := { iso_constant := λ α F X, ⟨ nat_iso.of_components (λ Y, (nonempty.some $ is_preconnected.iso_constant (F.right_op ⋙ (discrete.opposite α).functor) (unop X)).app (unop Y)) (λ Y Z f, subsingleton.elim _ _) ⟩ } /-- If `J` is connected, then `Jᵒᵖ` is connected as well. -/ instance is_connected_op [is_connected J] : is_connected Jᵒᵖ := { is_nonempty := nonempty.intro (op (classical.arbitrary J)) } lemma is_preconnected_of_is_preconnected_op [is_preconnected Jᵒᵖ] : is_preconnected J := is_preconnected_of_equivalent (op_op_equivalence J) lemma is_connected_of_is_connected_op [is_connected Jᵒᵖ] : is_connected J := is_connected_of_equivalent (op_op_equivalence J) /-- j₁ and j₂ are related by `zag` if there is a morphism between them. -/ @[reducible] def zag (j₁ j₂ : J) : Prop := nonempty (j₁ ⟶ j₂) ∨ nonempty (j₂ ⟶ j₁) lemma zag_symmetric : symmetric (@zag J _) := λ j₂ j₁ h, h.swap /-- `j₁` and `j₂` are related by `zigzag` if there is a chain of morphisms from `j₁` to `j₂`, with backward morphisms allowed. -/ @[reducible] def zigzag : J → J → Prop := relation.refl_trans_gen zag lemma zigzag_symmetric : symmetric (@zigzag J _) := relation.refl_trans_gen.symmetric zag_symmetric lemma zigzag_equivalence : _root_.equivalence (@zigzag J _) := mk_equivalence _ relation.reflexive_refl_trans_gen zigzag_symmetric relation.transitive_refl_trans_gen /-- The setoid given by the equivalence relation `zigzag`. A quotient for this setoid is a connected component of the category. -/ def zigzag.setoid (J : Type u₂) [category.{v₁} J] : setoid J := { r := zigzag, iseqv := zigzag_equivalence } /-- If there is a zigzag from `j₁` to `j₂`, then there is a zigzag from `F j₁` to `F j₂` as long as `F` is a functor. -/ lemma zigzag_obj_of_zigzag (F : J ⥤ K) {j₁ j₂ : J} (h : zigzag j₁ j₂) : zigzag (F.obj j₁) (F.obj j₂) := h.lift _ $ λ j k, or.imp (nonempty.map (λ f, F.map f)) (nonempty.map (λ f, F.map f)) -- TODO: figure out the right way to generalise this to `zigzag`. lemma zag_of_zag_obj (F : J ⥤ K) [full F] {j₁ j₂ : J} (h : zag (F.obj j₁) (F.obj j₂)) : zag j₁ j₂ := or.imp (nonempty.map F.preimage) (nonempty.map F.preimage) h /-- Any equivalence relation containing (⟶) holds for all pairs of a connected category. -/ lemma equiv_relation [is_connected J] (r : J → J → Prop) (hr : _root_.equivalence r) (h : ∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), r j₁ j₂) : ∀ (j₁ j₂ : J), r j₁ j₂ := begin have z : ∀ (j : J), r (classical.arbitrary J) j := induct_on_objects (λ k, r (classical.arbitrary J) k) (hr.1 (classical.arbitrary J)) (λ _ _ f, ⟨λ t, hr.2.2 t (h f), λ t, hr.2.2 t (hr.2.1 (h f))⟩), intros, apply hr.2.2 (hr.2.1 (z _)) (z _) end /-- In a connected category, any two objects are related by `zigzag`. -/ lemma is_connected_zigzag [is_connected J] (j₁ j₂ : J) : zigzag j₁ j₂ := equiv_relation _ zigzag_equivalence (λ _ _ f, relation.refl_trans_gen.single (or.inl (nonempty.intro f))) _ _ /-- If any two objects in an nonempty category are related by `zigzag`, the category is connected. -/ lemma zigzag_is_connected [nonempty J] (h : ∀ (j₁ j₂ : J), zigzag j₁ j₂) : is_connected J := begin apply is_connected.of_induct, intros p hp hjp j, have: ∀ (j₁ j₂ : J), zigzag j₁ j₂ → (j₁ ∈ p ↔ j₂ ∈ p), { introv k, induction k with _ _ rt_zag zag, { refl }, { rw k_ih, rcases zag with ⟨⟨_⟩⟩ | ⟨⟨_⟩⟩, apply hjp zag, apply (hjp zag).symm } }, rwa this j (classical.arbitrary J) (h _ _) end lemma exists_zigzag' [is_connected J] (j₁ j₂ : J) : ∃ l, list.chain zag j₁ l ∧ list.last (j₁ :: l) (list.cons_ne_nil _ _) = j₂ := list.exists_chain_of_relation_refl_trans_gen (is_connected_zigzag _ _) /-- If any two objects in an nonempty category are linked by a sequence of (potentially reversed) morphisms, then J is connected. The converse of `exists_zigzag'`. -/ lemma is_connected_of_zigzag [nonempty J] (h : ∀ (j₁ j₂ : J), ∃ l, list.chain zag j₁ l ∧ list.last (j₁ :: l) (list.cons_ne_nil _ _) = j₂) : is_connected J := begin apply zigzag_is_connected, intros j₁ j₂, rcases h j₁ j₂ with ⟨l, hl₁, hl₂⟩, apply list.relation_refl_trans_gen_of_exists_chain l hl₁ hl₂, end /-- If `discrete α` is connected, then `α` is (type-)equivalent to `punit`. -/ def discrete_is_connected_equiv_punit {α : Type u₁} [is_connected (discrete α)] : α ≃ punit := discrete.equiv_of_equivalence { functor := functor.star α, inverse := discrete.functor (λ _, classical.arbitrary _), unit_iso := by { exact (iso_constant _ (classical.arbitrary _)), }, counit_iso := functor.punit_ext _ _ } variables {C : Type u₂} [category.{u₁} C] /-- For objects `X Y : C`, any natural transformation `α : const X ⟶ const Y` from a connected category must be constant. This is the key property of connected categories which we use to establish properties about limits. -/ lemma nat_trans_from_is_connected [is_preconnected J] {X Y : C} (α : (functor.const J).obj X ⟶ (functor.const J).obj Y) : ∀ (j j' : J), α.app j = (α.app j' : X ⟶ Y) := @constant_of_preserves_morphisms _ _ _ (X ⟶ Y) (λ j, α.app j) (λ _ _ f, (by { have := α.naturality f, erw [id_comp, comp_id] at this, exact this.symm })) instance [is_connected J] : full (functor.const J : C ⥤ J ⥤ C) := { preimage := λ X Y f, f.app (classical.arbitrary J), witness' := λ X Y f, begin ext j, apply nat_trans_from_is_connected f (classical.arbitrary J) j, end } instance nonempty_hom_of_connected_groupoid {G} [groupoid G] [is_connected G] : ∀ (x y : G), nonempty (x ⟶ y) := begin refine equiv_relation _ _ (λ j₁ j₂, nonempty.intro), exact ⟨λ j, ⟨𝟙 _⟩, λ j₁ j₂, nonempty.map (λ f, inv f), λ _ _ _, nonempty.map2 (≫)⟩, end end category_theory
cfb4de7f3a12ad5a680ea50c9a625e74c72e4a0b
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/Mbar/bounded.lean
7045bba9e8090e1903a18dde51d96bc1d7f249e8
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,609
lean
import data.fintype.intervals import data.real.basic import algebra.big_operators.ring import data.fintype.card import category_theory.Fintype import topology.order import topology.separation import topology.subset_properties import data.real.nnreal /-! # \overline{\mathcal{M}}_r(S)_{\leq c} Throughout, `S` is finite (not profinite). `ℳ-bar_{r'}(S)_{≤c}` is the filtration on `ℳ-bar_{r'}(S)` by profinite subsets described at the beginning of section 9 of `analytic.pdf`. It's defined not as a subset of `ℳ-bar_{r'}(S)` but as an independent type. It's a projective limit of types `Mbar_bdd r' S c M` which have an additional hypothesis that the power series involved are actually polynomials of degree at most `M`. ## Main definitions - `Mbar_bdd r' S c M`: the subset of `S → Tℤ[[T]]/(T^{M+1})` consisting of elements `F_s = ∑_{n=1}^M a_{n,s} T^n` such that `∑_{s,n} |a_{n,s}| (r')^n ≤ c`. - `Mbar_bdd.limit r' S c`: the projective limit of `Mbar_bdd r' S c M`, as a subtype of the product. -/ noncomputable theory open_locale big_operators classical nnreal open set /-- `Mbar_bdd r' S c M` is the subset of `S → Tℤ[[T]]/(T^{M+1})` consisting of elements `F_s = ∑_{n=1}^M a_{n,s} T^n` such that `∑_{s,n} |a_{n,s}| r^n ≤ c`. This is an auxiliary object used to define the profinite topology on `Mbar r' S`. -/ structure Mbar_bdd (r : ℝ≥0) (S : Fintype) (c : ℝ≥0) (M : ℕ) := (to_fun : S → fin (M + 1) → ℤ) (coeff_zero' : ∀ s, to_fun s 0 = 0) (sum_le' : (∑ s i, (↑(to_fun s i).nat_abs * r^(i : ℕ))) ≤ c) namespace Mbar_bdd variables {r' : ℝ≥0} {S : Fintype} {c c₁ c₂ : ℝ≥0} {M : ℕ} instance has_coe_to_fun : has_coe_to_fun (Mbar_bdd r' S c M) := ⟨_, Mbar_bdd.to_fun⟩ @[simp] lemma coe_mk (x h₁ h₂) : ((⟨x, h₁, h₂⟩ : Mbar_bdd r' S c M) : S → ℕ → ℤ) = x := rfl @[simp] protected lemma coeff_zero (x : Mbar_bdd r' S c M) (s : S) : x s 0 = 0 := x.coeff_zero' s protected lemma sum_le (x : Mbar_bdd r' S c M) : (∑ s i, ((↑(x s i).nat_abs * r'^(i:ℕ)))) ≤ c := x.sum_le' /-- The obvious map from `Mbar_bdd r' S c₁ M` to `Mbar_bdd r' S c₂ M`, for `c₁ ≤ c₂`. -/ protected def cast_le [hc : fact (c₁ ≤ c₂)] (x : Mbar_bdd r' S c₁ M) : Mbar_bdd r' S c₂ M := ⟨x.1, x.coeff_zero, x.sum_le.trans hc.out⟩ @[ext] lemma ext (x y : Mbar_bdd r' S c M) (h : ⇑x = y) : x = y := by { cases x, cases y, congr, exact h } instance : has_zero (Mbar_bdd r' S c M) := { zero := { to_fun := 0, coeff_zero' := λ s, rfl, sum_le' := by simp only [zero_mul, pi.zero_apply, finset.sum_const_zero, nat.cast_zero, zero_le', int.nat_abs_zero] } } instance : inhabited (Mbar_bdd r' S c M) := ⟨0⟩ open finset lemma coeff_bound [h0r : fact (0 < r')] (F : S → fin (M + 1) → ℤ) (hF : ∑ s i, (↑(F s i).nat_abs * r'^(i : ℕ)) ≤ c) (n : fin (M + 1)) (s : S) : ↑(F s n).nat_abs ≤ c / min (r' ^ M) 1 := begin rw [div_eq_mul_inv], apply le_mul_inv_of_mul_le ((lt_min (pow_pos h0r.out _) zero_lt_one).ne.symm), calc ↑(F s n).nat_abs * min (r' ^ M) 1 ≤ ↑(F s n).nat_abs * r' ^ (n:ℕ) : _ -- see below for proof ... ≤ ∑ i, (↑(F s i).nat_abs * r' ^ (i:ℕ)) : single_le_sum (λ (i : fin (M + 1)) _, _) (mem_univ n) ... ≤ ∑ s i, (↑(F s i).nat_abs * r'^(i:ℕ)) : by { refine single_le_sum (λ _ _, _) (mem_univ s), exact sum_nonneg (λ _ _, (subtype.property (_ : ℝ≥0))) } ... ≤ c : hF, { refine mul_le_mul_of_nonneg_left _ (subtype.property (_ : ℝ≥0)), cases le_or_lt r' 1 with hr1 hr1, { refine le_trans (min_le_left _ _) _, exact pow_le_pow_of_le_one h0r.out.le hr1 (nat.lt_add_one_iff.1 n.2) }, { exact le_trans (min_le_right _ _) (one_le_pow_of_one_le (le_of_lt hr1) _) } }, apply subtype.property (_ : ℝ≥0) end /-- An auxiliary function used to prove finiteness of `Mbar_bdd r' S c M`. -/ private def temp_map [fact (0 < r')] (F : Mbar_bdd r' S c M) (n : fin (M + 1)) (s : S) : Icc (ceil (-(c / min (r' ^ M) 1) : ℝ)) (floor (c / min (r' ^ M) 1 : ℝ)) := have h : (-(c / min (r' ^ M) 1) : ℝ) ≤ F s n ∧ (F s n : ℝ) ≤ (c / min (r' ^ M) 1 : ℝ), by { rw [← abs_le, ← nnreal.coe_nnabs, ← cast_nat_abs_eq_nnabs_cast], exact_mod_cast coeff_bound F F.sum_le n s }, ⟨F s n, ceil_le.2 $ h.1, le_floor.2 h.2⟩ instance [fact (0 < r')] : fintype (Mbar_bdd r' S c M) := fintype.of_injective temp_map begin rintros ⟨f1, hf1, hf1'⟩ ⟨f2, hf2, hf2'⟩ h, ext s n, change (temp_map ⟨f1, hf1, hf1'⟩ n s).1 = (temp_map ⟨f2, hf2, hf2'⟩ n s).1, rw h, end /-- The transition map from `Mbar_bdd r' S c N` to `Mbar_bdd r' S c M`, given `M ≤ N`. -/ def transition (r' : ℝ≥0) {S : Fintype} {c : ℝ≥0} {M N : ℕ} (h : M ≤ N) (x : Mbar_bdd r' S c N) : Mbar_bdd r' S c M := { to_fun := λ s i, x s (fin.cast_le (add_le_add_right h 1) i), coeff_zero' := λ s, x.coeff_zero _, sum_le' := begin refine le_trans _ x.sum_le, apply finset.sum_le_sum, intros s hs, let I := finset.map (fin.cast_le (add_le_add_right h 1)).to_embedding (finset.univ : finset (fin (M+1))), refine le_trans _ (finset.sum_le_sum_of_subset_of_nonneg (finset.subset_univ I) _), { rw finset.sum_map, apply le_of_eq, congr }, { intros, exact subtype.property (_ : ℝ≥0) } end } lemma transition_eq {r' : ℝ≥0} {S : Fintype} {c : ℝ≥0} {M N : ℕ} (h : M ≤ N) (F : Mbar_bdd r' S c N) (s : S) (i : fin (M+1)) : (transition r' h F).1 s i = F.1 s (fin.cast_le (add_le_add_right h 1) i) := rfl lemma transition_transition {r' : ℝ≥0} {S : Fintype} {c : ℝ≥0} {M N K : ℕ} (h : M ≤ N) (hh : N ≤ K) (x : Mbar_bdd r' S c K) : transition r' h (transition r' hh x) = transition r' (le_trans h hh) x := rfl lemma transition_cast_le {N : ℕ} (h : M ≤ N) [hc : fact (c₁ ≤ c₂)] (x : Mbar_bdd r' S c₁ N) : transition r' h (Mbar_bdd.cast_le x : Mbar_bdd r' S c₂ N) = Mbar_bdd.cast_le (transition r' h x) := rfl /-- The limit of `Mbar_bdd r' S c M` along the `transition` maps as `M` increases. -/ abbreviation limit (r' S c) := { F : Π (M : ℕ), Mbar_bdd r' S c M // ∀ (M N : ℕ) (h : M ≤ N), transition r' h (F N) = F M } /-- The obvious embedding `Mbar_bdd.limit r' S c` into the product of `Mbar_bdd r' S c M` as `M` varies. -/ def emb_aux : limit r' S c → (Π (M : ℕ), Mbar_bdd r' S c M) := coe section topological_structure instance : topological_space (Mbar_bdd r' S c M) := ⊥ instance : discrete_topology (Mbar_bdd r' S c M) := ⟨rfl⟩ -- sanity check example : t2_space (limit r' S c) := by apply_instance example : totally_disconnected_space (limit r' S c) := by apply_instance example [fact (0 < r')] : compact_space (Mbar_bdd r' S c M) := by apply_instance lemma emb (r' S c) : closed_embedding (@emb_aux r' S c) := { induced := rfl, inj := subtype.coe_injective, closed_range := begin have : range emb_aux = ⋂ (x : {y : ℕ × ℕ // y.1 ≤ y.2}), {F : Π M, Mbar_bdd r' S c M | transition r' x.2 (F x.val.2) = F x.val.1}, { ext, simp only [emb_aux, prod.forall, mem_Inter, mem_set_of_eq, subtype.range_coe_subtype, subtype.forall], }, rw this, apply is_closed_Inter, rintros ⟨⟨m, n⟩, h0 : m ≤ n⟩, refine is_closed_eq (continuous.comp _ $ continuous_apply _) (continuous_apply _), exact continuous_of_discrete_topology, end } instance [fact (0 < r')] : compact_space (limit r' S c) := begin erw [← is_compact_iff_compact_space, is_compact_iff_is_compact_univ, compact_iff_compact_in_subtype], apply is_closed.is_compact, exact (emb r' S c).is_closed_map _ is_closed_univ end /-- The projection from `Mbar_bdd.limit r' S c M` to `Mbar_bdd r' S c M`. -/ def proj (M : ℕ) : Mbar_bdd.limit r' S c → Mbar_bdd r' S c M := λ F, F.1 M lemma continuous_iff {α : Type*} [topological_space α] (f : α → Mbar_bdd.limit r' S c) : continuous f ↔ (∀ (M : ℕ), continuous ((proj M) ∘ f)) := begin split, { intros hf M, exact continuous.comp ((continuous_apply _).comp continuous_subtype_val) hf, }, { intros h, rw [embedding.continuous_iff (emb r' S c).to_embedding], exact continuous_pi h } end end topological_structure section addition /-- The addition on `Mbar_bdd r' S c M`. It takes a term of type `Mbar_bdd r' S c₁ M` and a term of type `Mbar_bdd r' S c₂ M` and produces a term of type `Mbar_bdd r' S (c₁ + c₂) M`. -/ def add (F : Mbar_bdd r' S c₁ M) (G : Mbar_bdd r' S c₂ M) : Mbar_bdd r' S (c₁ + c₂) M := { to_fun := F + G, coeff_zero' := λ s, by simp, sum_le' := begin refine le_trans _ (add_le_add F.sum_le G.sum_le), rw ← finset.sum_add_distrib, refine finset.sum_le_sum _, rintro s -, rw ← sum_add_distrib, refine finset.sum_le_sum _, rintro i -, rw ← add_mul, apply mul_le_mul_right', norm_cast, apply int.nat_abs_add_le end } /-- Negation on `Mbar_bdd r' S c M` -/ def neg (F : Mbar_bdd r' S c M) : Mbar_bdd r' S c M := { to_fun := -F, coeff_zero' := λ s, by simp, sum_le' := by { simp only [abs_neg, pi.neg_apply, int.nat_abs_neg], exact F.sum_le } } end addition section map -- To avoid defeq nonsense, we need to use ⟨S⟩ as opposed to S. -- TODO: Use `Fintype` everywhere?! /-- TODO -/ def map {S T : Fintype} (f : S ⟶ T) : Mbar_bdd r' ⟨S⟩ c M → Mbar_bdd r' ⟨T⟩ c M := λ F, { to_fun := λ t i, ∑ s in finset.univ.filter (λ s', f s' = t), F s i, coeff_zero' := by simp, sum_le' := calc ∑ (t : T) (i : fin (M+1)), ↑((∑ (s : S) in finset.univ.filter (λ s', f s' = t), F s i).nat_abs) * r' ^ (i : ℕ) ≤ ∑ (t : T) (i : fin (M+1)), ∑ s in finset.univ.filter (λ s', f s' = t), ↑(F s i).nat_abs * r' ^ (i : ℕ) : begin apply finset.sum_le_sum, rintros t -, apply finset.sum_le_sum, rintros i -, rw ← finset.sum_mul, refine mul_le_mul _ (le_refl _) zero_le' zero_le', rw ← nat.cast_sum, rw nat.cast_le, apply nat_abs_sum_le, end ... ≤ ∑ (s : S) (i : fin (M+1)), ↑(F s i).nat_abs * r' ^ (i : ℕ) : begin rw finset.sum_comm, nth_rewrite 1 finset.sum_comm, apply finset.sum_le_sum, rintro i -, rw ← finset.sum_bUnion, { apply finset.sum_le_sum_of_subset, intros _ _, simp }, { rintros t1 - t2 - h s hs, simp at hs ⊢, apply h, rw [← hs.1, ← hs.2] } end ... ≤ _ : F.sum_le } end map end Mbar_bdd #lint-