(*
  File:     Polya_Vinogradov.thy
  Authors:  Rodrigo Raya, EPFL; Manuel Eberl, TUM

  The Pólya–Vinogradov inequality, both the general case and the stronger variant for
  primitive characters.
*)
section \<open>The Pólya--Vinogradov Inequality\<close>
theory Polya_Vinogradov
imports
  Gauss_Sums
  "Dirichlet_Series.Divisor_Count"
begin

unbundle no_vec_lambda_notation

subsection \<open>The case of primitive characters\<close>

text \<open>
  We first prove a stronger variant of the Pólya--Vinogradov inequality for primitive characters.
  The fully general variant will then simply be a corollary of this. First, we need some bounds on
  logarithms, exponentials, and the harmonic numbers:
\<close>

(* TODO: Move? *)
lemma ln_add_one_self_less_self:
  fixes x :: real
  assumes "x > 0"
  shows "ln (1 + x) < x"
proof -
  have "0 \<le> x" "0 < x" "exp x > 0" "1+x > 0" using assms by simp+
  have "1 + x < 1 + x + x\<^sup>2 / 2"
    using \<open>0 < x\<close> by auto
  also have "\<dots> \<le> exp x"
    using exp_lower_Taylor_quadratic[OF \<open>0 \<le> x\<close>] by blast
  finally have "1 + x < exp (x)" by blast
  then have "ln (1 + x) < ln (exp (x))"
    using ln_less_cancel_iff[OF \<open>1+x > 0\<close> \<open>exp(x) > 0\<close>] by auto
  also have "\<dots> = x" using ln_exp by blast
  finally show ?thesis by auto
qed

lemma exp_1_bounds:
  assumes "x > (0::real)"
  shows   "exp 1 > (1 + 1 / x) powr x" and "exp 1 < (1 + 1 / x) powr (x+1)"
proof -
  have "ln (1 + 1 / x) < 1 / x"
    using ln_add_one_self_less_self assms by simp
  thus "exp 1 > (1 + 1 / x) powr x" using assms
    by (simp add: field_simps powr_def)
next
  have "1 < (x + 1) * ln ((x + 1) / x)" (is "_ < ?f x")
  proof (rule DERIV_neg_imp_decreasing_at_top[where ?f = ?f])
    fix t assume t: "x \<le> t"
    have "(?f has_field_derivative (ln (1 + 1 / t) - 1 / t)) (at t)"
      using t assms by (auto intro!: derivative_eq_intros simp:divide_simps)
    moreover have "ln (1 + 1 / t) - 1 / t < 0"
      using ln_add_one_self_less_self[of "1 / t"] t assms by auto
    ultimately show "\<exists>y. ((\<lambda>t. (t + 1) * ln ((t + 1) / t)) has_real_derivative y) (at t) \<and> y < 0"
      by blast
  qed real_asymp
  thus "exp 1 < (1 + 1 / x) powr (x + 1)"
    using assms by (simp add: powr_def field_simps)
qed

lemma harm_aux_ineq_1:
  fixes k :: real
  assumes "k > 1"
  shows "1 / k < ln (1 + 1 / (k - 1))"
proof -
  have "k-1 > 0" \<open>k > 0\<close> using assms by simp+
  from exp_1_bounds(2)[OF \<open>k-1 > 0\<close>]
  have "exp 1 < (1 + 1 / (k - 1)) powr k" by simp
  then have n_z: "(1 + 1 / (k - 1)) powr k > 0"
      using assms not_exp_less_zero by auto

  have "(1::real) = ln (exp(1))" using ln_exp by auto
  also have "\<dots> < ln ((1 + 1 / (k - 1)) powr k)"
    using ln_less_cancel_iff[of "exp(1)",simplified,OF \<open>(1 + 1 / (k - 1)) powr k > 0\<close>]
          exp_1_bounds[OF \<open>k - 1 > 0\<close>] by simp
  also have "\<dots> = k * ln (1 + 1 / (k - 1))"
    using ln_powr n_z by simp
  finally have "1 < k * ln (1 + 1 / (k - 1))"
    by blast
  then show ?thesis using assms by (simp add: field_simps)
qed

lemma harm_aux_ineq_2_lemma:
  assumes "x \<ge> (0::real)"
  shows   "1 < (x + 1) * ln (1 + 2 / (2 * x + 1))"
proof -
  have "0 < ln (1+2/(2*x+1)) - 1 / (x + 1)" (is "_ < ?f x")
  proof (rule DERIV_neg_imp_decreasing_at_top[where ?f = ?f])
    fix t assume t: "x \<le> t"
    from assms t have "3 + 8 * t + 4 * t^2 > 0"
      by (intro add_pos_nonneg) auto
    hence *: "3 + 8 * t + 4 * t^2 \<noteq> 0"
      by auto
    have "(?f has_field_derivative (-1 / ((1 + t)^2 * (3 + 8 * t + 4 * t ^ 2)))) (at t)"
      apply (insert assms t *, (rule derivative_eq_intros refl | simp add: add_pos_pos)+)
      apply (auto simp: divide_simps)
      apply (auto simp: algebra_simps power2_eq_square)
      done
    moreover have "-1 / ((1 + t)^2 * (3 + 8 * t + 4 * t^2)) < 0"
      using t assms by (intro divide_neg_pos mult_pos_pos add_pos_nonneg) auto
    ultimately show "\<exists>y. (?f has_real_derivative y) (at t) \<and> y < 0"
      by blast
  qed real_asymp
  thus "1 < (x + 1) * ln (1+2/(2*x+1))"
    using assms by (simp add: field_simps)
qed

lemma harm_aux_ineq_2:
  fixes k :: real
  assumes "k \<ge> 1"
  shows   "1 / (k + 1) < ln (1 + 2 / (2 * k + 1))"
proof -
  have "k > 0" using assms by auto
  have "1 < (k + 1) * ln (1 + 2 / (2 * k + 1))"
    using harm_aux_ineq_2_lemma assms by simp
  then show ?thesis
    by (simp add: \<open>0 < k\<close> add_pos_pos mult.commute mult_imp_div_pos_less)
qed

lemma nat_0_1_induct [case_names 0 1 step]:
  assumes "P 0" "P 1" "\<And>n. n \<ge> 1 \<Longrightarrow> P n \<Longrightarrow> P (Suc n)"
  shows   "P n"
proof (induction n rule: less_induct)
  case (less n)
  show ?case
    using assms(3)[OF _ less.IH[of "n - 1"]]
    by (cases "n \<le> 1")
      (insert assms(1-2),auto simp: eval_nat_numeral le_Suc_eq)
qed

lemma harm_less_ln:
  fixes m :: nat
  assumes "m > 0"
  shows   "harm m < ln (2 * m + 1)"
  using assms
proof (induct m rule: nat_0_1_induct)
  case 0
  then show ?case by blast
next
  case 1
  have "harm 1 = (1::real)" unfolding harm_def by simp
  have "harm 1 < ln (3::real)"
    by (subst \<open>harm 1 = 1\<close>,subst ln3_gt_1,simp)
  then show ?case by simp
next
  case (step n)
  have "harm (n+1) = harm n + 1/(n+1)"
    by ((subst Suc_eq_plus1[symmetric])+,subst harm_Suc,subst inverse_eq_divide,blast)
  also have "\<dots> < ln (real (2 * n + 1)) + 1/(n+1)"
    using step(1-2) by auto
  also have "\<dots> < ln (real (2 * n + 1)) + ln (1+2/(2*n+1))"
  proof -
    from step(1) have "real n \<ge> 1" by simp
    have "1 / real (n + 1) < ln (1 + 2 / real (2 * n + 1))"
      using harm_aux_ineq_2[OF \<open>1 \<le> (real n)\<close>]  by (simp add: add.commute)
    then show ?thesis by auto
  qed
  also have "\<dots> = ln ((2 * n + 1) * (1+2/(2*n+1)))"
    by (rule ln_mult[symmetric],simp,simp add: field_simps)
  also have "\<dots> = ln (2*(n+1)+1)"
  proof -
    have "(2 * n + 1) * (1+2/(2*n+1)) = 2*(n+1)+1"
      by (simp add: field_simps)
    then show ?thesis by presburger
  qed
  finally show ?case by simp
qed
(* END TODO *)

text\<open>Theorem 8.21\<close>
theorem (in primitive_dchar) polya_vinogradov_inequality_primitive:
  fixes x :: nat
  shows "norm (\<Sum>m=1..x. \<chi> m) < sqrt n * ln n"
proof -
  define \<tau> :: complex where "\<tau> = gauss_sum 1 div sqrt n"
  have \<tau>_mod: "norm \<tau> = 1" using fourier_primitive(2)
    by (simp add: \<tau>_def)
  {
    fix m
    have "\<chi> m = (\<tau> div sqrt n) * (\<Sum>k = 1..n. (cnj (\<chi> k)) * unity_root n (-m*k))"
    using fourier_primitive(1)[of m] \<tau>_def by blast}
    note chi_expr = this
    have "(\<Sum>m = 1..x. \<chi>(m)) = (\<Sum>m = 1..x. (\<tau> div sqrt n) * (\<Sum>k = 1..n. (cnj (\<chi> k)) * unity_root n (-m*k)))"
      by(rule sum.cong[OF refl]) (use chi_expr in blast)
    also have "\<dots> = (\<Sum>m = 1..x. (\<Sum>k = 1..n. (\<tau> div sqrt n) * ((cnj (\<chi> k)) * unity_root n (-m*k))))"
      by (rule sum.cong,simp,simp add: sum_distrib_left)
    also have "\<dots> = (\<Sum>k = 1..n. (\<Sum>m = 1..x. (\<tau> div sqrt n) * ((cnj (\<chi> k)) * unity_root n (-m*k))))"
      by (rule sum.swap)
    also have "\<dots> = (\<Sum>k = 1..n. (\<tau> div sqrt n) *  (cnj (\<chi> k) * (\<Sum>m = 1..x. unity_root n (-m*k))))"
      by (rule sum.cong,simp,simp add: sum_distrib_left)
    also have "\<dots> = (\<Sum>k = 1..<n. (\<tau> div sqrt n) * (cnj (\<chi> k) * (\<Sum>m = 1..x. unity_root n (-m*k))))"
      using n by (intro sum.mono_neutral_right) (auto intro: eq_zero)
    also have "\<dots> = (\<tau> div sqrt n) * (\<Sum>k = 1..<n. (cnj (\<chi> k) * (\<Sum>m = 1..x. unity_root n (-m*k))))"
      by (simp add: sum_distrib_left)
    finally have "(\<Sum>m = 1..x. \<chi>(m)) = (\<tau> div sqrt n) * (\<Sum>k = 1..<n. (cnj (\<chi> k) * (\<Sum>m = 1..x. unity_root n (-m*k))))"
      by blast
    hence eq: "sqrt n * (\<Sum>m=1..x. \<chi>(m)) = \<tau> * (\<Sum>k=1..<n. (cnj (\<chi> k) * (\<Sum>m=1..x. unity_root n (-m*k))))"
      by auto
    define f where "f = (\<lambda>k. (\<Sum>m = 1..x. unity_root n (-m*k)))"

    hence "(sqrt n) * norm(\<Sum>m = 1..x. \<chi>(m)) = norm(\<tau> * (\<Sum>k=1..<n. (cnj (\<chi> k) * (\<Sum>m = 1..x. unity_root n (-m*k)))))"
    proof -
      have "norm(sqrt n * (\<Sum>m=1..x. \<chi>(m))) = norm (sqrt n) * norm((\<Sum>m = 1..x. \<chi>(m)))"
        by (simp add: norm_mult)
      also have "\<dots> = (sqrt n) * norm((\<Sum>m = 1..x. \<chi>(m)))"
        by simp
      finally have 1: "norm((sqrt n) * (\<Sum>m = 1..x. \<chi>(m))) = (sqrt n) * norm((\<Sum>m = 1..x. \<chi>(m)))"
        by blast
      then show ?thesis using eq by algebra
    qed
    also have "\<dots> = norm (\<Sum>k = 1..<n. (cnj (\<chi> k) * (\<Sum>m = 1..x. unity_root n (-m*k))))"
      by (simp add: norm_mult \<tau>_mod)
    also have "\<dots> \<le> (\<Sum>k = 1..<n. norm (cnj (\<chi> k) * (\<Sum> m = 1..x. unity_root n (-m*k))))"
      using norm_sum by blast
    also have "\<dots> = (\<Sum>k = 1..<n. norm (cnj (\<chi> k)) * norm((\<Sum> m = 1..x. unity_root n (-m*k))))"
      by (rule sum.cong,simp, simp add: norm_mult)
    also have "\<dots> \<le> (\<Sum>k = 1..<n. norm((\<Sum>m = 1..x. unity_root n (-m*k))))"
    proof -
      show ?thesis
      proof (rule sum_mono)
        fix k
        assume "k \<in> {1..<n}"
        define sum_aux :: real where "sum_aux = norm (\<Sum>m=1..x. unity_root n (- int m * int k))"
        have "sum_aux \<ge> 0" unfolding sum_aux_def by auto
        have "norm (cnj (\<chi> k)) \<le> 1" using norm_le_1[of k] by simp
        then have "norm (cnj (\<chi> k)) * sum_aux \<le> 1 * sum_aux"
          using \<open>sum_aux \<ge> 0\<close> by (simp add: mult_left_le_one_le)
        then show " norm (cnj (\<chi> k)) *
           norm (\<Sum>m = 1..x. unity_root n (- int m * int k))
           \<le> norm (\<Sum>m = 1..x. unity_root n (- int m * int k))"
          unfolding sum_aux_def by argo
      qed
    qed
    also have "\<dots> = (\<Sum>k = 1..<n. norm(f k))"
      using f_def by blast
    finally have 24: "(sqrt n) * norm(\<Sum>m = 1..x. \<chi>(m)) \<le> (\<Sum>k = 1..<n. norm(f k))"
      by blast

    {
      fix k :: int
      have "f(n-k) = cnj(f(k))"
      proof -
        have "f(n-k) = (\<Sum>m = 1..x. unity_root n (-m*(n-k)))"
          unfolding f_def by blast
        also have "\<dots> = (\<Sum>m = 1..x. unity_root n (m*k))"
        proof (rule sum.cong,simp)
          fix xa
          assume "xa \<in> {1..x}"
          have "(k * int xa - int n * int xa) mod int n = (k * int xa - 0) mod int n"
            by (intro mod_diff_cong) auto
          thus "unity_root n (-int xa * (int n - k)) = unity_root n (int xa * k)"
            unfolding ring_distribs by (intro unity_root_cong) (auto simp: cong_def algebra_simps)
        qed
        also have "\<dots> = cnj(f(k))"
        proof -
          have "cnj(f(k)) = cnj (\<Sum>m = 1..x. unity_root n (- int m * k))"
            unfolding f_def by blast
          also have "cnj (\<Sum>m = 1..x. unity_root n (- int m * k)) =
                (\<Sum>m = 1..x. cnj(unity_root n (- int m * k)))"
            by (rule cnj_sum)
          also have "\<dots> = (\<Sum>m = 1..x. unity_root n (int m * k))"
            by (intro sum.cong) (auto simp: unity_root_uminus)
          finally show ?thesis by auto
        qed
        finally show "f(n-k) = cnj(f(k))" by blast
      qed
      hence "norm(f(n-k)) = norm(cnj(f(k)))" by simp
      hence "norm(f(n-k)) = norm(f(k))" by auto
    }
    note eq = this
    have 25:
      "odd n \<Longrightarrow> (\<Sum>k = 1..n - 1. norm (f (int k))) \<le>
                    2 * (\<Sum>k = 1..(n-1) div 2. norm (f (int k)))"
      "even n \<Longrightarrow> (\<Sum>k = 1..n - 1. norm (f (int k))) \<le>
                    2 * (\<Sum>k = 1..(n-2) div 2. norm (f (int k))) + norm(f(n div 2))"
    proof -
      assume "odd n"
      define g where "g = (\<lambda>k. norm (f k))"
      have "(n-1) div 2  = n div 2" using \<open>odd n\<close> n
        using div_mult_self1_is_m[OF pos2,of "n-1"]
              odd_two_times_div_two_nat[OF \<open>odd n\<close>] by linarith
      have "(\<Sum>i=1..n-1. g i) = (\<Sum>i\<in>{1..n div 2}\<union>{n div 2<..n-1}. g i)"
        using n by (intro sum.cong,auto)
      also have "\<dots> = (\<Sum>i\<in>{1..n div 2}. g i) + (\<Sum>i\<in>{n div 2<..n-1}. g i)"
        by (subst sum.union_disjoint,auto)
      also have "(\<Sum>i\<in>{n div 2<..n-1}. g i) = (\<Sum>i\<in>{1..n - (n div 2 + 1)}. g (n - i))"
        by (rule sum.reindex_bij_witness[of _ "\<lambda>i. n - i" "\<lambda>i. n - i"],auto)
      also have "\<dots> \<le> (\<Sum>i\<in>{1..n div 2}. g (n - i))"
        by (intro sum_mono2,simp,auto simp add: g_def)
      finally have 1: "(\<Sum>i=1..n-1. g i) \<le> (\<Sum>i=1..n div 2. g i + g (n - i))"
        by (simp add: sum.distrib)
      have "(\<Sum>i=1..n div 2. g i + g (n - i)) = (\<Sum>i=1..n div 2. 2 * g i)"
        unfolding g_def
        apply(rule sum.cong,simp)
        using eq int_ops(6) by force
      also have "\<dots> = 2 * (\<Sum>i=1..n div 2. g i)"
        by (rule sum_distrib_left[symmetric])
      finally have 2: "(\<Sum>i=1..n div 2. g i + g (n - i)) = 2 * (\<Sum>i=1..n div 2. g i)"
        by blast
      from 1 2 have "(\<Sum>i=1..n-1. g i) \<le> 2 * (\<Sum>i=1..n div 2. g i)" by algebra
      then show "(\<Sum>n = 1..n - 1. norm (f (int n))) \<le> 2 * (\<Sum>n = 1..(n-1) div 2. norm (f (int n)))"
        unfolding g_def \<open>(n-1) div 2 = n div 2\<close> by blast
    next
      assume "even n"
      define g where "g = (\<lambda>n. norm (f (n)))"
      have "(n-2) div 2 = n div 2 - 1" using \<open>even n\<close> n by simp
      have "(\<Sum>i=1..n-1. g i) = (\<Sum>i\<in>{1..<n div 2}\<union> {n div 2} \<union> {n div 2<..n-1}. g i)"
        using n by (intro sum.cong,auto)
      also have "\<dots> = (\<Sum>i\<in>{1..<n div 2}. g i) + (\<Sum>i\<in>{n div 2<..n-1}. g i) + g(n div 2)"
        by (subst sum.union_disjoint,auto)
      also have "(\<Sum>i\<in>{n div 2<..n-1}. g i) = (\<Sum>i\<in>{1..n - (n div 2+1)}. g (n - i))"
        by (rule sum.reindex_bij_witness[of _ "\<lambda>i. n - i" "\<lambda>i. n - i"],auto)
      also have "\<dots> \<le> (\<Sum>i\<in>{1..<n div 2}. g (n - i))"
      proof (intro sum_mono2,simp)
        have "n - n div 2 = n div 2" using \<open>even n\<close> n by auto
        then have "n - (n div 2 + 1) < n div 2"
          using n by (simp add: divide_simps)
        then show "{1..n - (n div 2 + 1)} \<subseteq> {1..<n div 2}" by fastforce
      qed auto
      finally have 1: "(\<Sum>i=1..n-1. g i) \<le> (\<Sum>i=1..<n div 2. g i + g (n - i)) + g(n div 2)"
        by (simp add: sum.distrib)
      have "(\<Sum>i=1..<n div 2. g i + g (n - i)) = (\<Sum>i=1..<n div 2. 2 * g i)"
        unfolding g_def
        apply(rule sum.cong,simp)
        using eq int_ops(6) by force
      also have "\<dots> = 2 * (\<Sum>i=1..<n div 2. g i)"
        by (rule sum_distrib_left[symmetric])
      finally have 2: "(\<Sum>i=1..<n div 2. g i + g (n - i)) = 2 * (\<Sum>i=1..<n div 2. g i)"
        by blast
      from 1 2 have 3: "(\<Sum>i=1..n-1. g i) \<le> 2 * (\<Sum>i=1..<n div 2. g i) + g(n div 2)" by algebra
      then have "(\<Sum>i=1..n-1. g i) \<le> 2 * (\<Sum>i=1..(n-2) div 2. g i) + g(n div 2)"
      proof -
        have "{1..<n div 2} = {1..(n-2) div 2}" by auto
        then have "(\<Sum>i=1..<n div 2. g i) = (\<Sum>i=1..(n-2) div 2. g i)"
          by (rule sum.cong,simp)
        then show ?thesis using 3 by presburger
      qed
      then show "(\<Sum>k = 1..n - 1. norm (f (int k))) \<le> 2 * (\<Sum>n = 1..(n-2) div 2. norm (f (int n))) + g(n div 2)"
        unfolding g_def by blast
    qed

    (* expression for each f(n) *)
    {fix k :: int
    assume "1 \<le> k" "k \<le> n div 2"
    have "k \<le> n - 1"
      using \<open>k \<le> n div 2\<close> n by linarith
    define y where "y = unity_root n (-k)"
    define z where "z = exp (-(pi*k/n)* \<i>)"
    have "z^2 = exp (2*(-(pi*k/n)* \<i>))"
      unfolding z_def using exp_double[symmetric] by blast
    also have "\<dots> = y"
      unfolding y_def unity_root_conv_exp by (simp add: algebra_simps)
    finally have z_eq: "y = z^2" by blast
    have z_not_0: "z \<noteq> 0"
      using z_eq by (simp add: z_def)

    then have "y \<noteq> 1"
      using unity_root_eq_1_iff_int \<open>1 \<le> k\<close> \<open>k \<le> n - 1\<close> not_less
            unity_root_eq_1_iff_int y_def zdvd_not_zless by auto

    have "f(k) = (\<Sum>m = 1..x . y^m)"
      unfolding f_def y_def
      by (subst unity_root_pow,rule sum.cong,simp,simp add: algebra_simps)
    also have sum: "\<dots> = (\<Sum>m = 1..<x+1 . y^m)"
      by (rule sum.cong,fastforce,simp)
    also have "\<dots> = (\<Sum>m = 0..<x+1 . y^m) - 1"
      by (subst (2) sum.atLeast_Suc_lessThan) auto
    also have "\<dots> = (y^(x+1) - 1) div (y - 1) - 1"
      using geometric_sum[OF \<open>y \<noteq> 1\<close>, of "x+1"] by (simp add: atLeast0LessThan)
    also have "\<dots> = (y^(x+1) - 1 - (y-1)) div (y - 1)"
    proof -
      have "y - 1 \<noteq> 0" using \<open>y \<noteq> 1\<close> by simp
      show ?thesis
        using divide_diff_eq_iff[OF \<open>y - 1 \<noteq> 0\<close>, of "(y^(x+1) - 1)" 1] by auto
    qed
    also have "\<dots> = (y^(x+1) - y) div (y - 1)"
      by (simp add: algebra_simps)
    also have "\<dots> = y * (y^x - 1) div (y - 1)"
      by (simp add: algebra_simps)
    also have "\<dots> = z^2 * ((z^2)^x - 1) div (z^2 - 1)"
      unfolding z_eq by blast
    also have "\<dots> = z^2 * (z^(2*x) - 1) div (z^2 - 1)"
      by (subst power_mult[symmetric, of z 2 x],blast)
    also have "\<dots> = z^(x+1)*((z ^x -inverse(z^x))) / (z - inverse(z))"
    proof -
      have "z^x \<noteq> 0" using z_not_0 by auto
      have 1: "z ^ (2 * x) - 1 = z^x*(z ^x -inverse(z^x))"
        by (simp add: semiring_normalization_rules(36) right_inverse[OF \<open>z^x \<noteq> 0\<close>]  right_diff_distrib')
      have 2: "z\<^sup>2 - 1 = z*(z - inverse(z))"
        by (simp add: right_diff_distrib' semiring_normalization_rules(29) right_inverse[OF \<open>z \<noteq> 0\<close>])

      have 3: "z\<^sup>2 * (z^x / z) = z^(x+1)"
      proof -
        have "z\<^sup>2 * (z^x / z) = z\<^sup>2 * (z^x * inverse z)"
          by (simp add: inverse_eq_divide)
        also have "\<dots> = z^(x+1)"
          by (simp add: algebra_simps power2_eq_square right_inverse[OF \<open>z \<noteq> 0\<close>])
        finally show ?thesis by blast
      qed
      have "z\<^sup>2 * (z ^ (2 * x) - 1) / (z\<^sup>2 - 1) =
            z\<^sup>2 * (z^x*(z ^x -inverse(z^x))) / (z*(z - inverse(z)))"
        by (subst 1, subst 2,blast)
      also have "\<dots> =  (z\<^sup>2 * (z^x / z)) * ((z ^x -inverse(z^x))) / (z - inverse(z))"
        by simp
      also have "\<dots> = z^(x+1) *((z ^x -inverse(z^x))) / (z - inverse(z))"
        by (subst 3,simp)
      finally show ?thesis by simp
    qed
    finally have "f(k) = z^(x+1) *((z ^x -inverse(z^x))) / (z - inverse(z))" by blast

    (* inequality for each f(k) *)
    then have "norm(f(k)) = norm(z^(x+1) * (((z ^x -inverse(z^x))) / (z - inverse(z))))" by auto
    also have "\<dots> = norm(z^(x+1)) * norm(((z ^x -inverse(z^x))) / (z - inverse(z)))"
      using norm_mult by blast
    also have "\<dots> = norm(((z ^x -inverse(z^x))) / (z - inverse(z)))"
    proof -
      have "norm(z) = 1"
        unfolding z_def by auto
      have "norm(z^(x+1)) = 1"
        by (subst norm_power,simp add: \<open>norm(z) = 1\<close>)
      then show ?thesis by simp
    qed
    also have "\<dots> = norm((exp (-(x*pi*k/n)* \<i>) - exp ((x*pi*k/n)* \<i>)) div
                     (exp (-(pi*k/n)* \<i>) - exp ((pi*k/n)* \<i>)))"
    proof -
      have 1: "z ^ x = exp (-(x*pi*k/n)* \<i>)"
        unfolding z_def
        by (subst exp_of_nat_mult[symmetric],simp add: algebra_simps)
      have "inverse (z ^ x) = inverse (exp (-(x*pi*k/n)* \<i>))"
        using \<open>z ^ x = exp (-(x*pi*k/n)* \<i>)\<close> by auto
      also have "\<dots> = (exp ((x*pi*k/n)* \<i>))"
        by (simp add: exp_minus)
      finally have 2: "inverse(z^x) = exp ((x*pi*k/n)* \<i>)" by simp
      have 3: "inverse z = exp ((pi*k/n)* \<i>)"
        by (simp add: exp_minus z_def)
      show ?thesis using 1 2 3 z_def by simp
    qed
    also have "\<dots> = norm((sin (x*pi*k/n)) div (sin (pi*k/n)))"
    proof -
      have num: "(exp (-(x*pi*k/n)* \<i>) - exp ((x*pi*k/n)* \<i>)) = (-2*\<i>* sin((x*pi*k/n)))"
      proof -
        have 1: "exp (-(x*pi*k/n)* \<i>) = cos(-(x*pi*k/n)) + \<i> * sin(-(x*pi*k/n))"
                "exp ((x*pi*k/n)* \<i>) = cos((x*pi*k/n)) + \<i> * sin((x*pi*k/n))"
          using Euler Im_complex_of_real Im_divide_of_nat Im_i_times Re_complex_of_real
                complex_Re_of_int complex_i_mult_minus exp_zero mult.assoc mult.commute by force+
        have "(exp (-(x*pi*k/n)* \<i>) - exp ((x*pi*k/n)* \<i>)) =
              (cos(-(x*pi*k/n)) + \<i> * sin(-(x*pi*k/n))) -
              (cos((x*pi*k/n)) + \<i> * sin((x*pi*k/n)))"
          using 1 by argo
        also have "\<dots> = -2*\<i>* sin((x*pi*k/n))" by simp
        finally show ?thesis by blast
      qed

      have den: "(exp (-(pi*k/n)* \<i>) - exp ((pi*k/n)* \<i>)) = -2*\<i>* sin((pi*k/n))"
      proof -
        have 1: "exp (-(pi*k/n)* \<i>) = cos(-(pi*k/n)) + \<i> * sin(-(pi*k/n))"
                "exp ((pi*k/n)* \<i>) = cos((pi*k/n)) + \<i> * sin((pi*k/n))"
          using Euler Im_complex_of_real Im_divide_of_nat Im_i_times Re_complex_of_real
                complex_Re_of_int complex_i_mult_minus exp_zero mult.assoc mult.commute by force+
        have "(exp (-(pi*k/n)* \<i>) - exp ((pi*k/n)* \<i>)) =
              (cos(-(pi*k/n)) + \<i> * sin(-(pi*k/n))) -
              (cos((pi*k/n)) + \<i> * sin((pi*k/n)))"
          using 1 by argo
        also have "\<dots> = -2*\<i>* sin((pi*k/n))" by simp
        finally show ?thesis by blast
      qed

      have "norm((exp (-(x*pi*k/n)* \<i>) - exp ((x*pi*k/n)* \<i>)) div
                     (exp (-(pi*k/n)* \<i>) - exp ((pi*k/n)* \<i>))) =
            norm((-2*\<i>* sin((x*pi*k/n))) div (-2*\<i>* sin((pi*k/n))))"
        using num den by presburger
      also have "\<dots> = norm(sin((x*pi*k/n)) div sin((pi*k/n)))"
        by (simp add: norm_divide)
      finally show ?thesis by blast
    qed
    also have "\<dots> = norm((sin (x*pi*k/n))) div norm((sin (pi*k/n)))"
      by (simp add: norm_divide)
    also have "\<dots> \<le> 1 div norm((sin (pi*k/n)))"
    proof -
      have "norm((sin (pi*k/n))) \<ge> 0" by simp
      have "norm (sin (x*pi*k/n)) \<le> 1" by simp
      then show ?thesis
        using divide_right_mono[OF \<open>norm (sin (x*pi*k/n)) \<le> 1\<close> \<open>norm((sin (pi*k/n))) \<ge> 0\<close>]
        by blast
    qed
    finally have 26: "norm(f(k)) \<le> 1 div norm((sin (pi*k/n)))"
      by blast

    (* inequality with sin *)
    {
      fix t
      assume "t \<ge> 0" "t \<le> pi div 2"
      then have "t \<in> {0..pi div 2}" by auto
      have "convex_on {0..pi/2} (\<lambda>x. -sin x)"
       by (rule convex_on_realI[where f' = "\<lambda>x. - cos x"])
          (auto intro!: derivative_eq_intros simp: cos_monotone_0_pi_le)
      from convex_onD_Icc'[OF this \<open>t \<in> {0..pi div 2}\<close>] have "sin(t) \<ge> (2 div pi)*t" by simp
    }
    note sin_ineq = this

    have sin_ineq_inst: "sin ((pi*k) / n) \<ge> (2 * k) / n"
    proof -
      have "pi / n \<ge> 0" by simp
      have 1: "(pi*k) / n \<ge> 0" using \<open>1 \<le> k\<close> by auto
      have "(pi*k)/n = (pi / n) * k" by simp
      also have "\<dots> \<le> (pi / n) * (n / 2)"
        using mult_left_mono[of "k" "n / 2" "pi / n"]
              \<open>k \<le> n div 2\<close> \<open>0 \<le> pi / real n\<close> by linarith
      also have "\<dots> \<le> pi / 2"
        by (simp add: divide_simps)
      finally have 2: "(pi*k)/n \<le> pi / 2" by auto

      have "(2 / pi) * (pi * k / n) \<le> sin((pi * k) / n)"
        using sin_ineq[OF 1 2] by blast
      then show "sin((pi * k) / n) \<ge> (2*k) / n"
        by auto
    qed

    from 26 have "norm(f(k)) \<le> 1 div abs((sin (pi*k/n)))" by simp
    also have "\<dots> \<le> 1 / abs((2*k) / n)"
    proof -
      have "sin (pi*k/n) \<ge> (2*k) / n" using sin_ineq_inst by simp
      moreover have "(2*k) / n > 0" using n \<open>1 \<le> k\<close> by auto
      ultimately have "abs((sin (pi*k/n))) \<ge> abs((2*k)/n)" by auto
      have "abs((2*k)/n) > 0" using \<open>(2*k)/n > 0\<close> by linarith
      then show "1 div abs((sin (pi*k/n))) \<le> 1 / abs(((2*k)/n))"
        using \<open>abs((2*k)/n) > 0\<close> \<open>abs((sin (pi*k/n))) \<ge> abs(((2*k)/n))\<close>
        by (intro frac_le) auto
    qed
    also have "\<dots> = n / (2*k)" using \<open>k \<ge> 1\<close> by simp
    finally have "norm(f(k)) \<le> n / (2*k)" by blast
  }
  note ineq = this

  (* inequality for the odd and even case*)
  have "sqrt n * norm (sum \<chi> {1..x}) < n * ln n"
  proof (cases "even n")
    case True
    have "norm (f(n div 2)) \<le> 1"
    proof -
      have "int (n div 2) \<ge> 1" using n \<open>even n\<close> by auto
      show ?thesis
        using ineq[OF \<open>int (n div 2) \<ge> 1\<close>] True n by force
    qed
    from 24 have "sqrt n * norm (sum \<chi> {1..x})
               \<le> (\<Sum>k = 1..<n. norm (f (int k)))" by blast
    also have "\<dots> = (\<Sum>k = 1..n-1. norm (f (int k)))"
      by (intro sum.cong) auto
    also have "\<dots> \<le> 2 * (\<Sum>k = 1..(n - 2) div 2. norm (f (int k))) + norm(f(n div 2))"
      using 25(2)[OF True] by blast
    also have "\<dots> \<le>  real n * (\<Sum>k = 1..(n - 2) div 2. 1 / k) + norm(f(n div 2))"
    proof -
      have "(\<Sum>k = 1..(n - 2) div 2. norm (f (int k))) \<le> (\<Sum>k = 1..(n - 2) div 2. real n div (2*k))"
      proof (rule sum_mono)
        fix k
        assume "k \<in> {1..(n - 2) div 2}"
        then have "1 \<le> int k" "int k \<le> n div 2" by auto
        show "norm (f (int k)) \<le> real n / (2*k)"
          using ineq[OF \<open>1 \<le> int k\<close> \<open>int k \<le> n div 2\<close>] by auto
      qed
      also have "\<dots> = (\<Sum>k = 1..(n - 2) div 2. (real n div 2) * (1 / k))"
        by (rule sum.cong,auto)
      also have "\<dots> = (real n div 2) * (\<Sum>k = 1..(n - 2) div 2. 1 / k)"
        using sum_distrib_left[symmetric] by fast
      finally have "(\<Sum>k = 1..(n - 2) div 2. norm (f (int k))) \<le>
                  (real n div 2) * (\<Sum>k = 1..(n - 2) div 2. 1 / k)"
        by blast
      then show ?thesis by argo
    qed
    also have "\<dots> = real n * harm ((n - 2) div 2) + norm(f(n div 2))"
      unfolding harm_def inverse_eq_divide by simp
    also have "\<dots> < n * ln n"
    proof (cases "n = 2")
      case True
      have "real n * harm ((n - 2) div 2) + norm (f (int (n div 2))) \<le> 1"
        using \<open>n = 2\<close> \<open>norm (f (int (n div 2))) \<le> 1\<close>
        unfolding harm_def by simp
      moreover have "real n * ln (real n) \<ge> 4 / 3"
        using \<open>n = 2\<close> ln2_ge_two_thirds by auto
      ultimately show ?thesis by argo
    next
      case False
      have "n > 3" using n \<open>n \<noteq> 2\<close> \<open>even n\<close> by auto
      then have "(n-2) div 2 > 0" by simp
      then have "harm ((n - 2) div 2) < ln (real (2 * ((n - 2) div 2) + 1))"
        using harm_less_ln by blast
      also have "\<dots> = ln (real (n - 1))"
        using \<open>even n\<close> \<open>n > 3\<close> by simp
      finally have 1: "harm ((n - 2) div 2) < ln (real (n - 1))"
        by blast
      then have "real n * harm ((n - 2) div 2) < real n * ln (real (n - 1))"
        using n by simp
      then have "real n * harm ((n - 2) div 2) + norm (f (int (n div 2)))
            < real n * ln (real (n - 1)) + 1"
        using \<open>norm (f (int (n div 2))) \<le> 1\<close> by argo
      also have "\<dots> = real n * ln (real (n - 1)) + real n * 1 / real n"
        using n by auto
      also have "\<dots> < real n * ln (real (n - 1)) + real n * ln (1 + 1 / (real n - 1))"
      proof -
        have "real n > 1" "real n > 0" using n by simp+
        then have "real n * (1 / real n) < real n * ln (1 + 1 / (real n - 1))"
          by (intro mult_strict_left_mono harm_aux_ineq_1) auto
        then show ?thesis by auto
      qed
      also have "\<dots> = real n * ( ln (real (n - 1)) + ln (1 + 1 / (real n - 1)))"
        by argo
      also have "\<dots> = real n * ( ln (real (n - 1) * (1 + 1 / (real n - 1))))"
      proof -
        have "real (n - 1) > 0" "1 + 1 / (real n - 1) > 0"
          using n by (auto simp add: add_pos_nonneg)
        show ?thesis
          by (subst ln_mult [OF \<open>real (n - 1) > 0\<close> \<open>1 + 1 / (real n - 1) > 0\<close>,symmetric],blast)
      qed
      also have "\<dots> = real n * ln n"
        using n by (auto simp add: divide_simps)
      finally show ?thesis by blast
    qed
    finally show ?thesis by blast
  next
    case False
    from 24 have "sqrt n * norm (sum \<chi> {1..x}) \<le> (\<Sum>k= 1..<n. norm (f (int k)))"
      by blast
    also have "\<dots> = (\<Sum>k= 1..n-1. norm (f (int k)))"
      by (intro sum.cong) auto
    also have "\<dots> \<le> 2 * (\<Sum>k = 1..(n - 1) div 2. norm (f (int k)))"
      using 25(1)[OF False] by blast
    also have "\<dots> \<le> real n * (\<Sum>k = 1..(n - 1) div 2. 1 / k)"
    proof -
      have "(\<Sum>k = 1..(n - 1) div 2. norm (f (int k))) \<le> (\<Sum>k = 1..(n - 1) div 2. real n div (2*k))"
      proof (rule sum_mono)
        fix k
        assume "k \<in> {1..(n - 1) div 2}"
        then have "1 \<le> int k" "int k \<le> n div 2" by auto
        show "norm (f (int k)) \<le> real n / (2*k)"
          using ineq[OF \<open>1 \<le> int k\<close> \<open>int k \<le> n div 2\<close>] by auto
      qed
      also have "\<dots> = (\<Sum>k = 1..(n - 1) div 2. (n / 2) * (1 / k))"
        by (rule sum.cong,auto)
      also have "\<dots> = (n / 2) * (\<Sum>k = 1..(n - 1) div 2. 1 / k)"
        using sum_distrib_left[symmetric] by fast
      finally have "(\<Sum>k = 1..(n - 1) div 2. norm (f (int k))) \<le>
                  (real n div 2) * (\<Sum>k = 1..(n - 1) div 2. 1 / k)"
        by blast
      then show ?thesis by argo
    qed
    also have "\<dots> = real n * harm ((n - 1) div 2)"
      unfolding harm_def inverse_eq_divide by simp
    also have "\<dots> < n * ln n"
    proof -
      have "n > 2" using n \<open>odd n\<close> by presburger
      then have "(n-1) div 2 > 0" by auto
      then have "harm ((n - 1) div 2) < ln (real (2 * ((n - 1) div 2) + 1))"
        using harm_less_ln by blast
      also have "\<dots> = ln (real n)" using \<open>odd n\<close> by simp
      finally show ?thesis using n by simp
    qed
    finally show ?thesis by blast
  qed

  then have 1: "sqrt n * norm (sum \<chi> {1..x}) < n * ln n"
    by blast
  show  "norm (sum \<chi> {1..x}) < sqrt n * ln n"
  proof -
    have 2: "norm (sum \<chi> {1..x}) * sqrt n < n * ln n"
      using 1 by argo
    have "sqrt n > 0" using n by simp
    have 3: "(n * ln n) / sqrt n = sqrt n * ln n"
      using n by (simp add: field_simps)
    show "norm (sum \<chi> {1..x}) < sqrt n * ln n"
      using mult_imp_less_div_pos[OF \<open>sqrt n > 0\<close> 2] 3 by argo
  qed
qed

subsection \<open>General case\<close>

text \<open>
  We now first prove the inequality for the general case in terms of the divisor function:
\<close>
theorem (in dcharacter) polya_vinogradov_inequality_explicit:
  assumes nonprincipal: "\<chi> \<noteq> principal_dchar n"
  shows   "norm (sum \<chi> {1..x}) < sqrt conductor * ln conductor * divisor_count (n div conductor)"
proof -
  write primitive_extension ("\<Phi>")
  write conductor ("c")
  interpret \<Phi>: primitive_dchar c "residue_mult_group c" primitive_extension
    using primitive_primitive_extension nonprincipal by metis

  have *: "k \<le> x div b \<longleftrightarrow> b * k \<le> x" if "b > 0" for b k
    by (metis that antisym_conv div_le_mono div_mult_self1_is_m
              less_imp_le not_less times_div_less_eq_dividend)
  have **: "a > 0" if "a dvd n" for a
    using n that by (auto intro!: Nat.gr0I)

  from nonprincipal have "(\<Sum>m=1..x. \<chi> m) = (\<Sum>m | m \<in> {1..x} \<and> coprime m n. \<Phi> m)"
    by (intro sum.mono_neutral_cong_right) (auto simp: eq_zero_iff principal_decomposition)
  also have "\<dots> = (\<Sum>m=1..x. \<Phi> m * (\<Sum>d | d dvd gcd m n. moebius_mu d))"
    by (subst sum_moebius_mu_divisors', intro sum.mono_neutral_cong_left)
       (auto simp: coprime_iff_gcd_eq_1 simp del: coprime_imp_gcd_eq_1)
  also have "\<dots> = (\<Sum>m=1..x. \<Sum>d | d dvd gcd m n. \<Phi> m * moebius_mu d)"
    by (simp add: sum_distrib_left)
  also have "\<dots> = (\<Sum>m=1..x. \<Sum>d | d dvd m \<and> d dvd n. \<Phi> m * moebius_mu d)"
    by (intro sum.cong) auto
  also have "\<dots> = (\<Sum>(m, d)\<in>(SIGMA m:{1..x}. {d. d dvd m \<and> d dvd n}). \<Phi> m * moebius_mu d)"
    using n by (subst sum.Sigma) auto
  also have "\<dots> = (\<Sum>(d, q)\<in>(SIGMA d:{d. d dvd n}. {1..x div d}). moebius_mu d * \<Phi> (d * q))"
    by (intro sum.reindex_bij_witness[of _ "\<lambda>(d,q). (d * q, d)" "\<lambda>(m,d). (d, m div d)"])
       (auto simp: * ** Suc_le_eq)
  also have "\<dots> = (\<Sum>d | d dvd n. moebius_mu d * \<Phi> d * (\<Sum>q=1..x div d. \<Phi> q))"
    using n by (subst sum.Sigma [symmetric]) (auto simp: sum_distrib_left mult.assoc)
  finally have eq: "(\<Sum>m=1..x. \<chi> m) = \<dots>" .

  have "norm (\<Sum>m=1..x. \<chi> m) \<le>
          (\<Sum>d | d dvd n. norm (moebius_mu d * \<Phi> d) * norm (\<Sum>q=1..x div d. \<Phi> q))"
    unfolding eq by (intro sum_norm_le) (simp add: norm_mult)
  also have "\<dots> < (\<Sum>d | d dvd n. norm (moebius_mu d * \<Phi> d) * (sqrt c * ln c))"
    (is "sum ?lhs _ < sum ?rhs _")
  proof (rule sum_strict_mono_ex1)
    show "\<forall>d\<in>{d. d dvd n}. ?lhs d \<le> ?rhs d"
      by (intro ballI mult_left_mono less_imp_le[OF \<Phi>.polya_vinogradov_inequality_primitive]) auto
    show "\<exists>d\<in>{d. d dvd n}. ?lhs d < ?rhs d"
      by (intro bexI[of _ 1] mult_strict_left_mono \<Phi>.polya_vinogradov_inequality_primitive) auto
  qed (use n in auto)
  also have "\<dots> = sqrt c * ln c * (\<Sum>d | d dvd n. norm (moebius_mu d * \<Phi> d))"
    by (simp add: sum_distrib_left sum_distrib_right mult_ac)
  also have "(\<Sum>d | d dvd n. norm (moebius_mu d * \<Phi> d)) =
               (\<Sum>d | d dvd n \<and> squarefree d \<and> coprime d c. 1)"
    using n by (intro sum.mono_neutral_cong_right)
               (auto simp: moebius_mu_def \<Phi>.eq_zero_iff norm_mult norm_power \<Phi>.norm)
  also have "\<dots> = card {d. d dvd n \<and> squarefree d \<and> coprime d c}"
    by simp
  also have "card {d. d dvd n \<and> squarefree d \<and> coprime d c} \<le> card {d. d dvd (n div c)}"
  proof (intro card_mono; safe?)
    show "finite {d. d dvd (n div c)}"
      using dvd_div_eq_0_iff[of c n] n conductor_dvd by (intro finite_divisors_nat) auto
  next
    fix d assume d: "d dvd n" "squarefree d" "coprime d c"
    hence "d > 0" by (intro Nat.gr0I) auto
    show "d dvd (n div c)"
    proof (rule multiplicity_le_imp_dvd)
      fix p :: nat assume p: "prime p"
      show "multiplicity p d \<le> multiplicity p (n div c)"
      proof (cases "p dvd d")
        assume "p dvd d"
        with d \<open>d > 0\<close> p have "multiplicity p d = 1"
          by (auto simp: squarefree_factorial_semiring' in_prime_factors_iff)
        moreover have "p dvd (n div c)"
        proof -
          have "p dvd c * (n div c)"
            using \<open>p dvd d\<close> \<open>d dvd n\<close> conductor_dvd by auto
          moreover have "\<not>(p dvd c)"
            using d p \<open>p dvd d\<close> coprime_common_divisor not_prime_unit by blast
          ultimately show "p dvd (n div c)"
            using p prime_dvd_mult_iff by blast
        qed
        hence "multiplicity p (n div c) \<ge> 1"
          using n p conductor_dvd dvd_div_eq_0_iff[of c n]
          by (intro multiplicity_geI) (auto intro: Nat.gr0I)
        ultimately show ?thesis by simp
      qed (auto simp: not_dvd_imp_multiplicity_0)
    qed (use \<open>d > 0\<close> in simp_all)
  qed
  also have "card {d. d dvd (n div c)} = divisor_count (n div c)"
    by (simp add: divisor_count_def)
  finally show "norm (sum \<chi> {1..x}) < sqrt c * ln c * divisor_count (n div c)"
    using conductor_gr_0 by (simp add: mult_left_mono)
qed

(* TODO: Move? *)
text \<open>
  Next, we obtain a suitable upper bound on the number of divisors of \<open>n\<close>:
\<close>
lemma divisor_count_upper_bound_aux:
  fixes n :: nat
  shows "divisor_count n \<le> 2 * card {d. d dvd n \<and> d \<le> sqrt n}"
proof (cases "n = 0")
  case False
  hence n: "n > 0" by simp
  have *: "x > 0" if "x dvd n" for x
    using that n by (auto intro!: Nat.gr0I)
  have **: "real n = sqrt (real n) * sqrt (real n)"
    by simp
  have ***: "n < x * sqrt n \<longleftrightarrow> sqrt n < x" "x * sqrt n < n \<longleftrightarrow> x < sqrt n" for x
    by (metis ** n of_nat_0_less_iff real_mult_less_iff1 real_sqrt_gt_0_iff)+

  have "divisor_count n = card {d. d dvd n}"
    by (simp add: divisor_count_def)
  also have "{d. d dvd n} = {d. d dvd n \<and> d \<le> sqrt n} \<union> {d. d dvd n \<and> d > sqrt n}"
    by auto
  also have "card \<dots> = card {d. d dvd n \<and> d \<le> sqrt n} + card {d. d dvd n \<and> d > sqrt n}"
    using n by (subst card_Un_disjoint) auto
  also have "bij_betw (\<lambda>d. n div d) {d. d dvd n \<and> d > sqrt n} {d. d dvd n \<and> d < sqrt n}"
    using n by (intro bij_betwI[of _ _ _ "\<lambda>d. n div d"])
               (auto simp: Real.real_of_nat_div real_sqrt_divide field_simps * ***)
  hence "card {d. d dvd n \<and> d > sqrt n} = card {d. d dvd n \<and> d < sqrt n}"
    by (rule bij_betw_same_card)
  also have "\<dots> \<le> card {d. d dvd n \<and> d \<le> sqrt n}"
    using n by (intro card_mono) auto
  finally show "divisor_count n \<le> 2 * \<dots>" by simp
qed auto

lemma divisor_count_upper_bound:
  fixes n :: nat
  shows "divisor_count n \<le> 2 * nat \<lfloor>sqrt n\<rfloor>"
proof (cases "n = 0")
  case False
  have "divisor_count n \<le> 2 * card {d. d dvd n \<and> d \<le> sqrt n}"
    by (rule divisor_count_upper_bound_aux)
  also have "card {d. d dvd n \<and> d \<le> sqrt n} \<le> card {1..nat \<lfloor>sqrt n\<rfloor>}"
    using False by (intro card_mono) (auto simp: le_nat_iff le_floor_iff Suc_le_eq intro!: Nat.gr0I)
  also have "\<dots> = nat \<lfloor>sqrt n\<rfloor>" by simp
  finally show ?thesis by simp
qed auto

lemma divisor_count_upper_bound':
  fixes n :: nat
  shows "real (divisor_count n) \<le> 2 * sqrt n"
proof -
  have "real (divisor_count n) \<le> 2 * real (nat \<lfloor>sqrt n\<rfloor>)"
    using divisor_count_upper_bound[of n] by linarith
  also have "\<dots> \<le> 2 * sqrt n"
    by simp
  finally show ?thesis .
qed
(* END TODO *)

text \<open>
  We are now ready to prove the `regular' Pólya--Vinogradov inequality.

  Apostol formulates it in the following way (Theorem 13.15, notation adapted):
  `If \<open>\<chi>\<close> is any nonprincipal character mod \<open>n\<close>, then for all \<open>x \<ge> 2\<close> we have
  $\sum_{m\leq x} \chi(m) = O(\sqrt{n}\log n)$.'

  The precondition \<open>x \<ge> 2\<close> here is completely unnecessary. The `Big-O' notation is somewhat
  problematic since it does not make explicit in what way the variables are quantified
  (in particular the \<open>x\<close> and the \<open>\<chi>\<close>). The statement of the theorem in this way (for a fixed
  character \<open>\<chi>\<close>) seems to suggest that \<open>n\<close> is fixed here, which would make the use of `Big-O'
  completely vacuous, since it is an asymptotic statement about \<open>n\<close>.

  We therefore decided to formulate the inequality in the following more explicit way,
  even giving an explicit constant factor:
\<close>
theorem (in dcharacter) polya_vinogradov_inequality:
  assumes nonprincipal: "\<chi> \<noteq> principal_dchar n"
  shows   "norm (\<Sum>m=1..x. \<chi> m) < 2 * sqrt n * ln n"
proof -
  have "n div conductor > 0"
    using n conductor_dvd dvd_div_eq_0_iff[of conductor n] by auto
  have "norm (\<Sum>m=1..x. \<chi> m) < sqrt conductor * ln conductor * divisor_count (n div conductor)"
    using nonprincipal by (rule polya_vinogradov_inequality_explicit)
  also have "\<dots> \<le> sqrt conductor * ln conductor * (2 * sqrt (n div conductor))"
    using conductor_gr_0 \<open>n div conductor > 0\<close>
    by (intro mult_left_mono divisor_count_upper_bound') (auto simp: Suc_le_eq)
  also have "sqrt (n div conductor) = sqrt n / sqrt conductor"
    using conductor_dvd by (simp add: Real.real_of_nat_div real_sqrt_divide)
  also have "sqrt conductor * ln conductor * (2 * (sqrt n / sqrt conductor)) =
               2 * sqrt n * ln conductor"
    using conductor_gr_0 n by (simp add: algebra_simps)
  also have "\<dots> \<le> 2 * sqrt n * ln n"
    using conductor_le_modulus conductor_gr_0 by (intro mult_left_mono) auto
  finally show ?thesis .
qed

unbundle vec_lambda_notation

end


theory Lehmer
imports
  Main
  "HOL-Number_Theory.Residues"
begin

section \<open>Lehmer's Theorem\<close>
text_raw \<open>\label{sec:lehmer}\<close>

text \<open>
  In this section we prove Lehmer's Theorem~\cite{lehmer1927fermat_converse} and its converse.
  These two theorems characterize a necessary and complete criterion for primality. This criterion
  is the basis of the Lucas-Lehmer primality test and the primality certificates of
  Pratt~\cite{pratt1975certificate}.
\<close>

lemma mod_1_coprime_nat:
  "coprime a b" if "0 < n" "[a ^ n = 1] (mod b)" for a b :: nat
proof -
  from that coprime_1_left have "coprime (a ^ n) b"
    using cong_imp_coprime cong_sym by blast
  with \<open>0 < n\<close> show ?thesis
    by simp
qed

text \<open>
  This is a weak variant of Lehmer's theorem: All numbers less then @{term "p - 1 :: nat"}
  must be considered.
\<close>

lemma lehmers_weak_theorem:
  assumes "2 \<le> p"
  assumes min_cong1: "\<And>x. 0 < x \<Longrightarrow> x < p - 1 \<Longrightarrow> [a ^ x \<noteq> 1] (mod p)"
  assumes cong1: "[a ^ (p - 1) = 1] (mod p)"
  shows "prime p"
proof (rule totient_imp_prime)
  from \<open>2 \<le> p\<close> cong1 have "coprime a p"
    by (intro mod_1_coprime_nat[of "p - 1"]) auto
  then have "[a ^ totient p = 1] (mod p)"
    by (intro euler_theorem) auto
  then have "totient p \<ge> p - 1 \<or> totient p = 0"
    using min_cong1[of "totient p"] by fastforce
  moreover have "totient p > 0"
    using \<open>2 \<le> p\<close> by simp
  moreover from \<open>p \<ge> 2\<close> have "totient p < p" by (intro totient_less) auto
  ultimately show "totient p = p - 1" by presburger
qed (insert \<open>p \<ge> 2\<close>, auto)

lemma prime_factors_elem:
  fixes n :: nat assumes "1 < n" shows "\<exists>p. p \<in> prime_factors n"
  using assms by (cases "prime n") (auto simp: prime_factors_dvd prime_factor_nat)

lemma cong_pow_1_nat:
  "[a ^ x = 1] (mod b)" if "[a = 1] (mod b)" for a b :: nat
  using cong_pow [of a 1 b x] that by simp

lemma cong_gcd_eq_1_nat:
  fixes a b :: nat
  assumes "0 < m" and cong_props: "[a ^ m = 1] (mod b)" "[a ^ n = 1] (mod b)"
  shows "[a ^ gcd m n = 1] (mod b)"
proof -
  obtain c d where gcd: "m * c = n * d + gcd m n" using bezout_nat[of m n] \<open>0 < m\<close>
    by auto
  have cong_m: "[a ^ (m * c) = 1] (mod b)" and cong_n: "[a ^ (n * d) = 1] (mod b)"
    using cong_props by (simp_all only: cong_pow_1_nat power_mult)
  have "[1 * a ^ gcd m n = a ^ (n * d) * a ^ gcd m n] (mod b)"
    by (rule cong_scalar_right, rule cong_sym) (fact cong_n)
  also have "[a ^ (n * d) * a ^ gcd m n = a ^ (m * c)] (mod b)"
    using gcd by (simp add: power_add)
  also have "[a ^ (m * c) = 1] (mod b)" using cong_m by simp
  finally show "[a ^ gcd m n = 1] (mod b)" by simp
qed

lemma One_leq_div:
  "1 < b div a" if "a dvd b" "a < b" for a b :: nat
  using that by (metis dvd_div_mult_self mult.left_neutral mult_less_cancel2)

theorem lehmers_theorem:
  assumes "2 \<le> p"
  assumes pf_notcong1: "\<And>x. x \<in> prime_factors (p - 1) \<Longrightarrow> [a ^ ((p - 1) div x) \<noteq> 1] (mod p)"
  assumes cong1: "[a ^ (p - 1) = 1] (mod p)"
  shows "prime p"
proof cases
  assume "[a = 1] (mod p)" with \<open>2 \<le>p\<close> pf_notcong1 show ?thesis
    by (metis cong_pow_1_nat less_diff_conv linorder_neqE_nat linorder_not_less
      one_add_one prime_factors_elem two_is_prime_nat)
next
  assume A_notcong_1: "[a \<noteq> 1] (mod p)"
  { fix x assume "0 < x" "x < p - 1"
    have "[a ^ x \<noteq> 1] (mod p)"
    proof
      assume "[a ^ x = 1] (mod p)"
      then have gcd_cong_1: "[a ^ gcd x (p - 1) = 1] (mod p)"
        by (rule cong_gcd_eq_1_nat[OF \<open>0 < x\<close> _ cong1])

      have "gcd x (p - 1) = p - 1"
      proof (rule ccontr)
        assume "\<not>?thesis"
        then have gcd_p1: "gcd x (p - 1) dvd (p - 1)" "gcd x (p - 1) < p - 1"
          using gcd_le2_nat[of "p - 1" x] \<open>2 \<le> p\<close> by (simp, linarith)

        define c where "c = (p - 1) div (gcd x (p - 1))"
        then have p_1_eq: "p - 1 = gcd x (p - 1) * c" unfolding c_def using gcd_p1
          by (metis dvd_mult_div_cancel)

        from gcd_p1 have "1 < c" unfolding c_def by (rule One_leq_div)
        then obtain q where q_pf: "q \<in> prime_factors c"
          using prime_factors_elem by auto
        then have "q dvd c" by auto

        have "q \<in> prime_factors (p - 1)" using q_pf \<open>1 < c\<close> \<open>2 \<le> p\<close>
          by (subst p_1_eq) (simp add: prime_factors_product)
        moreover
        have "[a ^ ((p - 1) div q) = 1] (mod p)"
          by (subst p_1_eq,subst dvd_div_mult_self[OF \<open>q dvd c\<close>,symmetric])
             (simp del: One_nat_def add: power_mult gcd_cong_1 cong_pow_1_nat)
        ultimately
        show False using pf_notcong1 by metis
      qed
      then show False using \<open>x < p - 1\<close>
        by (metis \<open>0 < x\<close> gcd_le1_nat gr_implies_not0 linorder_not_less)
    qed
  }
  with lehmers_weak_theorem[OF \<open>2 \<le> p\<close> _ cong1] show ?thesis by metis
qed

text \<open>
  The converse of Lehmer's theorem is also true.
\<close>

lemma converse_lehmer_weak:
 assumes prime_p: "prime p"
 shows "\<exists> a. [a^(p - 1) = 1] (mod p) \<and> (\<forall> x . 0 < x \<longrightarrow> x \<le> p - 2 \<longrightarrow> [a^x \<noteq> 1] (mod p))
             \<and> a > 0 \<and> a < p"
 proof -
   have "p \<ge> 2" by (rule prime_ge_2_nat[OF prime_p])
   obtain a where a:"a \<in> {1 .. p - 1} \<and> {1 .. p - 1} = {a^i mod p | i . i \<in> UNIV}"
    using residue_prime_mult_group_has_gen[OF prime_p] by blast
  {
   { fix x::nat assume x:"0 < x \<and> x \<le> p - 2 \<and> [a^x = 1] (mod p)"
     have "{a^i mod p| i. i \<in> UNIV} = {a^i mod p | i. 0 < i \<and> i \<le> x}"
     proof
      show "{a ^ i mod p | i. 0 < i \<and> i \<le> x} \<subseteq> {a ^ i mod p | i. i \<in> UNIV}" by blast
      { fix y assume y:"y \<in> {a^i mod p| i . i \<in> UNIV}"
        then obtain i where i:"y = a^i mod p" by auto
        define q r where "q = i div x" and "r = i mod x"
        have "i = q*x + r" by (simp add: r_def q_def)
        hence y_q_r:"y = (((a ^ (q*x)) mod p) * ((a ^ r) mod p)) mod p"
          by (simp add: i power_add mod_mult_eq)
        have "a ^ (q*x) mod p = (a ^ x mod p) ^ q mod p"
          by (simp add: power_mod mult.commute power_mult[symmetric])
        then have y_r:"y = a ^ r mod p" using \<open>p\<ge>2\<close> x
          by (simp add: cong_def y_q_r)
        have "y \<in> {a ^ i mod p | i. 0 < i \<and> i \<le> x}"
        proof (cases)
          assume "r = 0"
          then have "y = a^x mod p" using \<open>p\<ge>2\<close> x
            by (simp add: cong_def y_r)
          thus ?thesis using x by blast
        next
          assume "r \<noteq> 0"
          thus ?thesis using x by (auto simp add: y_r r_def)
        qed
      }
      thus " {a ^ i mod p|i. i \<in> UNIV} \<subseteq> {a ^ i mod p | i. 0 < i \<and> i \<le> x}" by auto
    qed
    note X = this

    have "p - 1 = card {1 .. p - 1}" by auto
    also have "{1 .. p - 1} = {a^i mod p | i. 1 \<le> i \<and> i \<le> x}" using X a by auto
    also have "\<dots> = (\<lambda> i. a^i mod p) ` {1..x}" by auto
    also have "card \<dots> \<le> p - 2"
      using Finite_Set.card_image_le[of "{1..x}" "\<lambda> i. a^i mod p"] x by auto
    finally have False using \<open>2 \<le> p\<close> by arith
   }
   hence "\<forall> x . 0 < x \<longrightarrow> x \<le> p - 2 \<longrightarrow> [a^x \<noteq> 1] (mod p)" by auto
 } note a_is_gen = this
 {
    assume "a>1"
    have "\<not> p dvd a "
    proof (rule ccontr)
      assume "\<not> \<not> p dvd a"
      hence "p dvd a" by auto
      have "p \<le> a" using dvd_nat_bounds[OF _ \<open>p dvd a\<close>] a by simp
      thus False using \<open>a>1\<close> a by force
    qed
    hence "coprime a p"
      using prime_imp_coprime_nat [OF prime_p] by (simp add: ac_simps)
    then have "[a ^ totient p = 1] (mod p)"
      by (rule euler_theorem)
    also from prime_p have "totient p = p - 1"
      by (rule totient_prime)
    finally have "[a ^ (p - 1) = 1] (mod p)" .
  }
  hence "[a^(p - 1) = 1] (mod p)" using a by fastforce
  thus ?thesis using a_is_gen a by auto
 qed

theorem converse_lehmer:
 assumes prime_p:"prime(p)"
 shows "\<exists> a . [a^(p - 1) = 1] (mod p) \<and>
              (\<forall> q. q \<in> prime_factors (p - 1) \<longrightarrow> [a^((p - 1) div q) \<noteq> 1] (mod p))
              \<and> a > 0 \<and> a < p"
 proof -
  have "p \<ge> 2" by (rule prime_ge_2_nat[OF prime_p])
  obtain a where a:"[a^(p - 1) = 1] (mod p) \<and> (\<forall> x . 0 < x \<longrightarrow> x \<le> p - 2 \<longrightarrow> [a^x \<noteq> 1] (mod p))
                    \<and> a > 0 \<and> a < p"
    using converse_lehmer_weak[OF prime_p] by blast
  { fix q assume q:"q \<in> prime_factors (p - 1)"
    hence "0 < q \<and> q \<le> p - 1" using \<open>p\<ge>2\<close>
      by (auto simp add: dvd_nat_bounds prime_factors_gt_0_nat)
    hence "(p - 1) div q \<ge> 1" using div_le_mono[of "q" "p - 1" q] div_self[of q] by simp
    have "q \<ge> 2" using q by (auto intro: prime_ge_2_nat)
    hence "(p - 1) div q < p - 1" using \<open>p \<ge> 2\<close> by simp
    hence "[a^((p - 1) div q) \<noteq> 1] (mod p)" using a \<open>(p - 1) div q \<ge> 1\<close>
      by (auto simp add: Suc_diff_Suc less_eq_Suc_le)
  }
  thus ?thesis using a by auto
 qed

end


(*  Title:      ZF/OrderArith.thy
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
    Copyright   1994  University of Cambridge
*)

section{*Combining Orderings: Foundations of Ordinal Arithmetic*}

theory OrderArith imports Order Sum Ordinal begin

definition
  (*disjoint sum of two relations; underlies ordinal addition*)
  radd    :: "[i,i,i,i]=>i"  where
    "radd(A,r,B,s) ==
                {z: (A+B) * (A+B).
                    (\<exists>x y. z = <Inl(x), Inr(y)>)   |
                    (\<exists>x' x. z = <Inl(x'), Inl(x)> & <x',x>:r)   |
                    (\<exists>y' y. z = <Inr(y'), Inr(y)> & <y',y>:s)}"

definition
  (*lexicographic product of two relations; underlies ordinal multiplication*)
  rmult   :: "[i,i,i,i]=>i"  where
    "rmult(A,r,B,s) ==
                {z: (A*B) * (A*B).
                    \<exists>x' y' x y. z = <<x',y'>, <x,y>> &
                       (<x',x>: r | (x'=x & <y',y>: s))}"

definition
  (*inverse image of a relation*)
  rvimage :: "[i,i,i]=>i"  where
    "rvimage(A,f,r) == {z \<in> A*A. \<exists>x y. z = <x,y> & <f`x,f`y>: r}"

definition
  measure :: "[i, i\<Rightarrow>i] \<Rightarrow> i"  where
    "measure(A,f) == {<x,y>: A*A. f(x) < f(y)}"

subsection{*Addition of Relations -- Disjoint Sum*}

subsubsection{*Rewrite rules.  Can be used to obtain introduction rules*}

lemma radd_Inl_Inr_iff [iff]:
    "<Inl(a), Inr(b)> \<in> radd(A,r,B,s)  \<longleftrightarrow>  a \<in> A & b \<in> B"
by (unfold radd_def, blast)

lemma radd_Inl_iff [iff]:
    "<Inl(a'), Inl(a)> \<in> radd(A,r,B,s)  \<longleftrightarrow>  a':A & a \<in> A & <a',a>:r"
by (unfold radd_def, blast)

lemma radd_Inr_iff [iff]:
    "<Inr(b'), Inr(b)> \<in> radd(A,r,B,s) \<longleftrightarrow>  b':B & b \<in> B & <b',b>:s"
by (unfold radd_def, blast)

lemma radd_Inr_Inl_iff [simp]:
    "<Inr(b), Inl(a)> \<in> radd(A,r,B,s) \<longleftrightarrow> False"
by (unfold radd_def, blast)

declare radd_Inr_Inl_iff [THEN iffD1, dest!]

subsubsection{*Elimination Rule*}

lemma raddE:
    "[| <p',p> \<in> radd(A,r,B,s);
        !!x y. [| p'=Inl(x); x \<in> A; p=Inr(y); y \<in> B |] ==> Q;
        !!x' x. [| p'=Inl(x'); p=Inl(x); <x',x>: r; x':A; x \<in> A |] ==> Q;
        !!y' y. [| p'=Inr(y'); p=Inr(y); <y',y>: s; y':B; y \<in> B |] ==> Q
     |] ==> Q"
by (unfold radd_def, blast)

subsubsection{*Type checking*}

lemma radd_type: "radd(A,r,B,s) \<subseteq> (A+B) * (A+B)"
apply (unfold radd_def)
apply (rule Collect_subset)
done

lemmas field_radd = radd_type [THEN field_rel_subset]

subsubsection{*Linearity*}

lemma linear_radd:
    "[| linear(A,r);  linear(B,s) |] ==> linear(A+B,radd(A,r,B,s))"
by (unfold linear_def, blast)

subsubsection{*Well-foundedness*}

lemma wf_on_radd: "[| wf[A](r);  wf[B](s) |] ==> wf[A+B](radd(A,r,B,s))"
apply (rule wf_onI2)
apply (subgoal_tac "\<forall>x\<in>A. Inl (x) \<in> Ba")
 --{*Proving the lemma, which is needed twice!*}
 prefer 2
 apply (erule_tac V = "y \<in> A + B" in thin_rl)
 apply (rule_tac ballI)
 apply (erule_tac r = r and a = x in wf_on_induct, assumption)
 apply blast
txt{*Returning to main part of proof*}
apply safe
apply blast
apply (erule_tac r = s and a = ya in wf_on_induct, assumption, blast)
done

lemma wf_radd: "[| wf(r);  wf(s) |] ==> wf(radd(field(r),r,field(s),s))"
apply (simp add: wf_iff_wf_on_field)
apply (rule wf_on_subset_A [OF _ field_radd])
apply (blast intro: wf_on_radd)
done

lemma well_ord_radd:
     "[| well_ord(A,r);  well_ord(B,s) |] ==> well_ord(A+B, radd(A,r,B,s))"
apply (rule well_ordI)
apply (simp add: well_ord_def wf_on_radd)
apply (simp add: well_ord_def tot_ord_def linear_radd)
done

subsubsection{*An @{term ord_iso} congruence law*}

lemma sum_bij:
     "[| f \<in> bij(A,C);  g \<in> bij(B,D) |]
      ==> (\<lambda>z\<in>A+B. case(%x. Inl(f`x), %y. Inr(g`y), z)) \<in> bij(A+B, C+D)"
apply (rule_tac d = "case (%x. Inl (converse(f)`x), %y. Inr(converse(g)`y))"
       in lam_bijective)
apply (typecheck add: bij_is_inj inj_is_fun)
apply (auto simp add: left_inverse_bij right_inverse_bij)
done

lemma sum_ord_iso_cong:
    "[| f \<in> ord_iso(A,r,A',r');  g \<in> ord_iso(B,s,B',s') |] ==>
            (\<lambda>z\<in>A+B. case(%x. Inl(f`x), %y. Inr(g`y), z))
            \<in> ord_iso(A+B, radd(A,r,B,s), A'+B', radd(A',r',B',s'))"
apply (unfold ord_iso_def)
apply (safe intro!: sum_bij)
(*Do the beta-reductions now*)
apply (auto cong add: conj_cong simp add: bij_is_fun [THEN apply_type])
done

(*Could we prove an ord_iso result?  Perhaps
     ord_iso(A+B, radd(A,r,B,s), A \<union> B, r \<union> s) *)
lemma sum_disjoint_bij: "A \<inter> B = 0 ==>
            (\<lambda>z\<in>A+B. case(%x. x, %y. y, z)) \<in> bij(A+B, A \<union> B)"
apply (rule_tac d = "%z. if z \<in> A then Inl (z) else Inr (z) " in lam_bijective)
apply auto
done

subsubsection{*Associativity*}

lemma sum_assoc_bij:
     "(\<lambda>z\<in>(A+B)+C. case(case(Inl, %y. Inr(Inl(y))), %y. Inr(Inr(y)), z))
      \<in> bij((A+B)+C, A+(B+C))"
apply (rule_tac d = "case (%x. Inl (Inl (x)), case (%x. Inl (Inr (x)), Inr))"
       in lam_bijective)
apply auto
done

lemma sum_assoc_ord_iso:
     "(\<lambda>z\<in>(A+B)+C. case(case(Inl, %y. Inr(Inl(y))), %y. Inr(Inr(y)), z))
      \<in> ord_iso((A+B)+C, radd(A+B, radd(A,r,B,s), C, t),
                A+(B+C), radd(A, r, B+C, radd(B,s,C,t)))"
by (rule sum_assoc_bij [THEN ord_isoI], auto)

subsection{*Multiplication of Relations -- Lexicographic Product*}

subsubsection{*Rewrite rule.  Can be used to obtain introduction rules*}

lemma  rmult_iff [iff]:
    "<<a',b'>, <a,b>> \<in> rmult(A,r,B,s) \<longleftrightarrow>
            (<a',a>: r  & a':A & a \<in> A & b': B & b \<in> B) |
            (<b',b>: s  & a'=a & a \<in> A & b': B & b \<in> B)"

by (unfold rmult_def, blast)

lemma rmultE:
    "[| <<a',b'>, <a,b>> \<in> rmult(A,r,B,s);
        [| <a',a>: r;  a':A;  a \<in> A;  b':B;  b \<in> B |] ==> Q;
        [| <b',b>: s;  a \<in> A;  a'=a;  b':B;  b \<in> B |] ==> Q
     |] ==> Q"
by blast

subsubsection{*Type checking*}

lemma rmult_type: "rmult(A,r,B,s) \<subseteq> (A*B) * (A*B)"
by (unfold rmult_def, rule Collect_subset)

lemmas field_rmult = rmult_type [THEN field_rel_subset]

subsubsection{*Linearity*}

lemma linear_rmult:
    "[| linear(A,r);  linear(B,s) |] ==> linear(A*B,rmult(A,r,B,s))"
by (simp add: linear_def, blast)

subsubsection{*Well-foundedness*}

lemma wf_on_rmult: "[| wf[A](r);  wf[B](s) |] ==> wf[A*B](rmult(A,r,B,s))"
apply (rule wf_onI2)
apply (erule SigmaE)
apply (erule ssubst)
apply (subgoal_tac "\<forall>b\<in>B. <x,b>: Ba", blast)
apply (erule_tac a = x in wf_on_induct, assumption)
apply (rule ballI)
apply (erule_tac a = b in wf_on_induct, assumption)
apply (best elim!: rmultE bspec [THEN mp])
done

lemma wf_rmult: "[| wf(r);  wf(s) |] ==> wf(rmult(field(r),r,field(s),s))"
apply (simp add: wf_iff_wf_on_field)
apply (rule wf_on_subset_A [OF _ field_rmult])
apply (blast intro: wf_on_rmult)
done

lemma well_ord_rmult:
     "[| well_ord(A,r);  well_ord(B,s) |] ==> well_ord(A*B, rmult(A,r,B,s))"
apply (rule well_ordI)
apply (simp add: well_ord_def wf_on_rmult)
apply (simp add: well_ord_def tot_ord_def linear_rmult)
done

subsubsection{*An @{term ord_iso} congruence law*}

lemma prod_bij:
     "[| f \<in> bij(A,C);  g \<in> bij(B,D) |]
      ==> (lam <x,y>:A*B. <f`x, g`y>) \<in> bij(A*B, C*D)"
apply (rule_tac d = "%<x,y>. <converse (f) `x, converse (g) `y>"
       in lam_bijective)
apply (typecheck add: bij_is_inj inj_is_fun)
apply (auto simp add: left_inverse_bij right_inverse_bij)
done

lemma prod_ord_iso_cong:
    "[| f \<in> ord_iso(A,r,A',r');  g \<in> ord_iso(B,s,B',s') |]
     ==> (lam <x,y>:A*B. <f`x, g`y>)
         \<in> ord_iso(A*B, rmult(A,r,B,s), A'*B', rmult(A',r',B',s'))"
apply (unfold ord_iso_def)
apply (safe intro!: prod_bij)
apply (simp_all add: bij_is_fun [THEN apply_type])
apply (blast intro: bij_is_inj [THEN inj_apply_equality])
done

lemma singleton_prod_bij: "(\<lambda>z\<in>A. <x,z>) \<in> bij(A, {x}*A)"
by (rule_tac d = snd in lam_bijective, auto)

(*Used??*)
lemma singleton_prod_ord_iso:
     "well_ord({x},xr) ==>
          (\<lambda>z\<in>A. <x,z>) \<in> ord_iso(A, r, {x}*A, rmult({x}, xr, A, r))"
apply (rule singleton_prod_bij [THEN ord_isoI])
apply (simp (no_asm_simp))
apply (blast dest: well_ord_is_wf [THEN wf_on_not_refl])
done

(*Here we build a complicated function term, then simplify it using
  case_cong, id_conv, comp_lam, case_case.*)
lemma prod_sum_singleton_bij:
     "a\<notin>C ==>
       (\<lambda>x\<in>C*B + D. case(%x. x, %y.<a,y>, x))
       \<in> bij(C*B + D, C*B \<union> {a}*D)"
apply (rule subst_elem)
apply (rule id_bij [THEN sum_bij, THEN comp_bij])
apply (rule singleton_prod_bij)
apply (rule sum_disjoint_bij, blast)
apply (simp (no_asm_simp) cong add: case_cong)
apply (rule comp_lam [THEN trans, symmetric])
apply (fast elim!: case_type)
apply (simp (no_asm_simp) add: case_case)
done

lemma prod_sum_singleton_ord_iso:
 "[| a \<in> A;  well_ord(A,r) |] ==>
    (\<lambda>x\<in>pred(A,a,r)*B + pred(B,b,s). case(%x. x, %y.<a,y>, x))
    \<in> ord_iso(pred(A,a,r)*B + pred(B,b,s),
                  radd(A*B, rmult(A,r,B,s), B, s),
              pred(A,a,r)*B \<union> {a}*pred(B,b,s), rmult(A,r,B,s))"
apply (rule prod_sum_singleton_bij [THEN ord_isoI])
apply (simp (no_asm_simp) add: pred_iff well_ord_is_wf [THEN wf_on_not_refl])
apply (auto elim!: well_ord_is_wf [THEN wf_on_asym] predE)
done

subsubsection{*Distributive law*}

lemma sum_prod_distrib_bij:
     "(lam <x,z>:(A+B)*C. case(%y. Inl(<y,z>), %y. Inr(<y,z>), x))
      \<in> bij((A+B)*C, (A*C)+(B*C))"
by (rule_tac d = "case (%<x,y>.<Inl (x),y>, %<x,y>.<Inr (x),y>) "
    in lam_bijective, auto)

lemma sum_prod_distrib_ord_iso:
 "(lam <x,z>:(A+B)*C. case(%y. Inl(<y,z>), %y. Inr(<y,z>), x))
  \<in> ord_iso((A+B)*C, rmult(A+B, radd(A,r,B,s), C, t),
            (A*C)+(B*C), radd(A*C, rmult(A,r,C,t), B*C, rmult(B,s,C,t)))"
by (rule sum_prod_distrib_bij [THEN ord_isoI], auto)

subsubsection{*Associativity*}

lemma prod_assoc_bij:
     "(lam <<x,y>, z>:(A*B)*C. <x,<y,z>>) \<in> bij((A*B)*C, A*(B*C))"
by (rule_tac d = "%<x, <y,z>>. <<x,y>, z>" in lam_bijective, auto)

lemma prod_assoc_ord_iso:
 "(lam <<x,y>, z>:(A*B)*C. <x,<y,z>>)
  \<in> ord_iso((A*B)*C, rmult(A*B, rmult(A,r,B,s), C, t),
            A*(B*C), rmult(A, r, B*C, rmult(B,s,C,t)))"
by (rule prod_assoc_bij [THEN ord_isoI], auto)

subsection{*Inverse Image of a Relation*}

subsubsection{*Rewrite rule*}

lemma rvimage_iff: "<a,b> \<in> rvimage(A,f,r)  \<longleftrightarrow>  <f`a,f`b>: r & a \<in> A & b \<in> A"
by (unfold rvimage_def, blast)

subsubsection{*Type checking*}

lemma rvimage_type: "rvimage(A,f,r) \<subseteq> A*A"
by (unfold rvimage_def, rule Collect_subset)

lemmas field_rvimage = rvimage_type [THEN field_rel_subset]

lemma rvimage_converse: "rvimage(A,f, converse(r)) = converse(rvimage(A,f,r))"
by (unfold rvimage_def, blast)

subsubsection{*Partial Ordering Properties*}

lemma irrefl_rvimage:
    "[| f \<in> inj(A,B);  irrefl(B,r) |] ==> irrefl(A, rvimage(A,f,r))"
apply (unfold irrefl_def rvimage_def)
apply (blast intro: inj_is_fun [THEN apply_type])
done

lemma trans_on_rvimage:
    "[| f \<in> inj(A,B);  trans[B](r) |] ==> trans[A](rvimage(A,f,r))"
apply (unfold trans_on_def rvimage_def)
apply (blast intro: inj_is_fun [THEN apply_type])
done

lemma part_ord_rvimage:
    "[| f \<in> inj(A,B);  part_ord(B,r) |] ==> part_ord(A, rvimage(A,f,r))"
apply (unfold part_ord_def)
apply (blast intro!: irrefl_rvimage trans_on_rvimage)
done

subsubsection{*Linearity*}

lemma linear_rvimage:
    "[| f \<in> inj(A,B);  linear(B,r) |] ==> linear(A,rvimage(A,f,r))"
apply (simp add: inj_def linear_def rvimage_iff)
apply (blast intro: apply_funtype)
done

lemma tot_ord_rvimage:
    "[| f \<in> inj(A,B);  tot_ord(B,r) |] ==> tot_ord(A, rvimage(A,f,r))"
apply (unfold tot_ord_def)
apply (blast intro!: part_ord_rvimage linear_rvimage)
done

subsubsection{*Well-foundedness*}

lemma wf_rvimage [intro!]: "wf(r) ==> wf(rvimage(A,f,r))"
apply (simp (no_asm_use) add: rvimage_def wf_eq_minimal)
apply clarify
apply (subgoal_tac "\<exists>w. w \<in> {w: {f`x. x \<in> Q}. \<exists>x. x \<in> Q & (f`x = w) }")
 apply (erule allE)
 apply (erule impE)
 apply assumption
 apply blast
apply blast
done

text{*But note that the combination of @{text wf_imp_wf_on} and
 @{text wf_rvimage} gives @{prop "wf(r) ==> wf[C](rvimage(A,f,r))"}*}
lemma wf_on_rvimage: "[| f \<in> A->B;  wf[B](r) |] ==> wf[A](rvimage(A,f,r))"
apply (rule wf_onI2)
apply (subgoal_tac "\<forall>z\<in>A. f`z=f`y \<longrightarrow> z \<in> Ba")
 apply blast
apply (erule_tac a = "f`y" in wf_on_induct)
 apply (blast intro!: apply_funtype)
apply (blast intro!: apply_funtype dest!: rvimage_iff [THEN iffD1])
done

(*Note that we need only wf[A](...) and linear(A,...) to get the result!*)
lemma well_ord_rvimage:
     "[| f \<in> inj(A,B);  well_ord(B,r) |] ==> well_ord(A, rvimage(A,f,r))"
apply (rule well_ordI)
apply (unfold well_ord_def tot_ord_def)
apply (blast intro!: wf_on_rvimage inj_is_fun)
apply (blast intro!: linear_rvimage)
done

lemma ord_iso_rvimage:
    "f \<in> bij(A,B) ==> f \<in> ord_iso(A, rvimage(A,f,s), B, s)"
apply (unfold ord_iso_def)
apply (simp add: rvimage_iff)
done

lemma ord_iso_rvimage_eq:
    "f \<in> ord_iso(A,r, B,s) ==> rvimage(A,f,s) = r \<inter> A*A"
by (unfold ord_iso_def rvimage_def, blast)

subsection{*Every well-founded relation is a subset of some inverse image of
      an ordinal*}

lemma wf_rvimage_Ord: "Ord(i) \<Longrightarrow> wf(rvimage(A, f, Memrel(i)))"
by (blast intro: wf_rvimage wf_Memrel)

definition
  wfrank :: "[i,i]=>i"  where
    "wfrank(r,a) == wfrec(r, a, %x f. \<Union>y \<in> r-``{x}. succ(f`y))"

definition
  wftype :: "i=>i"  where
    "wftype(r) == \<Union>y \<in> range(r). succ(wfrank(r,y))"

lemma wfrank: "wf(r) ==> wfrank(r,a) = (\<Union>y \<in> r-``{a}. succ(wfrank(r,y)))"
by (subst wfrank_def [THEN def_wfrec], simp_all)

lemma Ord_wfrank: "wf(r) ==> Ord(wfrank(r,a))"
apply (rule_tac a=a in wf_induct, assumption)
apply (subst wfrank, assumption)
apply (rule Ord_succ [THEN Ord_UN], blast)
done

lemma wfrank_lt: "[|wf(r); <a,b> \<in> r|] ==> wfrank(r,a) < wfrank(r,b)"
apply (rule_tac a1 = b in wfrank [THEN ssubst], assumption)
apply (rule UN_I [THEN ltI])
apply (simp add: Ord_wfrank vimage_iff)+
done

lemma Ord_wftype: "wf(r) ==> Ord(wftype(r))"
by (simp add: wftype_def Ord_wfrank)

lemma wftypeI: "\<lbrakk>wf(r);  x \<in> field(r)\<rbrakk> \<Longrightarrow> wfrank(r,x) \<in> wftype(r)"
apply (simp add: wftype_def)
apply (blast intro: wfrank_lt [THEN ltD])
done

lemma wf_imp_subset_rvimage:
     "[|wf(r); r \<subseteq> A*A|] ==> \<exists>i f. Ord(i) & r \<subseteq> rvimage(A, f, Memrel(i))"
apply (rule_tac x="wftype(r)" in exI)
apply (rule_tac x="\<lambda>x\<in>A. wfrank(r,x)" in exI)
apply (simp add: Ord_wftype, clarify)
apply (frule subsetD, assumption, clarify)
apply (simp add: rvimage_iff wfrank_lt [THEN ltD])
apply (blast intro: wftypeI)
done

theorem wf_iff_subset_rvimage:
  "relation(r) ==> wf(r) \<longleftrightarrow> (\<exists>i f A. Ord(i) & r \<subseteq> rvimage(A, f, Memrel(i)))"
by (blast dest!: relation_field_times_field wf_imp_subset_rvimage
          intro: wf_rvimage_Ord [THEN wf_subset])

subsection{*Other Results*}

lemma wf_times: "A \<inter> B = 0 ==> wf(A*B)"
by (simp add: wf_def, blast)

text{*Could also be used to prove @{text wf_radd}*}
lemma wf_Un:
     "[| range(r) \<inter> domain(s) = 0; wf(r);  wf(s) |] ==> wf(r \<union> s)"
apply (simp add: wf_def, clarify)
apply (rule equalityI)
 prefer 2 apply blast
apply clarify
apply (drule_tac x=Z in spec)
apply (drule_tac x="Z \<inter> domain(s)" in spec)
apply simp
apply (blast intro: elim: equalityE)
done

subsubsection{*The Empty Relation*}

lemma wf0: "wf(0)"
by (simp add: wf_def, blast)

lemma linear0: "linear(0,0)"
by (simp add: linear_def)

lemma well_ord0: "well_ord(0,0)"
by (blast intro: wf_imp_wf_on well_ordI wf0 linear0)

subsubsection{*The "measure" relation is useful with wfrec*}

lemma measure_eq_rvimage_Memrel:
     "measure(A,f) = rvimage(A,Lambda(A,f),Memrel(Collect(RepFun(A,f),Ord)))"
apply (simp (no_asm) add: measure_def rvimage_def Memrel_iff)
apply (rule equalityI, auto)
apply (auto intro: Ord_in_Ord simp add: lt_def)
done

lemma wf_measure [iff]: "wf(measure(A,f))"
by (simp (no_asm) add: measure_eq_rvimage_Memrel wf_Memrel wf_rvimage)

lemma measure_iff [iff]: "<x,y> \<in> measure(A,f) \<longleftrightarrow> x \<in> A & y \<in> A & f(x)<f(y)"
by (simp (no_asm) add: measure_def)

lemma linear_measure:
 assumes Ordf: "!!x. x \<in> A ==> Ord(f(x))"
     and inj:  "!!x y. [|x \<in> A; y \<in> A; f(x) = f(y) |] ==> x=y"
 shows "linear(A, measure(A,f))"
apply (auto simp add: linear_def)
apply (rule_tac i="f(x)" and j="f(y)" in Ord_linear_lt)
    apply (simp_all add: Ordf)
apply (blast intro: inj)
done

lemma wf_on_measure: "wf[B](measure(A,f))"
by (rule wf_imp_wf_on [OF wf_measure])

lemma well_ord_measure:
 assumes Ordf: "!!x. x \<in> A ==> Ord(f(x))"
     and inj:  "!!x y. [|x \<in> A; y \<in> A; f(x) = f(y) |] ==> x=y"
 shows "well_ord(A, measure(A,f))"
apply (rule well_ordI)
apply (rule wf_on_measure)
apply (blast intro: linear_measure Ordf inj)
done

lemma measure_type: "measure(A,f) \<subseteq> A*A"
by (auto simp add: measure_def)

subsubsection{*Well-foundedness of Unions*}

lemma wf_on_Union:
 assumes wfA: "wf[A](r)"
     and wfB: "!!a. a\<in>A ==> wf[B(a)](s)"
     and ok: "!!a u v. [|<u,v> \<in> s; v \<in> B(a); a \<in> A|]
                       ==> (\<exists>a'\<in>A. <a',a> \<in> r & u \<in> B(a')) | u \<in> B(a)"
 shows "wf[\<Union>a\<in>A. B(a)](s)"
apply (rule wf_onI2)
apply (erule UN_E)
apply (subgoal_tac "\<forall>z \<in> B(a). z \<in> Ba", blast)
apply (rule_tac a = a in wf_on_induct [OF wfA], assumption)
apply (rule ballI)
apply (rule_tac a = z in wf_on_induct [OF wfB], assumption, assumption)
apply (rename_tac u)
apply (drule_tac x=u in bspec, blast)
apply (erule mp, clarify)
apply (frule ok, assumption+, blast)
done

subsubsection{*Bijections involving Powersets*}

lemma Pow_sum_bij:
    "(\<lambda>Z \<in> Pow(A+B). <{x \<in> A. Inl(x) \<in> Z}, {y \<in> B. Inr(y) \<in> Z}>)
     \<in> bij(Pow(A+B), Pow(A)*Pow(B))"
apply (rule_tac d = "%<X,Y>. {Inl (x). x \<in> X} \<union> {Inr (y). y \<in> Y}"
       in lam_bijective)
apply force+
done

text{*As a special case, we have @{term "bij(Pow(A*B), A -> Pow(B))"} *}
lemma Pow_Sigma_bij:
    "(\<lambda>r \<in> Pow(Sigma(A,B)). \<lambda>x \<in> A. r``{x})
     \<in> bij(Pow(Sigma(A,B)), \<Pi> x \<in> A. Pow(B(x)))"
apply (rule_tac d = "%f. \<Union>x \<in> A. \<Union>y \<in> f`x. {<x,y>}" in lam_bijective)
apply (blast intro: lam_type)
apply (blast dest: apply_type, simp_all)
apply fast (*strange, but blast can't do it*)
apply (rule fun_extension, auto)
by blast

end