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