new division laws taking advantage of (m div 0) = 0 and (m mod 0) = m
authorpaulson
Mon Jul 19 15:18:16 1999 +0200 (1999-07-19)
changeset 702908d4eb8500dd
parent 7028 6ea3b385e731
child 7030 53934985426a
new division laws taking advantage of (m div 0) = 0 and (m mod 0) = m
src/HOL/Divides.ML
src/HOL/Divides.thy
     1.1 --- a/src/HOL/Divides.ML	Sun Jul 18 11:06:08 1999 +0200
     1.2 +++ b/src/HOL/Divides.ML	Mon Jul 19 15:18:16 1999 +0200
     1.3 @@ -12,30 +12,55 @@
     1.4  val wf_less_trans = [eq_reflection, wf_pred_nat RS wf_trancl] MRS 
     1.5                      def_wfrec RS trans;
     1.6  
     1.7 -(*** Remainder ***)
     1.8 -
     1.9  Goal "(%m. m mod n) = wfrec (trancl pred_nat) \
    1.10 -\                           (%f j. if j<n then j else f (j-n))";
    1.11 +\                           (%f j. if j<n | n=0 then j else f (j-n))";
    1.12  by (simp_tac (simpset() addsimps [mod_def]) 1);
    1.13  qed "mod_eq";
    1.14  
    1.15 +Goal "(%m. m div n) = wfrec (trancl pred_nat) \
    1.16 +\            (%f j. if j<n | n=0 then 0 else Suc (f (j-n)))";
    1.17 +by (simp_tac (simpset() addsimps [div_def]) 1);
    1.18 +qed "div_eq";
    1.19 +
    1.20 +
    1.21 +(** Aribtrary definitions for division by zero.  Useful to simplify 
    1.22 +    certain equations **)
    1.23 +
    1.24 +Goal "a div 0 = 0";
    1.25 +by (rtac (div_eq RS wf_less_trans) 1);
    1.26 +by (Asm_simp_tac 1);
    1.27 +qed "DIVISION_BY_ZERO_DIV";  (*NOT for adding to default simpset*)
    1.28 +
    1.29 +Goal "a mod 0 = a";
    1.30 +by (rtac (mod_eq RS wf_less_trans) 1);
    1.31 +by (Asm_simp_tac 1);
    1.32 +qed "DIVISION_BY_ZERO_MOD";  (*NOT for adding to default simpset*)
    1.33 +
    1.34 +fun div_undefined_case_tac s i =
    1.35 +  case_tac s i THEN 
    1.36 +  Full_simp_tac (i+1) THEN
    1.37 +  asm_simp_tac (simpset() addsimps [DIVISION_BY_ZERO_DIV, 
    1.38 +				    DIVISION_BY_ZERO_MOD]) i;
    1.39 +
    1.40 +(*** Remainder ***)
    1.41 +
    1.42  Goal "m<n ==> m mod n = (m::nat)";
    1.43  by (rtac (mod_eq RS wf_less_trans) 1);
    1.44  by (Asm_simp_tac 1);
    1.45  qed "mod_less";
    1.46  
    1.47 -Goal "[| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
    1.48 +Goal "~ m < (n::nat) ==> m mod n = (m-n) mod n";
    1.49 +by (div_undefined_case_tac "n=0" 1);
    1.50  by (rtac (mod_eq RS wf_less_trans) 1);
    1.51  by (asm_simp_tac (simpset() addsimps [diff_less, cut_apply, less_eq]) 1);
    1.52  qed "mod_geq";
    1.53  
    1.54  (*Avoids the ugly ~m<n above*)
    1.55 -Goal "[| 0<n;  n<=m |] ==> m mod n = (m-n) mod n";
    1.56 +Goal "(n::nat) <= m ==> m mod n = (m-n) mod n";
    1.57  by (asm_simp_tac (simpset() addsimps [mod_geq, not_less_iff_le]) 1);
    1.58  qed "le_mod_geq";
    1.59  
    1.60 -(*NOT suitable for rewriting: loops*)
    1.61 -Goal "0<n ==> m mod n = (if m<n then m else (m-n) mod n)";
    1.62 +Goal "m mod (n::nat) = (if m<n then m else (m-n) mod n)";
    1.63  by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq]) 1);
    1.64  qed "mod_if";
    1.65  
    1.66 @@ -45,32 +70,38 @@
    1.67  qed "mod_1";
    1.68  Addsimps [mod_1];
    1.69  
    1.70 -Goal "0<n ==> n mod n = 0";
    1.71 +Goal "n mod n = 0";
    1.72 +by (div_undefined_case_tac "n=0" 1);
    1.73  by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq]) 1);
    1.74  qed "mod_self";
    1.75  
    1.76 -Goal "0<n ==> (m+n) mod n = m mod n";
    1.77 +Goal "(m+n) mod n = m mod (n::nat)";
    1.78  by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
    1.79  by (stac (mod_geq RS sym) 2);
    1.80  by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [add_commute])));
    1.81  qed "mod_add_self2";
    1.82  
    1.83 -Goal "0<n ==> (n+m) mod n = m mod n";
    1.84 +Goal "(n+m) mod n = m mod (n::nat)";
    1.85  by (asm_simp_tac (simpset() addsimps [add_commute, mod_add_self2]) 1);
    1.86  qed "mod_add_self1";
    1.87  
    1.88 -Goal "!!n. 0<n ==> (m + k*n) mod n = m mod n";
    1.89 +Goal "(m + k*n) mod n = m mod (n::nat)";
    1.90  by (induct_tac "k" 1);
    1.91 -by (ALLGOALS (asm_simp_tac (simpset() addsimps add_ac @ [mod_add_self1])));
    1.92 +by (ALLGOALS
    1.93 +    (asm_simp_tac 
    1.94 +     (simpset() addsimps [read_instantiate [("y","n")] add_left_commute, 
    1.95 +			  mod_add_self1])));
    1.96  qed "mod_mult_self1";
    1.97  
    1.98 -Goal "0<n ==> (m + n*k) mod n = m mod n";
    1.99 +Goal "(m + n*k) mod n = m mod (n::nat)";
   1.100  by (asm_simp_tac (simpset() addsimps [mult_commute, mod_mult_self1]) 1);
   1.101  qed "mod_mult_self2";
   1.102  
   1.103  Addsimps [mod_mult_self1, mod_mult_self2];
   1.104  
   1.105 -Goal "[| 0<k; 0<n |] ==> (m mod n)*k = (m*k) mod (n*k)";
   1.106 +Goal "(m mod n) * (k::nat) = (m*k) mod (n*k)";
   1.107 +by (div_undefined_case_tac "n=0" 1);
   1.108 +by (div_undefined_case_tac "k=0" 1);
   1.109  by (res_inst_tac [("n","m")] less_induct 1);
   1.110  by (stac mod_if 1);
   1.111  by (Asm_simp_tac 1);
   1.112 @@ -78,29 +109,25 @@
   1.113  				      diff_less, diff_mult_distrib]) 1);
   1.114  qed "mod_mult_distrib";
   1.115  
   1.116 -Goal "[| 0<k; 0<n |] ==> k*(m mod n) = (k*m) mod (k*n)";
   1.117 -by (res_inst_tac [("n","m")] less_induct 1);
   1.118 -by (stac mod_if 1);
   1.119 -by (Asm_simp_tac 1);
   1.120 -by (asm_simp_tac (simpset() addsimps [mod_less, mod_geq, 
   1.121 -				      diff_less, diff_mult_distrib2]) 1);
   1.122 +Goal "(k::nat) * (m mod n) = (k*m) mod (k*n)";
   1.123 +by (asm_simp_tac 
   1.124 +    (simpset() addsimps [read_instantiate [("m","k")] mult_commute, 
   1.125 +			 mod_mult_distrib]) 1);
   1.126  qed "mod_mult_distrib2";
   1.127  
   1.128 -Goal "0<n ==> m*n mod n = 0";
   1.129 +Goal "(m*n) mod n = 0";
   1.130 +by (div_undefined_case_tac "n=0" 1);
   1.131  by (induct_tac "m" 1);
   1.132  by (asm_simp_tac (simpset() addsimps [mod_less]) 1);
   1.133 -by (dres_inst_tac [("m","na*n")] mod_add_self2 1);
   1.134 +by (rename_tac "k" 1);
   1.135 +by (cut_inst_tac [("m","k*n"),("n","n")] mod_add_self2 1);
   1.136  by (asm_full_simp_tac (simpset() addsimps [add_commute]) 1);
   1.137  qed "mod_mult_self_is_0";
   1.138  Addsimps [mod_mult_self_is_0];
   1.139  
   1.140 +
   1.141  (*** Quotient ***)
   1.142  
   1.143 -Goal "(%m. m div n) = wfrec (trancl pred_nat) \
   1.144 -\            (%f j. if j<n then 0 else Suc (f (j-n)))";
   1.145 -by (simp_tac (simpset() addsimps [div_def]) 1);
   1.146 -qed "div_eq";
   1.147 -
   1.148  Goal "m<n ==> m div n = 0";
   1.149  by (rtac (div_eq RS wf_less_trans) 1);
   1.150  by (Asm_simp_tac 1);
   1.151 @@ -120,8 +147,10 @@
   1.152  by (asm_simp_tac (simpset() addsimps [div_less, div_geq]) 1);
   1.153  qed "div_if";
   1.154  
   1.155 +
   1.156  (*Main Result about quotient and remainder.*)
   1.157 -Goal "0<n ==> (m div n)*n + m mod n = m";
   1.158 +Goal "(m div n)*n + m mod n = (m::nat)";
   1.159 +by (div_undefined_case_tac "n=0" 1);
   1.160  by (res_inst_tac [("n","m")] less_induct 1);
   1.161  by (stac mod_if 1);
   1.162  by (ALLGOALS (asm_simp_tac 
   1.163 @@ -130,8 +159,8 @@
   1.164  qed "mod_div_equality";
   1.165  
   1.166  (* a simple rearrangement of mod_div_equality: *)
   1.167 -Goal "0<k ==> k*(m div k) = m - (m mod k)";
   1.168 -by (dres_inst_tac [("m","m")] mod_div_equality 1);
   1.169 +Goal "(n::nat) * (m div n) = m - (m mod n)";
   1.170 +by (cut_inst_tac [("m","m"),("n","n")] mod_div_equality 1);
   1.171  by (EVERY1[etac subst, simp_tac (simpset() addsimps mult_ac),
   1.172             K(IF_UNSOLVED no_tac)]);
   1.173  qed "mult_div_cancel";
   1.174 @@ -168,9 +197,22 @@
   1.175  
   1.176  Addsimps [div_mult_self1, div_mult_self2];
   1.177  
   1.178 +(** A dividend of zero **)
   1.179 +
   1.180 +Goal "0 div m = 0";
   1.181 +by (div_undefined_case_tac "m=0" 1);
   1.182 +by (asm_simp_tac (simpset() addsimps [div_less]) 1);
   1.183 +qed "div_0"; 
   1.184 +
   1.185 +Goal "0 mod m = 0";
   1.186 +by (div_undefined_case_tac "m=0" 1);
   1.187 +by (asm_simp_tac (simpset() addsimps [mod_less]) 1);
   1.188 +qed "mod_0"; 
   1.189 +Addsimps [div_0, mod_0];
   1.190  
   1.191  (* Monotonicity of div in first argument *)
   1.192 -Goal "0<k ==> ALL m. m <= n --> (m div k) <= (n div k)";
   1.193 +Goal "ALL m::nat. m <= n --> (m div k) <= (n div k)";
   1.194 +by (div_undefined_case_tac "k=0" 1);
   1.195  by (res_inst_tac [("n","n")] less_induct 1);
   1.196  by (Clarify_tac 1);
   1.197  by (case_tac "n<k" 1);
   1.198 @@ -184,7 +226,6 @@
   1.199  by (asm_simp_tac (simpset() addsimps [div_geq, diff_less, diff_le_mono]) 1);
   1.200  qed_spec_mp "div_le_mono";
   1.201  
   1.202 -
   1.203  (* Antimonotonicity of div in second argument *)
   1.204  Goal "[| 0<m; m<=n |] ==> (k div n) <= (k div m)";
   1.205  by (subgoal_tac "0<n" 1);
   1.206 @@ -197,13 +238,14 @@
   1.207   by (Asm_simp_tac 2);
   1.208  by (asm_simp_tac (simpset() addsimps [div_geq]) 1);
   1.209  by (subgoal_tac "(k-n) div n <= (k-m) div n" 1);
   1.210 -by (REPEAT (eresolve_tac [div_le_mono,diff_le_mono2] 2));
   1.211 + by (REPEAT (ares_tac [div_le_mono,diff_le_mono2] 2));
   1.212  by (rtac le_trans 1);
   1.213  by (Asm_simp_tac 1);
   1.214  by (asm_simp_tac (simpset() addsimps [diff_less]) 1);
   1.215  qed "div_le_mono2";
   1.216  
   1.217 -Goal "0<n ==> m div n <= m";
   1.218 +Goal "m div n <= (m::nat)";
   1.219 +by (div_undefined_case_tac "n=0" 1);
   1.220  by (subgoal_tac "m div n <= m div 1" 1);
   1.221  by (Asm_full_simp_tac 1);
   1.222  by (rtac div_le_mono2 1);
   1.223 @@ -264,7 +306,7 @@
   1.224  (*With less_zeroE, causes case analysis on b<2*)
   1.225  AddSEs [less_SucE];
   1.226  
   1.227 -Goal "b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
   1.228 +Goal "b<2 ==> (k mod 2 = b) | (k mod 2 = (if b=1 then 0 else 1))";
   1.229  by (subgoal_tac "k mod 2 < 2" 1);
   1.230  by (asm_simp_tac (simpset() addsimps [mod_less_divisor]) 2);
   1.231  by (Asm_simp_tac 1);
   1.232 @@ -306,18 +348,18 @@
   1.233  (*** More division laws ***)
   1.234  
   1.235  Goal "0<n ==> (m*n) div n = m";
   1.236 -by (cut_inst_tac [("m", "m*n")] mod_div_equality 1);
   1.237 -by (assume_tac 1);
   1.238 +by (cut_inst_tac [("m", "m*n"),("n","n")] mod_div_equality 1);
   1.239  by (asm_full_simp_tac (simpset() addsimps [mod_mult_self_is_0]) 1);
   1.240  qed "div_mult_self_is_m";
   1.241  Addsimps [div_mult_self_is_m];
   1.242  
   1.243  (*Cancellation law for division*)
   1.244 -Goal "[| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
   1.245 +Goal "0<k ==> (k*m) div (k*n) = m div n";
   1.246 +by (div_undefined_case_tac "n=0" 1);
   1.247  by (res_inst_tac [("n","m")] less_induct 1);
   1.248  by (case_tac "na<n" 1);
   1.249  by (asm_simp_tac (simpset() addsimps [div_less, zero_less_mult_iff, 
   1.250 -                                     mult_less_mono2]) 1);
   1.251 +				      mult_less_mono2]) 1);
   1.252  by (subgoal_tac "~ k*na < k*n" 1);
   1.253  by (asm_simp_tac
   1.254       (simpset() addsimps [zero_less_mult_iff, div_geq,
   1.255 @@ -327,18 +369,7 @@
   1.256  qed "div_cancel";
   1.257  Addsimps [div_cancel];
   1.258  
   1.259 -Goal "[| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
   1.260 -by (res_inst_tac [("n","m")] less_induct 1);
   1.261 -by (case_tac "na<n" 1);
   1.262 -by (asm_simp_tac (simpset() addsimps [mod_less, zero_less_mult_iff, 
   1.263 -                                     mult_less_mono2]) 1);
   1.264 -by (subgoal_tac "~ k*na < k*n" 1);
   1.265 -by (asm_simp_tac
   1.266 -     (simpset() addsimps [zero_less_mult_iff, mod_geq,
   1.267 -                         diff_mult_distrib2 RS sym, diff_less]) 1);
   1.268 -by (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, 
   1.269 -                                          le_refl RS mult_le_mono]) 1);
   1.270 -qed "mult_mod_distrib";
   1.271 +(*mod_mult_distrib2 above is the counterpart for remainder*)
   1.272  
   1.273  
   1.274  (************************************************)
   1.275 @@ -348,7 +379,7 @@
   1.276  Goalw [dvd_def] "m dvd 0";
   1.277  by (blast_tac (claset() addIs [mult_0_right RS sym]) 1);
   1.278  qed "dvd_0_right";
   1.279 -Addsimps [dvd_0_right];
   1.280 +AddIffs [dvd_0_right];
   1.281  
   1.282  Goalw [dvd_def] "0 dvd m ==> m = 0";
   1.283  by Auto_tac;
   1.284 @@ -400,15 +431,16 @@
   1.285  
   1.286  Goalw [dvd_def] "[| f dvd m; f dvd n; 0<n |] ==> f dvd (m mod n)";
   1.287  by (Clarify_tac 1);
   1.288 -by (full_simp_tac (simpset() addsimps [zero_less_mult_iff]) 1);
   1.289 +by (Full_simp_tac 1);
   1.290  by (res_inst_tac 
   1.291      [("x", "(((k div ka)*ka + k mod ka) - ((f*k) div (f*ka)) * ka)")] 
   1.292      exI 1);
   1.293 -by (asm_simp_tac (simpset() addsimps [diff_mult_distrib2, 
   1.294 -                                     mult_mod_distrib, add_mult_distrib2]) 1);
   1.295 +by (asm_simp_tac
   1.296 +    (simpset() addsimps [diff_mult_distrib2, mod_mult_distrib2 RS sym, 
   1.297 +			 add_mult_distrib2]) 1);
   1.298  qed "dvd_mod";
   1.299  
   1.300 -Goal "[| k dvd (m mod n); k dvd n; 0<n |] ==> k dvd m";
   1.301 +Goal "[| (k::nat) dvd (m mod n);  k dvd n |] ==> k dvd m";
   1.302  by (subgoal_tac "k dvd ((m div n)*n + m mod n)" 1);
   1.303  by (asm_simp_tac (simpset() addsimps [dvd_add, dvd_mult]) 2);
   1.304  by (asm_full_simp_tac (simpset() addsimps [mod_div_equality]) 1);
   1.305 @@ -441,11 +473,11 @@
   1.306  by (Simp_tac 1);
   1.307  qed "dvd_imp_le";
   1.308  
   1.309 -Goalw [dvd_def] "0<k ==> (k dvd n) = (n mod k = 0)";
   1.310 +Goalw [dvd_def] "(k dvd n) = (n mod k = 0)";
   1.311 +by (div_undefined_case_tac "k=0" 1);
   1.312  by Safe_tac;
   1.313  by (asm_simp_tac (simpset() addsimps [mult_commute]) 1);
   1.314 -by (eres_inst_tac [("t","n")] (mod_div_equality RS subst) 1);
   1.315 +by (res_inst_tac [("t","n"),("n1","k")] (mod_div_equality RS subst) 1);
   1.316  by (stac mult_commute 1);
   1.317  by (Asm_simp_tac 1);
   1.318 -by (Blast_tac 1);
   1.319  qed "dvd_eq_mod_eq_0";
     2.1 --- a/src/HOL/Divides.thy	Sun Jul 18 11:06:08 1999 +0200
     2.2 +++ b/src/HOL/Divides.thy	Mon Jul 19 15:18:16 1999 +0200
     2.3 @@ -28,10 +28,10 @@
     2.4  defs
     2.5  
     2.6    mod_def   "m mod n == wfrec (trancl pred_nat)
     2.7 -                          (%f j. if j<n then j else f (j-n)) m"
     2.8 +                          (%f j. if j<n | n=0 then j else f (j-n)) m"
     2.9  
    2.10    div_def   "m div n == wfrec (trancl pred_nat) 
    2.11 -                          (%f j. if j<n then 0 else Suc (f (j-n))) m"
    2.12 +                          (%f j. if j<n | n=0 then 0 else Suc (f (j-n))) m"
    2.13  
    2.14  (*The definition of dvd is polymorphic!*)
    2.15    dvd_def   "m dvd n == EX k. n = m*k"