src/HOL/Divides.thy
author nipkow
Sun Oct 21 14:53:44 2007 +0200 (2007-10-21)
changeset 25134 3d4953e88449
parent 25112 98824cc791c0
child 25162 ad4d5365d9d8
permissions -rw-r--r--
Eliminated most of the neq0_conv occurrences. As a result, many
theorems had to be rephrased with ~= 0 instead of > 0.
paulson@3366
     1
(*  Title:      HOL/Divides.thy
paulson@3366
     2
    ID:         $Id$
paulson@3366
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6865
     4
    Copyright   1999  University of Cambridge
huffman@18154
     5
*)
paulson@3366
     6
huffman@18154
     7
header {* The division operators div, mod and the divides relation "dvd" *}
paulson@3366
     8
nipkow@15131
     9
theory Divides
huffman@24268
    10
imports Power
haftmann@22993
    11
uses "~~/src/Provers/Arith/cancel_div_mod.ML"
nipkow@15131
    12
begin
paulson@3366
    13
haftmann@24993
    14
class div = times +
haftmann@25062
    15
  fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
haftmann@25062
    16
  fixes mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
haftmann@21408
    17
haftmann@22993
    18
instance nat :: Divides.div
haftmann@22993
    19
  div_def: "m div n == wfrec (pred_nat^+)
haftmann@22993
    20
                          (%f j. if j<n | n=0 then 0 else Suc (f (j-n))) m"
berghofe@22261
    21
  mod_def: "m mod n == wfrec (pred_nat^+)
haftmann@22993
    22
                          (%f j. if j<n | n=0 then j else f (j-n)) m" ..
haftmann@21408
    23
haftmann@24993
    24
definition (in div)
haftmann@25062
    25
  dvd  :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "dvd" 50)
haftmann@23684
    26
where
haftmann@25062
    27
  [code func del]: "m dvd n \<longleftrightarrow> (\<exists>k. n = m * k)"
haftmann@23684
    28
haftmann@24993
    29
class dvd_mod = div + zero + -- {* for code generation *}
haftmann@25062
    30
  assumes dvd_def_mod [code func]: "x dvd y \<longleftrightarrow> y mod x = 0"
paulson@6865
    31
wenzelm@22718
    32
definition
wenzelm@22718
    33
  quorem :: "(nat*nat) * (nat*nat) => bool" where
haftmann@21408
    34
  (*This definition helps prove the harder properties of div and mod.
haftmann@21408
    35
    It is copied from IntDiv.thy; should it be overloaded?*)
wenzelm@22718
    36
  "quorem = (%((a,b), (q,r)).
haftmann@21408
    37
                    a = b*q + r &
haftmann@21408
    38
                    (if 0<b then 0\<le>r & r<b else b<r & r \<le>0))"
paulson@14267
    39
paulson@14267
    40
paulson@14267
    41
paulson@14267
    42
subsection{*Initial Lemmas*}
paulson@14267
    43
wenzelm@22718
    44
lemmas wf_less_trans =
paulson@14267
    45
       def_wfrec [THEN trans, OF eq_reflection wf_pred_nat [THEN wf_trancl],
paulson@14267
    46
                  standard]
paulson@14267
    47
wenzelm@22718
    48
lemma mod_eq: "(%m. m mod n) =
berghofe@22261
    49
              wfrec (pred_nat^+) (%f j. if j<n | n=0 then j else f (j-n))"
paulson@14267
    50
by (simp add: mod_def)
paulson@14267
    51
wenzelm@22718
    52
lemma div_eq: "(%m. m div n) = wfrec (pred_nat^+)
paulson@14267
    53
               (%f j. if j<n | n=0 then 0 else Suc (f (j-n)))"
paulson@14267
    54
by (simp add: div_def)
paulson@14267
    55
paulson@14267
    56
wenzelm@22718
    57
(** Aribtrary definitions for division by zero.  Useful to simplify
paulson@14267
    58
    certain equations **)
paulson@14267
    59
paulson@14267
    60
lemma DIVISION_BY_ZERO_DIV [simp]: "a div 0 = (0::nat)"
wenzelm@22718
    61
  by (rule div_eq [THEN wf_less_trans], simp)
paulson@14267
    62
paulson@14267
    63
lemma DIVISION_BY_ZERO_MOD [simp]: "a mod 0 = (a::nat)"
wenzelm@22718
    64
  by (rule mod_eq [THEN wf_less_trans], simp)
paulson@14267
    65
paulson@14267
    66
paulson@14267
    67
subsection{*Remainder*}
paulson@14267
    68
paulson@14267
    69
lemma mod_less [simp]: "m<n ==> m mod n = (m::nat)"
wenzelm@22718
    70
  by (rule mod_eq [THEN wf_less_trans]) simp
paulson@14267
    71
paulson@14267
    72
lemma mod_geq: "~ m < (n::nat) ==> m mod n = (m-n) mod n"
wenzelm@22718
    73
  apply (cases "n=0")
wenzelm@22718
    74
   apply simp
wenzelm@22718
    75
  apply (rule mod_eq [THEN wf_less_trans])
wenzelm@22718
    76
  apply (simp add: cut_apply less_eq)
wenzelm@22718
    77
  done
paulson@14267
    78
paulson@14267
    79
(*Avoids the ugly ~m<n above*)
paulson@14267
    80
lemma le_mod_geq: "(n::nat) \<le> m ==> m mod n = (m-n) mod n"
wenzelm@22718
    81
  by (simp add: mod_geq linorder_not_less)
paulson@14267
    82
paulson@14267
    83
lemma mod_if: "m mod (n::nat) = (if m<n then m else (m-n) mod n)"
wenzelm@22718
    84
  by (simp add: mod_geq)
paulson@14267
    85
paulson@14267
    86
lemma mod_1 [simp]: "m mod Suc 0 = 0"
wenzelm@22718
    87
  by (induct m) (simp_all add: mod_geq)
paulson@14267
    88
paulson@14267
    89
lemma mod_self [simp]: "n mod n = (0::nat)"
wenzelm@22718
    90
  by (cases "n = 0") (simp_all add: mod_geq)
paulson@14267
    91
paulson@14267
    92
lemma mod_add_self2 [simp]: "(m+n) mod n = m mod (n::nat)"
wenzelm@22718
    93
  apply (subgoal_tac "(n + m) mod n = (n+m-n) mod n")
wenzelm@22718
    94
   apply (simp add: add_commute)
wenzelm@22718
    95
  apply (subst mod_geq [symmetric], simp_all)
wenzelm@22718
    96
  done
paulson@14267
    97
paulson@14267
    98
lemma mod_add_self1 [simp]: "(n+m) mod n = m mod (n::nat)"
wenzelm@22718
    99
  by (simp add: add_commute mod_add_self2)
paulson@14267
   100
paulson@14267
   101
lemma mod_mult_self1 [simp]: "(m + k*n) mod n = m mod (n::nat)"
wenzelm@22718
   102
  by (induct k) (simp_all add: add_left_commute [of _ n])
paulson@14267
   103
paulson@14267
   104
lemma mod_mult_self2 [simp]: "(m + n*k) mod n = m mod (n::nat)"
wenzelm@22718
   105
  by (simp add: mult_commute mod_mult_self1)
paulson@14267
   106
paulson@14267
   107
lemma mod_mult_distrib: "(m mod n) * (k::nat) = (m*k) mod (n*k)"
wenzelm@22718
   108
  apply (cases "n = 0", simp)
wenzelm@22718
   109
  apply (cases "k = 0", simp)
wenzelm@22718
   110
  apply (induct m rule: nat_less_induct)
wenzelm@22718
   111
  apply (subst mod_if, simp)
wenzelm@22718
   112
  apply (simp add: mod_geq diff_mult_distrib)
wenzelm@22718
   113
  done
paulson@14267
   114
paulson@14267
   115
lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
wenzelm@22718
   116
  by (simp add: mult_commute [of k] mod_mult_distrib)
paulson@14267
   117
paulson@14267
   118
lemma mod_mult_self_is_0 [simp]: "(m*n) mod n = (0::nat)"
wenzelm@22718
   119
  apply (cases "n = 0", simp)
wenzelm@22718
   120
  apply (induct m, simp)
wenzelm@22718
   121
  apply (rename_tac k)
wenzelm@22718
   122
  apply (cut_tac m = "k * n" and n = n in mod_add_self2)
wenzelm@22718
   123
  apply (simp add: add_commute)
wenzelm@22718
   124
  done
paulson@14267
   125
paulson@14267
   126
lemma mod_mult_self1_is_0 [simp]: "(n*m) mod n = (0::nat)"
wenzelm@22718
   127
  by (simp add: mult_commute mod_mult_self_is_0)
paulson@14267
   128
paulson@14267
   129
paulson@14267
   130
subsection{*Quotient*}
paulson@14267
   131
paulson@14267
   132
lemma div_less [simp]: "m<n ==> m div n = (0::nat)"
wenzelm@22718
   133
  by (rule div_eq [THEN wf_less_trans], simp)
paulson@14267
   134
paulson@14267
   135
lemma div_geq: "[| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)"
wenzelm@22718
   136
  apply (rule div_eq [THEN wf_less_trans])
wenzelm@22718
   137
  apply (simp add: cut_apply less_eq)
wenzelm@22718
   138
  done
paulson@14267
   139
paulson@14267
   140
(*Avoids the ugly ~m<n above*)
paulson@14267
   141
lemma le_div_geq: "[| 0<n;  n\<le>m |] ==> m div n = Suc((m-n) div n)"
wenzelm@22718
   142
  by (simp add: div_geq linorder_not_less)
paulson@14267
   143
paulson@14267
   144
lemma div_if: "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))"
wenzelm@22718
   145
  by (simp add: div_geq)
paulson@14267
   146
paulson@14267
   147
paulson@14267
   148
(*Main Result about quotient and remainder.*)
paulson@14267
   149
lemma mod_div_equality: "(m div n)*n + m mod n = (m::nat)"
wenzelm@22718
   150
  apply (cases "n = 0", simp)
wenzelm@22718
   151
  apply (induct m rule: nat_less_induct)
wenzelm@22718
   152
  apply (subst mod_if)
wenzelm@22718
   153
  apply (simp_all add: add_assoc div_geq add_diff_inverse)
wenzelm@22718
   154
  done
paulson@14267
   155
paulson@14267
   156
lemma mod_div_equality2: "n * (m div n) + m mod n = (m::nat)"
wenzelm@22718
   157
  apply (cut_tac m = m and n = n in mod_div_equality)
wenzelm@22718
   158
  apply (simp add: mult_commute)
wenzelm@22718
   159
  done
paulson@14267
   160
paulson@14267
   161
subsection{*Simproc for Cancelling Div and Mod*}
paulson@14267
   162
paulson@14267
   163
lemma div_mod_equality: "((m div n)*n + m mod n) + k = (m::nat) + k"
wenzelm@22718
   164
  by (simp add: mod_div_equality)
paulson@14267
   165
paulson@14267
   166
lemma div_mod_equality2: "(n*(m div n) + m mod n) + k = (m::nat) + k"
wenzelm@22718
   167
  by (simp add: mod_div_equality2)
paulson@14267
   168
paulson@14267
   169
ML
paulson@14267
   170
{*
paulson@14267
   171
structure CancelDivModData =
paulson@14267
   172
struct
paulson@14267
   173
wenzelm@22718
   174
val div_name = @{const_name Divides.div};
wenzelm@22718
   175
val mod_name = @{const_name Divides.mod};
paulson@14267
   176
val mk_binop = HOLogic.mk_binop;
paulson@14267
   177
val mk_sum = NatArithUtils.mk_sum;
paulson@14267
   178
val dest_sum = NatArithUtils.dest_sum;
paulson@14267
   179
paulson@14267
   180
(*logic*)
paulson@14267
   181
wenzelm@22718
   182
val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}]
paulson@14267
   183
paulson@14267
   184
val trans = trans
paulson@14267
   185
paulson@14267
   186
val prove_eq_sums =
wenzelm@22718
   187
  let val simps = @{thm add_0} :: @{thm add_0_right} :: @{thms add_ac}
wenzelm@17609
   188
  in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all_tac simps) end;
paulson@14267
   189
paulson@14267
   190
end;
paulson@14267
   191
paulson@14267
   192
structure CancelDivMod = CancelDivModFun(CancelDivModData);
paulson@14267
   193
paulson@14267
   194
val cancel_div_mod_proc = NatArithUtils.prep_simproc
wenzelm@20044
   195
      ("cancel_div_mod", ["(m::nat) + n"], K CancelDivMod.proc);
paulson@14267
   196
paulson@14267
   197
Addsimprocs[cancel_div_mod_proc];
paulson@14267
   198
*}
paulson@14267
   199
paulson@14267
   200
paulson@14267
   201
(* a simple rearrangement of mod_div_equality: *)
paulson@14267
   202
lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
wenzelm@22718
   203
  by (cut_tac m = m and n = n in mod_div_equality2, arith)
paulson@14267
   204
paulson@14267
   205
lemma mod_less_divisor [simp]: "0<n ==> m mod n < (n::nat)"
wenzelm@22718
   206
  apply (induct m rule: nat_less_induct)
wenzelm@22718
   207
  apply (rename_tac m)
wenzelm@22718
   208
  apply (case_tac "m<n", simp)
wenzelm@22718
   209
  txt{*case @{term "n \<le> m"}*}
wenzelm@22718
   210
  apply (simp add: mod_geq)
wenzelm@22718
   211
  done
nipkow@15439
   212
nipkow@15439
   213
lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
wenzelm@22718
   214
  apply (drule mod_less_divisor [where m = m])
wenzelm@22718
   215
  apply simp
wenzelm@22718
   216
  done
paulson@14267
   217
paulson@14267
   218
lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
wenzelm@22718
   219
  by (cut_tac m = "m*n" and n = n in mod_div_equality, auto)
paulson@14267
   220
paulson@14267
   221
lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
wenzelm@22718
   222
  by (simp add: mult_commute div_mult_self_is_m)
paulson@14267
   223
paulson@14267
   224
(*mod_mult_distrib2 above is the counterpart for remainder*)
paulson@14267
   225
paulson@14267
   226
paulson@14267
   227
subsection{*Proving facts about Quotient and Remainder*}
paulson@14267
   228
paulson@14267
   229
lemma unique_quotient_lemma:
wenzelm@22718
   230
     "[| b*q' + r'  \<le> b*q + r;  x < b;  r < b |]
paulson@14267
   231
      ==> q' \<le> (q::nat)"
wenzelm@22718
   232
  apply (rule leI)
wenzelm@22718
   233
  apply (subst less_iff_Suc_add)
wenzelm@22718
   234
  apply (auto simp add: add_mult_distrib2)
wenzelm@22718
   235
  done
paulson@14267
   236
paulson@14267
   237
lemma unique_quotient:
wenzelm@22718
   238
     "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
paulson@14267
   239
      ==> q = q'"
wenzelm@22718
   240
  apply (simp add: split_ifs quorem_def)
wenzelm@22718
   241
  apply (blast intro: order_antisym
wenzelm@22718
   242
    dest: order_eq_refl [THEN unique_quotient_lemma] sym)
wenzelm@22718
   243
  done
paulson@14267
   244
paulson@14267
   245
lemma unique_remainder:
wenzelm@22718
   246
     "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
paulson@14267
   247
      ==> r = r'"
wenzelm@22718
   248
  apply (subgoal_tac "q = q'")
wenzelm@22718
   249
   prefer 2 apply (blast intro: unique_quotient)
wenzelm@22718
   250
  apply (simp add: quorem_def)
wenzelm@22718
   251
  done
paulson@14267
   252
nipkow@25134
   253
lemma quorem_div_mod: "b \<noteq> 0 ==> quorem ((a, b), (a div b, a mod b))"
wenzelm@22718
   254
  unfolding quorem_def by simp
paulson@14267
   255
nipkow@25134
   256
lemma quorem_div: "[| quorem((a,b),(q,r));  b \<noteq> 0 |] ==> a div b = q"
wenzelm@22718
   257
  by (simp add: quorem_div_mod [THEN unique_quotient])
paulson@14267
   258
nipkow@25134
   259
lemma quorem_mod: "[| quorem((a,b),(q,r));  b \<noteq> 0 |] ==> a mod b = r"
wenzelm@22718
   260
  by (simp add: quorem_div_mod [THEN unique_remainder])
paulson@14267
   261
paulson@14267
   262
(** A dividend of zero **)
paulson@14267
   263
paulson@14267
   264
lemma div_0 [simp]: "0 div m = (0::nat)"
wenzelm@22718
   265
  by (cases "m = 0") simp_all
paulson@14267
   266
paulson@14267
   267
lemma mod_0 [simp]: "0 mod m = (0::nat)"
wenzelm@22718
   268
  by (cases "m = 0") simp_all
paulson@14267
   269
paulson@14267
   270
(** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
paulson@14267
   271
paulson@14267
   272
lemma quorem_mult1_eq:
nipkow@25134
   273
     "[| quorem((b,c),(q,r)); c \<noteq> 0 |]
paulson@14267
   274
      ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
wenzelm@22718
   275
  by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
paulson@14267
   276
paulson@14267
   277
lemma div_mult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)"
nipkow@25134
   278
apply (cases "c = 0", simp)
nipkow@25134
   279
apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_div])
nipkow@25134
   280
done
paulson@14267
   281
paulson@14267
   282
lemma mod_mult1_eq: "(a*b) mod c = a*(b mod c) mod (c::nat)"
nipkow@25134
   283
apply (cases "c = 0", simp)
nipkow@25134
   284
apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_mod])
nipkow@25134
   285
done
paulson@14267
   286
paulson@14267
   287
lemma mod_mult1_eq': "(a*b) mod (c::nat) = ((a mod c) * b) mod c"
wenzelm@22718
   288
  apply (rule trans)
wenzelm@22718
   289
   apply (rule_tac s = "b*a mod c" in trans)
wenzelm@22718
   290
    apply (rule_tac [2] mod_mult1_eq)
wenzelm@22718
   291
   apply (simp_all add: mult_commute)
wenzelm@22718
   292
  done
paulson@14267
   293
paulson@14267
   294
lemma mod_mult_distrib_mod: "(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c"
wenzelm@22718
   295
  apply (rule mod_mult1_eq' [THEN trans])
wenzelm@22718
   296
  apply (rule mod_mult1_eq)
wenzelm@22718
   297
  done
paulson@14267
   298
paulson@14267
   299
(** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
paulson@14267
   300
paulson@14267
   301
lemma quorem_add1_eq:
nipkow@25134
   302
     "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  c \<noteq> 0 |]
paulson@14267
   303
      ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
wenzelm@22718
   304
  by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
paulson@14267
   305
paulson@14267
   306
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@14267
   307
lemma div_add1_eq:
nipkow@25134
   308
  "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
nipkow@25134
   309
apply (cases "c = 0", simp)
nipkow@25134
   310
apply (blast intro: quorem_add1_eq [THEN quorem_div] quorem_div_mod)
nipkow@25134
   311
done
paulson@14267
   312
paulson@14267
   313
lemma mod_add1_eq: "(a+b) mod (c::nat) = (a mod c + b mod c) mod c"
nipkow@25134
   314
apply (cases "c = 0", simp)
nipkow@25134
   315
apply (blast intro: quorem_div_mod quorem_add1_eq [THEN quorem_mod])
nipkow@25134
   316
done
paulson@14267
   317
paulson@14267
   318
paulson@14267
   319
subsection{*Proving @{term "a div (b*c) = (a div b) div c"}*}
paulson@14267
   320
paulson@14267
   321
(** first, a lemma to bound the remainder **)
paulson@14267
   322
paulson@14267
   323
lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
wenzelm@22718
   324
  apply (cut_tac m = q and n = c in mod_less_divisor)
wenzelm@22718
   325
  apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
wenzelm@22718
   326
  apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
wenzelm@22718
   327
  apply (simp add: add_mult_distrib2)
wenzelm@22718
   328
  done
paulson@10559
   329
wenzelm@22718
   330
lemma quorem_mult2_eq: "[| quorem ((a,b), (q,r));  0 < b;  0 < c |]
paulson@14267
   331
      ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
wenzelm@22718
   332
  by (auto simp add: mult_ac quorem_def add_mult_distrib2 [symmetric] mod_lemma)
paulson@14267
   333
paulson@14267
   334
lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
wenzelm@22718
   335
  apply (cases "b = 0", simp)
wenzelm@22718
   336
  apply (cases "c = 0", simp)
wenzelm@22718
   337
  apply (force simp add: quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_div])
wenzelm@22718
   338
  done
paulson@14267
   339
paulson@14267
   340
lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
wenzelm@22718
   341
  apply (cases "b = 0", simp)
wenzelm@22718
   342
  apply (cases "c = 0", simp)
wenzelm@22718
   343
  apply (auto simp add: mult_commute quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_mod])
wenzelm@22718
   344
  done
paulson@14267
   345
paulson@14267
   346
paulson@14267
   347
subsection{*Cancellation of Common Factors in Division*}
paulson@14267
   348
paulson@14267
   349
lemma div_mult_mult_lemma:
wenzelm@22718
   350
    "[| (0::nat) < b;  0 < c |] ==> (c*a) div (c*b) = a div b"
wenzelm@22718
   351
  by (auto simp add: div_mult2_eq)
paulson@14267
   352
paulson@14267
   353
lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b"
wenzelm@22718
   354
  apply (cases "b = 0")
wenzelm@22718
   355
  apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma)
wenzelm@22718
   356
  done
paulson@14267
   357
paulson@14267
   358
lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b"
wenzelm@22718
   359
  apply (drule div_mult_mult1)
wenzelm@22718
   360
  apply (auto simp add: mult_commute)
wenzelm@22718
   361
  done
paulson@14267
   362
paulson@14267
   363
paulson@14267
   364
subsection{*Further Facts about Quotient and Remainder*}
paulson@14267
   365
paulson@14267
   366
lemma div_1 [simp]: "m div Suc 0 = m"
wenzelm@22718
   367
  by (induct m) (simp_all add: div_geq)
paulson@14267
   368
paulson@14267
   369
lemma div_self [simp]: "0<n ==> n div n = (1::nat)"
wenzelm@22718
   370
  by (simp add: div_geq)
paulson@14267
   371
paulson@14267
   372
lemma div_add_self2: "0<n ==> (m+n) div n = Suc (m div n)"
wenzelm@22718
   373
  apply (subgoal_tac "(n + m) div n = Suc ((n+m-n) div n) ")
wenzelm@22718
   374
   apply (simp add: add_commute)
wenzelm@22718
   375
  apply (subst div_geq [symmetric], simp_all)
wenzelm@22718
   376
  done
paulson@14267
   377
paulson@14267
   378
lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)"
wenzelm@22718
   379
  by (simp add: add_commute div_add_self2)
paulson@14267
   380
paulson@14267
   381
lemma div_mult_self1 [simp]: "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n"
wenzelm@22718
   382
  apply (subst div_add1_eq)
wenzelm@22718
   383
  apply (subst div_mult1_eq, simp)
wenzelm@22718
   384
  done
paulson@14267
   385
paulson@14267
   386
lemma div_mult_self2 [simp]: "0<n ==> (m + n*k) div n = k + m div (n::nat)"
wenzelm@22718
   387
  by (simp add: mult_commute div_mult_self1)
paulson@14267
   388
paulson@14267
   389
paulson@14267
   390
(* Monotonicity of div in first argument *)
paulson@14267
   391
lemma div_le_mono [rule_format (no_asm)]:
wenzelm@22718
   392
    "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
paulson@14267
   393
apply (case_tac "k=0", simp)
paulson@15251
   394
apply (induct "n" rule: nat_less_induct, clarify)
paulson@14267
   395
apply (case_tac "n<k")
paulson@14267
   396
(* 1  case n<k *)
paulson@14267
   397
apply simp
paulson@14267
   398
(* 2  case n >= k *)
paulson@14267
   399
apply (case_tac "m<k")
paulson@14267
   400
(* 2.1  case m<k *)
paulson@14267
   401
apply simp
paulson@14267
   402
(* 2.2  case m>=k *)
nipkow@15439
   403
apply (simp add: div_geq diff_le_mono)
paulson@14267
   404
done
paulson@14267
   405
paulson@14267
   406
(* Antimonotonicity of div in second argument *)
paulson@14267
   407
lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
paulson@14267
   408
apply (subgoal_tac "0<n")
wenzelm@22718
   409
 prefer 2 apply simp
paulson@15251
   410
apply (induct_tac k rule: nat_less_induct)
paulson@14267
   411
apply (rename_tac "k")
paulson@14267
   412
apply (case_tac "k<n", simp)
paulson@14267
   413
apply (subgoal_tac "~ (k<m) ")
wenzelm@22718
   414
 prefer 2 apply simp
paulson@14267
   415
apply (simp add: div_geq)
paulson@15251
   416
apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
paulson@14267
   417
 prefer 2
paulson@14267
   418
 apply (blast intro: div_le_mono diff_le_mono2)
paulson@14267
   419
apply (rule le_trans, simp)
nipkow@15439
   420
apply (simp)
paulson@14267
   421
done
paulson@14267
   422
paulson@14267
   423
lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
paulson@14267
   424
apply (case_tac "n=0", simp)
paulson@14267
   425
apply (subgoal_tac "m div n \<le> m div 1", simp)
paulson@14267
   426
apply (rule div_le_mono2)
paulson@14267
   427
apply (simp_all (no_asm_simp))
paulson@14267
   428
done
paulson@14267
   429
wenzelm@22718
   430
(* Similar for "less than" *)
paulson@17085
   431
lemma div_less_dividend [rule_format]:
paulson@14267
   432
     "!!n::nat. 1<n ==> 0 < m --> m div n < m"
paulson@15251
   433
apply (induct_tac m rule: nat_less_induct)
paulson@14267
   434
apply (rename_tac "m")
paulson@14267
   435
apply (case_tac "m<n", simp)
paulson@14267
   436
apply (subgoal_tac "0<n")
wenzelm@22718
   437
 prefer 2 apply simp
paulson@14267
   438
apply (simp add: div_geq)
paulson@14267
   439
apply (case_tac "n<m")
paulson@15251
   440
 apply (subgoal_tac "(m-n) div n < (m-n) ")
paulson@14267
   441
  apply (rule impI less_trans_Suc)+
paulson@14267
   442
apply assumption
nipkow@15439
   443
  apply (simp_all)
paulson@14267
   444
done
paulson@14267
   445
paulson@17085
   446
declare div_less_dividend [simp]
paulson@17085
   447
paulson@14267
   448
text{*A fact for the mutilated chess board*}
paulson@14267
   449
lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
paulson@14267
   450
apply (case_tac "n=0", simp)
paulson@15251
   451
apply (induct "m" rule: nat_less_induct)
paulson@14267
   452
apply (case_tac "Suc (na) <n")
paulson@14267
   453
(* case Suc(na) < n *)
paulson@14267
   454
apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
paulson@14267
   455
(* case n \<le> Suc(na) *)
paulson@16796
   456
apply (simp add: linorder_not_less le_Suc_eq mod_geq)
nipkow@15439
   457
apply (auto simp add: Suc_diff_le le_mod_geq)
paulson@14267
   458
done
paulson@14267
   459
paulson@14437
   460
lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
wenzelm@22718
   461
  by (cases "n = 0") auto
paulson@14437
   462
paulson@14437
   463
lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
wenzelm@22718
   464
  by (cases "n = 0") auto
paulson@14437
   465
paulson@14267
   466
paulson@14267
   467
subsection{*The Divides Relation*}
paulson@14267
   468
paulson@14267
   469
lemma dvdI [intro?]: "n = m * k ==> m dvd n"
wenzelm@22718
   470
  unfolding dvd_def by blast
paulson@14267
   471
paulson@14267
   472
lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
wenzelm@22718
   473
  unfolding dvd_def by blast
nipkow@13152
   474
paulson@14267
   475
lemma dvd_0_right [iff]: "m dvd (0::nat)"
wenzelm@22718
   476
  unfolding dvd_def by (blast intro: mult_0_right [symmetric])
paulson@14267
   477
paulson@14267
   478
lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
wenzelm@22718
   479
  by (force simp add: dvd_def)
paulson@14267
   480
paulson@14267
   481
lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
wenzelm@22718
   482
  by (blast intro: dvd_0_left)
paulson@14267
   483
paulson@24286
   484
declare dvd_0_left_iff [noatp]
paulson@24286
   485
paulson@14267
   486
lemma dvd_1_left [iff]: "Suc 0 dvd k"
wenzelm@22718
   487
  unfolding dvd_def by simp
paulson@14267
   488
paulson@14267
   489
lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
wenzelm@22718
   490
  by (simp add: dvd_def)
paulson@14267
   491
paulson@14267
   492
lemma dvd_refl [simp]: "m dvd (m::nat)"
wenzelm@22718
   493
  unfolding dvd_def by (blast intro: mult_1_right [symmetric])
paulson@14267
   494
paulson@14267
   495
lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
wenzelm@22718
   496
  unfolding dvd_def by (blast intro: mult_assoc)
paulson@14267
   497
paulson@14267
   498
lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
wenzelm@22718
   499
  unfolding dvd_def
wenzelm@22718
   500
  by (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
paulson@14267
   501
haftmann@23684
   502
text {* @{term "op dvd"} is a partial order *}
haftmann@23684
   503
haftmann@23684
   504
interpretation dvd: order ["op dvd" "\<lambda>n m \<Colon> nat. n dvd m \<and> m \<noteq> n"]
haftmann@23684
   505
  by unfold_locales (auto intro: dvd_trans dvd_anti_sym)
haftmann@23684
   506
paulson@14267
   507
lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
wenzelm@22718
   508
  unfolding dvd_def
wenzelm@22718
   509
  by (blast intro: add_mult_distrib2 [symmetric])
paulson@14267
   510
paulson@14267
   511
lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
wenzelm@22718
   512
  unfolding dvd_def
wenzelm@22718
   513
  by (blast intro: diff_mult_distrib2 [symmetric])
paulson@14267
   514
paulson@14267
   515
lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
wenzelm@22718
   516
  apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
wenzelm@22718
   517
  apply (blast intro: dvd_add)
wenzelm@22718
   518
  done
paulson@14267
   519
paulson@14267
   520
lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
wenzelm@22718
   521
  by (drule_tac m = m in dvd_diff, auto)
paulson@14267
   522
paulson@14267
   523
lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
wenzelm@22718
   524
  unfolding dvd_def by (blast intro: mult_left_commute)
paulson@14267
   525
paulson@14267
   526
lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
wenzelm@22718
   527
  apply (subst mult_commute)
wenzelm@22718
   528
  apply (erule dvd_mult)
wenzelm@22718
   529
  done
paulson@14267
   530
paulson@17084
   531
lemma dvd_triv_right [iff]: "k dvd (m*k :: nat)"
wenzelm@22718
   532
  by (rule dvd_refl [THEN dvd_mult])
paulson@17084
   533
paulson@17084
   534
lemma dvd_triv_left [iff]: "k dvd (k*m :: nat)"
wenzelm@22718
   535
  by (rule dvd_refl [THEN dvd_mult2])
paulson@14267
   536
paulson@14267
   537
lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
wenzelm@22718
   538
  apply (rule iffI)
wenzelm@22718
   539
   apply (erule_tac [2] dvd_add)
wenzelm@22718
   540
   apply (rule_tac [2] dvd_refl)
wenzelm@22718
   541
  apply (subgoal_tac "n = (n+k) -k")
wenzelm@22718
   542
   prefer 2 apply simp
wenzelm@22718
   543
  apply (erule ssubst)
wenzelm@22718
   544
  apply (erule dvd_diff)
wenzelm@22718
   545
  apply (rule dvd_refl)
wenzelm@22718
   546
  done
paulson@14267
   547
paulson@14267
   548
lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
wenzelm@22718
   549
  unfolding dvd_def
wenzelm@22718
   550
  apply (case_tac "n = 0", auto)
wenzelm@22718
   551
  apply (blast intro: mod_mult_distrib2 [symmetric])
wenzelm@22718
   552
  done
paulson@14267
   553
paulson@14267
   554
lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
wenzelm@22718
   555
  apply (subgoal_tac "k dvd (m div n) *n + m mod n")
wenzelm@22718
   556
   apply (simp add: mod_div_equality)
wenzelm@22718
   557
  apply (simp only: dvd_add dvd_mult)
wenzelm@22718
   558
  done
paulson@14267
   559
paulson@14267
   560
lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
wenzelm@22718
   561
  by (blast intro: dvd_mod_imp_dvd dvd_mod)
paulson@14267
   562
paulson@14267
   563
lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
wenzelm@22718
   564
  unfolding dvd_def
wenzelm@22718
   565
  apply (erule exE)
wenzelm@22718
   566
  apply (simp add: mult_ac)
wenzelm@22718
   567
  done
paulson@14267
   568
paulson@14267
   569
lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
wenzelm@22718
   570
  apply auto
wenzelm@22718
   571
   apply (subgoal_tac "m*n dvd m*1")
wenzelm@22718
   572
   apply (drule dvd_mult_cancel, auto)
wenzelm@22718
   573
  done
paulson@14267
   574
paulson@14267
   575
lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
wenzelm@22718
   576
  apply (subst mult_commute)
wenzelm@22718
   577
  apply (erule dvd_mult_cancel1)
wenzelm@22718
   578
  done
paulson@14267
   579
paulson@14267
   580
lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
wenzelm@22718
   581
  apply (unfold dvd_def, clarify)
wenzelm@22718
   582
  apply (rule_tac x = "k*ka" in exI)
wenzelm@22718
   583
  apply (simp add: mult_ac)
wenzelm@22718
   584
  done
paulson@14267
   585
paulson@14267
   586
lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
wenzelm@22718
   587
  by (simp add: dvd_def mult_assoc, blast)
paulson@14267
   588
paulson@14267
   589
lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
wenzelm@22718
   590
  apply (unfold dvd_def, clarify)
wenzelm@22718
   591
  apply (rule_tac x = "i*k" in exI)
wenzelm@22718
   592
  apply (simp add: mult_ac)
wenzelm@22718
   593
  done
paulson@14267
   594
paulson@14267
   595
lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
wenzelm@22718
   596
  apply (unfold dvd_def, clarify)
wenzelm@22718
   597
  apply (simp_all (no_asm_use) add: zero_less_mult_iff)
wenzelm@22718
   598
  apply (erule conjE)
wenzelm@22718
   599
  apply (rule le_trans)
wenzelm@22718
   600
   apply (rule_tac [2] le_refl [THEN mult_le_mono])
wenzelm@22718
   601
   apply (erule_tac [2] Suc_leI, simp)
wenzelm@22718
   602
  done
paulson@14267
   603
paulson@14267
   604
lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
wenzelm@22718
   605
  apply (unfold dvd_def)
wenzelm@22718
   606
  apply (case_tac "k=0", simp, safe)
wenzelm@22718
   607
   apply (simp add: mult_commute)
wenzelm@22718
   608
  apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
wenzelm@22718
   609
  apply (subst mult_commute, simp)
wenzelm@22718
   610
  done
paulson@14267
   611
paulson@14267
   612
lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
wenzelm@22718
   613
  apply (subgoal_tac "m mod n = 0")
wenzelm@22718
   614
   apply (simp add: mult_div_cancel)
wenzelm@22718
   615
  apply (simp only: dvd_eq_mod_eq_0)
wenzelm@22718
   616
  done
paulson@14267
   617
haftmann@21408
   618
lemma le_imp_power_dvd: "!!i::nat. m \<le> n ==> i^m dvd i^n"
wenzelm@22718
   619
  apply (unfold dvd_def)
wenzelm@22718
   620
  apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
wenzelm@22718
   621
  apply (simp add: power_add)
wenzelm@22718
   622
  done
haftmann@21408
   623
haftmann@21408
   624
lemma nat_zero_less_power_iff [simp]: "(0 < x^n) = (x \<noteq> (0::nat) | n=0)"
wenzelm@22718
   625
  by (induct n) auto
haftmann@21408
   626
haftmann@21408
   627
lemma power_le_dvd [rule_format]: "k^j dvd n --> i\<le>j --> k^i dvd (n::nat)"
wenzelm@22718
   628
  apply (induct j)
wenzelm@22718
   629
   apply (simp_all add: le_Suc_eq)
wenzelm@22718
   630
  apply (blast dest!: dvd_mult_right)
wenzelm@22718
   631
  done
haftmann@21408
   632
haftmann@21408
   633
lemma power_dvd_imp_le: "[|i^m dvd i^n;  (1::nat) < i|] ==> m \<le> n"
wenzelm@22718
   634
  apply (rule power_le_imp_le_exp, assumption)
wenzelm@22718
   635
  apply (erule dvd_imp_le, simp)
wenzelm@22718
   636
  done
haftmann@21408
   637
paulson@14267
   638
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
wenzelm@22718
   639
  by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084
   640
wenzelm@22718
   641
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267
   642
paulson@14267
   643
(*Loses information, namely we also have r<d provided d is nonzero*)
paulson@14267
   644
lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
wenzelm@22718
   645
  apply (cut_tac m = m in mod_div_equality)
wenzelm@22718
   646
  apply (simp only: add_ac)
wenzelm@22718
   647
  apply (blast intro: sym)
wenzelm@22718
   648
  done
paulson@14267
   649
paulson@14131
   650
nipkow@13152
   651
lemma split_div:
nipkow@13189
   652
 "P(n div k :: nat) =
nipkow@13189
   653
 ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189
   654
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   655
proof
nipkow@13189
   656
  assume P: ?P
nipkow@13189
   657
  show ?Q
nipkow@13189
   658
  proof (cases)
nipkow@13189
   659
    assume "k = 0"
nipkow@13189
   660
    with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
nipkow@13189
   661
  next
nipkow@13189
   662
    assume not0: "k \<noteq> 0"
nipkow@13189
   663
    thus ?Q
nipkow@13189
   664
    proof (simp, intro allI impI)
nipkow@13189
   665
      fix i j
nipkow@13189
   666
      assume n: "n = k*i + j" and j: "j < k"
nipkow@13189
   667
      show "P i"
nipkow@13189
   668
      proof (cases)
wenzelm@22718
   669
        assume "i = 0"
wenzelm@22718
   670
        with n j P show "P i" by simp
nipkow@13189
   671
      next
wenzelm@22718
   672
        assume "i \<noteq> 0"
wenzelm@22718
   673
        with not0 n j P show "P i" by(simp add:add_ac)
nipkow@13189
   674
      qed
nipkow@13189
   675
    qed
nipkow@13189
   676
  qed
nipkow@13189
   677
next
nipkow@13189
   678
  assume Q: ?Q
nipkow@13189
   679
  show ?P
nipkow@13189
   680
  proof (cases)
nipkow@13189
   681
    assume "k = 0"
nipkow@13189
   682
    with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
nipkow@13189
   683
  next
nipkow@13189
   684
    assume not0: "k \<noteq> 0"
nipkow@13189
   685
    with Q have R: ?R by simp
nipkow@13189
   686
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   687
    show ?P by simp
nipkow@13189
   688
  qed
nipkow@13189
   689
qed
nipkow@13189
   690
berghofe@13882
   691
lemma split_div_lemma:
paulson@14267
   692
  "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
berghofe@13882
   693
  apply (rule iffI)
berghofe@13882
   694
  apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
nipkow@16733
   695
prefer 3; apply assumption
webertj@20432
   696
  apply (simp_all add: quorem_def) apply arith
berghofe@13882
   697
  apply (rule conjI)
berghofe@13882
   698
  apply (rule_tac P="%x. n * (m div n) \<le> x" in
berghofe@13882
   699
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   700
  apply (simp only: add: mult_ac)
berghofe@13882
   701
  apply (rule_tac P="%x. x < n + n * (m div n)" in
berghofe@13882
   702
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   703
  apply (simp only: add: mult_ac add_ac)
paulson@14208
   704
  apply (rule add_less_mono1, simp)
berghofe@13882
   705
  done
berghofe@13882
   706
berghofe@13882
   707
theorem split_div':
berghofe@13882
   708
  "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267
   709
   (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
berghofe@13882
   710
  apply (case_tac "0 < n")
berghofe@13882
   711
  apply (simp only: add: split_div_lemma)
berghofe@13882
   712
  apply (simp_all add: DIVISION_BY_ZERO_DIV)
berghofe@13882
   713
  done
berghofe@13882
   714
nipkow@13189
   715
lemma split_mod:
nipkow@13189
   716
 "P(n mod k :: nat) =
nipkow@13189
   717
 ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189
   718
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   719
proof
nipkow@13189
   720
  assume P: ?P
nipkow@13189
   721
  show ?Q
nipkow@13189
   722
  proof (cases)
nipkow@13189
   723
    assume "k = 0"
nipkow@13189
   724
    with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
nipkow@13189
   725
  next
nipkow@13189
   726
    assume not0: "k \<noteq> 0"
nipkow@13189
   727
    thus ?Q
nipkow@13189
   728
    proof (simp, intro allI impI)
nipkow@13189
   729
      fix i j
nipkow@13189
   730
      assume "n = k*i + j" "j < k"
nipkow@13189
   731
      thus "P j" using not0 P by(simp add:add_ac mult_ac)
nipkow@13189
   732
    qed
nipkow@13189
   733
  qed
nipkow@13189
   734
next
nipkow@13189
   735
  assume Q: ?Q
nipkow@13189
   736
  show ?P
nipkow@13189
   737
  proof (cases)
nipkow@13189
   738
    assume "k = 0"
nipkow@13189
   739
    with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
nipkow@13189
   740
  next
nipkow@13189
   741
    assume not0: "k \<noteq> 0"
nipkow@13189
   742
    with Q have R: ?R by simp
nipkow@13189
   743
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   744
    show ?P by simp
nipkow@13189
   745
  qed
nipkow@13189
   746
qed
nipkow@13189
   747
berghofe@13882
   748
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
berghofe@13882
   749
  apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
berghofe@13882
   750
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   751
  apply arith
berghofe@13882
   752
  done
berghofe@13882
   753
haftmann@22800
   754
lemma div_mod_equality':
haftmann@22800
   755
  fixes m n :: nat
haftmann@22800
   756
  shows "m div n * n = m - m mod n"
haftmann@22800
   757
proof -
haftmann@22800
   758
  have "m mod n \<le> m mod n" ..
haftmann@22800
   759
  from div_mod_equality have 
haftmann@22800
   760
    "m div n * n + m mod n - m mod n = m - m mod n" by simp
haftmann@22800
   761
  with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have
haftmann@22800
   762
    "m div n * n + (m mod n - m mod n) = m - m mod n"
haftmann@22800
   763
    by simp
haftmann@22800
   764
  then show ?thesis by simp
haftmann@22800
   765
qed
haftmann@22800
   766
haftmann@22800
   767
paulson@14640
   768
subsection {*An ``induction'' law for modulus arithmetic.*}
paulson@14640
   769
paulson@14640
   770
lemma mod_induct_0:
paulson@14640
   771
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
   772
  and base: "P i" and i: "i<p"
paulson@14640
   773
  shows "P 0"
paulson@14640
   774
proof (rule ccontr)
paulson@14640
   775
  assume contra: "\<not>(P 0)"
paulson@14640
   776
  from i have p: "0<p" by simp
paulson@14640
   777
  have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640
   778
  proof
paulson@14640
   779
    fix k
paulson@14640
   780
    show "?A k"
paulson@14640
   781
    proof (induct k)
paulson@14640
   782
      show "?A 0" by simp  -- "by contradiction"
paulson@14640
   783
    next
paulson@14640
   784
      fix n
paulson@14640
   785
      assume ih: "?A n"
paulson@14640
   786
      show "?A (Suc n)"
paulson@14640
   787
      proof (clarsimp)
wenzelm@22718
   788
        assume y: "P (p - Suc n)"
wenzelm@22718
   789
        have n: "Suc n < p"
wenzelm@22718
   790
        proof (rule ccontr)
wenzelm@22718
   791
          assume "\<not>(Suc n < p)"
wenzelm@22718
   792
          hence "p - Suc n = 0"
wenzelm@22718
   793
            by simp
wenzelm@22718
   794
          with y contra show "False"
wenzelm@22718
   795
            by simp
wenzelm@22718
   796
        qed
wenzelm@22718
   797
        hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718
   798
        from p have "p - Suc n < p" by arith
wenzelm@22718
   799
        with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718
   800
          by blast
wenzelm@22718
   801
        show "False"
wenzelm@22718
   802
        proof (cases "n=0")
wenzelm@22718
   803
          case True
wenzelm@22718
   804
          with z n2 contra show ?thesis by simp
wenzelm@22718
   805
        next
wenzelm@22718
   806
          case False
wenzelm@22718
   807
          with p have "p-n < p" by arith
wenzelm@22718
   808
          with z n2 False ih show ?thesis by simp
wenzelm@22718
   809
        qed
paulson@14640
   810
      qed
paulson@14640
   811
    qed
paulson@14640
   812
  qed
paulson@14640
   813
  moreover
paulson@14640
   814
  from i obtain k where "0<k \<and> i+k=p"
paulson@14640
   815
    by (blast dest: less_imp_add_positive)
paulson@14640
   816
  hence "0<k \<and> i=p-k" by auto
paulson@14640
   817
  moreover
paulson@14640
   818
  note base
paulson@14640
   819
  ultimately
paulson@14640
   820
  show "False" by blast
paulson@14640
   821
qed
paulson@14640
   822
paulson@14640
   823
lemma mod_induct:
paulson@14640
   824
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
   825
  and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640
   826
  shows "P j"
paulson@14640
   827
proof -
paulson@14640
   828
  have "\<forall>j<p. P j"
paulson@14640
   829
  proof
paulson@14640
   830
    fix j
paulson@14640
   831
    show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640
   832
    proof (induct j)
paulson@14640
   833
      from step base i show "?A 0"
wenzelm@22718
   834
        by (auto elim: mod_induct_0)
paulson@14640
   835
    next
paulson@14640
   836
      fix k
paulson@14640
   837
      assume ih: "?A k"
paulson@14640
   838
      show "?A (Suc k)"
paulson@14640
   839
      proof
wenzelm@22718
   840
        assume suc: "Suc k < p"
wenzelm@22718
   841
        hence k: "k<p" by simp
wenzelm@22718
   842
        with ih have "P k" ..
wenzelm@22718
   843
        with step k have "P (Suc k mod p)"
wenzelm@22718
   844
          by blast
wenzelm@22718
   845
        moreover
wenzelm@22718
   846
        from suc have "Suc k mod p = Suc k"
wenzelm@22718
   847
          by simp
wenzelm@22718
   848
        ultimately
wenzelm@22718
   849
        show "P (Suc k)" by simp
paulson@14640
   850
      qed
paulson@14640
   851
    qed
paulson@14640
   852
  qed
paulson@14640
   853
  with j show ?thesis by blast
paulson@14640
   854
qed
paulson@14640
   855
paulson@14640
   856
chaieb@18202
   857
lemma mod_add_left_eq: "((a::nat) + b) mod c = (a mod c + b) mod c"
chaieb@18202
   858
  apply (rule trans [symmetric])
wenzelm@22718
   859
   apply (rule mod_add1_eq, simp)
chaieb@18202
   860
  apply (rule mod_add1_eq [symmetric])
chaieb@18202
   861
  done
chaieb@18202
   862
chaieb@18202
   863
lemma mod_add_right_eq: "(a+b) mod (c::nat) = (a + (b mod c)) mod c"
wenzelm@22718
   864
  apply (rule trans [symmetric])
wenzelm@22718
   865
   apply (rule mod_add1_eq, simp)
wenzelm@22718
   866
  apply (rule mod_add1_eq [symmetric])
wenzelm@22718
   867
  done
chaieb@18202
   868
haftmann@22800
   869
lemma mod_div_decomp:
haftmann@22800
   870
  fixes n k :: nat
haftmann@22800
   871
  obtains m q where "m = n div k" and "q = n mod k"
haftmann@22800
   872
    and "n = m * k + q"
haftmann@22800
   873
proof -
haftmann@22800
   874
  from mod_div_equality have "n = n div k * k + n mod k" by auto
haftmann@22800
   875
  moreover have "n div k = n div k" ..
haftmann@22800
   876
  moreover have "n mod k = n mod k" ..
haftmann@22800
   877
  note that ultimately show thesis by blast
haftmann@22800
   878
qed
haftmann@22800
   879
haftmann@20589
   880
haftmann@22744
   881
subsection {* Code generation for div, mod and dvd on nat *}
haftmann@20589
   882
haftmann@22845
   883
definition [code func del]:
haftmann@20589
   884
  "divmod (m\<Colon>nat) n = (m div n, m mod n)"
haftmann@20589
   885
wenzelm@22718
   886
lemma divmod_zero [code]: "divmod m 0 = (0, m)"
haftmann@20589
   887
  unfolding divmod_def by simp
haftmann@20589
   888
haftmann@20589
   889
lemma divmod_succ [code]:
haftmann@20589
   890
  "divmod m (Suc k) = (if m < Suc k then (0, m) else
haftmann@20589
   891
    let
haftmann@20589
   892
      (p, q) = divmod (m - Suc k) (Suc k)
wenzelm@22718
   893
    in (Suc p, q))"
haftmann@20589
   894
  unfolding divmod_def Let_def split_def
haftmann@20589
   895
  by (auto intro: div_geq mod_geq)
haftmann@20589
   896
wenzelm@22718
   897
lemma div_divmod [code]: "m div n = fst (divmod m n)"
haftmann@20589
   898
  unfolding divmod_def by simp
haftmann@20589
   899
wenzelm@22718
   900
lemma mod_divmod [code]: "m mod n = snd (divmod m n)"
haftmann@20589
   901
  unfolding divmod_def by simp
haftmann@20589
   902
haftmann@23684
   903
instance nat :: dvd_mod
haftmann@24993
   904
  by default (simp add: dvd_eq_mod_eq_0)
haftmann@22744
   905
haftmann@21191
   906
code_modulename SML
haftmann@23017
   907
  Divides Nat
haftmann@20640
   908
haftmann@21911
   909
code_modulename OCaml
haftmann@23017
   910
  Divides Nat
haftmann@23017
   911
haftmann@23017
   912
code_modulename Haskell
haftmann@23017
   913
  Divides Nat
haftmann@21911
   914
haftmann@23684
   915
hide (open) const divmod
paulson@14267
   916
paulson@3366
   917
end