src/HOL/Arith.ML
 author paulson Fri, 12 Dec 1997 10:31:25 +0100 changeset 4389 1865cb8df116 parent 4378 e52f864c5b88 child 4423 a129b817b58a permissions -rw-r--r--
Faster proof of mult_less_cancel2
```
(*  Title:      HOL/Arith.ML
ID:         \$Id\$
Author:     Lawrence C Paulson, Cambridge University Computer Laboratory

Some from the Hoare example from Norbert Galm
*)

(*** Basic rewrite rules for the arithmetic operators ***)

(** Difference **)

qed_goal "diff_0_eq_0" Arith.thy
"0 - n = 0"
(fn _ => [induct_tac "n" 1,  ALLGOALS Asm_simp_tac]);

(*Must simplify BEFORE the induction!!  (Else we get a critical pair)
Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
qed_goal "diff_Suc_Suc" Arith.thy
"Suc(m) - Suc(n) = m - n"
(fn _ =>
[Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]);

(* Could be (and is, below) generalized in various ways;
However, none of the generalizations are currently in the simpset,
and I dread to think what happens if I put them in *)
goal Arith.thy "!!n. 0 < n ==> Suc(n-1) = n";
qed "Suc_pred";

(* Generalize? *)
goal Arith.thy "!!n. 0<n ==> n-1 < n";
qed "pred_less";

Delsimps [diff_Suc];

(**** Inductive properties of the operators ****)

qed_goal "add_0_right" Arith.thy "m + 0 = m"
(fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
(fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
(fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
(fn _ =>  [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

(fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,

goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
by (induct_tac "k" 1);
by (Simp_tac 1);
by (Asm_simp_tac 1);

goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
by (induct_tac "k" 1);
by (Simp_tac 1);
by (Asm_simp_tac 1);

goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
by (induct_tac "k" 1);
by (Simp_tac 1);
by (Asm_simp_tac 1);

goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
by (induct_tac "k" 1);
by (Simp_tac 1);
by (Asm_simp_tac 1);

(** Reasoning about m+0=0, etc. **)

goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
by (induct_tac "m" 1);
by (ALLGOALS Asm_simp_tac);

goal Arith.thy "(0<m+n) = (0<m | 0<n)";
by(simp_tac (simpset() delsimps [neq0_conv] addsimps [neq0_conv RS sym]) 1);

(* FIXME: really needed?? *)
goal Arith.thy "((m+n)-1 = 0) = (m=0 & n-1 = 0 | m-1 = 0 & n=0)";
by (exhaust_tac "m" 1);
by (ALLGOALS (fast_tac (claset() addss (simpset()))));

(* Could be generalized, eg to "!!n. k<n ==> m+(n-(Suc k)) = (m+n)-(Suc k)" *)
goal Arith.thy "!!n. 0<n ==> m + (n-1) = (m+n)-1";
by (exhaust_tac "m" 1);
by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc]

goal Arith.thy "i<j --> (EX k. j = Suc(i+k))";
by (induct_tac "j" 1);
by (Simp_tac 1);
val lemma = result();

(* [| i<j;  !!x. j = Suc(i+x) ==> Q |] ==> Q *)
bind_thm ("less_natE", lemma RS mp RS exE);

goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
by (induct_tac "n" 1);
by (ALLGOALS (simp_tac (simpset() addsimps [less_Suc_eq])));

goal Arith.thy "n <= ((m + n)::nat)";
by (induct_tac "m" 1);
by (ALLGOALS Simp_tac);
by (etac le_trans 1);
by (rtac (lessI RS less_imp_le) 1);

goal Arith.thy "n <= ((n + m)::nat)";

(*"i <= j ==> i <= j+m"*)

(*"i <= j ==> i <= m+j"*)

(*"i < j ==> i < j+m"*)

(*"i < j ==> i < m+j"*)

goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
by (etac rev_mp 1);
by (induct_tac "j" 1);
by (ALLGOALS Asm_simp_tac);
by (blast_tac (claset() addDs [Suc_lessD]) 1);

goal Arith.thy "!!i::nat. ~ (i+j < i)";
by (rtac notI 1);
by (etac (add_lessD1 RS less_irrefl) 1);

goal Arith.thy "!!i::nat. ~ (j+i < i)";

goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
by (etac le_trans 1);

goal Arith.thy "!!k::nat. m < n ==> m < n+k";
by (etac less_le_trans 1);

goal Arith.thy "m+k<=n --> m<=(n::nat)";
by (induct_tac "k" 1);
by (ALLGOALS Asm_simp_tac);
by (blast_tac (claset() addDs [Suc_leD]) 1);

goal Arith.thy "!!n::nat. m+k<=n ==> k<=n";

goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n";

goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
by (asm_full_simp_tac
by (etac subst 1);

(*strict, in 1st argument*)
goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
by (induct_tac "k" 1);
by (ALLGOALS Asm_simp_tac);

(*strict, in both arguments*)
goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
by (rtac (add_less_mono1 RS less_trans) 1);
by (REPEAT (assume_tac 1));
by (induct_tac "j" 1);
by (ALLGOALS Asm_simp_tac);

(*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
val [lt_mono,le] = goal Arith.thy
"[| !!i j::nat. i<j ==> f(i) < f(j);       \
\        i <= j                                 \
\     |] ==> f(i) <= (f(j)::nat)";
by (cut_facts_tac [le] 1);
by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
by (blast_tac (claset() addSIs [lt_mono]) 1);
qed "less_mono_imp_le_mono";

(*non-strict, in 1st argument*)
goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
by (assume_tac 1);

(*non-strict, in both arguments*)
goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
by (etac (add_le_mono1 RS le_trans) 1);
(*j moves to the end because it is free while k, l are bound*)

(*** Multiplication ***)

(*right annihilation in product*)
qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
(fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

(*right successor law for multiplication*)
qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
(fn _ => [induct_tac "m" 1,

goal Arith.thy "1 * n = n";
by (Asm_simp_tac 1);
qed "mult_1";

goal Arith.thy "n * 1 = n";
by (Asm_simp_tac 1);
qed "mult_1_right";

(*Commutative law for multiplication*)
qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
(fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
(fn _ => [induct_tac "m" 1,

qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
(fn _ => [induct_tac "m" 1,

(*Associative law for multiplication*)
qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
(fn _ => [induct_tac "m" 1,

qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
(fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);

val mult_ac = [mult_assoc,mult_commute,mult_left_commute];

goal Arith.thy "(m*n = 0) = (m=0 | n=0)";
by (induct_tac "m" 1);
by (induct_tac "n" 2);
by (ALLGOALS Asm_simp_tac);
qed "mult_is_0";

goal Arith.thy "!!m::nat. m <= m*m";
by (induct_tac "m" 1);
by (etac (le_add2 RSN (2,le_trans)) 1);
qed "le_square";

(*** Difference ***)

qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
(fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);

(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
goal Arith.thy "~ m<n --> n+(m-n) = (m::nat)";
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);

goal Arith.thy "!!m. n<=m ==> n+(m-n) = (m::nat)";

goal Arith.thy "!!m. n<=m ==> (m-n)+n = (m::nat)";

(*** More results about difference ***)

val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
by (rtac (prem RS rev_mp) 1);
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);
qed "Suc_diff_n";

goal Arith.thy "m - n < Suc(m)";
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (etac less_SucE 3);
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
qed "diff_less_Suc";

goal Arith.thy "!!m::nat. m - n <= m";
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);
qed "diff_le_self";

goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);
qed "diff_diff_left";

(* This is a trivial consequence of diff_diff_left;
could be got rid of if diff_diff_left were in the simpset...
*)
goal Arith.thy "(Suc m - n)-1 = m - n";
qed "pred_Suc_diff";

(*This and the next few suggested by Florian Kammueller*)
goal Arith.thy "!!i::nat. i-j-k = i-k-j";
qed "diff_commute";

goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);
by (asm_simp_tac
(simpset() addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
qed_spec_mp "diff_diff_right";

goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);

goal Arith.thy "!!n::nat. (n+m) - n = m";
by (induct_tac "n" 1);
by (ALLGOALS Asm_simp_tac);

goal Arith.thy "!!n::nat.(m+n) - n = m";

goal Arith.thy "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
by Safe_tac;
by (ALLGOALS Asm_simp_tac);

val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
by (rtac (prem RS rev_mp) 1);
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
by (ALLGOALS Asm_simp_tac);
qed "less_imp_diff_is_0";

val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
qed_spec_mp "diffs0_imp_equal";

val [prem] = goal Arith.thy "m<n ==> 0<n-m";
by (rtac (prem RS rev_mp) 1);
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);
qed "less_imp_diff_positive";

goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
by (simp_tac (simpset() addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
qed "if_Suc_diff_n";

goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
qed "zero_induct_lemma";

val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
by (rtac (diff_self_eq_0 RS subst) 1);
by (rtac (zero_induct_lemma RS mp RS mp) 1);
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
qed "zero_induct";

goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
by (induct_tac "k" 1);
by (ALLGOALS Asm_simp_tac);
qed "diff_cancel";

goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
qed "diff_cancel2";

(*From Clemens Ballarin*)
goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
by (Asm_full_simp_tac 1);
by (induct_tac "k" 1);
by (Simp_tac 1);
(* Induction step *)
by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
\                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
by (Asm_full_simp_tac 1);
by (blast_tac (claset() addIs [le_trans]) 1);
by (auto_tac (claset() addIs [Suc_leD], simpset() delsimps [diff_Suc_Suc]));
by (asm_full_simp_tac (simpset() delsimps [Suc_less_eq]
addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
qed "diff_right_cancel";

goal Arith.thy "!!n::nat. n - (n+m) = 0";
by (induct_tac "n" 1);
by (ALLGOALS Asm_simp_tac);

(** Difference distributes over multiplication **)

goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
by (ALLGOALS Asm_simp_tac);
qed "diff_mult_distrib" ;

goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
qed "diff_mult_distrib2" ;
(*NOT added as rewrites, since sometimes they are used from right-to-left*)

(*** Monotonicity of Multiplication ***)

goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
by (induct_tac "k" 1);
qed "mult_le_mono1";

(*<=monotonicity, BOTH arguments*)
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
by (etac (mult_le_mono1 RS le_trans) 1);
by (rtac le_trans 1);
by (stac mult_commute 2);
by (etac mult_le_mono1 2);
by (simp_tac (simpset() addsimps [mult_commute]) 1);
qed "mult_le_mono";

(*strict, in 1st argument; proof is by induction on k>0*)
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
by (eres_inst_tac [("i","0")] less_natE 1);
by (Asm_simp_tac 1);
by (induct_tac "x" 1);
qed "mult_less_mono2";

goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
by (dtac mult_less_mono2 1);
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
qed "mult_less_mono1";

goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
by (induct_tac "m" 1);
by (induct_tac "n" 2);
by (ALLGOALS Asm_simp_tac);
qed "zero_less_mult_iff";

goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
by (induct_tac "m" 1);
by (Simp_tac 1);
by (induct_tac "n" 1);
by (Simp_tac 1);
by (fast_tac (claset() addss simpset()) 1);
qed "mult_eq_1_iff";

goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
by (cut_facts_tac [less_linear] 1);
qed "mult_less_cancel2";

goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
by (dtac mult_less_cancel2 1);
by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
qed "mult_less_cancel1";

goal Arith.thy "(Suc k * m < Suc k * n) = (m < n)";
br mult_less_cancel1 1;
by (Simp_tac 1);
qed "Suc_mult_less_cancel1";

goalw Arith.thy [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
by (simp_tac (simpset_of HOL.thy) 1);
br Suc_mult_less_cancel1 1;
qed "Suc_mult_le_cancel1";

goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
by (cut_facts_tac [less_linear] 1);
by Safe_tac;
by (assume_tac 2);
by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
by (ALLGOALS Asm_full_simp_tac);
qed "mult_cancel2";

goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
by (dtac mult_cancel2 1);
by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
qed "mult_cancel1";

goal Arith.thy "(Suc k * m = Suc k * n) = (m = n)";
br mult_cancel1 1;
by (Simp_tac 1);
qed "Suc_mult_cancel1";

(** Lemma for gcd **)

goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
by (dtac sym 1);
by (rtac disjCI 1);
by (rtac nat_less_cases 1 THEN assume_tac 2);
qed "mult_eq_self_implies_10";

(*** Subtraction laws -- from Clemens Ballarin ***)

goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
by (Full_simp_tac 1);
by (subgoal_tac "c <= b" 1);
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
by (Asm_simp_tac 1);
qed "diff_less_mono";

goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
by (dtac diff_less_mono 1);
by (Asm_full_simp_tac 1);

goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
by (rtac Suc_diff_n 1);
by (asm_full_simp_tac (simpset() addsimps [le_eq_less_Suc]) 1);
qed "Suc_diff_le";

goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
by (asm_full_simp_tac
(simpset() addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
qed "Suc_diff_Suc";

goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
by (etac rev_mp 1);
by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
qed "diff_diff_cancel";

goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
by (etac rev_mp 1);
by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
by (Simp_tac 1);
by (Simp_tac 1);

(** (Anti)Monotonicity of subtraction -- by Stefan Merz **)

(* Monotonicity of subtraction in first argument *)
goal Arith.thy "!!n::nat. m<=n --> (m-l) <= (n-l)";
by (induct_tac "n" 1);
by (Simp_tac 1);
by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
by (rtac impI 1);
by (etac impE 1);
by (atac 1);
by (etac le_trans 1);
by (res_inst_tac [("m1","n")] (pred_Suc_diff RS subst) 1);