src/HOL/Nat_Numeral.thy
author wenzelm
Mon Mar 22 20:58:52 2010 +0100 (2010-03-22)
changeset 35898 c890a3835d15
parent 35815 10e723e54076
child 36699 816da1023508
permissions -rw-r--r--
recovered header;
     1 (*  Title:      HOL/Nat_Numeral.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1999  University of Cambridge
     4 *)
     5 
     6 header {* Binary numerals for the natural numbers *}
     7 
     8 theory Nat_Numeral
     9 imports Int
    10 begin
    11 
    12 subsection {* Numerals for natural numbers *}
    13 
    14 text {*
    15   Arithmetic for naturals is reduced to that for the non-negative integers.
    16 *}
    17 
    18 instantiation nat :: number
    19 begin
    20 
    21 definition
    22   nat_number_of_def [code_unfold, code del]: "number_of v = nat (number_of v)"
    23 
    24 instance ..
    25 
    26 end
    27 
    28 lemma [code_post]:
    29   "nat (number_of v) = number_of v"
    30   unfolding nat_number_of_def ..
    31 
    32 
    33 subsection {* Special case: squares and cubes *}
    34 
    35 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
    36   by (simp add: nat_number_of_def)
    37 
    38 lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
    39   by (simp add: nat_number_of_def)
    40 
    41 context power
    42 begin
    43 
    44 abbreviation (xsymbols)
    45   power2 :: "'a \<Rightarrow> 'a"  ("(_\<twosuperior>)" [1000] 999) where
    46   "x\<twosuperior> \<equiv> x ^ 2"
    47 
    48 notation (latex output)
    49   power2  ("(_\<twosuperior>)" [1000] 999)
    50 
    51 notation (HTML output)
    52   power2  ("(_\<twosuperior>)" [1000] 999)
    53 
    54 end
    55 
    56 context monoid_mult
    57 begin
    58 
    59 lemma power2_eq_square: "a\<twosuperior> = a * a"
    60   by (simp add: numeral_2_eq_2)
    61 
    62 lemma power3_eq_cube: "a ^ 3 = a * a * a"
    63   by (simp add: numeral_3_eq_3 mult_assoc)
    64 
    65 lemma power_even_eq:
    66   "a ^ (2*n) = (a ^ n) ^ 2"
    67   by (subst mult_commute) (simp add: power_mult)
    68 
    69 lemma power_odd_eq:
    70   "a ^ Suc (2*n) = a * (a ^ n) ^ 2"
    71   by (simp add: power_even_eq)
    72 
    73 end
    74 
    75 context semiring_1
    76 begin
    77 
    78 lemma zero_power2 [simp]: "0\<twosuperior> = 0"
    79   by (simp add: power2_eq_square)
    80 
    81 lemma one_power2 [simp]: "1\<twosuperior> = 1"
    82   by (simp add: power2_eq_square)
    83 
    84 end
    85 
    86 context comm_ring_1
    87 begin
    88 
    89 lemma power2_minus [simp]:
    90   "(- a)\<twosuperior> = a\<twosuperior>"
    91   by (simp add: power2_eq_square)
    92 
    93 text{*
    94   We cannot prove general results about the numeral @{term "-1"},
    95   so we have to use @{term "- 1"} instead.
    96 *}
    97 
    98 lemma power_minus1_even [simp]:
    99   "(- 1) ^ (2*n) = 1"
   100 proof (induct n)
   101   case 0 show ?case by simp
   102 next
   103   case (Suc n) then show ?case by (simp add: power_add)
   104 qed
   105 
   106 lemma power_minus1_odd:
   107   "(- 1) ^ Suc (2*n) = - 1"
   108   by simp
   109 
   110 lemma power_minus_even [simp]:
   111   "(-a) ^ (2*n) = a ^ (2*n)"
   112   by (simp add: power_minus [of a]) 
   113 
   114 end
   115 
   116 context linordered_ring
   117 begin
   118 
   119 lemma sum_squares_ge_zero:
   120   "0 \<le> x * x + y * y"
   121   by (intro add_nonneg_nonneg zero_le_square)
   122 
   123 lemma not_sum_squares_lt_zero:
   124   "\<not> x * x + y * y < 0"
   125   by (simp add: not_less sum_squares_ge_zero)
   126 
   127 end
   128 
   129 context linordered_ring_strict
   130 begin
   131 
   132 lemma sum_squares_eq_zero_iff:
   133   "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   134   by (simp add: add_nonneg_eq_0_iff)
   135 
   136 lemma sum_squares_le_zero_iff:
   137   "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   138   by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
   139 
   140 lemma sum_squares_gt_zero_iff:
   141   "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   142   by (simp add: not_le [symmetric] sum_squares_le_zero_iff)
   143 
   144 end
   145 
   146 context linordered_semidom
   147 begin
   148 
   149 lemma power2_le_imp_le:
   150   "x\<twosuperior> \<le> y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
   151   unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
   152 
   153 lemma power2_less_imp_less:
   154   "x\<twosuperior> < y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
   155   by (rule power_less_imp_less_base)
   156 
   157 lemma power2_eq_imp_eq:
   158   "x\<twosuperior> = y\<twosuperior> \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
   159   unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
   160 
   161 end
   162 
   163 context linordered_idom
   164 begin
   165 
   166 lemma zero_eq_power2 [simp]:
   167   "a\<twosuperior> = 0 \<longleftrightarrow> a = 0"
   168   by (force simp add: power2_eq_square)
   169 
   170 lemma zero_le_power2 [simp]:
   171   "0 \<le> a\<twosuperior>"
   172   by (simp add: power2_eq_square)
   173 
   174 lemma zero_less_power2 [simp]:
   175   "0 < a\<twosuperior> \<longleftrightarrow> a \<noteq> 0"
   176   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
   177 
   178 lemma power2_less_0 [simp]:
   179   "\<not> a\<twosuperior> < 0"
   180   by (force simp add: power2_eq_square mult_less_0_iff) 
   181 
   182 lemma abs_power2 [simp]:
   183   "abs (a\<twosuperior>) = a\<twosuperior>"
   184   by (simp add: power2_eq_square abs_mult abs_mult_self)
   185 
   186 lemma power2_abs [simp]:
   187   "(abs a)\<twosuperior> = a\<twosuperior>"
   188   by (simp add: power2_eq_square abs_mult_self)
   189 
   190 lemma odd_power_less_zero:
   191   "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
   192 proof (induct n)
   193   case 0
   194   then show ?case by simp
   195 next
   196   case (Suc n)
   197   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
   198     by (simp add: mult_ac power_add power2_eq_square)
   199   thus ?case
   200     by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
   201 qed
   202 
   203 lemma odd_0_le_power_imp_0_le:
   204   "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
   205   using odd_power_less_zero [of a n]
   206     by (force simp add: linorder_not_less [symmetric]) 
   207 
   208 lemma zero_le_even_power'[simp]:
   209   "0 \<le> a ^ (2*n)"
   210 proof (induct n)
   211   case 0
   212     show ?case by simp
   213 next
   214   case (Suc n)
   215     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
   216       by (simp add: mult_ac power_add power2_eq_square)
   217     thus ?case
   218       by (simp add: Suc zero_le_mult_iff)
   219 qed
   220 
   221 lemma sum_power2_ge_zero:
   222   "0 \<le> x\<twosuperior> + y\<twosuperior>"
   223   unfolding power2_eq_square by (rule sum_squares_ge_zero)
   224 
   225 lemma not_sum_power2_lt_zero:
   226   "\<not> x\<twosuperior> + y\<twosuperior> < 0"
   227   unfolding power2_eq_square by (rule not_sum_squares_lt_zero)
   228 
   229 lemma sum_power2_eq_zero_iff:
   230   "x\<twosuperior> + y\<twosuperior> = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   231   unfolding power2_eq_square by (rule sum_squares_eq_zero_iff)
   232 
   233 lemma sum_power2_le_zero_iff:
   234   "x\<twosuperior> + y\<twosuperior> \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   235   unfolding power2_eq_square by (rule sum_squares_le_zero_iff)
   236 
   237 lemma sum_power2_gt_zero_iff:
   238   "0 < x\<twosuperior> + y\<twosuperior> \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   239   unfolding power2_eq_square by (rule sum_squares_gt_zero_iff)
   240 
   241 end
   242 
   243 lemma power2_sum:
   244   fixes x y :: "'a::number_ring"
   245   shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
   246   by (simp add: ring_distribs power2_eq_square mult_2) (rule mult_commute)
   247 
   248 lemma power2_diff:
   249   fixes x y :: "'a::number_ring"
   250   shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
   251   by (simp add: ring_distribs power2_eq_square mult_2) (rule mult_commute)
   252 
   253 
   254 subsection {* Predicate for negative binary numbers *}
   255 
   256 definition neg  :: "int \<Rightarrow> bool" where
   257   "neg Z \<longleftrightarrow> Z < 0"
   258 
   259 lemma not_neg_int [simp]: "~ neg (of_nat n)"
   260 by (simp add: neg_def)
   261 
   262 lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
   263 by (simp add: neg_def del: of_nat_Suc)
   264 
   265 lemmas neg_eq_less_0 = neg_def
   266 
   267 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
   268 by (simp add: neg_def linorder_not_less)
   269 
   270 text{*To simplify inequalities when Numeral1 can get simplified to 1*}
   271 
   272 lemma not_neg_0: "~ neg 0"
   273 by (simp add: One_int_def neg_def)
   274 
   275 lemma not_neg_1: "~ neg 1"
   276 by (simp add: neg_def linorder_not_less)
   277 
   278 lemma neg_nat: "neg z ==> nat z = 0"
   279 by (simp add: neg_def order_less_imp_le) 
   280 
   281 lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
   282 by (simp add: linorder_not_less neg_def)
   283 
   284 text {*
   285   If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
   286   @{term Numeral0} IS @{term "number_of Pls"}
   287 *}
   288 
   289 lemma not_neg_number_of_Pls: "~ neg (number_of Int.Pls)"
   290   by (simp add: neg_def)
   291 
   292 lemma neg_number_of_Min: "neg (number_of Int.Min)"
   293   by (simp add: neg_def)
   294 
   295 lemma neg_number_of_Bit0:
   296   "neg (number_of (Int.Bit0 w)) = neg (number_of w)"
   297   by (simp add: neg_def)
   298 
   299 lemma neg_number_of_Bit1:
   300   "neg (number_of (Int.Bit1 w)) = neg (number_of w)"
   301   by (simp add: neg_def)
   302 
   303 lemmas neg_simps [simp] =
   304   not_neg_0 not_neg_1
   305   not_neg_number_of_Pls neg_number_of_Min
   306   neg_number_of_Bit0 neg_number_of_Bit1
   307 
   308 
   309 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
   310 
   311 declare nat_1 [simp]
   312 
   313 lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
   314 by (simp add: nat_number_of_def)
   315 
   316 lemma nat_numeral_0_eq_0 [simp, code_post]: "Numeral0 = (0::nat)"
   317 by (simp add: nat_number_of_def)
   318 
   319 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
   320 by (simp add: nat_number_of_def)
   321 
   322 lemma numeral_1_eq_Suc_0 [code_post]: "Numeral1 = Suc 0"
   323 by (simp only: nat_numeral_1_eq_1 One_nat_def)
   324 
   325 
   326 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
   327 
   328 lemma int_nat_number_of [simp]:
   329      "int (number_of v) =  
   330          (if neg (number_of v :: int) then 0  
   331           else (number_of v :: int))"
   332   unfolding nat_number_of_def number_of_is_id neg_def
   333   by simp
   334 
   335 
   336 subsubsection{*Successor *}
   337 
   338 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
   339 apply (rule sym)
   340 apply (simp add: nat_eq_iff int_Suc)
   341 done
   342 
   343 lemma Suc_nat_number_of_add:
   344      "Suc (number_of v + n) =  
   345         (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)"
   346   unfolding nat_number_of_def number_of_is_id neg_def numeral_simps
   347   by (simp add: Suc_nat_eq_nat_zadd1 add_ac)
   348 
   349 lemma Suc_nat_number_of [simp]:
   350      "Suc (number_of v) =  
   351         (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
   352 apply (cut_tac n = 0 in Suc_nat_number_of_add)
   353 apply (simp cong del: if_weak_cong)
   354 done
   355 
   356 
   357 subsubsection{*Addition *}
   358 
   359 lemma add_nat_number_of [simp]:
   360      "(number_of v :: nat) + number_of v' =  
   361          (if v < Int.Pls then number_of v'  
   362           else if v' < Int.Pls then number_of v  
   363           else number_of (v + v'))"
   364   unfolding nat_number_of_def number_of_is_id numeral_simps
   365   by (simp add: nat_add_distrib)
   366 
   367 lemma nat_number_of_add_1 [simp]:
   368   "number_of v + (1::nat) =
   369     (if v < Int.Pls then 1 else number_of (Int.succ v))"
   370   unfolding nat_number_of_def number_of_is_id numeral_simps
   371   by (simp add: nat_add_distrib)
   372 
   373 lemma nat_1_add_number_of [simp]:
   374   "(1::nat) + number_of v =
   375     (if v < Int.Pls then 1 else number_of (Int.succ v))"
   376   unfolding nat_number_of_def number_of_is_id numeral_simps
   377   by (simp add: nat_add_distrib)
   378 
   379 lemma nat_1_add_1 [simp]: "1 + 1 = (2::nat)"
   380   by (rule int_int_eq [THEN iffD1]) simp
   381 
   382 
   383 subsubsection{*Subtraction *}
   384 
   385 lemma diff_nat_eq_if:
   386      "nat z - nat z' =  
   387         (if neg z' then nat z   
   388          else let d = z-z' in     
   389               if neg d then 0 else nat d)"
   390 by (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
   391 
   392 
   393 lemma diff_nat_number_of [simp]: 
   394      "(number_of v :: nat) - number_of v' =  
   395         (if v' < Int.Pls then number_of v  
   396          else let d = number_of (v + uminus v') in     
   397               if neg d then 0 else nat d)"
   398   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def
   399   by auto
   400 
   401 lemma nat_number_of_diff_1 [simp]:
   402   "number_of v - (1::nat) =
   403     (if v \<le> Int.Pls then 0 else number_of (Int.pred v))"
   404   unfolding nat_number_of_def number_of_is_id numeral_simps
   405   by auto
   406 
   407 
   408 subsubsection{*Multiplication *}
   409 
   410 lemma mult_nat_number_of [simp]:
   411      "(number_of v :: nat) * number_of v' =  
   412        (if v < Int.Pls then 0 else number_of (v * v'))"
   413   unfolding nat_number_of_def number_of_is_id numeral_simps
   414   by (simp add: nat_mult_distrib)
   415 
   416 
   417 subsection{*Comparisons*}
   418 
   419 subsubsection{*Equals (=) *}
   420 
   421 lemma eq_nat_number_of [simp]:
   422      "((number_of v :: nat) = number_of v') =  
   423       (if neg (number_of v :: int) then (number_of v' :: int) \<le> 0
   424        else if neg (number_of v' :: int) then (number_of v :: int) = 0
   425        else v = v')"
   426   unfolding nat_number_of_def number_of_is_id neg_def
   427   by auto
   428 
   429 
   430 subsubsection{*Less-than (<) *}
   431 
   432 lemma less_nat_number_of [simp]:
   433   "(number_of v :: nat) < number_of v' \<longleftrightarrow>
   434     (if v < v' then Int.Pls < v' else False)"
   435   unfolding nat_number_of_def number_of_is_id numeral_simps
   436   by auto
   437 
   438 
   439 subsubsection{*Less-than-or-equal *}
   440 
   441 lemma le_nat_number_of [simp]:
   442   "(number_of v :: nat) \<le> number_of v' \<longleftrightarrow>
   443     (if v \<le> v' then True else v \<le> Int.Pls)"
   444   unfolding nat_number_of_def number_of_is_id numeral_simps
   445   by auto
   446 
   447 (*Maps #n to n for n = 0, 1, 2*)
   448 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
   449 
   450 
   451 subsection{*Powers with Numeric Exponents*}
   452 
   453 text{*Squares of literal numerals will be evaluated.*}
   454 lemmas power2_eq_square_number_of [simp] =
   455     power2_eq_square [of "number_of w", standard]
   456 
   457 
   458 text{*Simprules for comparisons where common factors can be cancelled.*}
   459 lemmas zero_compare_simps =
   460     add_strict_increasing add_strict_increasing2 add_increasing
   461     zero_le_mult_iff zero_le_divide_iff 
   462     zero_less_mult_iff zero_less_divide_iff 
   463     mult_le_0_iff divide_le_0_iff 
   464     mult_less_0_iff divide_less_0_iff 
   465     zero_le_power2 power2_less_0
   466 
   467 subsubsection{*Nat *}
   468 
   469 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
   470 by simp
   471 
   472 (*Expresses a natural number constant as the Suc of another one.
   473   NOT suitable for rewriting because n recurs in the condition.*)
   474 lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
   475 
   476 subsubsection{*Arith *}
   477 
   478 lemma Suc_eq_plus1: "Suc n = n + 1"
   479   unfolding One_nat_def by simp
   480 
   481 lemma Suc_eq_plus1_left: "Suc n = 1 + n"
   482   unfolding One_nat_def by simp
   483 
   484 (* These two can be useful when m = number_of... *)
   485 
   486 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
   487   unfolding One_nat_def by (cases m) simp_all
   488 
   489 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
   490   unfolding One_nat_def by (cases m) simp_all
   491 
   492 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
   493   unfolding One_nat_def by (cases m) simp_all
   494 
   495 
   496 subsection{*Comparisons involving (0::nat) *}
   497 
   498 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
   499 
   500 lemma eq_number_of_0 [simp]:
   501   "number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls"
   502   unfolding nat_number_of_def number_of_is_id numeral_simps
   503   by auto
   504 
   505 lemma eq_0_number_of [simp]:
   506   "(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls"
   507 by (rule trans [OF eq_sym_conv eq_number_of_0])
   508 
   509 lemma less_0_number_of [simp]:
   510    "(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v"
   511   unfolding nat_number_of_def number_of_is_id numeral_simps
   512   by simp
   513 
   514 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
   515 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
   516 
   517 
   518 
   519 subsection{*Comparisons involving  @{term Suc} *}
   520 
   521 lemma eq_number_of_Suc [simp]:
   522      "(number_of v = Suc n) =  
   523         (let pv = number_of (Int.pred v) in  
   524          if neg pv then False else nat pv = n)"
   525 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   526                   number_of_pred nat_number_of_def 
   527             split add: split_if)
   528 apply (rule_tac x = "number_of v" in spec)
   529 apply (auto simp add: nat_eq_iff)
   530 done
   531 
   532 lemma Suc_eq_number_of [simp]:
   533      "(Suc n = number_of v) =  
   534         (let pv = number_of (Int.pred v) in  
   535          if neg pv then False else nat pv = n)"
   536 by (rule trans [OF eq_sym_conv eq_number_of_Suc])
   537 
   538 lemma less_number_of_Suc [simp]:
   539      "(number_of v < Suc n) =  
   540         (let pv = number_of (Int.pred v) in  
   541          if neg pv then True else nat pv < n)"
   542 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   543                   number_of_pred nat_number_of_def  
   544             split add: split_if)
   545 apply (rule_tac x = "number_of v" in spec)
   546 apply (auto simp add: nat_less_iff)
   547 done
   548 
   549 lemma less_Suc_number_of [simp]:
   550      "(Suc n < number_of v) =  
   551         (let pv = number_of (Int.pred v) in  
   552          if neg pv then False else n < nat pv)"
   553 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   554                   number_of_pred nat_number_of_def
   555             split add: split_if)
   556 apply (rule_tac x = "number_of v" in spec)
   557 apply (auto simp add: zless_nat_eq_int_zless)
   558 done
   559 
   560 lemma le_number_of_Suc [simp]:
   561      "(number_of v <= Suc n) =  
   562         (let pv = number_of (Int.pred v) in  
   563          if neg pv then True else nat pv <= n)"
   564 by (simp add: Let_def linorder_not_less [symmetric])
   565 
   566 lemma le_Suc_number_of [simp]:
   567      "(Suc n <= number_of v) =  
   568         (let pv = number_of (Int.pred v) in  
   569          if neg pv then False else n <= nat pv)"
   570 by (simp add: Let_def linorder_not_less [symmetric])
   571 
   572 
   573 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
   574 by auto
   575 
   576 
   577 
   578 subsection{*Max and Min Combined with @{term Suc} *}
   579 
   580 lemma max_number_of_Suc [simp]:
   581      "max (Suc n) (number_of v) =  
   582         (let pv = number_of (Int.pred v) in  
   583          if neg pv then Suc n else Suc(max n (nat pv)))"
   584 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   585             split add: split_if nat.split)
   586 apply (rule_tac x = "number_of v" in spec) 
   587 apply auto
   588 done
   589  
   590 lemma max_Suc_number_of [simp]:
   591      "max (number_of v) (Suc n) =  
   592         (let pv = number_of (Int.pred v) in  
   593          if neg pv then Suc n else Suc(max (nat pv) n))"
   594 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   595             split add: split_if nat.split)
   596 apply (rule_tac x = "number_of v" in spec) 
   597 apply auto
   598 done
   599  
   600 lemma min_number_of_Suc [simp]:
   601      "min (Suc n) (number_of v) =  
   602         (let pv = number_of (Int.pred v) in  
   603          if neg pv then 0 else Suc(min n (nat pv)))"
   604 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   605             split add: split_if nat.split)
   606 apply (rule_tac x = "number_of v" in spec) 
   607 apply auto
   608 done
   609  
   610 lemma min_Suc_number_of [simp]:
   611      "min (number_of v) (Suc n) =  
   612         (let pv = number_of (Int.pred v) in  
   613          if neg pv then 0 else Suc(min (nat pv) n))"
   614 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   615             split add: split_if nat.split)
   616 apply (rule_tac x = "number_of v" in spec) 
   617 apply auto
   618 done
   619  
   620 subsection{*Literal arithmetic involving powers*}
   621 
   622 lemma power_nat_number_of:
   623      "(number_of v :: nat) ^ n =  
   624        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
   625 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
   626          split add: split_if cong: imp_cong)
   627 
   628 
   629 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
   630 declare power_nat_number_of_number_of [simp]
   631 
   632 
   633 
   634 text{*For arbitrary rings*}
   635 
   636 lemma power_number_of_even:
   637   fixes z :: "'a::number_ring"
   638   shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
   639 by (cases "w \<ge> 0") (auto simp add: Let_def Bit0_def nat_number_of_def number_of_is_id
   640   nat_add_distrib power_add simp del: nat_number_of)
   641 
   642 lemma power_number_of_odd:
   643   fixes z :: "'a::number_ring"
   644   shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
   645      then (let w = z ^ (number_of w) in z * w * w) else 1)"
   646 unfolding Let_def Bit1_def nat_number_of_def number_of_is_id
   647 apply (cases "0 <= w")
   648 apply (simp only: mult_assoc nat_add_distrib power_add, simp)
   649 apply (simp add: not_le mult_2 [symmetric] add_assoc)
   650 done
   651 
   652 lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
   653 lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
   654 
   655 lemmas power_number_of_even_number_of [simp] =
   656     power_number_of_even [of "number_of v", standard]
   657 
   658 lemmas power_number_of_odd_number_of [simp] =
   659     power_number_of_odd [of "number_of v", standard]
   660 
   661 lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
   662   by (simp add: nat_number_of_def)
   663 
   664 lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
   665   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
   666   done
   667 
   668 lemma nat_number_of_Bit0:
   669     "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
   670 by (cases "w \<ge> 0") (auto simp add: Let_def Bit0_def nat_number_of_def number_of_is_id
   671   nat_add_distrib simp del: nat_number_of)
   672 
   673 lemma nat_number_of_Bit1:
   674   "number_of (Int.Bit1 w) =
   675     (if neg (number_of w :: int) then 0
   676      else let n = number_of w in Suc (n + n))"
   677 unfolding Let_def Bit1_def nat_number_of_def number_of_is_id neg_def
   678 apply (cases "w < 0")
   679 apply (simp add: mult_2 [symmetric] add_assoc)
   680 apply (simp only: nat_add_distrib, simp)
   681 done
   682 
   683 lemmas nat_number =
   684   nat_number_of_Pls nat_number_of_Min
   685   nat_number_of_Bit0 nat_number_of_Bit1
   686 
   687 lemmas nat_number' =
   688   nat_number_of_Bit0 nat_number_of_Bit1
   689 
   690 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
   691   by (fact Let_def)
   692 
   693 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring})"
   694   by (simp only: number_of_Min power_minus1_even)
   695 
   696 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring})"
   697   by (simp only: number_of_Min power_minus1_odd)
   698 
   699 lemma nat_number_of_add_left:
   700      "number_of v + (number_of v' + (k::nat)) =  
   701          (if neg (number_of v :: int) then number_of v' + k  
   702           else if neg (number_of v' :: int) then number_of v + k  
   703           else number_of (v + v') + k)"
   704 by (auto simp add: neg_def)
   705 
   706 lemma nat_number_of_mult_left:
   707      "number_of v * (number_of v' * (k::nat)) =  
   708          (if v < Int.Pls then 0
   709           else number_of (v * v') * k)"
   710 by (auto simp add: not_less Pls_def nat_number_of_def number_of_is_id
   711   nat_mult_distrib simp del: nat_number_of)
   712 
   713 
   714 subsection{*Literal arithmetic and @{term of_nat}*}
   715 
   716 lemma of_nat_double:
   717      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
   718 by (simp only: mult_2 nat_add_distrib of_nat_add) 
   719 
   720 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
   721 by (simp only: nat_number_of_def)
   722 
   723 lemma of_nat_number_of_lemma:
   724      "of_nat (number_of v :: nat) =  
   725          (if 0 \<le> (number_of v :: int) 
   726           then (number_of v :: 'a :: number_ring)
   727           else 0)"
   728 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat)
   729 
   730 lemma of_nat_number_of_eq [simp]:
   731      "of_nat (number_of v :: nat) =  
   732          (if neg (number_of v :: int) then 0  
   733           else (number_of v :: 'a :: number_ring))"
   734 by (simp only: of_nat_number_of_lemma neg_def, simp) 
   735 
   736 
   737 subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
   738 
   739 text{*Where K above is a literal*}
   740 
   741 lemma Suc_diff_eq_diff_pred: "Numeral0 < n ==> Suc m - n = m - (n - Numeral1)"
   742 by (simp split: nat_diff_split)
   743 
   744 text {*Now just instantiating @{text n} to @{text "number_of v"} does
   745   the right simplification, but with some redundant inequality
   746   tests.*}
   747 lemma neg_number_of_pred_iff_0:
   748   "neg (number_of (Int.pred v)::int) = (number_of v = (0::nat))"
   749 apply (subgoal_tac "neg (number_of (Int.pred v)) = (number_of v < Suc 0) ")
   750 apply (simp only: less_Suc_eq_le le_0_eq)
   751 apply (subst less_number_of_Suc, simp)
   752 done
   753 
   754 text{*No longer required as a simprule because of the @{text inverse_fold}
   755    simproc*}
   756 lemma Suc_diff_number_of:
   757      "Int.Pls < v ==>
   758       Suc m - (number_of v) = m - (number_of (Int.pred v))"
   759 apply (subst Suc_diff_eq_diff_pred)
   760 apply simp
   761 apply (simp del: nat_numeral_1_eq_1)
   762 apply (auto simp only: diff_nat_number_of less_0_number_of [symmetric]
   763                         neg_number_of_pred_iff_0)
   764 done
   765 
   766 lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
   767 by (simp split: nat_diff_split)
   768 
   769 
   770 subsubsection{*For @{term nat_case} and @{term nat_rec}*}
   771 
   772 lemma nat_case_number_of [simp]:
   773      "nat_case a f (number_of v) =
   774         (let pv = number_of (Int.pred v) in
   775          if neg pv then a else f (nat pv))"
   776 by (simp split add: nat.split add: Let_def neg_number_of_pred_iff_0)
   777 
   778 lemma nat_case_add_eq_if [simp]:
   779      "nat_case a f ((number_of v) + n) =
   780        (let pv = number_of (Int.pred v) in
   781          if neg pv then nat_case a f n else f (nat pv + n))"
   782 apply (subst add_eq_if)
   783 apply (simp split add: nat.split
   784             del: nat_numeral_1_eq_1
   785             add: nat_numeral_1_eq_1 [symmetric]
   786                  numeral_1_eq_Suc_0 [symmetric]
   787                  neg_number_of_pred_iff_0)
   788 done
   789 
   790 lemma nat_rec_number_of [simp]:
   791      "nat_rec a f (number_of v) =
   792         (let pv = number_of (Int.pred v) in
   793          if neg pv then a else f (nat pv) (nat_rec a f (nat pv)))"
   794 apply (case_tac " (number_of v) ::nat")
   795 apply (simp_all (no_asm_simp) add: Let_def neg_number_of_pred_iff_0)
   796 apply (simp split add: split_if_asm)
   797 done
   798 
   799 lemma nat_rec_add_eq_if [simp]:
   800      "nat_rec a f (number_of v + n) =
   801         (let pv = number_of (Int.pred v) in
   802          if neg pv then nat_rec a f n
   803                    else f (nat pv + n) (nat_rec a f (nat pv + n)))"
   804 apply (subst add_eq_if)
   805 apply (simp split add: nat.split
   806             del: nat_numeral_1_eq_1
   807             add: nat_numeral_1_eq_1 [symmetric]
   808                  numeral_1_eq_Suc_0 [symmetric]
   809                  neg_number_of_pred_iff_0)
   810 done
   811 
   812 
   813 subsubsection{*Various Other Lemmas*}
   814 
   815 lemma card_UNIV_bool[simp]: "card (UNIV :: bool set) = 2"
   816 by(simp add: UNIV_bool)
   817 
   818 text {*Evens and Odds, for Mutilated Chess Board*}
   819 
   820 text{*Lemmas for specialist use, NOT as default simprules*}
   821 lemma nat_mult_2: "2 * z = (z+z::nat)"
   822 unfolding nat_1_add_1 [symmetric] left_distrib by simp
   823 
   824 lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
   825 by (subst mult_commute, rule nat_mult_2)
   826 
   827 text{*Case analysis on @{term "n<2"}*}
   828 lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
   829 by (auto simp add: nat_1_add_1 [symmetric])
   830 
   831 text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
   832 
   833 lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
   834 by simp
   835 
   836 lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
   837 by simp
   838 
   839 text{*Can be used to eliminate long strings of Sucs, but not by default*}
   840 lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
   841 by simp
   842 
   843 end