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