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