src/HOL/Library/Discrete.thy
author wenzelm
Sat Jul 16 12:11:02 2016 +0200 (2016-07-16)
changeset 63516 76492eaf3dc1
parent 62390 842917225d56
child 63540 f8652d0534fa
permissions -rw-r--r--
tuned proofs;
wenzelm@59349
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@51174
     2
wenzelm@60500
     3
section \<open>Common discrete functions\<close>
haftmann@51174
     4
haftmann@51174
     5
theory Discrete
haftmann@51174
     6
imports Main
haftmann@51174
     7
begin
haftmann@51174
     8
wenzelm@60500
     9
subsection \<open>Discrete logarithm\<close>
haftmann@51174
    10
wenzelm@61115
    11
context
wenzelm@61115
    12
begin
wenzelm@61115
    13
wenzelm@61115
    14
qualified fun log :: "nat \<Rightarrow> nat"
wenzelm@59349
    15
  where [simp del]: "log n = (if n < 2 then 0 else Suc (log (n div 2)))"
haftmann@51174
    16
haftmann@61831
    17
lemma log_induct [consumes 1, case_names one double]:
haftmann@61831
    18
  fixes n :: nat
haftmann@61831
    19
  assumes "n > 0"
haftmann@61831
    20
  assumes one: "P 1"
haftmann@61831
    21
  assumes double: "\<And>n. n \<ge> 2 \<Longrightarrow> P (n div 2) \<Longrightarrow> P n"
haftmann@61831
    22
  shows "P n"
wenzelm@61975
    23
using \<open>n > 0\<close> proof (induct n rule: log.induct)
haftmann@61831
    24
  fix n
haftmann@61831
    25
  assume "\<not> n < 2 \<Longrightarrow>
haftmann@61831
    26
          0 < n div 2 \<Longrightarrow> P (n div 2)"
haftmann@61831
    27
  then have *: "n \<ge> 2 \<Longrightarrow> P (n div 2)" by simp
haftmann@61831
    28
  assume "n > 0"
haftmann@61831
    29
  show "P n"
haftmann@61831
    30
  proof (cases "n = 1")
haftmann@61831
    31
    case True with one show ?thesis by simp
haftmann@61831
    32
  next
wenzelm@61975
    33
    case False with \<open>n > 0\<close> have "n \<ge> 2" by auto
haftmann@61831
    34
    moreover with * have "P (n div 2)" .
haftmann@61831
    35
    ultimately show ?thesis by (rule double)
haftmann@61831
    36
  qed
haftmann@61831
    37
qed
haftmann@61831
    38
  
wenzelm@59349
    39
lemma log_zero [simp]: "log 0 = 0"
haftmann@51174
    40
  by (simp add: log.simps)
haftmann@51174
    41
wenzelm@59349
    42
lemma log_one [simp]: "log 1 = 0"
haftmann@51174
    43
  by (simp add: log.simps)
haftmann@51174
    44
wenzelm@59349
    45
lemma log_Suc_zero [simp]: "log (Suc 0) = 0"
haftmann@51174
    46
  using log_one by simp
haftmann@51174
    47
wenzelm@59349
    48
lemma log_rec: "n \<ge> 2 \<Longrightarrow> log n = Suc (log (n div 2))"
haftmann@51174
    49
  by (simp add: log.simps)
haftmann@51174
    50
wenzelm@59349
    51
lemma log_twice [simp]: "n \<noteq> 0 \<Longrightarrow> log (2 * n) = Suc (log n)"
haftmann@51174
    52
  by (simp add: log_rec)
haftmann@51174
    53
wenzelm@59349
    54
lemma log_half [simp]: "log (n div 2) = log n - 1"
haftmann@51174
    55
proof (cases "n < 2")
haftmann@51174
    56
  case True
haftmann@51174
    57
  then have "n = 0 \<or> n = 1" by arith
haftmann@51174
    58
  then show ?thesis by (auto simp del: One_nat_def)
haftmann@51174
    59
next
wenzelm@59349
    60
  case False
wenzelm@59349
    61
  then show ?thesis by (simp add: log_rec)
haftmann@51174
    62
qed
haftmann@51174
    63
wenzelm@59349
    64
lemma log_exp [simp]: "log (2 ^ n) = n"
haftmann@51174
    65
  by (induct n) simp_all
haftmann@51174
    66
wenzelm@59349
    67
lemma log_mono: "mono log"
haftmann@51174
    68
proof
haftmann@51174
    69
  fix m n :: nat
haftmann@51174
    70
  assume "m \<le> n"
haftmann@51174
    71
  then show "log m \<le> log n"
haftmann@51174
    72
  proof (induct m arbitrary: n rule: log.induct)
haftmann@51174
    73
    case (1 m)
haftmann@51174
    74
    then have mn2: "m div 2 \<le> n div 2" by arith
haftmann@51174
    75
    show "log m \<le> log n"
haftmann@61831
    76
    proof (cases "m \<ge> 2")
haftmann@61831
    77
      case False
haftmann@51174
    78
      then have "m = 0 \<or> m = 1" by arith
haftmann@51174
    79
      then show ?thesis by (auto simp del: One_nat_def)
haftmann@51174
    80
    next
haftmann@61831
    81
      case True then have "\<not> m < 2" by simp
haftmann@61831
    82
      with mn2 have "n \<ge> 2" by arith
haftmann@61831
    83
      from True have m2_0: "m div 2 \<noteq> 0" by arith
haftmann@51174
    84
      with mn2 have n2_0: "n div 2 \<noteq> 0" by arith
wenzelm@61975
    85
      from \<open>\<not> m < 2\<close> "1.hyps" mn2 have "log (m div 2) \<le> log (n div 2)" by blast
haftmann@51174
    86
      with m2_0 n2_0 have "log (2 * (m div 2)) \<le> log (2 * (n div 2))" by simp
wenzelm@60500
    87
      with m2_0 n2_0 \<open>m \<ge> 2\<close> \<open>n \<ge> 2\<close> show ?thesis by (simp only: log_rec [of m] log_rec [of n]) simp
haftmann@51174
    88
    qed
haftmann@51174
    89
  qed
haftmann@51174
    90
qed
haftmann@51174
    91
haftmann@61831
    92
lemma log_exp2_le:
haftmann@61831
    93
  assumes "n > 0"
haftmann@61831
    94
  shows "2 ^ log n \<le> n"
wenzelm@63516
    95
  using assms
wenzelm@63516
    96
proof (induct n rule: log_induct)
wenzelm@63516
    97
  case one
wenzelm@63516
    98
  then show ?case by simp
haftmann@61831
    99
next
wenzelm@63516
   100
  case (double n)
haftmann@61831
   101
  with log_mono have "log n \<ge> Suc 0"
haftmann@61831
   102
    by (simp add: log.simps)
haftmann@61831
   103
  assume "2 ^ log (n div 2) \<le> n div 2"
wenzelm@61975
   104
  with \<open>n \<ge> 2\<close> have "2 ^ (log n - Suc 0) \<le> n div 2" by simp
haftmann@61831
   105
  then have "2 ^ (log n - Suc 0) * 2 ^ 1 \<le> n div 2 * 2" by simp
wenzelm@61975
   106
  with \<open>log n \<ge> Suc 0\<close> have "2 ^ log n \<le> n div 2 * 2"
haftmann@61831
   107
    unfolding power_add [symmetric] by simp
haftmann@61831
   108
  also have "n div 2 * 2 \<le> n" by (cases "even n") simp_all
wenzelm@63516
   109
  finally show ?case .
haftmann@61831
   110
qed
haftmann@61831
   111
haftmann@51174
   112
wenzelm@60500
   113
subsection \<open>Discrete square root\<close>
haftmann@51174
   114
wenzelm@61115
   115
qualified definition sqrt :: "nat \<Rightarrow> nat"
wenzelm@59349
   116
  where "sqrt n = Max {m. m\<^sup>2 \<le> n}"
haftmann@51263
   117
haftmann@51263
   118
lemma sqrt_aux:
haftmann@51263
   119
  fixes n :: nat
wenzelm@53015
   120
  shows "finite {m. m\<^sup>2 \<le> n}" and "{m. m\<^sup>2 \<le> n} \<noteq> {}"
haftmann@51263
   121
proof -
haftmann@51263
   122
  { fix m
wenzelm@53015
   123
    assume "m\<^sup>2 \<le> n"
haftmann@51263
   124
    then have "m \<le> n"
haftmann@51263
   125
      by (cases m) (simp_all add: power2_eq_square)
haftmann@51263
   126
  } note ** = this
wenzelm@53015
   127
  then have "{m. m\<^sup>2 \<le> n} \<subseteq> {m. m \<le> n}" by auto
wenzelm@53015
   128
  then show "finite {m. m\<^sup>2 \<le> n}" by (rule finite_subset) rule
wenzelm@53015
   129
  have "0\<^sup>2 \<le> n" by simp
wenzelm@53015
   130
  then show *: "{m. m\<^sup>2 \<le> n} \<noteq> {}" by blast
haftmann@51263
   131
qed
haftmann@51263
   132
wenzelm@59349
   133
lemma [code]: "sqrt n = Max (Set.filter (\<lambda>m. m\<^sup>2 \<le> n) {0..n})"
haftmann@51263
   134
proof -
wenzelm@53015
   135
  from power2_nat_le_imp_le [of _ n] have "{m. m \<le> n \<and> m\<^sup>2 \<le> n} = {m. m\<^sup>2 \<le> n}" by auto
haftmann@51263
   136
  then show ?thesis by (simp add: sqrt_def Set.filter_def)
haftmann@51263
   137
qed
haftmann@51174
   138
wenzelm@59349
   139
lemma sqrt_inverse_power2 [simp]: "sqrt (n\<^sup>2) = n"
haftmann@51174
   140
proof -
haftmann@51174
   141
  have "{m. m \<le> n} \<noteq> {}" by auto
haftmann@51174
   142
  then have "Max {m. m \<le> n} \<le> n" by auto
haftmann@51174
   143
  then show ?thesis
haftmann@51174
   144
    by (auto simp add: sqrt_def power2_nat_le_eq_le intro: antisym)
haftmann@51174
   145
qed
haftmann@51174
   146
wenzelm@59349
   147
lemma sqrt_zero [simp]: "sqrt 0 = 0"
haftmann@58787
   148
  using sqrt_inverse_power2 [of 0] by simp
haftmann@58787
   149
wenzelm@59349
   150
lemma sqrt_one [simp]: "sqrt 1 = 1"
haftmann@58787
   151
  using sqrt_inverse_power2 [of 1] by simp
haftmann@58787
   152
wenzelm@59349
   153
lemma mono_sqrt: "mono sqrt"
haftmann@51263
   154
proof
haftmann@51263
   155
  fix m n :: nat
haftmann@51263
   156
  have *: "0 * 0 \<le> m" by simp
haftmann@51263
   157
  assume "m \<le> n"
haftmann@51263
   158
  then show "sqrt m \<le> sqrt n"
wenzelm@60500
   159
    by (auto intro!: Max_mono \<open>0 * 0 \<le> m\<close> finite_less_ub simp add: power2_eq_square sqrt_def)
haftmann@51263
   160
qed
haftmann@51263
   161
wenzelm@59349
   162
lemma sqrt_greater_zero_iff [simp]: "sqrt n > 0 \<longleftrightarrow> n > 0"
haftmann@51174
   163
proof -
wenzelm@53015
   164
  have *: "0 < Max {m. m\<^sup>2 \<le> n} \<longleftrightarrow> (\<exists>a\<in>{m. m\<^sup>2 \<le> n}. 0 < a)"
haftmann@51263
   165
    by (rule Max_gr_iff) (fact sqrt_aux)+
haftmann@51263
   166
  show ?thesis
haftmann@51263
   167
  proof
haftmann@51263
   168
    assume "0 < sqrt n"
wenzelm@53015
   169
    then have "0 < Max {m. m\<^sup>2 \<le> n}" by (simp add: sqrt_def)
haftmann@51263
   170
    with * show "0 < n" by (auto dest: power2_nat_le_imp_le)
haftmann@51263
   171
  next
haftmann@51263
   172
    assume "0 < n"
wenzelm@53015
   173
    then have "1\<^sup>2 \<le> n \<and> 0 < (1::nat)" by simp
wenzelm@53015
   174
    then have "\<exists>q. q\<^sup>2 \<le> n \<and> 0 < q" ..
wenzelm@53015
   175
    with * have "0 < Max {m. m\<^sup>2 \<le> n}" by blast
haftmann@51263
   176
    then show "0 < sqrt n" by  (simp add: sqrt_def)
haftmann@51263
   177
  qed
haftmann@51263
   178
qed
haftmann@51263
   179
wenzelm@59349
   180
lemma sqrt_power2_le [simp]: "(sqrt n)\<^sup>2 \<le> n" (* FIXME tune proof *)
haftmann@51263
   181
proof (cases "n > 0")
haftmann@58787
   182
  case False then show ?thesis by simp
haftmann@51263
   183
next
haftmann@51263
   184
  case True then have "sqrt n > 0" by simp
wenzelm@53015
   185
  then have "mono (times (Max {m. m\<^sup>2 \<le> n}))" by (auto intro: mono_times_nat simp add: sqrt_def)
wenzelm@53015
   186
  then have *: "Max {m. m\<^sup>2 \<le> n} * Max {m. m\<^sup>2 \<le> n} = Max (times (Max {m. m\<^sup>2 \<le> n}) ` {m. m\<^sup>2 \<le> n})"
haftmann@51263
   187
    using sqrt_aux [of n] by (rule mono_Max_commute)
haftmann@51263
   188
  have "Max (op * (Max {m. m * m \<le> n}) ` {m. m * m \<le> n}) \<le> n"
haftmann@51263
   189
    apply (subst Max_le_iff)
haftmann@51263
   190
    apply (metis (mono_tags) finite_imageI finite_less_ub le_square)
haftmann@51263
   191
    apply simp
haftmann@51263
   192
    apply (metis le0 mult_0_right)
haftmann@51263
   193
    apply auto
haftmann@51263
   194
    proof -
haftmann@51263
   195
      fix q
haftmann@51263
   196
      assume "q * q \<le> n"
haftmann@51263
   197
      show "Max {m. m * m \<le> n} * q \<le> n"
haftmann@51263
   198
      proof (cases "q > 0")
haftmann@51263
   199
        case False then show ?thesis by simp
haftmann@51263
   200
      next
haftmann@51263
   201
        case True then have "mono (times q)" by (rule mono_times_nat)
haftmann@51263
   202
        then have "q * Max {m. m * m \<le> n} = Max (times q ` {m. m * m \<le> n})"
haftmann@51263
   203
          using sqrt_aux [of n] by (auto simp add: power2_eq_square intro: mono_Max_commute)
haftmann@57514
   204
        then have "Max {m. m * m \<le> n} * q = Max (times q ` {m. m * m \<le> n})" by (simp add: ac_simps)
wenzelm@59349
   205
        then show ?thesis
wenzelm@59349
   206
          apply simp
haftmann@51263
   207
          apply (subst Max_le_iff)
haftmann@51263
   208
          apply auto
haftmann@51263
   209
          apply (metis (mono_tags) finite_imageI finite_less_ub le_square)
wenzelm@60500
   210
          apply (metis \<open>q * q \<le> n\<close>)
wenzelm@60500
   211
          apply (metis \<open>q * q \<le> n\<close> le_cases mult_le_mono1 mult_le_mono2 order_trans)
wenzelm@59349
   212
          done
haftmann@51263
   213
      qed
haftmann@51263
   214
    qed
haftmann@51263
   215
  with * show ?thesis by (simp add: sqrt_def power2_eq_square)
haftmann@51174
   216
qed
haftmann@51174
   217
wenzelm@59349
   218
lemma sqrt_le: "sqrt n \<le> n"
haftmann@51263
   219
  using sqrt_aux [of n] by (auto simp add: sqrt_def intro: power2_nat_le_imp_le)
haftmann@51174
   220
haftmann@51174
   221
end
haftmann@51174
   222
nipkow@62390
   223
end