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