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