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