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