src/HOL/Divides.thy
author huffman
Tue Aug 14 23:04:27 2007 +0200 (2007-08-14)
changeset 24268 9b4d7c59cc90
parent 23948 261bd4678076
child 24286 7619080e49f0
permissions -rw-r--r--
minimize imports
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@14267
   486
lemma dvd_1_left [iff]: "Suc 0 dvd k"
wenzelm@22718
   487
  unfolding dvd_def by simp
paulson@14267
   488
paulson@14267
   489
lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
wenzelm@22718
   490
  by (simp add: dvd_def)
paulson@14267
   491
paulson@14267
   492
lemma dvd_refl [simp]: "m dvd (m::nat)"
wenzelm@22718
   493
  unfolding dvd_def by (blast intro: mult_1_right [symmetric])
paulson@14267
   494
paulson@14267
   495
lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
wenzelm@22718
   496
  unfolding dvd_def by (blast intro: mult_assoc)
paulson@14267
   497
paulson@14267
   498
lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
wenzelm@22718
   499
  unfolding dvd_def
wenzelm@22718
   500
  by (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
paulson@14267
   501
haftmann@23684
   502
text {* @{term "op dvd"} is a partial order *}
haftmann@23684
   503
haftmann@23684
   504
interpretation dvd: order ["op dvd" "\<lambda>n m \<Colon> nat. n dvd m \<and> m \<noteq> n"]
haftmann@23684
   505
  by unfold_locales (auto intro: dvd_trans dvd_anti_sym)
haftmann@23684
   506
paulson@14267
   507
lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
wenzelm@22718
   508
  unfolding dvd_def
wenzelm@22718
   509
  by (blast intro: add_mult_distrib2 [symmetric])
paulson@14267
   510
paulson@14267
   511
lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
wenzelm@22718
   512
  unfolding dvd_def
wenzelm@22718
   513
  by (blast intro: diff_mult_distrib2 [symmetric])
paulson@14267
   514
paulson@14267
   515
lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
wenzelm@22718
   516
  apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
wenzelm@22718
   517
  apply (blast intro: dvd_add)
wenzelm@22718
   518
  done
paulson@14267
   519
paulson@14267
   520
lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
wenzelm@22718
   521
  by (drule_tac m = m in dvd_diff, auto)
paulson@14267
   522
paulson@14267
   523
lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
wenzelm@22718
   524
  unfolding dvd_def by (blast intro: mult_left_commute)
paulson@14267
   525
paulson@14267
   526
lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
wenzelm@22718
   527
  apply (subst mult_commute)
wenzelm@22718
   528
  apply (erule dvd_mult)
wenzelm@22718
   529
  done
paulson@14267
   530
paulson@17084
   531
lemma dvd_triv_right [iff]: "k dvd (m*k :: nat)"
wenzelm@22718
   532
  by (rule dvd_refl [THEN dvd_mult])
paulson@17084
   533
paulson@17084
   534
lemma dvd_triv_left [iff]: "k dvd (k*m :: nat)"
wenzelm@22718
   535
  by (rule dvd_refl [THEN dvd_mult2])
paulson@14267
   536
paulson@14267
   537
lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
wenzelm@22718
   538
  apply (rule iffI)
wenzelm@22718
   539
   apply (erule_tac [2] dvd_add)
wenzelm@22718
   540
   apply (rule_tac [2] dvd_refl)
wenzelm@22718
   541
  apply (subgoal_tac "n = (n+k) -k")
wenzelm@22718
   542
   prefer 2 apply simp
wenzelm@22718
   543
  apply (erule ssubst)
wenzelm@22718
   544
  apply (erule dvd_diff)
wenzelm@22718
   545
  apply (rule dvd_refl)
wenzelm@22718
   546
  done
paulson@14267
   547
paulson@14267
   548
lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
wenzelm@22718
   549
  unfolding dvd_def
wenzelm@22718
   550
  apply (case_tac "n = 0", auto)
wenzelm@22718
   551
  apply (blast intro: mod_mult_distrib2 [symmetric])
wenzelm@22718
   552
  done
paulson@14267
   553
paulson@14267
   554
lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
wenzelm@22718
   555
  apply (subgoal_tac "k dvd (m div n) *n + m mod n")
wenzelm@22718
   556
   apply (simp add: mod_div_equality)
wenzelm@22718
   557
  apply (simp only: dvd_add dvd_mult)
wenzelm@22718
   558
  done
paulson@14267
   559
paulson@14267
   560
lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
wenzelm@22718
   561
  by (blast intro: dvd_mod_imp_dvd dvd_mod)
paulson@14267
   562
paulson@14267
   563
lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
wenzelm@22718
   564
  unfolding dvd_def
wenzelm@22718
   565
  apply (erule exE)
wenzelm@22718
   566
  apply (simp add: mult_ac)
wenzelm@22718
   567
  done
paulson@14267
   568
paulson@14267
   569
lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
wenzelm@22718
   570
  apply auto
wenzelm@22718
   571
   apply (subgoal_tac "m*n dvd m*1")
wenzelm@22718
   572
   apply (drule dvd_mult_cancel, auto)
wenzelm@22718
   573
  done
paulson@14267
   574
paulson@14267
   575
lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
wenzelm@22718
   576
  apply (subst mult_commute)
wenzelm@22718
   577
  apply (erule dvd_mult_cancel1)
wenzelm@22718
   578
  done
paulson@14267
   579
paulson@14267
   580
lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
wenzelm@22718
   581
  apply (unfold dvd_def, clarify)
wenzelm@22718
   582
  apply (rule_tac x = "k*ka" in exI)
wenzelm@22718
   583
  apply (simp add: mult_ac)
wenzelm@22718
   584
  done
paulson@14267
   585
paulson@14267
   586
lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
wenzelm@22718
   587
  by (simp add: dvd_def mult_assoc, blast)
paulson@14267
   588
paulson@14267
   589
lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
wenzelm@22718
   590
  apply (unfold dvd_def, clarify)
wenzelm@22718
   591
  apply (rule_tac x = "i*k" in exI)
wenzelm@22718
   592
  apply (simp add: mult_ac)
wenzelm@22718
   593
  done
paulson@14267
   594
paulson@14267
   595
lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
wenzelm@22718
   596
  apply (unfold dvd_def, clarify)
wenzelm@22718
   597
  apply (simp_all (no_asm_use) add: zero_less_mult_iff)
wenzelm@22718
   598
  apply (erule conjE)
wenzelm@22718
   599
  apply (rule le_trans)
wenzelm@22718
   600
   apply (rule_tac [2] le_refl [THEN mult_le_mono])
wenzelm@22718
   601
   apply (erule_tac [2] Suc_leI, simp)
wenzelm@22718
   602
  done
paulson@14267
   603
paulson@14267
   604
lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
wenzelm@22718
   605
  apply (unfold dvd_def)
wenzelm@22718
   606
  apply (case_tac "k=0", simp, safe)
wenzelm@22718
   607
   apply (simp add: mult_commute)
wenzelm@22718
   608
  apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
wenzelm@22718
   609
  apply (subst mult_commute, simp)
wenzelm@22718
   610
  done
paulson@14267
   611
paulson@14267
   612
lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
wenzelm@22718
   613
  apply (subgoal_tac "m mod n = 0")
wenzelm@22718
   614
   apply (simp add: mult_div_cancel)
wenzelm@22718
   615
  apply (simp only: dvd_eq_mod_eq_0)
wenzelm@22718
   616
  done
paulson@14267
   617
haftmann@21408
   618
lemma le_imp_power_dvd: "!!i::nat. m \<le> n ==> i^m dvd i^n"
wenzelm@22718
   619
  apply (unfold dvd_def)
wenzelm@22718
   620
  apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
wenzelm@22718
   621
  apply (simp add: power_add)
wenzelm@22718
   622
  done
haftmann@21408
   623
haftmann@21408
   624
lemma nat_zero_less_power_iff [simp]: "(0 < x^n) = (x \<noteq> (0::nat) | n=0)"
wenzelm@22718
   625
  by (induct n) auto
haftmann@21408
   626
haftmann@21408
   627
lemma power_le_dvd [rule_format]: "k^j dvd n --> i\<le>j --> k^i dvd (n::nat)"
wenzelm@22718
   628
  apply (induct j)
wenzelm@22718
   629
   apply (simp_all add: le_Suc_eq)
wenzelm@22718
   630
  apply (blast dest!: dvd_mult_right)
wenzelm@22718
   631
  done
haftmann@21408
   632
haftmann@21408
   633
lemma power_dvd_imp_le: "[|i^m dvd i^n;  (1::nat) < i|] ==> m \<le> n"
wenzelm@22718
   634
  apply (rule power_le_imp_le_exp, assumption)
wenzelm@22718
   635
  apply (erule dvd_imp_le, simp)
wenzelm@22718
   636
  done
haftmann@21408
   637
paulson@14267
   638
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
wenzelm@22718
   639
  by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084
   640
wenzelm@22718
   641
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267
   642
paulson@14267
   643
(*Loses information, namely we also have r<d provided d is nonzero*)
paulson@14267
   644
lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
wenzelm@22718
   645
  apply (cut_tac m = m in mod_div_equality)
wenzelm@22718
   646
  apply (simp only: add_ac)
wenzelm@22718
   647
  apply (blast intro: sym)
wenzelm@22718
   648
  done
paulson@14267
   649
paulson@14131
   650
nipkow@13152
   651
lemma split_div:
nipkow@13189
   652
 "P(n div k :: nat) =
nipkow@13189
   653
 ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189
   654
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   655
proof
nipkow@13189
   656
  assume P: ?P
nipkow@13189
   657
  show ?Q
nipkow@13189
   658
  proof (cases)
nipkow@13189
   659
    assume "k = 0"
nipkow@13189
   660
    with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
nipkow@13189
   661
  next
nipkow@13189
   662
    assume not0: "k \<noteq> 0"
nipkow@13189
   663
    thus ?Q
nipkow@13189
   664
    proof (simp, intro allI impI)
nipkow@13189
   665
      fix i j
nipkow@13189
   666
      assume n: "n = k*i + j" and j: "j < k"
nipkow@13189
   667
      show "P i"
nipkow@13189
   668
      proof (cases)
wenzelm@22718
   669
        assume "i = 0"
wenzelm@22718
   670
        with n j P show "P i" by simp
nipkow@13189
   671
      next
wenzelm@22718
   672
        assume "i \<noteq> 0"
wenzelm@22718
   673
        with not0 n j P show "P i" by(simp add:add_ac)
nipkow@13189
   674
      qed
nipkow@13189
   675
    qed
nipkow@13189
   676
  qed
nipkow@13189
   677
next
nipkow@13189
   678
  assume Q: ?Q
nipkow@13189
   679
  show ?P
nipkow@13189
   680
  proof (cases)
nipkow@13189
   681
    assume "k = 0"
nipkow@13189
   682
    with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
nipkow@13189
   683
  next
nipkow@13189
   684
    assume not0: "k \<noteq> 0"
nipkow@13189
   685
    with Q have R: ?R by simp
nipkow@13189
   686
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   687
    show ?P by simp
nipkow@13189
   688
  qed
nipkow@13189
   689
qed
nipkow@13189
   690
berghofe@13882
   691
lemma split_div_lemma:
paulson@14267
   692
  "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
berghofe@13882
   693
  apply (rule iffI)
berghofe@13882
   694
  apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
nipkow@16733
   695
prefer 3; apply assumption
webertj@20432
   696
  apply (simp_all add: quorem_def) apply arith
berghofe@13882
   697
  apply (rule conjI)
berghofe@13882
   698
  apply (rule_tac P="%x. n * (m div n) \<le> x" in
berghofe@13882
   699
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   700
  apply (simp only: add: mult_ac)
berghofe@13882
   701
  apply (rule_tac P="%x. x < n + n * (m div n)" in
berghofe@13882
   702
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   703
  apply (simp only: add: mult_ac add_ac)
paulson@14208
   704
  apply (rule add_less_mono1, simp)
berghofe@13882
   705
  done
berghofe@13882
   706
berghofe@13882
   707
theorem split_div':
berghofe@13882
   708
  "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267
   709
   (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
berghofe@13882
   710
  apply (case_tac "0 < n")
berghofe@13882
   711
  apply (simp only: add: split_div_lemma)
berghofe@13882
   712
  apply (simp_all add: DIVISION_BY_ZERO_DIV)
berghofe@13882
   713
  done
berghofe@13882
   714
nipkow@13189
   715
lemma split_mod:
nipkow@13189
   716
 "P(n mod k :: nat) =
nipkow@13189
   717
 ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189
   718
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   719
proof
nipkow@13189
   720
  assume P: ?P
nipkow@13189
   721
  show ?Q
nipkow@13189
   722
  proof (cases)
nipkow@13189
   723
    assume "k = 0"
nipkow@13189
   724
    with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
nipkow@13189
   725
  next
nipkow@13189
   726
    assume not0: "k \<noteq> 0"
nipkow@13189
   727
    thus ?Q
nipkow@13189
   728
    proof (simp, intro allI impI)
nipkow@13189
   729
      fix i j
nipkow@13189
   730
      assume "n = k*i + j" "j < k"
nipkow@13189
   731
      thus "P j" using not0 P by(simp add:add_ac mult_ac)
nipkow@13189
   732
    qed
nipkow@13189
   733
  qed
nipkow@13189
   734
next
nipkow@13189
   735
  assume Q: ?Q
nipkow@13189
   736
  show ?P
nipkow@13189
   737
  proof (cases)
nipkow@13189
   738
    assume "k = 0"
nipkow@13189
   739
    with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
nipkow@13189
   740
  next
nipkow@13189
   741
    assume not0: "k \<noteq> 0"
nipkow@13189
   742
    with Q have R: ?R by simp
nipkow@13189
   743
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   744
    show ?P by simp
nipkow@13189
   745
  qed
nipkow@13189
   746
qed
nipkow@13189
   747
berghofe@13882
   748
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
berghofe@13882
   749
  apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
berghofe@13882
   750
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   751
  apply arith
berghofe@13882
   752
  done
berghofe@13882
   753
haftmann@22800
   754
lemma div_mod_equality':
haftmann@22800
   755
  fixes m n :: nat
haftmann@22800
   756
  shows "m div n * n = m - m mod n"
haftmann@22800
   757
proof -
haftmann@22800
   758
  have "m mod n \<le> m mod n" ..
haftmann@22800
   759
  from div_mod_equality have 
haftmann@22800
   760
    "m div n * n + m mod n - m mod n = m - m mod n" by simp
haftmann@22800
   761
  with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have
haftmann@22800
   762
    "m div n * n + (m mod n - m mod n) = m - m mod n"
haftmann@22800
   763
    by simp
haftmann@22800
   764
  then show ?thesis by simp
haftmann@22800
   765
qed
haftmann@22800
   766
haftmann@22800
   767
paulson@14640
   768
subsection {*An ``induction'' law for modulus arithmetic.*}
paulson@14640
   769
paulson@14640
   770
lemma mod_induct_0:
paulson@14640
   771
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
   772
  and base: "P i" and i: "i<p"
paulson@14640
   773
  shows "P 0"
paulson@14640
   774
proof (rule ccontr)
paulson@14640
   775
  assume contra: "\<not>(P 0)"
paulson@14640
   776
  from i have p: "0<p" by simp
paulson@14640
   777
  have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640
   778
  proof
paulson@14640
   779
    fix k
paulson@14640
   780
    show "?A k"
paulson@14640
   781
    proof (induct k)
paulson@14640
   782
      show "?A 0" by simp  -- "by contradiction"
paulson@14640
   783
    next
paulson@14640
   784
      fix n
paulson@14640
   785
      assume ih: "?A n"
paulson@14640
   786
      show "?A (Suc n)"
paulson@14640
   787
      proof (clarsimp)
wenzelm@22718
   788
        assume y: "P (p - Suc n)"
wenzelm@22718
   789
        have n: "Suc n < p"
wenzelm@22718
   790
        proof (rule ccontr)
wenzelm@22718
   791
          assume "\<not>(Suc n < p)"
wenzelm@22718
   792
          hence "p - Suc n = 0"
wenzelm@22718
   793
            by simp
wenzelm@22718
   794
          with y contra show "False"
wenzelm@22718
   795
            by simp
wenzelm@22718
   796
        qed
wenzelm@22718
   797
        hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718
   798
        from p have "p - Suc n < p" by arith
wenzelm@22718
   799
        with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718
   800
          by blast
wenzelm@22718
   801
        show "False"
wenzelm@22718
   802
        proof (cases "n=0")
wenzelm@22718
   803
          case True
wenzelm@22718
   804
          with z n2 contra show ?thesis by simp
wenzelm@22718
   805
        next
wenzelm@22718
   806
          case False
wenzelm@22718
   807
          with p have "p-n < p" by arith
wenzelm@22718
   808
          with z n2 False ih show ?thesis by simp
wenzelm@22718
   809
        qed
paulson@14640
   810
      qed
paulson@14640
   811
    qed
paulson@14640
   812
  qed
paulson@14640
   813
  moreover
paulson@14640
   814
  from i obtain k where "0<k \<and> i+k=p"
paulson@14640
   815
    by (blast dest: less_imp_add_positive)
paulson@14640
   816
  hence "0<k \<and> i=p-k" by auto
paulson@14640
   817
  moreover
paulson@14640
   818
  note base
paulson@14640
   819
  ultimately
paulson@14640
   820
  show "False" by blast
paulson@14640
   821
qed
paulson@14640
   822
paulson@14640
   823
lemma mod_induct:
paulson@14640
   824
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
   825
  and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640
   826
  shows "P j"
paulson@14640
   827
proof -
paulson@14640
   828
  have "\<forall>j<p. P j"
paulson@14640
   829
  proof
paulson@14640
   830
    fix j
paulson@14640
   831
    show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640
   832
    proof (induct j)
paulson@14640
   833
      from step base i show "?A 0"
wenzelm@22718
   834
        by (auto elim: mod_induct_0)
paulson@14640
   835
    next
paulson@14640
   836
      fix k
paulson@14640
   837
      assume ih: "?A k"
paulson@14640
   838
      show "?A (Suc k)"
paulson@14640
   839
      proof
wenzelm@22718
   840
        assume suc: "Suc k < p"
wenzelm@22718
   841
        hence k: "k<p" by simp
wenzelm@22718
   842
        with ih have "P k" ..
wenzelm@22718
   843
        with step k have "P (Suc k mod p)"
wenzelm@22718
   844
          by blast
wenzelm@22718
   845
        moreover
wenzelm@22718
   846
        from suc have "Suc k mod p = Suc k"
wenzelm@22718
   847
          by simp
wenzelm@22718
   848
        ultimately
wenzelm@22718
   849
        show "P (Suc k)" by simp
paulson@14640
   850
      qed
paulson@14640
   851
    qed
paulson@14640
   852
  qed
paulson@14640
   853
  with j show ?thesis by blast
paulson@14640
   854
qed
paulson@14640
   855
paulson@14640
   856
chaieb@18202
   857
lemma mod_add_left_eq: "((a::nat) + b) mod c = (a mod c + b) mod c"
chaieb@18202
   858
  apply (rule trans [symmetric])
wenzelm@22718
   859
   apply (rule mod_add1_eq, simp)
chaieb@18202
   860
  apply (rule mod_add1_eq [symmetric])
chaieb@18202
   861
  done
chaieb@18202
   862
chaieb@18202
   863
lemma mod_add_right_eq: "(a+b) mod (c::nat) = (a + (b mod c)) mod c"
wenzelm@22718
   864
  apply (rule trans [symmetric])
wenzelm@22718
   865
   apply (rule mod_add1_eq, simp)
wenzelm@22718
   866
  apply (rule mod_add1_eq [symmetric])
wenzelm@22718
   867
  done
chaieb@18202
   868
haftmann@22800
   869
lemma mod_div_decomp:
haftmann@22800
   870
  fixes n k :: nat
haftmann@22800
   871
  obtains m q where "m = n div k" and "q = n mod k"
haftmann@22800
   872
    and "n = m * k + q"
haftmann@22800
   873
proof -
haftmann@22800
   874
  from mod_div_equality have "n = n div k * k + n mod k" by auto
haftmann@22800
   875
  moreover have "n div k = n div k" ..
haftmann@22800
   876
  moreover have "n mod k = n mod k" ..
haftmann@22800
   877
  note that ultimately show thesis by blast
haftmann@22800
   878
qed
haftmann@22800
   879
haftmann@20589
   880
haftmann@23684
   881
haftmann@23684
   882
haftmann@22744
   883
subsection {* Code generation for div, mod and dvd on nat *}
haftmann@20589
   884
haftmann@22845
   885
definition [code func del]:
haftmann@20589
   886
  "divmod (m\<Colon>nat) n = (m div n, m mod n)"
haftmann@20589
   887
wenzelm@22718
   888
lemma divmod_zero [code]: "divmod m 0 = (0, m)"
haftmann@20589
   889
  unfolding divmod_def by simp
haftmann@20589
   890
haftmann@20589
   891
lemma divmod_succ [code]:
haftmann@20589
   892
  "divmod m (Suc k) = (if m < Suc k then (0, m) else
haftmann@20589
   893
    let
haftmann@20589
   894
      (p, q) = divmod (m - Suc k) (Suc k)
wenzelm@22718
   895
    in (Suc p, q))"
haftmann@20589
   896
  unfolding divmod_def Let_def split_def
haftmann@20589
   897
  by (auto intro: div_geq mod_geq)
haftmann@20589
   898
wenzelm@22718
   899
lemma div_divmod [code]: "m div n = fst (divmod m n)"
haftmann@20589
   900
  unfolding divmod_def by simp
haftmann@20589
   901
wenzelm@22718
   902
lemma mod_divmod [code]: "m mod n = snd (divmod m n)"
haftmann@20589
   903
  unfolding divmod_def by simp
haftmann@20589
   904
haftmann@23684
   905
instance nat :: dvd_mod
haftmann@23684
   906
  by default (simp add: times_class.dvd [symmetric] dvd_eq_mod_eq_0)
haftmann@22744
   907
haftmann@21191
   908
code_modulename SML
haftmann@23017
   909
  Divides Nat
haftmann@20640
   910
haftmann@21911
   911
code_modulename OCaml
haftmann@23017
   912
  Divides Nat
haftmann@23017
   913
haftmann@23017
   914
code_modulename Haskell
haftmann@23017
   915
  Divides Nat
haftmann@21911
   916
haftmann@23684
   917
hide (open) const divmod
paulson@14267
   918
paulson@3366
   919
end