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