src/HOL/Integ/IntArith.ML
author wenzelm
Tue May 30 16:08:38 2000 +0200 (2000-05-30)
changeset 9000 c20d58286a51
parent 8834 074503906abf
child 9063 0d7628966069
permissions -rw-r--r--
cleaned up;
     1 (*  Title:      HOL/Integ/IntArith.thy
     2     ID:         $Id$
     3     Authors:    Larry Paulson and Tobias Nipkow
     4 
     5 Simprocs and decision procedure for linear arithmetic.
     6 *)
     7 
     8 (*** Simprocs for numeric literals ***)
     9 
    10 (** Combining of literal coefficients in sums of products **)
    11 
    12 Goal "(x < y) = (x-y < (#0::int))";
    13 by (simp_tac (simpset() addsimps zcompare_rls) 1);
    14 qed "zless_iff_zdiff_zless_0";
    15 
    16 Goal "(x = y) = (x-y = (#0::int))";
    17 by (simp_tac (simpset() addsimps zcompare_rls) 1);
    18 qed "eq_iff_zdiff_eq_0";
    19 
    20 Goal "(x <= y) = (x-y <= (#0::int))";
    21 by (simp_tac (simpset() addsimps zcompare_rls) 1);
    22 qed "zle_iff_zdiff_zle_0";
    23 
    24 
    25 (** For combine_numerals **)
    26 
    27 Goal "i*u + (j*u + k) = (i+j)*u + (k::int)";
    28 by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 1);
    29 qed "left_zadd_zmult_distrib";
    30 
    31 
    32 (** For cancel_numerals **)
    33 
    34 Goal "!!i::int. ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)";
    35 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
    36 qed "diff_add_eq1";
    37 
    38 Goal "!!i::int. ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))";
    39 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
    40 qed "diff_add_eq2";
    41 
    42 val rel_iff_rel_0_rls = map (inst "y" "?u+?v")
    43                           [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0, 
    44 			   zle_iff_zdiff_zle_0] @
    45 		        map (inst "y" "n")
    46                           [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0, 
    47 			   zle_iff_zdiff_zle_0];
    48 
    49 Goal "!!i::int. (i*u + m = j*u + n) = ((i-j)*u + m = n)";
    50 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
    51 		                     zadd_ac@rel_iff_rel_0_rls) 1);
    52 qed "eq_add_iff1";
    53 
    54 Goal "!!i::int. (i*u + m = j*u + n) = (m = (j-i)*u + n)";
    55 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
    56                                      zadd_ac@rel_iff_rel_0_rls) 1);
    57 qed "eq_add_iff2";
    58 
    59 Goal "!!i::int. (i*u + m < j*u + n) = ((i-j)*u + m < n)";
    60 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
    61                                      zadd_ac@rel_iff_rel_0_rls) 1);
    62 qed "less_add_iff1";
    63 
    64 Goal "!!i::int. (i*u + m < j*u + n) = (m < (j-i)*u + n)";
    65 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
    66                                      zadd_ac@rel_iff_rel_0_rls) 1);
    67 qed "less_add_iff2";
    68 
    69 Goal "!!i::int. (i*u + m <= j*u + n) = ((i-j)*u + m <= n)";
    70 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
    71                                      zadd_ac@rel_iff_rel_0_rls) 1);
    72 qed "le_add_iff1";
    73 
    74 Goal "!!i::int. (i*u + m <= j*u + n) = (m <= (j-i)*u + n)";
    75 by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]
    76                                      @zadd_ac@rel_iff_rel_0_rls) 1);
    77 qed "le_add_iff2";
    78 
    79 (*To tidy up the result of a simproc.  Only the RHS will be simplified.*)
    80 Goal "u = u' ==> (t==u) == (t==u')";
    81 by Auto_tac;
    82 qed "eq_cong2";
    83 
    84 
    85 structure Int_Numeral_Simprocs =
    86 struct
    87 
    88 (*Utilities*)
    89 
    90 fun mk_numeral n = HOLogic.number_of_const HOLogic.intT $ 
    91                    NumeralSyntax.mk_bin n;
    92 
    93 (*Decodes a binary INTEGER*)
    94 fun dest_numeral (Const("Numeral.number_of", _) $ w) = 
    95      (NumeralSyntax.dest_bin w
    96       handle Match => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
    97   | dest_numeral t = raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
    98 
    99 fun find_first_numeral past (t::terms) =
   100 	((dest_numeral t, rev past @ terms)
   101 	 handle TERM _ => find_first_numeral (t::past) terms)
   102   | find_first_numeral past [] = raise TERM("find_first_numeral", []);
   103 
   104 val zero = mk_numeral 0;
   105 val mk_plus = HOLogic.mk_binop "op +";
   106 
   107 val uminus_const = Const ("uminus", HOLogic.intT --> HOLogic.intT);
   108 
   109 (*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
   110 fun mk_sum []        = zero
   111   | mk_sum [t,u]     = mk_plus (t, u)
   112   | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
   113 
   114 (*this version ALWAYS includes a trailing zero*)
   115 fun long_mk_sum []        = zero
   116   | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
   117 
   118 val dest_plus = HOLogic.dest_bin "op +" HOLogic.intT;
   119 
   120 (*decompose additions AND subtractions as a sum*)
   121 fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
   122         dest_summing (pos, t, dest_summing (pos, u, ts))
   123   | dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
   124         dest_summing (pos, t, dest_summing (not pos, u, ts))
   125   | dest_summing (pos, t, ts) =
   126 	if pos then t::ts else uminus_const$t :: ts;
   127 
   128 fun dest_sum t = dest_summing (true, t, []);
   129 
   130 val mk_diff = HOLogic.mk_binop "op -";
   131 val dest_diff = HOLogic.dest_bin "op -" HOLogic.intT;
   132 
   133 val one = mk_numeral 1;
   134 val mk_times = HOLogic.mk_binop "op *";
   135 
   136 fun mk_prod [] = one
   137   | mk_prod [t] = t
   138   | mk_prod (t :: ts) = if t = one then mk_prod ts
   139                         else mk_times (t, mk_prod ts);
   140 
   141 val dest_times = HOLogic.dest_bin "op *" HOLogic.intT;
   142 
   143 fun dest_prod t =
   144       let val (t,u) = dest_times t 
   145       in  dest_prod t @ dest_prod u  end
   146       handle TERM _ => [t];
   147 
   148 (*DON'T do the obvious simplifications; that would create special cases*) 
   149 fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
   150 
   151 (*Express t as a product of (possibly) a numeral with other sorted terms*)
   152 fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
   153   | dest_coeff sign t =
   154     let val ts = sort Term.term_ord (dest_prod t)
   155 	val (n, ts') = find_first_numeral [] ts
   156                           handle TERM _ => (1, ts)
   157     in (sign*n, mk_prod ts') end;
   158 
   159 (*Find first coefficient-term THAT MATCHES u*)
   160 fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) 
   161   | find_first_coeff past u (t::terms) =
   162 	let val (n,u') = dest_coeff 1 t
   163 	in  if u aconv u' then (n, rev past @ terms)
   164 			  else find_first_coeff (t::past) u terms
   165 	end
   166 	handle TERM _ => find_first_coeff (t::past) u terms;
   167 
   168 
   169 (*Simplify #1*n and n*#1 to n*)
   170 val add_0s = [zadd_0, zadd_0_right];
   171 val mult_1s = [zmult_1, zmult_1_right, zmult_minus1, zmult_minus1_right];
   172 
   173 (*To perform binary arithmetic*)
   174 val bin_simps = [number_of_add RS sym, add_number_of_left] @ 
   175                 bin_arith_simps @ bin_rel_simps;
   176 
   177 (*To evaluate binary negations of coefficients*)
   178 val zminus_simps = NCons_simps @
   179                    [number_of_minus RS sym, 
   180 		    bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
   181 		    bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
   182 
   183 (*To let us treat subtraction as addition*)
   184 val diff_simps = [zdiff_def, zminus_zadd_distrib, zminus_zminus];
   185 
   186 (*Apply the given rewrite (if present) just once*)
   187 fun trans_tac None      = all_tac
   188   | trans_tac (Some th) = ALLGOALS (rtac (th RS trans));
   189 
   190 fun prove_conv name tacs sg (t, u) =
   191   if t aconv u then None
   192   else
   193   let val ct = cterm_of sg (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u)))
   194   in Some
   195      (prove_goalw_cterm [] ct (K tacs)
   196       handle ERROR => error 
   197 	  ("The error(s) above occurred while trying to prove " ^
   198 	   string_of_cterm ct ^ "\nInternal failure of simproc " ^ name))
   199   end;
   200 
   201 fun simplify_meta_eq rules =
   202     mk_meta_eq o
   203     simplify (HOL_basic_ss addeqcongs[eq_cong2] addsimps rules)
   204 
   205 fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
   206 fun prep_pat s = Thm.read_cterm (Theory.sign_of Int.thy) (s, HOLogic.termT);
   207 val prep_pats = map prep_pat;
   208 
   209 structure CancelNumeralsCommon =
   210   struct
   211   val mk_sum    	= mk_sum
   212   val dest_sum		= dest_sum
   213   val mk_coeff		= mk_coeff
   214   val dest_coeff	= dest_coeff 1
   215   val find_first_coeff	= find_first_coeff []
   216   val trans_tac         = trans_tac
   217   val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
   218                                                      zminus_simps@zadd_ac))
   219                  THEN ALLGOALS
   220                     (simp_tac (HOL_ss addsimps [zmult_zminus_right RS sym]@
   221                                                bin_simps@zadd_ac@zmult_ac))
   222   val numeral_simp_tac	= ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
   223   val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
   224   end;
   225 
   226 
   227 structure EqCancelNumerals = CancelNumeralsFun
   228  (open CancelNumeralsCommon
   229   val prove_conv = prove_conv "inteq_cancel_numerals"
   230   val mk_bal   = HOLogic.mk_eq
   231   val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
   232   val bal_add1 = eq_add_iff1 RS trans
   233   val bal_add2 = eq_add_iff2 RS trans
   234 );
   235 
   236 structure LessCancelNumerals = CancelNumeralsFun
   237  (open CancelNumeralsCommon
   238   val prove_conv = prove_conv "intless_cancel_numerals"
   239   val mk_bal   = HOLogic.mk_binrel "op <"
   240   val dest_bal = HOLogic.dest_bin "op <" HOLogic.intT
   241   val bal_add1 = less_add_iff1 RS trans
   242   val bal_add2 = less_add_iff2 RS trans
   243 );
   244 
   245 structure LeCancelNumerals = CancelNumeralsFun
   246  (open CancelNumeralsCommon
   247   val prove_conv = prove_conv "intle_cancel_numerals"
   248   val mk_bal   = HOLogic.mk_binrel "op <="
   249   val dest_bal = HOLogic.dest_bin "op <=" HOLogic.intT
   250   val bal_add1 = le_add_iff1 RS trans
   251   val bal_add2 = le_add_iff2 RS trans
   252 );
   253 
   254 val cancel_numerals = 
   255   map prep_simproc
   256    [("inteq_cancel_numerals",
   257      prep_pats ["(l::int) + m = n", "(l::int) = m + n", 
   258 		"(l::int) - m = n", "(l::int) = m - n", 
   259 		"(l::int) * m = n", "(l::int) = m * n"], 
   260      EqCancelNumerals.proc),
   261     ("intless_cancel_numerals", 
   262      prep_pats ["(l::int) + m < n", "(l::int) < m + n", 
   263 		"(l::int) - m < n", "(l::int) < m - n", 
   264 		"(l::int) * m < n", "(l::int) < m * n"], 
   265      LessCancelNumerals.proc),
   266     ("intle_cancel_numerals", 
   267      prep_pats ["(l::int) + m <= n", "(l::int) <= m + n", 
   268 		"(l::int) - m <= n", "(l::int) <= m - n", 
   269 		"(l::int) * m <= n", "(l::int) <= m * n"], 
   270      LeCancelNumerals.proc)];
   271 
   272 
   273 structure CombineNumeralsData =
   274   struct
   275   val mk_sum    	= long_mk_sum    (*to work for e.g. #2*x + #3*x *)
   276   val dest_sum		= dest_sum
   277   val mk_coeff		= mk_coeff
   278   val dest_coeff	= dest_coeff 1
   279   val left_distrib	= left_zadd_zmult_distrib RS trans
   280   val prove_conv	= prove_conv "int_combine_numerals"
   281   val trans_tac          = trans_tac
   282   val norm_tac = ALLGOALS
   283                    (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
   284                                               zminus_simps@zadd_ac))
   285                  THEN ALLGOALS
   286                     (simp_tac (HOL_ss addsimps [zmult_zminus_right RS sym]@
   287                                                bin_simps@zadd_ac@zmult_ac))
   288   val numeral_simp_tac	= ALLGOALS 
   289                     (simp_tac (HOL_ss addsimps add_0s@bin_simps))
   290   val simplify_meta_eq  = simplify_meta_eq (add_0s@mult_1s)
   291   end;
   292 
   293 structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
   294   
   295 val combine_numerals = 
   296     prep_simproc ("int_combine_numerals",
   297 		  prep_pats ["(i::int) + j", "(i::int) - j"],
   298 		  CombineNumerals.proc);
   299 
   300 end;
   301 
   302 
   303 Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
   304 Addsimprocs [Int_Numeral_Simprocs.combine_numerals];
   305 
   306 (*The Abel_Cancel simprocs are now obsolete*)
   307 Delsimprocs [Int_Cancel.sum_conv, Int_Cancel.rel_conv];
   308 
   309 (*examples:
   310 print_depth 22;
   311 set timing;
   312 set trace_simp;
   313 fun test s = (Goal s; by (Simp_tac 1)); 
   314 
   315 test "l + #2 + #2 + #2 + (l + #2) + (oo + #2) = (uu::int)";
   316 
   317 test "#2*u = (u::int)";
   318 test "(i + j + #12 + (k::int)) - #15 = y";
   319 test "(i + j + #12 + (k::int)) - #5 = y";
   320 
   321 test "y - b < (b::int)";
   322 test "y - (#3*b + c) < (b::int) - #2*c";
   323 
   324 test "(#2*x - (u*v) + y) - v*#3*u = (w::int)";
   325 test "(#2*x*u*v + (u*v)*#4 + y) - v*u*#4 = (w::int)";
   326 test "(#2*x*u*v + (u*v)*#4 + y) - v*u = (w::int)";
   327 test "u*v - (x*u*v + (u*v)*#4 + y) = (w::int)";
   328 
   329 test "(i + j + #12 + (k::int)) = u + #15 + y";
   330 test "(i + j*#2 + #12 + (k::int)) = j + #5 + y";
   331 
   332 test "#2*y + #3*z + #6*w + #2*y + #3*z + #2*u = #2*y' + #3*z' + #6*w' + #2*y' + #3*z' + u + (vv::int)";
   333 
   334 test "a + -(b+c) + b = (d::int)";
   335 test "a + -(b+c) - b = (d::int)";
   336 
   337 (*negative numerals*)
   338 test "(i + j + #-2 + (k::int)) - (u + #5 + y) = zz";
   339 test "(i + j + #-3 + (k::int)) < u + #5 + y";
   340 test "(i + j + #3 + (k::int)) < u + #-6 + y";
   341 test "(i + j + #-12 + (k::int)) - #15 = y";
   342 test "(i + j + #12 + (k::int)) - #-15 = y";
   343 test "(i + j + #-12 + (k::int)) - #-15 = y";
   344 *)
   345 
   346 
   347 (** Constant folding for integer plus and times **)
   348 
   349 (*We do not need
   350     structure Nat_Plus_Assoc = Assoc_Fold (Nat_Plus_Assoc_Data);
   351     structure Int_Plus_Assoc = Assoc_Fold (Int_Plus_Assoc_Data);
   352   because combine_numerals does the same thing*)
   353 
   354 structure Int_Times_Assoc_Data : ASSOC_FOLD_DATA =
   355 struct
   356   val ss		= HOL_ss
   357   val eq_reflection	= eq_reflection
   358   val thy    = Bin.thy
   359   val T	     = HOLogic.intT
   360   val plus   = Const ("op *", [HOLogic.intT,HOLogic.intT] ---> HOLogic.intT);
   361   val add_ac = zmult_ac
   362 end;
   363 
   364 structure Int_Times_Assoc = Assoc_Fold (Int_Times_Assoc_Data);
   365 
   366 Addsimprocs [Int_Times_Assoc.conv];
   367 
   368 
   369 (** The same for the naturals **)
   370 
   371 structure Nat_Times_Assoc_Data : ASSOC_FOLD_DATA =
   372 struct
   373   val ss		= HOL_ss
   374   val eq_reflection	= eq_reflection
   375   val thy    = Bin.thy
   376   val T	     = HOLogic.natT
   377   val plus   = Const ("op *", [HOLogic.natT,HOLogic.natT] ---> HOLogic.natT);
   378   val add_ac = mult_ac
   379 end;
   380 
   381 structure Nat_Times_Assoc = Assoc_Fold (Nat_Times_Assoc_Data);
   382 
   383 Addsimprocs [Nat_Times_Assoc.conv];
   384 
   385 
   386 
   387 (*** decision procedure for linear arithmetic ***)
   388 
   389 (*---------------------------------------------------------------------------*)
   390 (* Linear arithmetic                                                         *)
   391 (*---------------------------------------------------------------------------*)
   392 
   393 (*
   394 Instantiation of the generic linear arithmetic package for int.
   395 *)
   396 
   397 (* Update parameters of arithmetic prover *)
   398 let
   399 
   400 (* reduce contradictory <= to False *)
   401 val add_rules = simp_thms @ bin_arith_simps @ bin_rel_simps @
   402                 [int_0, zadd_0, zadd_0_right, zdiff_def,
   403 		 zadd_zminus_inverse, zadd_zminus_inverse2, 
   404 		 zmult_0, zmult_0_right, 
   405 		 zmult_1, zmult_1_right, 
   406 		 zmult_minus1, zmult_minus1_right];
   407 
   408 val simprocs = [Int_Times_Assoc.conv, Int_Numeral_Simprocs.combine_numerals]@
   409                Int_Numeral_Simprocs.cancel_numerals;
   410 
   411 val add_mono_thms =
   412   map (fn s => prove_goal Int.thy s
   413                  (fn prems => [cut_facts_tac prems 1,
   414                       asm_simp_tac (simpset() addsimps [zadd_zle_mono]) 1]))
   415     ["(i <= j) & (k <= l) ==> i + k <= j + (l::int)",
   416      "(i  = j) & (k <= l) ==> i + k <= j + (l::int)",
   417      "(i <= j) & (k  = l) ==> i + k <= j + (l::int)",
   418      "(i  = j) & (k  = l) ==> i + k  = j + (l::int)"
   419     ];
   420 
   421 in
   422 LA_Data_Ref.add_mono_thms := !LA_Data_Ref.add_mono_thms @ add_mono_thms;
   423 LA_Data_Ref.lessD := !LA_Data_Ref.lessD @ [add1_zle_eq RS iffD2];
   424 LA_Data_Ref.ss_ref := !LA_Data_Ref.ss_ref addsimps add_rules
   425                       addsimprocs simprocs
   426                       addcongs [if_weak_cong];
   427 LA_Data_Ref.discrete := !LA_Data_Ref.discrete @ [("IntDef.int",true)]
   428 end;
   429 
   430 let
   431 val int_arith_simproc_pats =
   432   map (fn s => Thm.read_cterm (Theory.sign_of Int.thy) (s, HOLogic.boolT))
   433       ["(m::int) < n","(m::int) <= n", "(m::int) = n"];
   434 
   435 val fast_int_arith_simproc = mk_simproc
   436   "fast_int_arith" int_arith_simproc_pats Fast_Arith.lin_arith_prover;
   437 in
   438 Addsimprocs [fast_int_arith_simproc]
   439 end;
   440 
   441 (* Some test data
   442 Goal "!!a::int. [| a <= b; c <= d; x+y<z |] ==> a+c <= b+d";
   443 by (fast_arith_tac 1);
   444 Goal "!!a::int. [| a < b; c < d |] ==> a-d+ #2 <= b+(-c)";
   445 by (fast_arith_tac 1);
   446 Goal "!!a::int. [| a < b; c < d |] ==> a+c+ #1 < b+d";
   447 by (fast_arith_tac 1);
   448 Goal "!!a::int. [| a <= b; b+b <= c |] ==> a+a <= c";
   449 by (fast_arith_tac 1);
   450 Goal "!!a::int. [| a+b <= i+j; a<=b; i<=j |] \
   451 \     ==> a+a <= j+j";
   452 by (fast_arith_tac 1);
   453 Goal "!!a::int. [| a+b < i+j; a<b; i<j |] \
   454 \     ==> a+a - - #-1 < j+j - #3";
   455 by (fast_arith_tac 1);
   456 Goal "!!a::int. a+b+c <= i+j+k & a<=b & b<=c & i<=j & j<=k --> a+a+a <= k+k+k";
   457 by (arith_tac 1);
   458 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   459 \     ==> a <= l";
   460 by (fast_arith_tac 1);
   461 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   462 \     ==> a+a+a+a <= l+l+l+l";
   463 by (fast_arith_tac 1);
   464 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   465 \     ==> a+a+a+a+a <= l+l+l+l+i";
   466 by (fast_arith_tac 1);
   467 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   468 \     ==> a+a+a+a+a+a <= l+l+l+l+i+l";
   469 by (fast_arith_tac 1);
   470 Goal "!!a::int. [| a+b+c+d <= i+j+k+l; a<=b; b<=c; c<=d; i<=j; j<=k; k<=l |] \
   471 \     ==> #6*a <= #5*l+i";
   472 by (fast_arith_tac 1);
   473 *)
   474 
   475 (*---------------------------------------------------------------------------*)
   476 (* End of linear arithmetic                                                  *)
   477 (*---------------------------------------------------------------------------*)
   478 
   479 (** Simplification of inequalities involving numerical constants **)
   480 
   481 Goal "(w <= z + (#1::int)) = (w<=z | w = z + (#1::int))";
   482 by (arith_tac 1);
   483 qed "zle_add1_eq";
   484 
   485 Goal "(w <= z - (#1::int)) = (w<(z::int))";
   486 by (arith_tac 1);
   487 qed "zle_diff1_eq";
   488 Addsimps [zle_diff1_eq];
   489 
   490 (*2nd premise can be proved automatically if v is a literal*)
   491 Goal "[| w <= z; #0 <= v |] ==> w <= z + (v::int)";
   492 by (fast_arith_tac 1);
   493 qed "zle_imp_zle_zadd";
   494 
   495 Goal "w <= z ==> w <= z + (#1::int)";
   496 by (fast_arith_tac 1);
   497 qed "zle_imp_zle_zadd1";
   498 
   499 (*2nd premise can be proved automatically if v is a literal*)
   500 Goal "[| w < z; #0 <= v |] ==> w < z + (v::int)";
   501 by (fast_arith_tac 1);
   502 qed "zless_imp_zless_zadd";
   503 
   504 Goal "w < z ==> w < z + (#1::int)";
   505 by (fast_arith_tac 1);
   506 qed "zless_imp_zless_zadd1";
   507 
   508 Goal "(w < z + #1) = (w<=(z::int))";
   509 by (arith_tac 1);
   510 qed "zle_add1_eq_le";
   511 Addsimps [zle_add1_eq_le];
   512 
   513 Goal "(z = z + w) = (w = (#0::int))";
   514 by (arith_tac 1);
   515 qed "zadd_left_cancel0";
   516 Addsimps [zadd_left_cancel0];
   517 
   518 (*LOOPS as a simprule!*)
   519 Goal "[| w + v < z; #0 <= v |] ==> w < (z::int)";
   520 by (fast_arith_tac 1);
   521 qed "zless_zadd_imp_zless";
   522 
   523 (*LOOPS as a simprule!  Analogous to Suc_lessD*)
   524 Goal "w + #1 < z ==> w < (z::int)";
   525 by (fast_arith_tac 1);
   526 qed "zless_zadd1_imp_zless";
   527 
   528 Goal "w + #-1 = w - (#1::int)";
   529 by (Simp_tac 1);
   530 qed "zplus_minus1_conv";
   531 
   532 
   533 (* nat *)
   534 
   535 Goal "#0 <= z ==> int (nat z) = z"; 
   536 by (asm_full_simp_tac
   537     (simpset() addsimps [neg_eq_less_0, zle_def, not_neg_nat]) 1); 
   538 qed "nat_0_le"; 
   539 
   540 Goal "z <= #0 ==> nat z = 0"; 
   541 by (case_tac "z = #0" 1);
   542 by (asm_simp_tac (simpset() addsimps [nat_le_int0]) 1); 
   543 by (asm_full_simp_tac 
   544     (simpset() addsimps [neg_eq_less_0, neg_nat, linorder_neq_iff]) 1);
   545 qed "nat_le_0"; 
   546 
   547 Addsimps [nat_0_le, nat_le_0];
   548 
   549 val [major,minor] = Goal "[| #0 <= z;  !!m. z = int m ==> P |] ==> P"; 
   550 by (rtac (major RS nat_0_le RS sym RS minor) 1);
   551 qed "nonneg_eq_int"; 
   552 
   553 Goal "#0 <= w ==> (nat w = m) = (w = int m)";
   554 by Auto_tac;
   555 qed "nat_eq_iff";
   556 
   557 Goal "#0 <= w ==> (m = nat w) = (w = int m)";
   558 by Auto_tac;
   559 qed "nat_eq_iff2";
   560 
   561 Goal "#0 <= w ==> (nat w < m) = (w < int m)";
   562 by (rtac iffI 1);
   563 by (asm_full_simp_tac 
   564     (simpset() delsimps [zless_int] addsimps [zless_int RS sym]) 2);
   565 by (etac (nat_0_le RS subst) 1);
   566 by (Simp_tac 1);
   567 qed "nat_less_iff";
   568 
   569 
   570 (*Users don't want to see (int 0), int(Suc 0) or w + - z*)
   571 Addsimps [int_0, int_Suc, symmetric zdiff_def];
   572 
   573 Goal "nat #0 = 0";
   574 by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
   575 qed "nat_0";
   576 
   577 Goal "nat #1 = 1";
   578 by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
   579 qed "nat_1";
   580 
   581 Goal "nat #2 = 2";
   582 by (simp_tac (simpset() addsimps [nat_eq_iff]) 1);
   583 qed "nat_2";
   584 
   585 Goal "#0 <= w ==> (nat w < nat z) = (w<z)";
   586 by (case_tac "neg z" 1);
   587 by (auto_tac (claset(), simpset() addsimps [nat_less_iff]));
   588 by (auto_tac (claset() addIs [zless_trans], 
   589 	      simpset() addsimps [neg_eq_less_0, zle_def]));
   590 qed "nat_less_eq_zless";
   591 
   592 Goal "#0 < w | #0 <= z ==> (nat w <= nat z) = (w<=z)";
   593 by (auto_tac (claset(), 
   594 	      simpset() addsimps [linorder_not_less RS sym, 
   595 				  zless_nat_conj]));
   596 qed "nat_le_eq_zle";
   597 
   598 (*Analogous to zadd_int, but more easily provable using the arithmetic in Bin*)
   599 Goal "n<=m --> int m - int n = int (m-n)";
   600 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   601 by Auto_tac;
   602 qed_spec_mp "zdiff_int";
   603 
   604 
   605 (** Products of signs **)
   606 
   607 Goal "(m::int) < #0 ==> (#0 < m*n) = (n < #0)";
   608 by Auto_tac;
   609 by (force_tac (claset() addDs [zmult_zless_mono1_neg], simpset()) 2);
   610 by (eres_inst_tac [("P", "#0 < m * n")] rev_mp 1);
   611 by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
   612 by (force_tac (claset() addDs [inst "k" "m" zmult_zless_mono1_neg], 
   613 	       simpset()addsimps [order_le_less, zmult_commute]) 1);
   614 qed "neg_imp_zmult_pos_iff";
   615 
   616 Goal "(m::int) < #0 ==> (m*n < #0) = (#0 < n)";
   617 by Auto_tac;
   618 by (force_tac (claset() addDs [zmult_zless_mono1], simpset()) 2);
   619 by (eres_inst_tac [("P", "m * n < #0")] rev_mp 1);
   620 by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
   621 by (force_tac (claset() addDs [zmult_zless_mono1_neg], 
   622 	       simpset() addsimps [order_le_less]) 1);
   623 qed "neg_imp_zmult_neg_iff";
   624 
   625 Goal "#0 < (m::int) ==> (m*n < #0) = (n < #0)";
   626 by Auto_tac;
   627 by (force_tac (claset() addDs [zmult_zless_mono1_neg], simpset()) 2);
   628 by (eres_inst_tac [("P", "m * n < #0")] rev_mp 1);
   629 by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
   630 by (force_tac (claset() addDs [zmult_zless_mono1], 
   631 	       simpset() addsimps [order_le_less]) 1);
   632 qed "pos_imp_zmult_neg_iff";
   633 
   634 Goal "#0 < (m::int) ==> (#0 < m*n) = (#0 < n)";
   635 by Auto_tac;
   636 by (force_tac (claset() addDs [zmult_zless_mono1], simpset()) 2);
   637 by (eres_inst_tac [("P", "#0 < m * n")] rev_mp 1);
   638 by (simp_tac (simpset() addsimps [linorder_not_le RS sym]) 1);
   639 by (force_tac (claset() addDs [inst "k" "m" zmult_zless_mono1], 
   640 	       simpset() addsimps [order_le_less, zmult_commute]) 1);
   641 qed "pos_imp_zmult_pos_iff";
   642 
   643 (** <= versions of the theorems above **)
   644 
   645 Goal "(m::int) < #0 ==> (m*n <= #0) = (#0 <= n)";
   646 by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
   647 				      neg_imp_zmult_pos_iff]) 1);
   648 qed "neg_imp_zmult_nonpos_iff";
   649 
   650 Goal "(m::int) < #0 ==> (#0 <= m*n) = (n <= #0)";
   651 by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
   652 				      neg_imp_zmult_neg_iff]) 1);
   653 qed "neg_imp_zmult_nonneg_iff";
   654 
   655 Goal "#0 < (m::int) ==> (m*n <= #0) = (n <= #0)";
   656 by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
   657 				      pos_imp_zmult_pos_iff]) 1);
   658 qed "pos_imp_zmult_nonpos_iff";
   659 
   660 Goal "#0 < (m::int) ==> (#0 <= m*n) = (#0 <= n)";
   661 by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym,
   662 				      pos_imp_zmult_neg_iff]) 1);
   663 qed "pos_imp_zmult_nonneg_iff";