src/HOL/Divides.ML
author paulson
Thu Aug 06 15:48:13 1998 +0200 (1998-08-06)
changeset 5278 a903b66822e2
parent 5183 89f162de39cf
child 5316 7a8975451a89
permissions -rw-r--r--
even more tidying of Goal commands
paulson@3366
     1
(*  Title:      HOL/Divides.ML
paulson@3366
     2
    ID:         $Id$
paulson@3366
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@3366
     4
    Copyright   1993  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@3366
     9
paulson@3366
    10
(** Less-then properties **)
paulson@3366
    11
paulson@3366
    12
(*In ordinary notation: if 0<n and n<=m then m-n < m *)
paulson@3366
    13
goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m";
paulson@3366
    14
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
paulson@3366
    15
by (Blast_tac 1);
paulson@3366
    16
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
wenzelm@4089
    17
by (ALLGOALS(asm_simp_tac(simpset() addsimps [diff_less_Suc])));
paulson@3366
    18
qed "diff_less";
paulson@3366
    19
paulson@3366
    20
val wf_less_trans = [eq_reflection, wf_pred_nat RS wf_trancl] MRS 
paulson@3366
    21
                    def_wfrec RS trans;
paulson@3366
    22
paulson@3366
    23
(*** Remainder ***)
paulson@3366
    24
wenzelm@5069
    25
Goal "(%m. m mod n) = wfrec (trancl pred_nat) \
paulson@3366
    26
             \                      (%f j. if j<n then j else f (j-n))";
wenzelm@4089
    27
by (simp_tac (simpset() addsimps [mod_def]) 1);
paulson@3366
    28
qed "mod_eq";
paulson@3366
    29
paulson@5143
    30
Goal "m<n ==> m mod n = m";
paulson@3366
    31
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3366
    32
by (Asm_simp_tac 1);
paulson@3366
    33
qed "mod_less";
paulson@3366
    34
paulson@5143
    35
Goal "[| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
paulson@3366
    36
by (rtac (mod_eq RS wf_less_trans) 1);
wenzelm@4089
    37
by (asm_simp_tac (simpset() addsimps [diff_less, cut_apply, less_eq]) 1);
paulson@3366
    38
qed "mod_geq";
paulson@3366
    39
paulson@4774
    40
(*NOT suitable for rewriting: loops*)
paulson@5143
    41
Goal "0<n ==> m mod n = (if m<n then m else (m-n) mod n)";
paulson@4774
    42
by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq]) 1);
paulson@4774
    43
qed "mod_if";
paulson@4774
    44
wenzelm@5069
    45
Goal "m mod 1 = 0";
paulson@3366
    46
by (induct_tac "m" 1);
wenzelm@4089
    47
by (ALLGOALS (asm_simp_tac (simpset() addsimps [mod_less, mod_geq])));
paulson@3366
    48
qed "mod_1";
paulson@3366
    49
Addsimps [mod_1];
paulson@3366
    50
paulson@5143
    51
Goal "0<n ==> n mod n = 0";
wenzelm@4089
    52
by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq]) 1);
paulson@3366
    53
qed "mod_self";
paulson@3366
    54
paulson@5143
    55
Goal "0<n ==> (m+n) mod n = m mod n";
paulson@3366
    56
by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
paulson@3366
    57
by (stac (mod_geq RS sym) 2);
wenzelm@4089
    58
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [add_commute])));
paulson@4811
    59
qed "mod_add_self2";
paulson@4810
    60
paulson@5143
    61
Goal "0<n ==> (n+m) mod n = m mod n";
paulson@4811
    62
by (asm_simp_tac (simpset() addsimps [add_commute, mod_add_self2]) 1);
paulson@4811
    63
qed "mod_add_self1";
paulson@4810
    64
wenzelm@5069
    65
Goal "!!n. 0<n ==> (m + k*n) mod n = m mod n";
paulson@4810
    66
by (induct_tac "k" 1);
paulson@4811
    67
by (ALLGOALS (asm_simp_tac (simpset() addsimps (add_ac @ [mod_add_self1]))));
paulson@4811
    68
qed "mod_mult_self1";
paulson@4810
    69
paulson@5143
    70
Goal "0<n ==> (m + n*k) mod n = m mod n";
paulson@4811
    71
by (asm_simp_tac (simpset() addsimps [mult_commute, mod_mult_self1]) 1);
paulson@4811
    72
qed "mod_mult_self2";
paulson@4810
    73
paulson@4811
    74
Addsimps [mod_mult_self1, mod_mult_self2];
paulson@3366
    75
paulson@5143
    76
Goal "[| 0<k; 0<n |] ==> (m mod n)*k = (m*k) mod (n*k)";
paulson@3366
    77
by (res_inst_tac [("n","m")] less_induct 1);
paulson@4774
    78
by (stac mod_if 1);
paulson@4774
    79
by (Asm_simp_tac 1);
paulson@4774
    80
by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq, 
paulson@4774
    81
				      diff_less, diff_mult_distrib]) 1);
paulson@3366
    82
qed "mod_mult_distrib";
paulson@3366
    83
paulson@5143
    84
Goal "[| 0<k; 0<n |] ==> k*(m mod n) = (k*m) mod (k*n)";
paulson@3366
    85
by (res_inst_tac [("n","m")] less_induct 1);
paulson@4774
    86
by (stac mod_if 1);
paulson@4774
    87
by (Asm_simp_tac 1);
paulson@4774
    88
by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq, 
paulson@4774
    89
				      diff_less, diff_mult_distrib2]) 1);
paulson@3366
    90
qed "mod_mult_distrib2";
paulson@3366
    91
paulson@5143
    92
Goal "0<n ==> m*n mod n = 0";
paulson@3366
    93
by (induct_tac "m" 1);
wenzelm@4089
    94
by (asm_simp_tac (simpset() addsimps [mod_less]) 1);
berghofe@5183
    95
by (dres_inst_tac [("m","na*n")] mod_add_self2 1);
wenzelm@4089
    96
by (asm_full_simp_tac (simpset() addsimps [add_commute]) 1);
paulson@3366
    97
qed "mod_mult_self_is_0";
paulson@3366
    98
Addsimps [mod_mult_self_is_0];
paulson@3366
    99
paulson@3366
   100
(*** Quotient ***)
paulson@3366
   101
wenzelm@5069
   102
Goal "(%m. m div n) = wfrec (trancl pred_nat) \
paulson@3366
   103
                        \            (%f j. if j<n then 0 else Suc (f (j-n)))";
wenzelm@4089
   104
by (simp_tac (simpset() addsimps [div_def]) 1);
paulson@3366
   105
qed "div_eq";
paulson@3366
   106
paulson@5143
   107
Goal "m<n ==> m div n = 0";
paulson@3366
   108
by (rtac (div_eq RS wf_less_trans) 1);
paulson@3366
   109
by (Asm_simp_tac 1);
paulson@3366
   110
qed "div_less";
paulson@3366
   111
paulson@5143
   112
Goal "[| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)";
paulson@3366
   113
by (rtac (div_eq RS wf_less_trans) 1);
wenzelm@4089
   114
by (asm_simp_tac (simpset() addsimps [diff_less, cut_apply, less_eq]) 1);
paulson@3366
   115
qed "div_geq";
paulson@3366
   116
paulson@4774
   117
(*NOT suitable for rewriting: loops*)
paulson@5143
   118
Goal "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))";
paulson@4774
   119
by (asm_simp_tac (simpset() addsimps [div_less, div_geq]) 1);
paulson@4774
   120
qed "div_if";
paulson@4774
   121
paulson@3366
   122
(*Main Result about quotient and remainder.*)
paulson@5143
   123
Goal "0<n ==> (m div n)*n + m mod n = m";
paulson@3366
   124
by (res_inst_tac [("n","m")] less_induct 1);
paulson@4774
   125
by (stac mod_if 1);
paulson@4774
   126
by (ALLGOALS (asm_simp_tac 
paulson@4774
   127
	      (simpset() addsimps ([add_assoc] @
paulson@4774
   128
				   [div_less, div_geq,
paulson@4774
   129
				    add_diff_inverse, diff_less]))));
paulson@3366
   130
qed "mod_div_equality";
paulson@3366
   131
nipkow@4358
   132
(* a simple rearrangement of mod_div_equality: *)
paulson@5143
   133
Goal "0<k ==> k*(m div k) = m - (m mod k)";
wenzelm@4423
   134
by (dres_inst_tac [("m","m")] mod_div_equality 1);
nipkow@4358
   135
by (EVERY1[etac subst, simp_tac (simpset() addsimps mult_ac),
nipkow@4358
   136
           K(IF_UNSOLVED no_tac)]);
nipkow@4358
   137
qed "mult_div_cancel";
nipkow@4358
   138
wenzelm@5069
   139
Goal "m div 1 = m";
paulson@3366
   140
by (induct_tac "m" 1);
wenzelm@4089
   141
by (ALLGOALS (asm_simp_tac (simpset() addsimps [div_less, div_geq])));
paulson@3366
   142
qed "div_1";
paulson@3366
   143
Addsimps [div_1];
paulson@3366
   144
paulson@5143
   145
Goal "0<n ==> n div n = 1";
wenzelm@4089
   146
by (asm_simp_tac (simpset() addsimps [div_less, div_geq]) 1);
paulson@3366
   147
qed "div_self";
paulson@3366
   148
paulson@4811
   149
paulson@5143
   150
Goal "0<n ==> (m+n) div n = Suc (m div n)";
paulson@4811
   151
by (subgoal_tac "(n + m) div n = Suc ((n+m-n) div n)" 1);
paulson@4811
   152
by (stac (div_geq RS sym) 2);
paulson@4811
   153
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [add_commute])));
paulson@4811
   154
qed "div_add_self2";
paulson@4811
   155
paulson@5143
   156
Goal "0<n ==> (n+m) div n = Suc (m div n)";
paulson@4811
   157
by (asm_simp_tac (simpset() addsimps [add_commute, div_add_self2]) 1);
paulson@4811
   158
qed "div_add_self1";
paulson@4811
   159
wenzelm@5069
   160
Goal "!!n. 0<n ==> (m + k*n) div n = k + m div n";
paulson@4811
   161
by (induct_tac "k" 1);
paulson@4811
   162
by (ALLGOALS (asm_simp_tac (simpset() addsimps (add_ac @ [div_add_self1]))));
paulson@4811
   163
qed "div_mult_self1";
paulson@4811
   164
paulson@5143
   165
Goal "0<n ==> (m + n*k) div n = k + m div n";
paulson@4811
   166
by (asm_simp_tac (simpset() addsimps [mult_commute, div_mult_self1]) 1);
paulson@4811
   167
qed "div_mult_self2";
paulson@4811
   168
paulson@4811
   169
Addsimps [div_mult_self1, div_mult_self2];
paulson@4811
   170
paulson@4811
   171
nipkow@3484
   172
(* Monotonicity of div in first argument *)
paulson@5143
   173
Goal "0<k ==> ALL m. m <= n --> (m div k) <= (n div k)";
nipkow@3484
   174
by (res_inst_tac [("n","n")] less_induct 1);
paulson@3718
   175
by (Clarify_tac 1);
paulson@5143
   176
by (case_tac "n<k" 1);
nipkow@3484
   177
(* 1  case n<k *)
nipkow@3484
   178
by (subgoal_tac "m<k" 1);
wenzelm@4089
   179
by (asm_simp_tac (simpset() addsimps [div_less]) 1);
paulson@3496
   180
by (trans_tac 1);
nipkow@3484
   181
(* 2  case n >= k *)
nipkow@3484
   182
by (case_tac "m<k" 1);
nipkow@3484
   183
(* 2.1  case m<k *)
wenzelm@4089
   184
by (asm_simp_tac (simpset() addsimps [div_less]) 1);
nipkow@3484
   185
(* 2.2  case m>=k *)
wenzelm@4089
   186
by (asm_simp_tac (simpset() addsimps [div_geq, diff_less, diff_le_mono]) 1);
nipkow@3484
   187
qed_spec_mp "div_le_mono";
nipkow@3484
   188
nipkow@3484
   189
nipkow@3484
   190
(* Antimonotonicity of div in second argument *)
paulson@5143
   191
Goal "[| 0<m; m<=n |] ==> (k div n) <= (k div m)";
nipkow@3484
   192
by (subgoal_tac "0<n" 1);
paulson@3496
   193
 by (trans_tac 2);
nipkow@3484
   194
by (res_inst_tac [("n","k")] less_induct 1);
paulson@3496
   195
by (Simp_tac 1);
paulson@3496
   196
by (rename_tac "k" 1);
nipkow@3484
   197
by (case_tac "k<n" 1);
wenzelm@4089
   198
 by (asm_simp_tac (simpset() addsimps [div_less]) 1);
nipkow@3484
   199
by (subgoal_tac "~(k<m)" 1);
paulson@3496
   200
 by (trans_tac 2);
wenzelm@4089
   201
by (asm_simp_tac (simpset() addsimps [div_geq]) 1);
nipkow@3484
   202
by (subgoal_tac "(k-n) div n <= (k-m) div n" 1);
wenzelm@4089
   203
 by (best_tac (claset() addIs [le_trans] 
wenzelm@4089
   204
                       addss (simpset() addsimps [diff_less])) 1);
nipkow@3484
   205
by (REPEAT (eresolve_tac [div_le_mono,diff_le_mono2] 1));
nipkow@3484
   206
qed "div_le_mono2";
nipkow@3484
   207
paulson@5143
   208
Goal "0<n ==> m div n <= m";
nipkow@3484
   209
by (subgoal_tac "m div n <= m div 1" 1);
nipkow@3484
   210
by (Asm_full_simp_tac 1);
nipkow@3484
   211
by (rtac div_le_mono2 1);
nipkow@3484
   212
by (ALLGOALS trans_tac);
nipkow@3484
   213
qed "div_le_dividend";
nipkow@3484
   214
Addsimps [div_le_dividend];
nipkow@3484
   215
nipkow@3484
   216
(* Similar for "less than" *)
paulson@5143
   217
Goal "1<n ==> (0 < m) --> (m div n < m)";
nipkow@3484
   218
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3496
   219
by (Simp_tac 1);
paulson@3496
   220
by (rename_tac "m" 1);
nipkow@3484
   221
by (case_tac "m<n" 1);
wenzelm@4089
   222
 by (asm_full_simp_tac (simpset() addsimps [div_less]) 1);
nipkow@3484
   223
by (subgoal_tac "0<n" 1);
paulson@3496
   224
 by (trans_tac 2);
wenzelm@4089
   225
by (asm_full_simp_tac (simpset() addsimps [div_geq]) 1);
nipkow@3484
   226
by (case_tac "n<m" 1);
nipkow@3484
   227
 by (subgoal_tac "(m-n) div n < (m-n)" 1);
nipkow@3484
   228
  by (REPEAT (ares_tac [impI,less_trans_Suc] 1));
wenzelm@4089
   229
  by (asm_full_simp_tac (simpset() addsimps [diff_less]) 1);
nipkow@3484
   230
 by (dres_inst_tac [("m","n")] less_imp_diff_positive 1);
wenzelm@4089
   231
 by (asm_full_simp_tac (simpset() addsimps [diff_less]) 1);
nipkow@3484
   232
(* case n=m *)
nipkow@3484
   233
by (subgoal_tac "m=n" 1);
paulson@3496
   234
 by (trans_tac 2);
wenzelm@4089
   235
by (asm_simp_tac (simpset() addsimps [div_less]) 1);
nipkow@3484
   236
qed_spec_mp "div_less_dividend";
nipkow@3484
   237
Addsimps [div_less_dividend];
paulson@3366
   238
paulson@3366
   239
(*** Further facts about mod (mainly for the mutilated chess board ***)
paulson@3366
   240
paulson@5278
   241
Goal "0<n ==> Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
paulson@3366
   242
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3366
   243
by (excluded_middle_tac "Suc(na)<n" 1);
paulson@3366
   244
(* case Suc(na) < n *)
paulson@3366
   245
by (forward_tac [lessI RS less_trans] 2);
wenzelm@4089
   246
by (asm_simp_tac (simpset() addsimps [mod_less, less_not_refl2 RS not_sym]) 2);
paulson@3366
   247
(* case n <= Suc(na) *)
wenzelm@4089
   248
by (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, mod_geq]) 1);
paulson@3366
   249
by (etac (le_imp_less_or_eq RS disjE) 1);
wenzelm@4089
   250
by (asm_simp_tac (simpset() addsimps [Suc_diff_n]) 1);
wenzelm@4089
   251
by (asm_full_simp_tac (simpset() addsimps [not_less_eq RS sym, 
paulson@3366
   252
                                          diff_less, mod_geq]) 1);
wenzelm@4089
   253
by (asm_simp_tac (simpset() addsimps [mod_less]) 1);
paulson@3366
   254
qed "mod_Suc";
paulson@3366
   255
paulson@5143
   256
Goal "0<n ==> m mod n < n";
paulson@3366
   257
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3366
   258
by (excluded_middle_tac "na<n" 1);
paulson@3366
   259
(*case na<n*)
wenzelm@4089
   260
by (asm_simp_tac (simpset() addsimps [mod_less]) 2);
paulson@3366
   261
(*case n le na*)
wenzelm@4089
   262
by (asm_full_simp_tac (simpset() addsimps [mod_geq, diff_less]) 1);
paulson@3366
   263
qed "mod_less_divisor";
paulson@3366
   264
paulson@3366
   265
paulson@3366
   266
(** Evens and Odds **)
paulson@3366
   267
paulson@3366
   268
(*With less_zeroE, causes case analysis on b<2*)
paulson@3366
   269
AddSEs [less_SucE];
paulson@3366
   270
paulson@5143
   271
Goal "b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
paulson@3366
   272
by (subgoal_tac "k mod 2 < 2" 1);
wenzelm@4089
   273
by (asm_simp_tac (simpset() addsimps [mod_less_divisor]) 2);
nipkow@4686
   274
by (Asm_simp_tac 1);
nipkow@4356
   275
by Safe_tac;
paulson@3366
   276
qed "mod2_cases";
paulson@3366
   277
wenzelm@5069
   278
Goal "Suc(Suc(m)) mod 2 = m mod 2";
paulson@3366
   279
by (subgoal_tac "m mod 2 < 2" 1);
wenzelm@4089
   280
by (asm_simp_tac (simpset() addsimps [mod_less_divisor]) 2);
paulson@3724
   281
by Safe_tac;
wenzelm@4089
   282
by (ALLGOALS (asm_simp_tac (simpset() addsimps [mod_Suc])));
paulson@3366
   283
qed "mod2_Suc_Suc";
paulson@3366
   284
Addsimps [mod2_Suc_Suc];
paulson@3366
   285
wenzelm@5069
   286
Goal "(0 < m mod 2) = (m mod 2 = 1)";
paulson@3366
   287
by (subgoal_tac "m mod 2 < 2" 1);
wenzelm@4089
   288
by (asm_simp_tac (simpset() addsimps [mod_less_divisor]) 2);
paulson@4477
   289
by Auto_tac;
nipkow@4356
   290
qed "mod2_gr_0";
nipkow@4356
   291
Addsimps [mod2_gr_0];
nipkow@4356
   292
wenzelm@5069
   293
Goal "(m+m) mod 2 = 0";
paulson@3366
   294
by (induct_tac "m" 1);
wenzelm@4089
   295
by (simp_tac (simpset() addsimps [mod_less]) 1);
paulson@3427
   296
by (Asm_simp_tac 1);
paulson@4385
   297
qed "mod2_add_self_eq_0";
paulson@4385
   298
Addsimps [mod2_add_self_eq_0];
paulson@4385
   299
wenzelm@5069
   300
Goal "((m+m)+n) mod 2 = n mod 2";
paulson@4385
   301
by (induct_tac "m" 1);
paulson@4385
   302
by (simp_tac (simpset() addsimps [mod_less]) 1);
paulson@4385
   303
by (Asm_simp_tac 1);
paulson@3366
   304
qed "mod2_add_self";
paulson@3366
   305
Addsimps [mod2_add_self];
paulson@3366
   306
paulson@3366
   307
Delrules [less_SucE];
paulson@3366
   308
paulson@3366
   309
paulson@3366
   310
(*** More division laws ***)
paulson@3366
   311
paulson@5143
   312
Goal "0<n ==> m*n div n = m";
paulson@3366
   313
by (cut_inst_tac [("m", "m*n")] mod_div_equality 1);
paulson@3457
   314
by (assume_tac 1);
wenzelm@4089
   315
by (asm_full_simp_tac (simpset() addsimps [mod_mult_self_is_0]) 1);
paulson@3366
   316
qed "div_mult_self_is_m";
paulson@3366
   317
Addsimps [div_mult_self_is_m];
paulson@3366
   318
paulson@3366
   319
(*Cancellation law for division*)
paulson@5143
   320
Goal "[| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
paulson@3366
   321
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3366
   322
by (case_tac "na<n" 1);
wenzelm@4089
   323
by (asm_simp_tac (simpset() addsimps [div_less, zero_less_mult_iff, 
paulson@3366
   324
                                     mult_less_mono2]) 1);
paulson@3366
   325
by (subgoal_tac "~ k*na < k*n" 1);
paulson@3366
   326
by (asm_simp_tac
wenzelm@4089
   327
     (simpset() addsimps [zero_less_mult_iff, div_geq,
paulson@3366
   328
                         diff_mult_distrib2 RS sym, diff_less]) 1);
wenzelm@4089
   329
by (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, 
paulson@3366
   330
                                          le_refl RS mult_le_mono]) 1);
paulson@3366
   331
qed "div_cancel";
paulson@3366
   332
Addsimps [div_cancel];
paulson@3366
   333
paulson@5143
   334
Goal "[| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
paulson@3366
   335
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3366
   336
by (case_tac "na<n" 1);
wenzelm@4089
   337
by (asm_simp_tac (simpset() addsimps [mod_less, zero_less_mult_iff, 
paulson@3366
   338
                                     mult_less_mono2]) 1);
paulson@3366
   339
by (subgoal_tac "~ k*na < k*n" 1);
paulson@3366
   340
by (asm_simp_tac
wenzelm@4089
   341
     (simpset() addsimps [zero_less_mult_iff, mod_geq,
paulson@3366
   342
                         diff_mult_distrib2 RS sym, diff_less]) 1);
wenzelm@4089
   343
by (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, 
paulson@3366
   344
                                          le_refl RS mult_le_mono]) 1);
paulson@3366
   345
qed "mult_mod_distrib";
paulson@3366
   346
paulson@3366
   347
paulson@3366
   348
(************************************************)
paulson@3366
   349
(** Divides Relation                           **)
paulson@3366
   350
(************************************************)
paulson@3366
   351
wenzelm@5069
   352
Goalw [dvd_def] "m dvd 0";
wenzelm@4089
   353
by (blast_tac (claset() addIs [mult_0_right RS sym]) 1);
paulson@3366
   354
qed "dvd_0_right";
paulson@3366
   355
Addsimps [dvd_0_right];
paulson@3366
   356
paulson@5143
   357
Goalw [dvd_def] "0 dvd m ==> m = 0";
wenzelm@4089
   358
by (fast_tac (claset() addss simpset()) 1);
paulson@3366
   359
qed "dvd_0_left";
paulson@3366
   360
wenzelm@5069
   361
Goalw [dvd_def] "1 dvd k";
paulson@3366
   362
by (Simp_tac 1);
paulson@3366
   363
qed "dvd_1_left";
paulson@3366
   364
AddIffs [dvd_1_left];
paulson@3366
   365
wenzelm@5069
   366
Goalw [dvd_def] "m dvd m";
wenzelm@4089
   367
by (blast_tac (claset() addIs [mult_1_right RS sym]) 1);
paulson@3366
   368
qed "dvd_refl";
paulson@3366
   369
Addsimps [dvd_refl];
paulson@3366
   370
paulson@5143
   371
Goalw [dvd_def] "[| m dvd n; n dvd p |] ==> m dvd p";
wenzelm@4089
   372
by (blast_tac (claset() addIs [mult_assoc] ) 1);
paulson@3366
   373
qed "dvd_trans";
paulson@3366
   374
paulson@5143
   375
Goalw [dvd_def] "[| m dvd n; n dvd m |] ==> m=n";
wenzelm@4089
   376
by (fast_tac (claset() addDs [mult_eq_self_implies_10]
wenzelm@4089
   377
                     addss (simpset() addsimps [mult_assoc, mult_eq_1_iff])) 1);
paulson@3366
   378
qed "dvd_anti_sym";
paulson@3366
   379
paulson@5143
   380
Goalw [dvd_def] "[| k dvd m; k dvd n |] ==> k dvd (m + n)";
wenzelm@4089
   381
by (blast_tac (claset() addIs [add_mult_distrib2 RS sym]) 1);
paulson@3366
   382
qed "dvd_add";
paulson@3366
   383
paulson@5143
   384
Goalw [dvd_def] "[| k dvd m; k dvd n |] ==> k dvd (m-n)";
wenzelm@4089
   385
by (blast_tac (claset() addIs [diff_mult_distrib2 RS sym]) 1);
paulson@3366
   386
qed "dvd_diff";
paulson@3366
   387
paulson@5143
   388
Goal "[| k dvd (m-n); k dvd n; n<=m |] ==> k dvd m";
paulson@3457
   389
by (etac (not_less_iff_le RS iffD2 RS add_diff_inverse RS subst) 1);
wenzelm@4089
   390
by (blast_tac (claset() addIs [dvd_add]) 1);
paulson@3366
   391
qed "dvd_diffD";
paulson@3366
   392
paulson@5143
   393
Goalw [dvd_def] "k dvd n ==> k dvd (m*n)";
wenzelm@4089
   394
by (blast_tac (claset() addIs [mult_left_commute]) 1);
paulson@3366
   395
qed "dvd_mult";
paulson@3366
   396
paulson@5143
   397
Goal "k dvd m ==> k dvd (m*n)";
paulson@3366
   398
by (stac mult_commute 1);
paulson@3366
   399
by (etac dvd_mult 1);
paulson@3366
   400
qed "dvd_mult2";
paulson@3366
   401
paulson@3366
   402
(* k dvd (m*k) *)
paulson@3366
   403
AddIffs [dvd_refl RS dvd_mult, dvd_refl RS dvd_mult2];
paulson@3366
   404
paulson@5143
   405
Goalw [dvd_def] "[| f dvd m; f dvd n; 0<n |] ==> f dvd (m mod n)";
paulson@3718
   406
by (Clarify_tac 1);
wenzelm@4089
   407
by (full_simp_tac (simpset() addsimps [zero_less_mult_iff]) 1);
paulson@3366
   408
by (res_inst_tac 
paulson@3366
   409
    [("x", "(((k div ka)*ka + k mod ka) - ((f*k) div (f*ka)) * ka)")] 
paulson@3366
   410
    exI 1);
wenzelm@4089
   411
by (asm_simp_tac (simpset() addsimps [diff_mult_distrib2, 
paulson@3366
   412
                                     mult_mod_distrib, add_mult_distrib2]) 1);
paulson@3366
   413
qed "dvd_mod";
paulson@3366
   414
paulson@5143
   415
Goal "[| k dvd (m mod n); k dvd n; 0<n |] ==> k dvd m";
paulson@3366
   416
by (subgoal_tac "k dvd ((m div n)*n + m mod n)" 1);
wenzelm@4089
   417
by (asm_simp_tac (simpset() addsimps [dvd_add, dvd_mult]) 2);
nipkow@4356
   418
by (asm_full_simp_tac (simpset() addsimps [mod_div_equality]) 1);
paulson@3366
   419
qed "dvd_mod_imp_dvd";
paulson@3366
   420
paulson@5143
   421
Goalw [dvd_def]  "!!k. [| (k*m) dvd (k*n); 0<k |] ==> m dvd n";
paulson@3366
   422
by (etac exE 1);
wenzelm@4089
   423
by (asm_full_simp_tac (simpset() addsimps mult_ac) 1);
paulson@3366
   424
by (Blast_tac 1);
paulson@3366
   425
qed "dvd_mult_cancel";
paulson@3366
   426
paulson@5143
   427
Goalw [dvd_def] "[| i dvd m; j dvd n|] ==> (i*j) dvd (m*n)";
paulson@3718
   428
by (Clarify_tac 1);
paulson@3366
   429
by (res_inst_tac [("x","k*ka")] exI 1);
wenzelm@4089
   430
by (asm_simp_tac (simpset() addsimps mult_ac) 1);
paulson@3366
   431
qed "mult_dvd_mono";
paulson@3366
   432
paulson@5143
   433
Goalw [dvd_def] "(i*j) dvd k ==> i dvd k";
wenzelm@4089
   434
by (full_simp_tac (simpset() addsimps [mult_assoc]) 1);
paulson@3366
   435
by (Blast_tac 1);
paulson@3366
   436
qed "dvd_mult_left";
paulson@3366
   437
paulson@5143
   438
Goalw [dvd_def] "[| k dvd n; 0 < n |] ==> k <= n";
paulson@3718
   439
by (Clarify_tac 1);
wenzelm@4089
   440
by (ALLGOALS (full_simp_tac (simpset() addsimps [zero_less_mult_iff])));
paulson@3457
   441
by (etac conjE 1);
paulson@3457
   442
by (rtac le_trans 1);
paulson@3457
   443
by (rtac (le_refl RS mult_le_mono) 2);
paulson@3366
   444
by (etac Suc_leI 2);
paulson@3366
   445
by (Simp_tac 1);
paulson@3366
   446
qed "dvd_imp_le";
paulson@3366
   447
paulson@5143
   448
Goalw [dvd_def] "0<k ==> (k dvd n) = (n mod k = 0)";
paulson@3724
   449
by Safe_tac;
paulson@5143
   450
by (asm_simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@5143
   451
by (eres_inst_tac [("t","n")] (mod_div_equality RS subst) 1);
paulson@3366
   452
by (stac mult_commute 1);
paulson@3366
   453
by (Asm_simp_tac 1);
paulson@3366
   454
by (Blast_tac 1);
paulson@3366
   455
qed "dvd_eq_mod_eq_0";