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