src/HOL/Nat_Numeral.thy
author haftmann
Wed Apr 22 19:09:21 2009 +0200 (2009-04-22)
changeset 30960 fec1a04b7220
parent 30925 c38cbc0ac8d1
child 31002 bc4117fe72ab
permissions -rw-r--r--
power operation defined generic
     1 (*  Title:      HOL/Nat_Numeral.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1999  University of Cambridge
     4 *)
     5 
     6 header {* Binary numerals for the natural numbers *}
     7 
     8 theory Nat_Numeral
     9 imports IntDiv
    10 uses ("Tools/nat_simprocs.ML")
    11 begin
    12 
    13 text {*
    14   Arithmetic for naturals is reduced to that for the non-negative integers.
    15 *}
    16 
    17 instantiation nat :: number
    18 begin
    19 
    20 definition
    21   nat_number_of_def [code inline, code del]: "number_of v = nat (number_of v)"
    22 
    23 instance ..
    24 
    25 end
    26 
    27 lemma [code post]:
    28   "nat (number_of v) = number_of v"
    29   unfolding nat_number_of_def ..
    30 
    31 context recpower
    32 begin
    33 
    34 abbreviation (xsymbols)
    35   power2 :: "'a \<Rightarrow> 'a"  ("(_\<twosuperior>)" [1000] 999) where
    36   "x\<twosuperior> \<equiv> x ^ 2"
    37 
    38 notation (latex output)
    39   power2  ("(_\<twosuperior>)" [1000] 999)
    40 
    41 notation (HTML output)
    42   power2  ("(_\<twosuperior>)" [1000] 999)
    43 
    44 end
    45 
    46 
    47 subsection {* Predicate for negative binary numbers *}
    48 
    49 definition neg  :: "int \<Rightarrow> bool" where
    50   "neg Z \<longleftrightarrow> Z < 0"
    51 
    52 lemma not_neg_int [simp]: "~ neg (of_nat n)"
    53 by (simp add: neg_def)
    54 
    55 lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
    56 by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
    57 
    58 lemmas neg_eq_less_0 = neg_def
    59 
    60 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
    61 by (simp add: neg_def linorder_not_less)
    62 
    63 text{*To simplify inequalities when Numeral1 can get simplified to 1*}
    64 
    65 lemma not_neg_0: "~ neg 0"
    66 by (simp add: One_int_def neg_def)
    67 
    68 lemma not_neg_1: "~ neg 1"
    69 by (simp add: neg_def linorder_not_less zero_le_one)
    70 
    71 lemma neg_nat: "neg z ==> nat z = 0"
    72 by (simp add: neg_def order_less_imp_le) 
    73 
    74 lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
    75 by (simp add: linorder_not_less neg_def)
    76 
    77 text {*
    78   If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
    79   @{term Numeral0} IS @{term "number_of Pls"}
    80 *}
    81 
    82 lemma not_neg_number_of_Pls: "~ neg (number_of Int.Pls)"
    83   by (simp add: neg_def)
    84 
    85 lemma neg_number_of_Min: "neg (number_of Int.Min)"
    86   by (simp add: neg_def)
    87 
    88 lemma neg_number_of_Bit0:
    89   "neg (number_of (Int.Bit0 w)) = neg (number_of w)"
    90   by (simp add: neg_def)
    91 
    92 lemma neg_number_of_Bit1:
    93   "neg (number_of (Int.Bit1 w)) = neg (number_of w)"
    94   by (simp add: neg_def)
    95 
    96 lemmas neg_simps [simp] =
    97   not_neg_0 not_neg_1
    98   not_neg_number_of_Pls neg_number_of_Min
    99   neg_number_of_Bit0 neg_number_of_Bit1
   100 
   101 
   102 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
   103 
   104 declare nat_0 [simp] nat_1 [simp]
   105 
   106 lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
   107 by (simp add: nat_number_of_def)
   108 
   109 lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
   110 by (simp add: nat_number_of_def)
   111 
   112 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
   113 by (simp add: nat_1 nat_number_of_def)
   114 
   115 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
   116 by (simp add: nat_numeral_1_eq_1)
   117 
   118 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
   119 apply (unfold nat_number_of_def)
   120 apply (rule nat_2)
   121 done
   122 
   123 
   124 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
   125 
   126 lemma int_nat_number_of [simp]:
   127      "int (number_of v) =  
   128          (if neg (number_of v :: int) then 0  
   129           else (number_of v :: int))"
   130   unfolding nat_number_of_def number_of_is_id neg_def
   131   by simp
   132 
   133 
   134 subsubsection{*Successor *}
   135 
   136 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
   137 apply (rule sym)
   138 apply (simp add: nat_eq_iff int_Suc)
   139 done
   140 
   141 lemma Suc_nat_number_of_add:
   142      "Suc (number_of v + n) =  
   143         (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)"
   144   unfolding nat_number_of_def number_of_is_id neg_def numeral_simps
   145   by (simp add: Suc_nat_eq_nat_zadd1 add_ac)
   146 
   147 lemma Suc_nat_number_of [simp]:
   148      "Suc (number_of v) =  
   149         (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
   150 apply (cut_tac n = 0 in Suc_nat_number_of_add)
   151 apply (simp cong del: if_weak_cong)
   152 done
   153 
   154 
   155 subsubsection{*Addition *}
   156 
   157 lemma add_nat_number_of [simp]:
   158      "(number_of v :: nat) + number_of v' =  
   159          (if v < Int.Pls then number_of v'  
   160           else if v' < Int.Pls then number_of v  
   161           else number_of (v + v'))"
   162   unfolding nat_number_of_def number_of_is_id numeral_simps
   163   by (simp add: nat_add_distrib)
   164 
   165 lemma nat_number_of_add_1 [simp]:
   166   "number_of v + (1::nat) =
   167     (if v < Int.Pls then 1 else number_of (Int.succ v))"
   168   unfolding nat_number_of_def number_of_is_id numeral_simps
   169   by (simp add: nat_add_distrib)
   170 
   171 lemma nat_1_add_number_of [simp]:
   172   "(1::nat) + number_of v =
   173     (if v < Int.Pls then 1 else number_of (Int.succ v))"
   174   unfolding nat_number_of_def number_of_is_id numeral_simps
   175   by (simp add: nat_add_distrib)
   176 
   177 lemma nat_1_add_1 [simp]: "1 + 1 = (2::nat)"
   178   by (rule int_int_eq [THEN iffD1]) simp
   179 
   180 
   181 subsubsection{*Subtraction *}
   182 
   183 lemma diff_nat_eq_if:
   184      "nat z - nat z' =  
   185         (if neg z' then nat z   
   186          else let d = z-z' in     
   187               if neg d then 0 else nat d)"
   188 by (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
   189 
   190 
   191 lemma diff_nat_number_of [simp]: 
   192      "(number_of v :: nat) - number_of v' =  
   193         (if v' < Int.Pls then number_of v  
   194          else let d = number_of (v + uminus v') in     
   195               if neg d then 0 else nat d)"
   196   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def
   197   by auto
   198 
   199 lemma nat_number_of_diff_1 [simp]:
   200   "number_of v - (1::nat) =
   201     (if v \<le> Int.Pls then 0 else number_of (Int.pred v))"
   202   unfolding nat_number_of_def number_of_is_id numeral_simps
   203   by auto
   204 
   205 
   206 subsubsection{*Multiplication *}
   207 
   208 lemma mult_nat_number_of [simp]:
   209      "(number_of v :: nat) * number_of v' =  
   210        (if v < Int.Pls then 0 else number_of (v * v'))"
   211   unfolding nat_number_of_def number_of_is_id numeral_simps
   212   by (simp add: nat_mult_distrib)
   213 
   214 
   215 subsubsection{*Quotient *}
   216 
   217 lemma div_nat_number_of [simp]:
   218      "(number_of v :: nat)  div  number_of v' =  
   219           (if neg (number_of v :: int) then 0  
   220            else nat (number_of v div number_of v'))"
   221   unfolding nat_number_of_def number_of_is_id neg_def
   222   by (simp add: nat_div_distrib)
   223 
   224 lemma one_div_nat_number_of [simp]:
   225      "Suc 0 div number_of v' = nat (1 div number_of v')" 
   226 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
   227 
   228 
   229 subsubsection{*Remainder *}
   230 
   231 lemma mod_nat_number_of [simp]:
   232      "(number_of v :: nat)  mod  number_of v' =  
   233         (if neg (number_of v :: int) then 0  
   234          else if neg (number_of v' :: int) then number_of v  
   235          else nat (number_of v mod number_of v'))"
   236   unfolding nat_number_of_def number_of_is_id neg_def
   237   by (simp add: nat_mod_distrib)
   238 
   239 lemma one_mod_nat_number_of [simp]:
   240      "Suc 0 mod number_of v' =  
   241         (if neg (number_of v' :: int) then Suc 0
   242          else nat (1 mod number_of v'))"
   243 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
   244 
   245 
   246 subsubsection{* Divisibility *}
   247 
   248 lemmas dvd_eq_mod_eq_0_number_of =
   249   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
   250 
   251 declare dvd_eq_mod_eq_0_number_of [simp]
   252 
   253 ML
   254 {*
   255 val nat_number_of_def = thm"nat_number_of_def";
   256 
   257 val nat_number_of = thm"nat_number_of";
   258 val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
   259 val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
   260 val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
   261 val numeral_2_eq_2 = thm"numeral_2_eq_2";
   262 val nat_div_distrib = thm"nat_div_distrib";
   263 val nat_mod_distrib = thm"nat_mod_distrib";
   264 val int_nat_number_of = thm"int_nat_number_of";
   265 val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
   266 val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
   267 val Suc_nat_number_of = thm"Suc_nat_number_of";
   268 val add_nat_number_of = thm"add_nat_number_of";
   269 val diff_nat_eq_if = thm"diff_nat_eq_if";
   270 val diff_nat_number_of = thm"diff_nat_number_of";
   271 val mult_nat_number_of = thm"mult_nat_number_of";
   272 val div_nat_number_of = thm"div_nat_number_of";
   273 val mod_nat_number_of = thm"mod_nat_number_of";
   274 *}
   275 
   276 
   277 subsection{*Comparisons*}
   278 
   279 subsubsection{*Equals (=) *}
   280 
   281 lemma eq_nat_nat_iff:
   282      "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
   283 by (auto elim!: nonneg_eq_int)
   284 
   285 lemma eq_nat_number_of [simp]:
   286      "((number_of v :: nat) = number_of v') =  
   287       (if neg (number_of v :: int) then (number_of v' :: int) \<le> 0
   288        else if neg (number_of v' :: int) then (number_of v :: int) = 0
   289        else v = v')"
   290   unfolding nat_number_of_def number_of_is_id neg_def
   291   by auto
   292 
   293 
   294 subsubsection{*Less-than (<) *}
   295 
   296 lemma less_nat_number_of [simp]:
   297   "(number_of v :: nat) < number_of v' \<longleftrightarrow>
   298     (if v < v' then Int.Pls < v' else False)"
   299   unfolding nat_number_of_def number_of_is_id numeral_simps
   300   by auto
   301 
   302 
   303 subsubsection{*Less-than-or-equal *}
   304 
   305 lemma le_nat_number_of [simp]:
   306   "(number_of v :: nat) \<le> number_of v' \<longleftrightarrow>
   307     (if v \<le> v' then True else v \<le> Int.Pls)"
   308   unfolding nat_number_of_def number_of_is_id numeral_simps
   309   by auto
   310 
   311 (*Maps #n to n for n = 0, 1, 2*)
   312 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
   313 
   314 
   315 subsection{*Powers with Numeric Exponents*}
   316 
   317 text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}.
   318 We cannot prove general results about the numeral @{term "-1"}, so we have to
   319 use @{term "- 1"} instead.*}
   320 
   321 lemma power2_eq_square: "(a::'a::recpower)\<twosuperior> = a * a"
   322   by (simp add: numeral_2_eq_2 Power.power_Suc)
   323 
   324 lemma zero_power2 [simp]: "(0::'a::{semiring_1,recpower})\<twosuperior> = 0"
   325   by (simp add: power2_eq_square)
   326 
   327 lemma one_power2 [simp]: "(1::'a::{semiring_1,recpower})\<twosuperior> = 1"
   328   by (simp add: power2_eq_square)
   329 
   330 lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x"
   331   apply (subgoal_tac "3 = Suc (Suc (Suc 0))")
   332   apply (erule ssubst)
   333   apply (simp add: power_Suc mult_ac)
   334   apply (unfold nat_number_of_def)
   335   apply (subst nat_eq_iff)
   336   apply simp
   337 done
   338 
   339 text{*Squares of literal numerals will be evaluated.*}
   340 lemmas power2_eq_square_number_of =
   341     power2_eq_square [of "number_of w", standard]
   342 declare power2_eq_square_number_of [simp]
   343 
   344 
   345 lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})"
   346   by (simp add: power2_eq_square)
   347 
   348 lemma zero_less_power2[simp]:
   349      "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))"
   350   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
   351 
   352 lemma power2_less_0[simp]:
   353   fixes a :: "'a::{ordered_idom,recpower}"
   354   shows "~ (a\<twosuperior> < 0)"
   355 by (force simp add: power2_eq_square mult_less_0_iff) 
   356 
   357 lemma zero_eq_power2[simp]:
   358      "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))"
   359   by (force simp add: power2_eq_square mult_eq_0_iff)
   360 
   361 lemma abs_power2[simp]:
   362      "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})"
   363   by (simp add: power2_eq_square abs_mult abs_mult_self)
   364 
   365 lemma power2_abs[simp]:
   366      "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})"
   367   by (simp add: power2_eq_square abs_mult_self)
   368 
   369 lemma power2_minus[simp]:
   370      "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})"
   371   by (simp add: power2_eq_square)
   372 
   373 lemma power2_le_imp_le:
   374   fixes x y :: "'a::{ordered_semidom,recpower}"
   375   shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y"
   376 unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
   377 
   378 lemma power2_less_imp_less:
   379   fixes x y :: "'a::{ordered_semidom,recpower}"
   380   shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y"
   381 by (rule power_less_imp_less_base)
   382 
   383 lemma power2_eq_imp_eq:
   384   fixes x y :: "'a::{ordered_semidom,recpower}"
   385   shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y"
   386 unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp)
   387 
   388 lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})"
   389 proof (induct n)
   390   case 0 show ?case by simp
   391 next
   392   case (Suc n) then show ?case by (simp add: power_Suc power_add)
   393 qed
   394 
   395 lemma power_minus1_odd: "(- 1) ^ Suc(2*n) = -(1::'a::{comm_ring_1,recpower})"
   396   by (simp add: power_Suc) 
   397 
   398 lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2"
   399 by (subst mult_commute) (simp add: power_mult)
   400 
   401 lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2"
   402 by (simp add: power_even_eq) 
   403 
   404 lemma power_minus_even [simp]:
   405   "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)"
   406   by (simp add: power_minus [of a]) 
   407 
   408 lemma zero_le_even_power'[simp]:
   409      "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)"
   410 proof (induct "n")
   411   case 0
   412     show ?case by (simp add: zero_le_one)
   413 next
   414   case (Suc n)
   415     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
   416       by (simp add: mult_ac power_add power2_eq_square)
   417     thus ?case
   418       by (simp add: prems zero_le_mult_iff)
   419 qed
   420 
   421 lemma odd_power_less_zero:
   422      "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0"
   423 proof (induct "n")
   424   case 0
   425   then show ?case by simp
   426 next
   427   case (Suc n)
   428   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
   429     by (simp add: mult_ac power_add power2_eq_square)
   430   thus ?case
   431     by (simp del: power_Suc add: prems mult_less_0_iff mult_neg_neg)
   432 qed
   433 
   434 lemma odd_0_le_power_imp_0_le:
   435      "0 \<le> a  ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})"
   436 apply (insert odd_power_less_zero [of a n]) 
   437 apply (force simp add: linorder_not_less [symmetric]) 
   438 done
   439 
   440 text{*Simprules for comparisons where common factors can be cancelled.*}
   441 lemmas zero_compare_simps =
   442     add_strict_increasing add_strict_increasing2 add_increasing
   443     zero_le_mult_iff zero_le_divide_iff 
   444     zero_less_mult_iff zero_less_divide_iff 
   445     mult_le_0_iff divide_le_0_iff 
   446     mult_less_0_iff divide_less_0_iff 
   447     zero_le_power2 power2_less_0
   448 
   449 subsubsection{*Nat *}
   450 
   451 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
   452 by (simp add: numerals)
   453 
   454 (*Expresses a natural number constant as the Suc of another one.
   455   NOT suitable for rewriting because n recurs in the condition.*)
   456 lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
   457 
   458 subsubsection{*Arith *}
   459 
   460 lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
   461 by (simp add: numerals)
   462 
   463 lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
   464 by (simp add: numerals)
   465 
   466 (* These two can be useful when m = number_of... *)
   467 
   468 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
   469   unfolding One_nat_def by (cases m) simp_all
   470 
   471 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
   472   unfolding One_nat_def by (cases m) simp_all
   473 
   474 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
   475   unfolding One_nat_def by (cases m) simp_all
   476 
   477 
   478 subsection{*Comparisons involving (0::nat) *}
   479 
   480 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
   481 
   482 lemma eq_number_of_0 [simp]:
   483   "number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls"
   484   unfolding nat_number_of_def number_of_is_id numeral_simps
   485   by auto
   486 
   487 lemma eq_0_number_of [simp]:
   488   "(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls"
   489 by (rule trans [OF eq_sym_conv eq_number_of_0])
   490 
   491 lemma less_0_number_of [simp]:
   492    "(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v"
   493   unfolding nat_number_of_def number_of_is_id numeral_simps
   494   by simp
   495 
   496 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
   497 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
   498 
   499 
   500 
   501 subsection{*Comparisons involving  @{term Suc} *}
   502 
   503 lemma eq_number_of_Suc [simp]:
   504      "(number_of v = Suc n) =  
   505         (let pv = number_of (Int.pred v) in  
   506          if neg pv then False else nat pv = n)"
   507 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   508                   number_of_pred nat_number_of_def 
   509             split add: split_if)
   510 apply (rule_tac x = "number_of v" in spec)
   511 apply (auto simp add: nat_eq_iff)
   512 done
   513 
   514 lemma Suc_eq_number_of [simp]:
   515      "(Suc n = number_of v) =  
   516         (let pv = number_of (Int.pred v) in  
   517          if neg pv then False else nat pv = n)"
   518 by (rule trans [OF eq_sym_conv eq_number_of_Suc])
   519 
   520 lemma less_number_of_Suc [simp]:
   521      "(number_of v < Suc n) =  
   522         (let pv = number_of (Int.pred v) in  
   523          if neg pv then True else nat pv < n)"
   524 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   525                   number_of_pred nat_number_of_def  
   526             split add: split_if)
   527 apply (rule_tac x = "number_of v" in spec)
   528 apply (auto simp add: nat_less_iff)
   529 done
   530 
   531 lemma less_Suc_number_of [simp]:
   532      "(Suc n < number_of v) =  
   533         (let pv = number_of (Int.pred v) in  
   534          if neg pv then False else n < nat pv)"
   535 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   536                   number_of_pred nat_number_of_def
   537             split add: split_if)
   538 apply (rule_tac x = "number_of v" in spec)
   539 apply (auto simp add: zless_nat_eq_int_zless)
   540 done
   541 
   542 lemma le_number_of_Suc [simp]:
   543      "(number_of v <= Suc n) =  
   544         (let pv = number_of (Int.pred v) in  
   545          if neg pv then True else nat pv <= n)"
   546 by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
   547 
   548 lemma le_Suc_number_of [simp]:
   549      "(Suc n <= number_of v) =  
   550         (let pv = number_of (Int.pred v) in  
   551          if neg pv then False else n <= nat pv)"
   552 by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
   553 
   554 
   555 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
   556 by auto
   557 
   558 
   559 
   560 subsection{*Max and Min Combined with @{term Suc} *}
   561 
   562 lemma max_number_of_Suc [simp]:
   563      "max (Suc n) (number_of v) =  
   564         (let pv = number_of (Int.pred v) in  
   565          if neg pv then Suc n else Suc(max n (nat pv)))"
   566 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   567             split add: split_if nat.split)
   568 apply (rule_tac x = "number_of v" in spec) 
   569 apply auto
   570 done
   571  
   572 lemma max_Suc_number_of [simp]:
   573      "max (number_of v) (Suc n) =  
   574         (let pv = number_of (Int.pred v) in  
   575          if neg pv then Suc n else Suc(max (nat pv) n))"
   576 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   577             split add: split_if nat.split)
   578 apply (rule_tac x = "number_of v" in spec) 
   579 apply auto
   580 done
   581  
   582 lemma min_number_of_Suc [simp]:
   583      "min (Suc n) (number_of v) =  
   584         (let pv = number_of (Int.pred v) in  
   585          if neg pv then 0 else Suc(min n (nat pv)))"
   586 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   587             split add: split_if nat.split)
   588 apply (rule_tac x = "number_of v" in spec) 
   589 apply auto
   590 done
   591  
   592 lemma min_Suc_number_of [simp]:
   593      "min (number_of v) (Suc n) =  
   594         (let pv = number_of (Int.pred v) in  
   595          if neg pv then 0 else Suc(min (nat pv) n))"
   596 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   597             split add: split_if nat.split)
   598 apply (rule_tac x = "number_of v" in spec) 
   599 apply auto
   600 done
   601  
   602 subsection{*Literal arithmetic involving powers*}
   603 
   604 lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
   605 apply (induct "n")
   606 apply (simp_all (no_asm_simp) add: nat_mult_distrib)
   607 done
   608 
   609 lemma power_nat_number_of:
   610      "(number_of v :: nat) ^ n =  
   611        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
   612 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
   613          split add: split_if cong: imp_cong)
   614 
   615 
   616 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
   617 declare power_nat_number_of_number_of [simp]
   618 
   619 
   620 
   621 text{*For arbitrary rings*}
   622 
   623 lemma power_number_of_even:
   624   fixes z :: "'a::{number_ring,recpower}"
   625   shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
   626 unfolding Let_def nat_number_of_def number_of_Bit0
   627 apply (rule_tac x = "number_of w" in spec, clarify)
   628 apply (case_tac " (0::int) <= x")
   629 apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
   630 done
   631 
   632 lemma power_number_of_odd:
   633   fixes z :: "'a::{number_ring,recpower}"
   634   shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
   635      then (let w = z ^ (number_of w) in z * w * w) else 1)"
   636 unfolding Let_def nat_number_of_def number_of_Bit1
   637 apply (rule_tac x = "number_of w" in spec, auto)
   638 apply (simp only: nat_add_distrib nat_mult_distrib)
   639 apply simp
   640 apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
   641 done
   642 
   643 lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
   644 lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
   645 
   646 lemmas power_number_of_even_number_of [simp] =
   647     power_number_of_even [of "number_of v", standard]
   648 
   649 lemmas power_number_of_odd_number_of [simp] =
   650     power_number_of_odd [of "number_of v", standard]
   651 
   652 
   653 
   654 ML
   655 {*
   656 val numeral_ss = @{simpset} addsimps @{thms numerals};
   657 
   658 val nat_bin_arith_setup =
   659  Lin_Arith.map_data
   660    (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
   661      {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
   662       inj_thms = inj_thms,
   663       lessD = lessD, neqE = neqE,
   664       simpset = simpset addsimps @{thms neg_simps} @
   665         [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}]})
   666 *}
   667 
   668 declaration {* K nat_bin_arith_setup *}
   669 
   670 (* Enable arith to deal with div/mod k where k is a numeral: *)
   671 declare split_div[of _ _ "number_of k", standard, arith_split]
   672 declare split_mod[of _ _ "number_of k", standard, arith_split]
   673 
   674 lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
   675   by (simp add: number_of_Pls nat_number_of_def)
   676 
   677 lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
   678   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
   679   done
   680 
   681 lemma nat_number_of_Bit0:
   682     "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
   683   unfolding nat_number_of_def number_of_is_id numeral_simps Let_def
   684   by auto
   685 
   686 lemma nat_number_of_Bit1:
   687   "number_of (Int.Bit1 w) =
   688     (if neg (number_of w :: int) then 0
   689      else let n = number_of w in Suc (n + n))"
   690   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def Let_def
   691   by auto
   692 
   693 lemmas nat_number =
   694   nat_number_of_Pls nat_number_of_Min
   695   nat_number_of_Bit0 nat_number_of_Bit1
   696 
   697 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
   698   by (simp add: Let_def)
   699 
   700 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})"
   701 by (simp add: power_mult power_Suc); 
   702 
   703 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})"
   704 by (simp add: power_mult power_Suc); 
   705 
   706 
   707 subsection{*Literal arithmetic and @{term of_nat}*}
   708 
   709 lemma of_nat_double:
   710      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
   711 by (simp only: mult_2 nat_add_distrib of_nat_add) 
   712 
   713 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
   714 by (simp only: nat_number_of_def)
   715 
   716 lemma of_nat_number_of_lemma:
   717      "of_nat (number_of v :: nat) =  
   718          (if 0 \<le> (number_of v :: int) 
   719           then (number_of v :: 'a :: number_ring)
   720           else 0)"
   721 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
   722 
   723 lemma of_nat_number_of_eq [simp]:
   724      "of_nat (number_of v :: nat) =  
   725          (if neg (number_of v :: int) then 0  
   726           else (number_of v :: 'a :: number_ring))"
   727 by (simp only: of_nat_number_of_lemma neg_def, simp) 
   728 
   729 
   730 subsection {*Lemmas for the Combination and Cancellation Simprocs*}
   731 
   732 lemma nat_number_of_add_left:
   733      "number_of v + (number_of v' + (k::nat)) =  
   734          (if neg (number_of v :: int) then number_of v' + k  
   735           else if neg (number_of v' :: int) then number_of v + k  
   736           else number_of (v + v') + k)"
   737   unfolding nat_number_of_def number_of_is_id neg_def
   738   by auto
   739 
   740 lemma nat_number_of_mult_left:
   741      "number_of v * (number_of v' * (k::nat)) =  
   742          (if v < Int.Pls then 0
   743           else number_of (v * v') * k)"
   744 by simp
   745 
   746 
   747 subsubsection{*For @{text combine_numerals}*}
   748 
   749 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
   750 by (simp add: add_mult_distrib)
   751 
   752 
   753 subsubsection{*For @{text cancel_numerals}*}
   754 
   755 lemma nat_diff_add_eq1:
   756      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
   757 by (simp split add: nat_diff_split add: add_mult_distrib)
   758 
   759 lemma nat_diff_add_eq2:
   760      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
   761 by (simp split add: nat_diff_split add: add_mult_distrib)
   762 
   763 lemma nat_eq_add_iff1:
   764      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
   765 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   766 
   767 lemma nat_eq_add_iff2:
   768      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
   769 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   770 
   771 lemma nat_less_add_iff1:
   772      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
   773 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   774 
   775 lemma nat_less_add_iff2:
   776      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
   777 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   778 
   779 lemma nat_le_add_iff1:
   780      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
   781 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   782 
   783 lemma nat_le_add_iff2:
   784      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
   785 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   786 
   787 
   788 subsubsection{*For @{text cancel_numeral_factors} *}
   789 
   790 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
   791 by auto
   792 
   793 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
   794 by auto
   795 
   796 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
   797 by auto
   798 
   799 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
   800 by auto
   801 
   802 lemma nat_mult_dvd_cancel_disj[simp]:
   803   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
   804 by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
   805 
   806 lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
   807 by(auto)
   808 
   809 
   810 subsubsection{*For @{text cancel_factor} *}
   811 
   812 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
   813 by auto
   814 
   815 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
   816 by auto
   817 
   818 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
   819 by auto
   820 
   821 lemma nat_mult_div_cancel_disj[simp]:
   822      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
   823 by (simp add: nat_mult_div_cancel1)
   824 
   825 
   826 subsection {* Simprocs for the Naturals *}
   827 
   828 use "Tools/nat_simprocs.ML"
   829 declaration {* K nat_simprocs_setup *}
   830 
   831 subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
   832 
   833 text{*Where K above is a literal*}
   834 
   835 lemma Suc_diff_eq_diff_pred: "Numeral0 < n ==> Suc m - n = m - (n - Numeral1)"
   836 by (simp add: numeral_0_eq_0 numeral_1_eq_1 split add: nat_diff_split)
   837 
   838 text {*Now just instantiating @{text n} to @{text "number_of v"} does
   839   the right simplification, but with some redundant inequality
   840   tests.*}
   841 lemma neg_number_of_pred_iff_0:
   842   "neg (number_of (Int.pred v)::int) = (number_of v = (0::nat))"
   843 apply (subgoal_tac "neg (number_of (Int.pred v)) = (number_of v < Suc 0) ")
   844 apply (simp only: less_Suc_eq_le le_0_eq)
   845 apply (subst less_number_of_Suc, simp)
   846 done
   847 
   848 text{*No longer required as a simprule because of the @{text inverse_fold}
   849    simproc*}
   850 lemma Suc_diff_number_of:
   851      "Int.Pls < v ==>
   852       Suc m - (number_of v) = m - (number_of (Int.pred v))"
   853 apply (subst Suc_diff_eq_diff_pred)
   854 apply simp
   855 apply (simp del: nat_numeral_1_eq_1)
   856 apply (auto simp only: diff_nat_number_of less_0_number_of [symmetric]
   857                         neg_number_of_pred_iff_0)
   858 done
   859 
   860 lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
   861 by (simp add: numerals split add: nat_diff_split)
   862 
   863 
   864 subsubsection{*For @{term nat_case} and @{term nat_rec}*}
   865 
   866 lemma nat_case_number_of [simp]:
   867      "nat_case a f (number_of v) =
   868         (let pv = number_of (Int.pred v) in
   869          if neg pv then a else f (nat pv))"
   870 by (simp split add: nat.split add: Let_def neg_number_of_pred_iff_0)
   871 
   872 lemma nat_case_add_eq_if [simp]:
   873      "nat_case a f ((number_of v) + n) =
   874        (let pv = number_of (Int.pred v) in
   875          if neg pv then nat_case a f n else f (nat pv + n))"
   876 apply (subst add_eq_if)
   877 apply (simp split add: nat.split
   878             del: nat_numeral_1_eq_1
   879             add: nat_numeral_1_eq_1 [symmetric]
   880                  numeral_1_eq_Suc_0 [symmetric]
   881                  neg_number_of_pred_iff_0)
   882 done
   883 
   884 lemma nat_rec_number_of [simp]:
   885      "nat_rec a f (number_of v) =
   886         (let pv = number_of (Int.pred v) in
   887          if neg pv then a else f (nat pv) (nat_rec a f (nat pv)))"
   888 apply (case_tac " (number_of v) ::nat")
   889 apply (simp_all (no_asm_simp) add: Let_def neg_number_of_pred_iff_0)
   890 apply (simp split add: split_if_asm)
   891 done
   892 
   893 lemma nat_rec_add_eq_if [simp]:
   894      "nat_rec a f (number_of v + n) =
   895         (let pv = number_of (Int.pred v) in
   896          if neg pv then nat_rec a f n
   897                    else f (nat pv + n) (nat_rec a f (nat pv + n)))"
   898 apply (subst add_eq_if)
   899 apply (simp split add: nat.split
   900             del: nat_numeral_1_eq_1
   901             add: nat_numeral_1_eq_1 [symmetric]
   902                  numeral_1_eq_Suc_0 [symmetric]
   903                  neg_number_of_pred_iff_0)
   904 done
   905 
   906 
   907 subsubsection{*Various Other Lemmas*}
   908 
   909 text {*Evens and Odds, for Mutilated Chess Board*}
   910 
   911 text{*Lemmas for specialist use, NOT as default simprules*}
   912 lemma nat_mult_2: "2 * z = (z+z::nat)"
   913 proof -
   914   have "2*z = (1 + 1)*z" by simp
   915   also have "... = z+z" by (simp add: left_distrib)
   916   finally show ?thesis .
   917 qed
   918 
   919 lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
   920 by (subst mult_commute, rule nat_mult_2)
   921 
   922 text{*Case analysis on @{term "n<2"}*}
   923 lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
   924 by arith
   925 
   926 lemma div2_Suc_Suc [simp]: "Suc(Suc m) div 2 = Suc (m div 2)"
   927 by arith
   928 
   929 lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
   930 by (simp add: nat_mult_2 [symmetric])
   931 
   932 lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
   933 apply (subgoal_tac "m mod 2 < 2")
   934 apply (erule less_2_cases [THEN disjE])
   935 apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
   936 done
   937 
   938 lemma mod2_gr_0 [simp]: "!!m::nat. (0 < m mod 2) = (m mod 2 = 1)"
   939 apply (subgoal_tac "m mod 2 < 2")
   940 apply (force simp del: mod_less_divisor, simp)
   941 done
   942 
   943 text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
   944 
   945 lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
   946 by simp
   947 
   948 lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
   949 by simp
   950 
   951 text{*Can be used to eliminate long strings of Sucs, but not by default*}
   952 lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
   953 by simp
   954 
   955 
   956 text{*These lemmas collapse some needless occurrences of Suc:
   957     at least three Sucs, since two and fewer are rewritten back to Suc again!
   958     We already have some rules to simplify operands smaller than 3.*}
   959 
   960 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
   961 by (simp add: Suc3_eq_add_3)
   962 
   963 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
   964 by (simp add: Suc3_eq_add_3)
   965 
   966 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
   967 by (simp add: Suc3_eq_add_3)
   968 
   969 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
   970 by (simp add: Suc3_eq_add_3)
   971 
   972 lemmas Suc_div_eq_add3_div_number_of =
   973     Suc_div_eq_add3_div [of _ "number_of v", standard]
   974 declare Suc_div_eq_add3_div_number_of [simp]
   975 
   976 lemmas Suc_mod_eq_add3_mod_number_of =
   977     Suc_mod_eq_add3_mod [of _ "number_of v", standard]
   978 declare Suc_mod_eq_add3_mod_number_of [simp]
   979 
   980 end