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