src/HOL/Arith.ML
changeset 6055 fdf4638bf726
parent 5983 79e301a6a51b
child 6059 aa00e235ea27
     1.1 --- a/src/HOL/Arith.ML	Mon Dec 28 17:03:47 1998 +0100
     1.2 +++ b/src/HOL/Arith.ML	Mon Jan 04 15:07:47 1999 +0100
     1.3 @@ -626,6 +626,7 @@
     1.4  
     1.5  signature ARITH_DATA =
     1.6  sig
     1.7 +  val nat_cancel_sums_add: simproc list
     1.8    val nat_cancel_sums: simproc list
     1.9    val nat_cancel_factor: simproc list
    1.10    val nat_cancel: simproc list
    1.11 @@ -816,11 +817,13 @@
    1.12  val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"];
    1.13  val diff_pats = prep_pats ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"];
    1.14  
    1.15 -val nat_cancel_sums = map prep_simproc
    1.16 +val nat_cancel_sums_add = map prep_simproc
    1.17    [("nateq_cancel_sums", eq_pats, EqCancelSums.proc),
    1.18     ("natless_cancel_sums", less_pats, LessCancelSums.proc),
    1.19 -   ("natle_cancel_sums", le_pats, LeCancelSums.proc),
    1.20 -   ("natdiff_cancel_sums", diff_pats, DiffCancelSums.proc)];
    1.21 +   ("natle_cancel_sums", le_pats, LeCancelSums.proc)];
    1.22 +
    1.23 +val nat_cancel_sums = nat_cancel_sums_add @
    1.24 +  [prep_simproc("natdiff_cancel_sums", diff_pats, DiffCancelSums.proc)];
    1.25  
    1.26  val nat_cancel_factor = map prep_simproc
    1.27    [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc),
    1.28 @@ -853,8 +856,9 @@
    1.29  val sym = sym;
    1.30  
    1.31  val nat = Type("nat",[]);
    1.32 +val boolT = Type("bool",[]);
    1.33  
    1.34 -fun nnb T = T = Type("fun",[nat,Type("fun",[nat,Type("bool",[])])])
    1.35 +fun nnb T = T = ([nat,nat] ---> boolT);
    1.36  
    1.37  (* Turn term into list of summand * multiplicity plus a constant *)
    1.38  fun poly(Const("Suc",_)$t, (p,i)) = poly(t, (p,i+1))
    1.39 @@ -882,13 +886,14 @@
    1.40    | decomp(_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
    1.41        negate(decomp2(rel,T,lhs,rhs))
    1.42    | decomp _ = None
    1.43 +
    1.44  (* reduce contradictory <= to False.
    1.45     Most of the work is done by the cancel tactics.
    1.46  *)
    1.47  val add_rules = [Zero_not_Suc,Suc_not_Zero,le_0_eq];
    1.48  
    1.49  val cancel_sums_ss = HOL_basic_ss addsimps add_rules
    1.50 -                                  addsimprocs nat_cancel_sums;
    1.51 +                                  addsimprocs nat_cancel_sums_add;
    1.52  
    1.53  val simp = simplify cancel_sums_ss;
    1.54  
    1.55 @@ -896,129 +901,120 @@
    1.56   (fn prems => [cut_facts_tac prems 1,
    1.57                 blast_tac (claset() addIs [add_le_mono]) 1]))
    1.58  ["(i <= j) & (k <= l) ==> i + k <= j + (l::nat)",
    1.59 - "(i = j) & (k <= l) ==> i + k <= j + (l::nat)",
    1.60 - "(i <= j) & (k = l) ==> i + k <= j + (l::nat)",
    1.61 - "(i = j) & (k = l) ==> i + k <= j + (l::nat)"
    1.62 + "(i  = j) & (k <= l) ==> i + k <= j + (l::nat)",
    1.63 + "(i <= j) & (k  = l) ==> i + k <= j + (l::nat)",
    1.64 + "(i  = j) & (k  = l) ==> i + k  = j + (l::nat)"
    1.65  ];
    1.66  
    1.67 +fun is_False thm =
    1.68 +  let val _ $ t = #prop(rep_thm thm)
    1.69 +  in t = Const("False",boolT) end;
    1.70 +
    1.71 +fun is_nat(t) = fastype_of1 t = nat;
    1.72 +
    1.73 +fun mk_nat_thm sg t =
    1.74 +  let val ct = cterm_of sg t  and cn = cterm_of sg (Var(("n",0),nat))
    1.75 +  in instantiate ([],[(cn,ct)]) le0 end;
    1.76 +
    1.77  end;
    1.78  
    1.79  structure Fast_Nat_Arith = Fast_Lin_Arith(Nat_LA_Data);
    1.80  
    1.81  simpset_ref () := (simpset() addSolver Fast_Nat_Arith.cut_lin_arith_tac);
    1.82  
    1.83 +val fast_nat_arith_tac = Fast_Nat_Arith.lin_arith_tac;
    1.84 +
    1.85 +(* Elimination of `-' on nat due to John Harrison *)
    1.86 +Goal "P(a - b::nat) = (!d. (b = a + d --> P 0) & (a = b + d --> P d))";
    1.87 +by(case_tac "a <= b" 1);
    1.88 +by(Auto_tac);
    1.89 +by(eres_inst_tac [("x","b-a")] allE 1);
    1.90 +by(Auto_tac);
    1.91 +qed "nat_diff_split";
    1.92 +
    1.93 +(* FIXME: K true should be replaced by a sensible test to speed things up
    1.94 +   in case there are lots of irrelevant terms involved
    1.95 +*)
    1.96 +val nat_arith_tac =
    1.97 +  refute_tac (K true) (REPEAT o split_tac[nat_diff_split])
    1.98 +             ((REPEAT o etac nat_neqE) THEN' fast_nat_arith_tac);
    1.99 +
   1.100  (*---------------------------------------------------------------------------*)
   1.101  (* End of proof procedures. Now go and USE them!                             *)
   1.102  (*---------------------------------------------------------------------------*)
   1.103  
   1.104 -
   1.105  (*** Subtraction laws -- mostly from Clemens Ballarin ***)
   1.106  
   1.107  Goal "[| a < (b::nat); c <= a |] ==> a-c < b-c";
   1.108 -by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   1.109 -by (Full_simp_tac 1);
   1.110 -by (subgoal_tac "c <= b" 1);
   1.111 -by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
   1.112 -by (Asm_simp_tac 1);
   1.113 +by(nat_arith_tac 1);
   1.114  qed "diff_less_mono";
   1.115  
   1.116  Goal "a+b < (c::nat) ==> a < c-b";
   1.117 -by (dtac diff_less_mono 1);
   1.118 -by (rtac le_add2 1);
   1.119 -by (Asm_full_simp_tac 1);
   1.120 +by(nat_arith_tac 1);
   1.121  qed "add_less_imp_less_diff";
   1.122  
   1.123  Goal "(i < j-k) = (i+k < (j::nat))";
   1.124 -by (rtac iffI 1);
   1.125 - by (case_tac "k <= j" 1);
   1.126 -  by (dtac le_add_diff_inverse2 1);
   1.127 -  by (dres_inst_tac [("k","k")] add_less_mono1 1);
   1.128 -  by (Asm_full_simp_tac 1);
   1.129 - by (rotate_tac 1 1);
   1.130 - by (Asm_full_simp_tac 1);
   1.131 -by (etac add_less_imp_less_diff 1);
   1.132 +by(nat_arith_tac 1);
   1.133  qed "less_diff_conv";
   1.134  
   1.135  Goal "(j-k <= (i::nat)) = (j <= i+k)";
   1.136 -by (simp_tac (simpset() addsimps [less_diff_conv, le_def]) 1);
   1.137 +by(nat_arith_tac 1);
   1.138  qed "le_diff_conv";
   1.139  
   1.140  Goal "k <= j ==> (i <= j-k) = (i+k <= (j::nat))";
   1.141 -by (asm_full_simp_tac
   1.142 -    (simpset() delsimps [less_Suc_eq_le]
   1.143 -               addsimps [less_Suc_eq_le RS sym, less_diff_conv,
   1.144 -			 Suc_diff_le RS sym]) 1);
   1.145 +by(nat_arith_tac 1);
   1.146  qed "le_diff_conv2";
   1.147  
   1.148  Goal "Suc i <= n ==> Suc (n - Suc i) = n - i";
   1.149 -by (asm_full_simp_tac (simpset() addsimps [Suc_diff_le RS sym]) 1);
   1.150 +by(nat_arith_tac 1);
   1.151  qed "Suc_diff_Suc";
   1.152  
   1.153  Goal "i <= (n::nat) ==> n - (n - i) = i";
   1.154 -by (etac rev_mp 1);
   1.155 -by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
   1.156 -by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
   1.157 +by(nat_arith_tac 1);
   1.158  qed "diff_diff_cancel";
   1.159  Addsimps [diff_diff_cancel];
   1.160  
   1.161  Goal "k <= (n::nat) ==> m <= n + m - k";
   1.162 -by (etac rev_mp 1);
   1.163 -by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   1.164 -by (Simp_tac 1);
   1.165 -by (simp_tac (simpset() addsimps [le_add2, less_imp_le]) 1);
   1.166 -by (Simp_tac 1);
   1.167 +by(nat_arith_tac 1);
   1.168  qed "le_add_diff";
   1.169  
   1.170 -Goal "0<k ==> j<i --> j+k-i < k";
   1.171 -by (res_inst_tac [("m","j"),("n","i")] diff_induct 1);
   1.172 -by (ALLGOALS Asm_simp_tac);
   1.173 -qed_spec_mp "add_diff_less";
   1.174 -
   1.175 +Goal "[| 0<k; j<i |] ==> j+k-i < k";
   1.176 +by(nat_arith_tac 1);
   1.177 +qed "add_diff_less";
   1.178  
   1.179  Goal "m-1 < n ==> m <= n";
   1.180 -by (exhaust_tac "m" 1);
   1.181 -by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
   1.182 +by(nat_arith_tac 1);
   1.183  qed "pred_less_imp_le";
   1.184  
   1.185  Goal "j<=i ==> i - j < Suc i - j";
   1.186 -by (REPEAT (etac rev_mp 1));
   1.187 -by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   1.188 -by Auto_tac;
   1.189 +by(nat_arith_tac 1);
   1.190  qed "diff_less_Suc_diff";
   1.191  
   1.192  Goal "i - j <= Suc i - j";
   1.193 -by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   1.194 -by Auto_tac;
   1.195 +by(nat_arith_tac 1);
   1.196  qed "diff_le_Suc_diff";
   1.197  AddIffs [diff_le_Suc_diff];
   1.198  
   1.199  Goal "n - Suc i <= n - i";
   1.200 -by (case_tac "i<n" 1);
   1.201 -by (dtac diff_Suc_less_diff 1);
   1.202 -by (auto_tac (claset(), simpset() addsimps [less_imp_le, leI]));
   1.203 +by(nat_arith_tac 1);
   1.204  qed "diff_Suc_le_diff";
   1.205  AddIffs [diff_Suc_le_diff];
   1.206  
   1.207  Goal "0 < n ==> (m <= n-1) = (m<n)";
   1.208 -by (exhaust_tac "n" 1);
   1.209 -by (auto_tac (claset(), simpset() addsimps le_simps));
   1.210 +by(nat_arith_tac 1);
   1.211  qed "le_pred_eq";
   1.212  
   1.213  Goal "0 < n ==> (m-1 < n) = (m<=n)";
   1.214 -by (exhaust_tac "m" 1);
   1.215 -by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
   1.216 +by(nat_arith_tac 1);
   1.217  qed "less_pred_eq";
   1.218  
   1.219  (*In ordinary notation: if 0<n and n<=m then m-n < m *)
   1.220  Goal "[| 0<n; ~ m<n |] ==> m - n < m";
   1.221 -by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
   1.222 -by (Blast_tac 1);
   1.223 -by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   1.224 -by (ALLGOALS(asm_simp_tac(simpset() addsimps [diff_less_Suc])));
   1.225 +by(nat_arith_tac 1);
   1.226  qed "diff_less";
   1.227  
   1.228  Goal "[| 0<n; n<=m |] ==> m - n < m";
   1.229 -by (asm_simp_tac (simpset() addsimps [diff_less, not_less_iff_le]) 1);
   1.230 +by(nat_arith_tac 1);
   1.231  qed "le_diff_less";
   1.232  
   1.233  
   1.234 @@ -1026,52 +1022,20 @@
   1.235  (** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
   1.236  
   1.237  (* Monotonicity of subtraction in first argument *)
   1.238 -Goal "m <= (n::nat) --> (m-l) <= (n-l)";
   1.239 -by (induct_tac "n" 1);
   1.240 -by (Simp_tac 1);
   1.241 -by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
   1.242 -by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1);
   1.243 -qed_spec_mp "diff_le_mono";
   1.244 +Goal "m <= (n::nat) ==> (m-l) <= (n-l)";
   1.245 +by(nat_arith_tac 1);
   1.246 +qed "diff_le_mono";
   1.247  
   1.248  Goal "m <= (n::nat) ==> (l-n) <= (l-m)";
   1.249 -by (induct_tac "l" 1);
   1.250 -by (Simp_tac 1);
   1.251 -by (case_tac "n <= na" 1);
   1.252 -by (subgoal_tac "m <= na" 1);
   1.253 -by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
   1.254 -by (fast_tac (claset() addEs [le_trans]) 1);
   1.255 -by (dtac not_leE 1);
   1.256 -by (asm_simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
   1.257 -qed_spec_mp "diff_le_mono2";
   1.258 +by(nat_arith_tac 1);
   1.259 +qed "diff_le_mono2";
   1.260  
   1.261  
   1.262  (*This proof requires natdiff_cancel_sums*)
   1.263 -Goal "m < (n::nat) --> m<l --> (l-n) < (l-m)";
   1.264 -by (induct_tac "l" 1);
   1.265 -by (Simp_tac 1);
   1.266 -by (Clarify_tac 1);
   1.267 -by (etac less_SucE 1);
   1.268 -by (Clarify_tac 2);
   1.269 -by (asm_simp_tac (simpset() addsimps [Suc_le_eq]) 2);
   1.270 -by (asm_simp_tac (simpset() addsimps [diff_Suc_le_Suc_diff RS le_less_trans,
   1.271 -				      Suc_diff_le, less_imp_le]) 1);
   1.272 -qed_spec_mp "diff_less_mono2";
   1.273 -
   1.274 -(** Elimination of `-' on nat due to John Harrison **)
   1.275 -(*This proof requires natle_cancel_sums*)
   1.276 +Goal "[| m < (n::nat); m<l |] ==> (l-n) < (l-m)";
   1.277 +by(nat_arith_tac 1);
   1.278 +qed "diff_less_mono2";
   1.279  
   1.280 -Goal "P(a - b::nat) = (!d. (b = a + d --> P 0) & (a = b + d --> P d))";
   1.281 -by(case_tac "a <= b" 1);
   1.282 -by(Auto_tac);
   1.283 -by(eres_inst_tac [("x","b-a")] allE 1);
   1.284 -by(Auto_tac);
   1.285 -qed "nat_diff_split";
   1.286 -
   1.287 -(*
   1.288 -This is an example of the power of nat_diff_split. Many of the `-' thms in
   1.289 -Arith.ML could take advantage of this, but would need to be moved.
   1.290 -*)
   1.291 -Goal "m-n = 0  -->  n-m = 0  -->  m=n";
   1.292 -by(simp_tac (simpset() addsplits [nat_diff_split]) 1);
   1.293 -qed_spec_mp "diffs0_imp_equal";
   1.294 -
   1.295 +Goal "[| m-n = 0; n-m = 0 |] ==>  m=n";
   1.296 +by(nat_arith_tac 1);
   1.297 +qed "diffs0_imp_equal";