src/HOL/Library/Discrete.thy
author haftmann
Sun, 17 Feb 2013 22:56:54 +0100
changeset 51174 071674018df9
child 51263 31e786e0e6a7
permissions -rw-r--r--
fundamentals about discrete logarithm and square root
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51174
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)  
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     2
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     3
header {* Common discrete functions *}
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     4
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     5
theory Discrete
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     6
imports Main
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     7
begin
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     8
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
     9
lemma power2_nat_le_eq_le:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    10
  fixes m n :: nat
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    11
  shows "m ^ 2 \<le> n ^ 2 \<longleftrightarrow> m \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    12
  by (auto intro: power2_le_imp_le power_mono)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    13
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    14
subsection {* Discrete logarithm *}
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    15
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    16
fun log :: "nat \<Rightarrow> nat" where
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    17
  [simp del]: "log n = (if n < 2 then 0 else Suc (log (n div 2)))"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    18
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    19
lemma log_zero [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    20
  "log 0 = 0"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    21
  by (simp add: log.simps)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    22
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    23
lemma log_one [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    24
  "log 1 = 0"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    25
  by (simp add: log.simps)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    26
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    27
lemma log_Suc_zero [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    28
  "log (Suc 0) = 0"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    29
  using log_one by simp
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    30
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    31
lemma log_rec:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    32
  "n \<ge> 2 \<Longrightarrow> log n = Suc (log (n div 2))"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    33
  by (simp add: log.simps)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    34
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    35
lemma log_twice [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    36
  "n \<noteq> 0 \<Longrightarrow> log (2 * n) = Suc (log n)"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    37
  by (simp add: log_rec)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    38
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    39
lemma log_half [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    40
  "log (n div 2) = log n - 1"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    41
proof (cases "n < 2")
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    42
  case True
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    43
  then have "n = 0 \<or> n = 1" by arith
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    44
  then show ?thesis by (auto simp del: One_nat_def)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    45
next
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    46
  case False then show ?thesis by (simp add: log_rec)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    47
qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    48
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    49
lemma log_exp [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    50
  "log (2 ^ n) = n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    51
  by (induct n) simp_all
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    52
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    53
lemma log_mono:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    54
  "mono log"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    55
proof
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    56
  fix m n :: nat
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    57
  assume "m \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    58
  then show "log m \<le> log n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    59
  proof (induct m arbitrary: n rule: log.induct)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    60
    case (1 m)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    61
    then have mn2: "m div 2 \<le> n div 2" by arith
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    62
    show "log m \<le> log n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    63
    proof (cases "m < 2")
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    64
      case True
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    65
      then have "m = 0 \<or> m = 1" by arith
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    66
      then show ?thesis by (auto simp del: One_nat_def)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    67
    next
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    68
      case False
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    69
      with mn2 have "m \<ge> 2" and "n \<ge> 2" by auto arith
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    70
      from False have m2_0: "m div 2 \<noteq> 0" by arith
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    71
      with mn2 have n2_0: "n div 2 \<noteq> 0" by arith
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    72
      from False "1.hyps" mn2 have "log (m div 2) \<le> log (n div 2)" by blast
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    73
      with m2_0 n2_0 have "log (2 * (m div 2)) \<le> log (2 * (n div 2))" by simp
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    74
      with m2_0 n2_0 `m \<ge> 2` `n \<ge> 2` show ?thesis by (simp only: log_rec [of m] log_rec [of n]) simp
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    75
    qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    76
  qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    77
qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    78
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    79
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    80
subsection {* Discrete square root *}
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    81
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    82
definition sqrt :: "nat \<Rightarrow> nat"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    83
where
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    84
  "sqrt n = Max {m. m ^ 2 \<le> n}"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    85
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    86
lemma sqrt_inverse_power2 [simp]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    87
  "sqrt (n ^ 2) = n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    88
proof -
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    89
  have "{m. m \<le> n} \<noteq> {}" by auto
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    90
  then have "Max {m. m \<le> n} \<le> n" by auto
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    91
  then show ?thesis
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    92
    by (auto simp add: sqrt_def power2_nat_le_eq_le intro: antisym)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    93
qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    94
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    95
lemma [code]:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    96
  "sqrt n = Max (Set.filter (\<lambda>m. m ^ 2 \<le> n) {0..n})"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    97
proof -
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    98
  { fix m
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
    99
    assume "m\<twosuperior> \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   100
    then have "m \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   101
      by (cases m) (simp_all add: power2_eq_square)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   102
  }
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   103
  then have "{m. m \<le> n \<and> m\<twosuperior> \<le> n} = {m. m\<twosuperior> \<le> n}" by auto
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   104
  then show ?thesis by (simp add: sqrt_def Set.filter_def)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   105
qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   106
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   107
lemma sqrt_le:
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   108
  "sqrt n \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   109
proof -
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   110
  have "0\<twosuperior> \<le> n" by simp
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   111
  then have *: "{m. m\<twosuperior> \<le> n} \<noteq> {}" by blast
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   112
  { fix m
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   113
    assume "m\<twosuperior> \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   114
    then have "m \<le> n"
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   115
      by (cases m) (simp_all add: power2_eq_square)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   116
  } note ** = this
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   117
  then have "{m. m\<twosuperior> \<le> n} \<subseteq> {m. m \<le> n}" by auto
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   118
  then have "finite {m. m\<twosuperior> \<le> n}" by (rule finite_subset) rule
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   119
  with * show ?thesis by (auto simp add: sqrt_def intro: **)
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   120
qed
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   121
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   122
hide_const (open) log sqrt
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   123
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   124
end
071674018df9 fundamentals about discrete logarithm and square root
haftmann
parents:
diff changeset
   125