src/HOL/NatBin.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 26342 0f65fa163304
child 27651 16a26996c30e
permissions -rw-r--r--
avoid rebinding of existing facts;
     1 (*  Title:      HOL/NatBin.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1999  University of Cambridge
     5 *)
     6 
     7 header {* Binary arithmetic for the natural numbers *}
     8 
     9 theory NatBin
    10 imports IntDiv
    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]: "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 abbreviation (xsymbols)
    32   square :: "'a::power => 'a"  ("(_\<twosuperior>)" [1000] 999) where
    33   "x\<twosuperior> == x^2"
    34 
    35 notation (latex output)
    36   square  ("(_\<twosuperior>)" [1000] 999)
    37 
    38 notation (HTML output)
    39   square  ("(_\<twosuperior>)" [1000] 999)
    40 
    41 
    42 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
    43 
    44 declare nat_0 [simp] nat_1 [simp]
    45 
    46 lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
    47 by (simp add: nat_number_of_def)
    48 
    49 lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
    50 by (simp add: nat_number_of_def)
    51 
    52 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
    53 by (simp add: nat_1 nat_number_of_def)
    54 
    55 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
    56 by (simp add: nat_numeral_1_eq_1)
    57 
    58 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
    59 apply (unfold nat_number_of_def)
    60 apply (rule nat_2)
    61 done
    62 
    63 
    64 text{*Distributive laws for type @{text nat}.  The others are in theory
    65    @{text IntArith}, but these require div and mod to be defined for type
    66    "int".  They also need some of the lemmas proved above.*}
    67 
    68 lemma nat_div_distrib: "(0::int) <= z ==> nat (z div z') = nat z div nat z'"
    69 apply (case_tac "0 <= z'")
    70 apply (auto simp add: div_nonneg_neg_le0 DIVISION_BY_ZERO_DIV)
    71 apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
    72 apply (auto elim!: nonneg_eq_int)
    73 apply (rename_tac m m')
    74 apply (subgoal_tac "0 <= int m div int m'")
    75  prefer 2 apply (simp add: nat_numeral_0_eq_0 pos_imp_zdiv_nonneg_iff) 
    76 apply (rule of_nat_eq_iff [where 'a=int, THEN iffD1], simp)
    77 apply (rule_tac r = "int (m mod m') " in quorem_div)
    78  prefer 2 apply force
    79 apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0
    80                  of_nat_add [symmetric] of_nat_mult [symmetric]
    81             del: of_nat_add of_nat_mult)
    82 done
    83 
    84 (*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*)
    85 lemma nat_mod_distrib:
    86      "[| (0::int) <= z;  0 <= z' |] ==> nat (z mod z') = nat z mod nat z'"
    87 apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
    88 apply (auto elim!: nonneg_eq_int)
    89 apply (rename_tac m m')
    90 apply (subgoal_tac "0 <= int m mod int m'")
    91  prefer 2 apply (simp add: nat_less_iff nat_numeral_0_eq_0 pos_mod_sign)
    92 apply (rule int_int_eq [THEN iffD1], simp)
    93 apply (rule_tac q = "int (m div m') " in quorem_mod)
    94  prefer 2 apply force
    95 apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0
    96                  of_nat_add [symmetric] of_nat_mult [symmetric]
    97             del: of_nat_add of_nat_mult)
    98 done
    99 
   100 text{*Suggested by Matthias Daum*}
   101 lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
   102 apply (subgoal_tac "nat x div nat k < nat x")
   103  apply (simp (asm_lr) add: nat_div_distrib [symmetric])
   104 apply (rule Divides.div_less_dividend, simp_all) 
   105 done
   106 
   107 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
   108 
   109 (*"neg" is used in rewrite rules for binary comparisons*)
   110 lemma int_nat_number_of [simp]:
   111      "int (number_of v) =  
   112          (if neg (number_of v :: int) then 0  
   113           else (number_of v :: int))"
   114 by (simp del: nat_number_of
   115 	 add: neg_nat nat_number_of_def not_neg_nat add_assoc)
   116 
   117 
   118 subsubsection{*Successor *}
   119 
   120 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
   121 apply (rule sym)
   122 apply (simp add: nat_eq_iff int_Suc)
   123 done
   124 
   125 lemma Suc_nat_number_of_add:
   126      "Suc (number_of v + n) =  
   127         (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)" 
   128 by (simp del: nat_number_of 
   129          add: nat_number_of_def neg_nat
   130               Suc_nat_eq_nat_zadd1 number_of_succ) 
   131 
   132 lemma Suc_nat_number_of [simp]:
   133      "Suc (number_of v) =  
   134         (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
   135 apply (cut_tac n = 0 in Suc_nat_number_of_add)
   136 apply (simp cong del: if_weak_cong)
   137 done
   138 
   139 
   140 subsubsection{*Addition *}
   141 
   142 (*"neg" is used in rewrite rules for binary comparisons*)
   143 lemma add_nat_number_of [simp]:
   144      "(number_of v :: nat) + number_of v' =  
   145          (if neg (number_of v :: int) then number_of v'  
   146           else if neg (number_of v' :: int) then number_of v  
   147           else number_of (v + v'))"
   148 by (force dest!: neg_nat
   149           simp del: nat_number_of
   150           simp add: nat_number_of_def nat_add_distrib [symmetric]) 
   151 
   152 
   153 subsubsection{*Subtraction *}
   154 
   155 lemma diff_nat_eq_if:
   156      "nat z - nat z' =  
   157         (if neg z' then nat z   
   158          else let d = z-z' in     
   159               if neg d then 0 else nat d)"
   160 apply (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
   161 done
   162 
   163 lemma diff_nat_number_of [simp]: 
   164      "(number_of v :: nat) - number_of v' =  
   165         (if neg (number_of v' :: int) then number_of v  
   166          else let d = number_of (v + uminus v') in     
   167               if neg d then 0 else nat d)"
   168 by (simp del: nat_number_of add: diff_nat_eq_if nat_number_of_def) 
   169 
   170 
   171 
   172 subsubsection{*Multiplication *}
   173 
   174 lemma mult_nat_number_of [simp]:
   175      "(number_of v :: nat) * number_of v' =  
   176        (if neg (number_of v :: int) then 0 else number_of (v * v'))"
   177 by (force dest!: neg_nat
   178           simp del: nat_number_of
   179           simp add: nat_number_of_def nat_mult_distrib [symmetric]) 
   180 
   181 
   182 
   183 subsubsection{*Quotient *}
   184 
   185 lemma div_nat_number_of [simp]:
   186      "(number_of v :: nat)  div  number_of v' =  
   187           (if neg (number_of v :: int) then 0  
   188            else nat (number_of v div number_of v'))"
   189 by (force dest!: neg_nat
   190           simp del: nat_number_of
   191           simp add: nat_number_of_def nat_div_distrib [symmetric]) 
   192 
   193 lemma one_div_nat_number_of [simp]:
   194      "(Suc 0)  div  number_of v' = (nat (1 div number_of v'))" 
   195 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
   196 
   197 
   198 subsubsection{*Remainder *}
   199 
   200 lemma mod_nat_number_of [simp]:
   201      "(number_of v :: nat)  mod  number_of v' =  
   202         (if neg (number_of v :: int) then 0  
   203          else if neg (number_of v' :: int) then number_of v  
   204          else nat (number_of v mod number_of v'))"
   205 by (force dest!: neg_nat
   206           simp del: nat_number_of
   207           simp add: nat_number_of_def nat_mod_distrib [symmetric]) 
   208 
   209 lemma one_mod_nat_number_of [simp]:
   210      "(Suc 0)  mod  number_of v' =  
   211         (if neg (number_of v' :: int) then Suc 0
   212          else nat (1 mod number_of v'))"
   213 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
   214 
   215 
   216 subsubsection{* Divisibility *}
   217 
   218 lemmas dvd_eq_mod_eq_0_number_of =
   219   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
   220 
   221 declare dvd_eq_mod_eq_0_number_of [simp]
   222 
   223 ML
   224 {*
   225 val nat_number_of_def = thm"nat_number_of_def";
   226 
   227 val nat_number_of = thm"nat_number_of";
   228 val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
   229 val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
   230 val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
   231 val numeral_2_eq_2 = thm"numeral_2_eq_2";
   232 val nat_div_distrib = thm"nat_div_distrib";
   233 val nat_mod_distrib = thm"nat_mod_distrib";
   234 val int_nat_number_of = thm"int_nat_number_of";
   235 val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
   236 val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
   237 val Suc_nat_number_of = thm"Suc_nat_number_of";
   238 val add_nat_number_of = thm"add_nat_number_of";
   239 val diff_nat_eq_if = thm"diff_nat_eq_if";
   240 val diff_nat_number_of = thm"diff_nat_number_of";
   241 val mult_nat_number_of = thm"mult_nat_number_of";
   242 val div_nat_number_of = thm"div_nat_number_of";
   243 val mod_nat_number_of = thm"mod_nat_number_of";
   244 *}
   245 
   246 
   247 subsection{*Comparisons*}
   248 
   249 subsubsection{*Equals (=) *}
   250 
   251 lemma eq_nat_nat_iff:
   252      "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
   253 by (auto elim!: nonneg_eq_int)
   254 
   255 (*"neg" is used in rewrite rules for binary comparisons*)
   256 lemma eq_nat_number_of [simp]:
   257      "((number_of v :: nat) = number_of v') =  
   258       (if neg (number_of v :: int) then (iszero (number_of v' :: int) | neg (number_of v' :: int))  
   259        else if neg (number_of v' :: int) then iszero (number_of v :: int)  
   260        else iszero (number_of (v + uminus v') :: int))"
   261 apply (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
   262                   eq_nat_nat_iff eq_number_of_eq nat_0 iszero_def
   263             split add: split_if cong add: imp_cong)
   264 apply (simp only: nat_eq_iff nat_eq_iff2)
   265 apply (simp add: not_neg_eq_ge_0 [symmetric])
   266 done
   267 
   268 
   269 subsubsection{*Less-than (<) *}
   270 
   271 (*"neg" is used in rewrite rules for binary comparisons*)
   272 lemma less_nat_number_of [simp]:
   273      "((number_of v :: nat) < number_of v') =  
   274          (if neg (number_of v :: int) then neg (number_of (uminus v') :: int)  
   275           else neg (number_of (v + uminus v') :: int))"
   276 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
   277                 nat_less_eq_zless less_number_of_eq_neg zless_nat_eq_int_zless
   278          cong add: imp_cong, simp add: Pls_def)
   279 
   280 
   281 (*Maps #n to n for n = 0, 1, 2*)
   282 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
   283 
   284 
   285 subsection{*Powers with Numeric Exponents*}
   286 
   287 text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}.
   288 We cannot prove general results about the numeral @{term "-1"}, so we have to
   289 use @{term "- 1"} instead.*}
   290 
   291 lemma power2_eq_square: "(a::'a::recpower)\<twosuperior> = a * a"
   292   by (simp add: numeral_2_eq_2 Power.power_Suc)
   293 
   294 lemma zero_power2 [simp]: "(0::'a::{semiring_1,recpower})\<twosuperior> = 0"
   295   by (simp add: power2_eq_square)
   296 
   297 lemma one_power2 [simp]: "(1::'a::{semiring_1,recpower})\<twosuperior> = 1"
   298   by (simp add: power2_eq_square)
   299 
   300 lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x"
   301   apply (subgoal_tac "3 = Suc (Suc (Suc 0))")
   302   apply (erule ssubst)
   303   apply (simp add: power_Suc mult_ac)
   304   apply (unfold nat_number_of_def)
   305   apply (subst nat_eq_iff)
   306   apply simp
   307 done
   308 
   309 text{*Squares of literal numerals will be evaluated.*}
   310 lemmas power2_eq_square_number_of =
   311     power2_eq_square [of "number_of w", standard]
   312 declare power2_eq_square_number_of [simp]
   313 
   314 
   315 lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})"
   316   by (simp add: power2_eq_square)
   317 
   318 lemma zero_less_power2[simp]:
   319      "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))"
   320   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
   321 
   322 lemma power2_less_0[simp]:
   323   fixes a :: "'a::{ordered_idom,recpower}"
   324   shows "~ (a\<twosuperior> < 0)"
   325 by (force simp add: power2_eq_square mult_less_0_iff) 
   326 
   327 lemma zero_eq_power2[simp]:
   328      "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))"
   329   by (force simp add: power2_eq_square mult_eq_0_iff)
   330 
   331 lemma abs_power2[simp]:
   332      "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})"
   333   by (simp add: power2_eq_square abs_mult abs_mult_self)
   334 
   335 lemma power2_abs[simp]:
   336      "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})"
   337   by (simp add: power2_eq_square abs_mult_self)
   338 
   339 lemma power2_minus[simp]:
   340      "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})"
   341   by (simp add: power2_eq_square)
   342 
   343 lemma power2_le_imp_le:
   344   fixes x y :: "'a::{ordered_semidom,recpower}"
   345   shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y"
   346 unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
   347 
   348 lemma power2_less_imp_less:
   349   fixes x y :: "'a::{ordered_semidom,recpower}"
   350   shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y"
   351 by (rule power_less_imp_less_base)
   352 
   353 lemma power2_eq_imp_eq:
   354   fixes x y :: "'a::{ordered_semidom,recpower}"
   355   shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y"
   356 unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp)
   357 
   358 lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})"
   359 apply (induct "n")
   360 apply (auto simp add: power_Suc power_add)
   361 done
   362 
   363 lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2"
   364 by (subst mult_commute) (simp add: power_mult)
   365 
   366 lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2"
   367 by (simp add: power_even_eq) 
   368 
   369 lemma power_minus_even [simp]:
   370      "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)"
   371 by (simp add: power_minus1_even power_minus [of a]) 
   372 
   373 lemma zero_le_even_power'[simp]:
   374      "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)"
   375 proof (induct "n")
   376   case 0
   377     show ?case by (simp add: zero_le_one)
   378 next
   379   case (Suc n)
   380     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
   381       by (simp add: mult_ac power_add power2_eq_square)
   382     thus ?case
   383       by (simp add: prems zero_le_mult_iff)
   384 qed
   385 
   386 lemma odd_power_less_zero:
   387      "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0"
   388 proof (induct "n")
   389   case 0
   390   then show ?case by (simp add: Power.power_Suc)
   391 next
   392   case (Suc n)
   393   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)" 
   394     by (simp add: mult_ac power_add power2_eq_square Power.power_Suc)
   395   thus ?case
   396     by (simp add: prems mult_less_0_iff mult_neg_neg)
   397 qed
   398 
   399 lemma odd_0_le_power_imp_0_le:
   400      "0 \<le> a  ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})"
   401 apply (insert odd_power_less_zero [of a n]) 
   402 apply (force simp add: linorder_not_less [symmetric]) 
   403 done
   404 
   405 text{*Simprules for comparisons where common factors can be cancelled.*}
   406 lemmas zero_compare_simps =
   407     add_strict_increasing add_strict_increasing2 add_increasing
   408     zero_le_mult_iff zero_le_divide_iff 
   409     zero_less_mult_iff zero_less_divide_iff 
   410     mult_le_0_iff divide_le_0_iff 
   411     mult_less_0_iff divide_less_0_iff 
   412     zero_le_power2 power2_less_0
   413 
   414 subsubsection{*Nat *}
   415 
   416 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
   417 by (simp add: numerals)
   418 
   419 (*Expresses a natural number constant as the Suc of another one.
   420   NOT suitable for rewriting because n recurs in the condition.*)
   421 lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
   422 
   423 subsubsection{*Arith *}
   424 
   425 lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
   426 by (simp add: numerals)
   427 
   428 lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
   429 by (simp add: numerals)
   430 
   431 (* These two can be useful when m = number_of... *)
   432 
   433 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
   434 apply (case_tac "m")
   435 apply (simp_all add: numerals)
   436 done
   437 
   438 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
   439 apply (case_tac "m")
   440 apply (simp_all add: numerals)
   441 done
   442 
   443 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
   444 apply (case_tac "m")
   445 apply (simp_all add: numerals)
   446 done
   447 
   448 
   449 subsection{*Comparisons involving (0::nat) *}
   450 
   451 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
   452 
   453 lemma eq_number_of_0 [simp]:
   454      "(number_of v = (0::nat)) =  
   455       (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
   456 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
   457 
   458 lemma eq_0_number_of [simp]:
   459      "((0::nat) = number_of v) =  
   460       (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
   461 by (rule trans [OF eq_sym_conv eq_number_of_0])
   462 
   463 lemma less_0_number_of [simp]:
   464      "((0::nat) < number_of v) = neg (number_of (uminus v) :: int)"
   465 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] Pls_def)
   466 
   467 
   468 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
   469 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
   470 
   471 
   472 
   473 subsection{*Comparisons involving  @{term Suc} *}
   474 
   475 lemma eq_number_of_Suc [simp]:
   476      "(number_of v = Suc n) =  
   477         (let pv = number_of (Int.pred v) in  
   478          if neg pv then False else nat pv = n)"
   479 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   480                   number_of_pred nat_number_of_def 
   481             split add: split_if)
   482 apply (rule_tac x = "number_of v" in spec)
   483 apply (auto simp add: nat_eq_iff)
   484 done
   485 
   486 lemma Suc_eq_number_of [simp]:
   487      "(Suc n = number_of v) =  
   488         (let pv = number_of (Int.pred v) in  
   489          if neg pv then False else nat pv = n)"
   490 by (rule trans [OF eq_sym_conv eq_number_of_Suc])
   491 
   492 lemma less_number_of_Suc [simp]:
   493      "(number_of v < Suc n) =  
   494         (let pv = number_of (Int.pred v) in  
   495          if neg pv then True else nat pv < n)"
   496 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   497                   number_of_pred nat_number_of_def  
   498             split add: split_if)
   499 apply (rule_tac x = "number_of v" in spec)
   500 apply (auto simp add: nat_less_iff)
   501 done
   502 
   503 lemma less_Suc_number_of [simp]:
   504      "(Suc n < number_of v) =  
   505         (let pv = number_of (Int.pred v) in  
   506          if neg pv then False else n < nat pv)"
   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: zless_nat_eq_int_zless)
   512 done
   513 
   514 lemma le_number_of_Suc [simp]:
   515      "(number_of v <= Suc n) =  
   516         (let pv = number_of (Int.pred v) in  
   517          if neg pv then True else nat pv <= n)"
   518 by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
   519 
   520 lemma le_Suc_number_of [simp]:
   521      "(Suc n <= number_of v) =  
   522         (let pv = number_of (Int.pred v) in  
   523          if neg pv then False else n <= nat pv)"
   524 by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
   525 
   526 
   527 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
   528 by auto
   529 
   530 
   531 
   532 subsection{*Max and Min Combined with @{term Suc} *}
   533 
   534 lemma max_number_of_Suc [simp]:
   535      "max (Suc n) (number_of v) =  
   536         (let pv = number_of (Int.pred v) in  
   537          if neg pv then Suc n else Suc(max n (nat pv)))"
   538 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   539             split add: split_if nat.split)
   540 apply (rule_tac x = "number_of v" in spec) 
   541 apply auto
   542 done
   543  
   544 lemma max_Suc_number_of [simp]:
   545      "max (number_of v) (Suc n) =  
   546         (let pv = number_of (Int.pred v) in  
   547          if neg pv then Suc n else Suc(max (nat pv) n))"
   548 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   549             split add: split_if nat.split)
   550 apply (rule_tac x = "number_of v" in spec) 
   551 apply auto
   552 done
   553  
   554 lemma min_number_of_Suc [simp]:
   555      "min (Suc n) (number_of v) =  
   556         (let pv = number_of (Int.pred v) in  
   557          if neg pv then 0 else Suc(min n (nat pv)))"
   558 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   559             split add: split_if nat.split)
   560 apply (rule_tac x = "number_of v" in spec) 
   561 apply auto
   562 done
   563  
   564 lemma min_Suc_number_of [simp]:
   565      "min (number_of v) (Suc n) =  
   566         (let pv = number_of (Int.pred v) in  
   567          if neg pv then 0 else Suc(min (nat pv) n))"
   568 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   569             split add: split_if nat.split)
   570 apply (rule_tac x = "number_of v" in spec) 
   571 apply auto
   572 done
   573  
   574 subsection{*Literal arithmetic involving powers*}
   575 
   576 lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
   577 apply (induct "n")
   578 apply (simp_all (no_asm_simp) add: nat_mult_distrib)
   579 done
   580 
   581 lemma power_nat_number_of:
   582      "(number_of v :: nat) ^ n =  
   583        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
   584 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
   585          split add: split_if cong: imp_cong)
   586 
   587 
   588 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
   589 declare power_nat_number_of_number_of [simp]
   590 
   591 
   592 
   593 text{*For arbitrary rings*}
   594 
   595 lemma power_number_of_even:
   596   fixes z :: "'a::{number_ring,recpower}"
   597   shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
   598 unfolding Let_def nat_number_of_def number_of_Bit0
   599 apply (rule_tac x = "number_of w" in spec, clarify)
   600 apply (case_tac " (0::int) <= x")
   601 apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
   602 done
   603 
   604 lemma power_number_of_odd:
   605   fixes z :: "'a::{number_ring,recpower}"
   606   shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
   607      then (let w = z ^ (number_of w) in z * w * w) else 1)"
   608 unfolding Let_def nat_number_of_def number_of_Bit1
   609 apply (rule_tac x = "number_of w" in spec, auto)
   610 apply (simp only: nat_add_distrib nat_mult_distrib)
   611 apply simp
   612 apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
   613 done
   614 
   615 lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
   616 lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
   617 
   618 lemmas power_number_of_even_number_of [simp] =
   619     power_number_of_even [of "number_of v", standard]
   620 
   621 lemmas power_number_of_odd_number_of [simp] =
   622     power_number_of_odd [of "number_of v", standard]
   623 
   624 
   625 
   626 ML
   627 {*
   628 val numeral_ss = @{simpset} addsimps @{thms numerals};
   629 
   630 val nat_bin_arith_setup =
   631  LinArith.map_data
   632    (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
   633      {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
   634       inj_thms = inj_thms,
   635       lessD = lessD, neqE = neqE,
   636       simpset = simpset addsimps [Suc_nat_number_of, int_nat_number_of,
   637         @{thm not_neg_number_of_Pls}, @{thm neg_number_of_Min},
   638         @{thm neg_number_of_Bit0}, @{thm neg_number_of_Bit1}]})
   639 *}
   640 
   641 declaration {* K nat_bin_arith_setup *}
   642 
   643 (* Enable arith to deal with div/mod k where k is a numeral: *)
   644 declare split_div[of _ _ "number_of k", standard, arith_split]
   645 declare split_mod[of _ _ "number_of k", standard, arith_split]
   646 
   647 lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
   648   by (simp add: number_of_Pls nat_number_of_def)
   649 
   650 lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
   651   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
   652   done
   653 
   654 lemma nat_number_of_Bit0:
   655     "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
   656   apply (simp only: nat_number_of_def Let_def)
   657   apply (cases "neg (number_of w :: int)")
   658    apply (simp add: neg_nat neg_number_of_Bit0)
   659   apply (rule int_int_eq [THEN iffD1])
   660   apply (simp only: not_neg_nat neg_number_of_Bit0 int_Suc zadd_int [symmetric] simp_thms)
   661   apply (simp only: number_of_Bit0 zadd_assoc)
   662   apply simp
   663   done
   664 
   665 lemma nat_number_of_Bit1:
   666   "number_of (Int.Bit1 w) =
   667     (if neg (number_of w :: int) then 0
   668      else let n = number_of w in Suc (n + n))"
   669   apply (simp only: nat_number_of_def Let_def split: split_if)
   670   apply (intro conjI impI)
   671    apply (simp add: neg_nat neg_number_of_Bit1)
   672   apply (rule int_int_eq [THEN iffD1])
   673   apply (simp only: not_neg_nat neg_number_of_Bit1 int_Suc zadd_int [symmetric] simp_thms)
   674   apply (simp only: number_of_Bit1 zadd_assoc)
   675   done
   676 
   677 lemmas nat_number =
   678   nat_number_of_Pls nat_number_of_Min
   679   nat_number_of_Bit0 nat_number_of_Bit1
   680 
   681 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
   682   by (simp add: Let_def)
   683 
   684 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})"
   685 by (simp add: power_mult power_Suc); 
   686 
   687 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})"
   688 by (simp add: power_mult power_Suc); 
   689 
   690 
   691 subsection{*Literal arithmetic and @{term of_nat}*}
   692 
   693 lemma of_nat_double:
   694      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
   695 by (simp only: mult_2 nat_add_distrib of_nat_add) 
   696 
   697 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
   698 by (simp only: nat_number_of_def)
   699 
   700 lemma of_nat_number_of_lemma:
   701      "of_nat (number_of v :: nat) =  
   702          (if 0 \<le> (number_of v :: int) 
   703           then (number_of v :: 'a :: number_ring)
   704           else 0)"
   705 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
   706 
   707 lemma of_nat_number_of_eq [simp]:
   708      "of_nat (number_of v :: nat) =  
   709          (if neg (number_of v :: int) then 0  
   710           else (number_of v :: 'a :: number_ring))"
   711 by (simp only: of_nat_number_of_lemma neg_def, simp) 
   712 
   713 
   714 subsection {*Lemmas for the Combination and Cancellation Simprocs*}
   715 
   716 lemma nat_number_of_add_left:
   717      "number_of v + (number_of v' + (k::nat)) =  
   718          (if neg (number_of v :: int) then number_of v' + k  
   719           else if neg (number_of v' :: int) then number_of v + k  
   720           else number_of (v + v') + k)"
   721 by simp
   722 
   723 lemma nat_number_of_mult_left:
   724      "number_of v * (number_of v' * (k::nat)) =  
   725          (if neg (number_of v :: int) then 0
   726           else number_of (v * v') * k)"
   727 by simp
   728 
   729 
   730 subsubsection{*For @{text combine_numerals}*}
   731 
   732 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
   733 by (simp add: add_mult_distrib)
   734 
   735 
   736 subsubsection{*For @{text cancel_numerals}*}
   737 
   738 lemma nat_diff_add_eq1:
   739      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
   740 by (simp split add: nat_diff_split add: add_mult_distrib)
   741 
   742 lemma nat_diff_add_eq2:
   743      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
   744 by (simp split add: nat_diff_split add: add_mult_distrib)
   745 
   746 lemma nat_eq_add_iff1:
   747      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
   748 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   749 
   750 lemma nat_eq_add_iff2:
   751      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
   752 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   753 
   754 lemma nat_less_add_iff1:
   755      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
   756 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   757 
   758 lemma nat_less_add_iff2:
   759      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
   760 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   761 
   762 lemma nat_le_add_iff1:
   763      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
   764 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   765 
   766 lemma nat_le_add_iff2:
   767      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
   768 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   769 
   770 
   771 subsubsection{*For @{text cancel_numeral_factors} *}
   772 
   773 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
   774 by auto
   775 
   776 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
   777 by auto
   778 
   779 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
   780 by auto
   781 
   782 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
   783 by auto
   784 
   785 lemma nat_mult_dvd_cancel_disj[simp]:
   786   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
   787 by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
   788 
   789 lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
   790 by(auto)
   791 
   792 
   793 subsubsection{*For @{text cancel_factor} *}
   794 
   795 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
   796 by auto
   797 
   798 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
   799 by auto
   800 
   801 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
   802 by auto
   803 
   804 lemma nat_mult_div_cancel_disj[simp]:
   805      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
   806 by (simp add: nat_mult_div_cancel1)
   807 
   808 end