src/HOL/Nat_Numeral.thy
author haftmann
Fri May 08 09:48:07 2009 +0200 (2009-05-08)
changeset 31068 f591144b0f17
parent 31034 736f521ad036
child 31080 21ffc770ebc0
permissions -rw-r--r--
modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
     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 IntDiv
    10 uses ("Tools/nat_numeral_simprocs.ML")
    11 begin
    12 
    13 subsection {* Numerals for natural numbers *}
    14 
    15 text {*
    16   Arithmetic for naturals is reduced to that for the non-negative integers.
    17 *}
    18 
    19 instantiation nat :: number
    20 begin
    21 
    22 definition
    23   nat_number_of_def [code inline, code del]: "number_of v = nat (number_of v)"
    24 
    25 instance ..
    26 
    27 end
    28 
    29 lemma [code post]:
    30   "nat (number_of v) = number_of v"
    31   unfolding nat_number_of_def ..
    32 
    33 
    34 subsection {* Special case: squares and cubes *}
    35 
    36 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
    37   by (simp add: nat_number_of_def)
    38 
    39 lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
    40   by (simp add: nat_number_of_def)
    41 
    42 context power
    43 begin
    44 
    45 abbreviation (xsymbols)
    46   power2 :: "'a \<Rightarrow> 'a"  ("(_\<twosuperior>)" [1000] 999) where
    47   "x\<twosuperior> \<equiv> x ^ 2"
    48 
    49 notation (latex output)
    50   power2  ("(_\<twosuperior>)" [1000] 999)
    51 
    52 notation (HTML output)
    53   power2  ("(_\<twosuperior>)" [1000] 999)
    54 
    55 end
    56 
    57 context monoid_mult
    58 begin
    59 
    60 lemma power2_eq_square: "a\<twosuperior> = a * a"
    61   by (simp add: numeral_2_eq_2)
    62 
    63 lemma power3_eq_cube: "a ^ 3 = a * a * a"
    64   by (simp add: numeral_3_eq_3 mult_assoc)
    65 
    66 lemma power_even_eq:
    67   "a ^ (2*n) = (a ^ n) ^ 2"
    68   by (subst OrderedGroup.mult_commute) (simp add: power_mult)
    69 
    70 lemma power_odd_eq:
    71   "a ^ Suc (2*n) = a * (a ^ n) ^ 2"
    72   by (simp add: power_even_eq)
    73 
    74 end
    75 
    76 context semiring_1
    77 begin
    78 
    79 lemma zero_power2 [simp]: "0\<twosuperior> = 0"
    80   by (simp add: power2_eq_square)
    81 
    82 lemma one_power2 [simp]: "1\<twosuperior> = 1"
    83   by (simp add: power2_eq_square)
    84 
    85 end
    86 
    87 context comm_ring_1
    88 begin
    89 
    90 lemma power2_minus [simp]:
    91   "(- a)\<twosuperior> = a\<twosuperior>"
    92   by (simp add: power2_eq_square)
    93 
    94 text{*
    95   We cannot prove general results about the numeral @{term "-1"},
    96   so we have to use @{term "- 1"} instead.
    97 *}
    98 
    99 lemma power_minus1_even [simp]:
   100   "(- 1) ^ (2*n) = 1"
   101 proof (induct n)
   102   case 0 show ?case by simp
   103 next
   104   case (Suc n) then show ?case by (simp add: power_add)
   105 qed
   106 
   107 lemma power_minus1_odd:
   108   "(- 1) ^ Suc (2*n) = - 1"
   109   by simp
   110 
   111 lemma power_minus_even [simp]:
   112   "(-a) ^ (2*n) = a ^ (2*n)"
   113   by (simp add: power_minus [of a]) 
   114 
   115 end
   116 
   117 context ordered_ring_strict
   118 begin
   119 
   120 lemma sum_squares_ge_zero:
   121   "0 \<le> x * x + y * y"
   122   by (intro add_nonneg_nonneg zero_le_square)
   123 
   124 lemma not_sum_squares_lt_zero:
   125   "\<not> x * x + y * y < 0"
   126   by (simp add: not_less sum_squares_ge_zero)
   127 
   128 lemma sum_squares_eq_zero_iff:
   129   "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   130   by (simp add: add_nonneg_eq_0_iff)
   131 
   132 lemma sum_squares_le_zero_iff:
   133   "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   134   by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
   135 
   136 lemma sum_squares_gt_zero_iff:
   137   "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   138 proof -
   139   have "x * x + y * y \<noteq> 0 \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   140     by (simp add: sum_squares_eq_zero_iff)
   141   then have "0 \<noteq> x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   142     by auto
   143   then show ?thesis
   144     by (simp add: less_le sum_squares_ge_zero)
   145 qed
   146 
   147 end
   148 
   149 context ordered_semidom
   150 begin
   151 
   152 lemma power2_le_imp_le:
   153   "x\<twosuperior> \<le> y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
   154   unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
   155 
   156 lemma power2_less_imp_less:
   157   "x\<twosuperior> < y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
   158   by (rule power_less_imp_less_base)
   159 
   160 lemma power2_eq_imp_eq:
   161   "x\<twosuperior> = y\<twosuperior> \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
   162   unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
   163 
   164 end
   165 
   166 context ordered_idom
   167 begin
   168 
   169 lemma zero_eq_power2 [simp]:
   170   "a\<twosuperior> = 0 \<longleftrightarrow> a = 0"
   171   by (force simp add: power2_eq_square)
   172 
   173 lemma zero_le_power2 [simp]:
   174   "0 \<le> a\<twosuperior>"
   175   by (simp add: power2_eq_square)
   176 
   177 lemma zero_less_power2 [simp]:
   178   "0 < a\<twosuperior> \<longleftrightarrow> a \<noteq> 0"
   179   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
   180 
   181 lemma power2_less_0 [simp]:
   182   "\<not> a\<twosuperior> < 0"
   183   by (force simp add: power2_eq_square mult_less_0_iff) 
   184 
   185 lemma abs_power2 [simp]:
   186   "abs (a\<twosuperior>) = a\<twosuperior>"
   187   by (simp add: power2_eq_square abs_mult abs_mult_self)
   188 
   189 lemma power2_abs [simp]:
   190   "(abs a)\<twosuperior> = a\<twosuperior>"
   191   by (simp add: power2_eq_square abs_mult_self)
   192 
   193 lemma odd_power_less_zero:
   194   "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
   195 proof (induct n)
   196   case 0
   197   then show ?case by simp
   198 next
   199   case (Suc n)
   200   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
   201     by (simp add: mult_ac power_add power2_eq_square)
   202   thus ?case
   203     by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
   204 qed
   205 
   206 lemma odd_0_le_power_imp_0_le:
   207   "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
   208   using odd_power_less_zero [of a n]
   209     by (force simp add: linorder_not_less [symmetric]) 
   210 
   211 lemma zero_le_even_power'[simp]:
   212   "0 \<le> a ^ (2*n)"
   213 proof (induct n)
   214   case 0
   215     show ?case by (simp add: zero_le_one)
   216 next
   217   case (Suc n)
   218     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
   219       by (simp add: mult_ac power_add power2_eq_square)
   220     thus ?case
   221       by (simp add: Suc zero_le_mult_iff)
   222 qed
   223 
   224 lemma sum_power2_ge_zero:
   225   "0 \<le> x\<twosuperior> + y\<twosuperior>"
   226   unfolding power2_eq_square by (rule sum_squares_ge_zero)
   227 
   228 lemma not_sum_power2_lt_zero:
   229   "\<not> x\<twosuperior> + y\<twosuperior> < 0"
   230   unfolding power2_eq_square by (rule not_sum_squares_lt_zero)
   231 
   232 lemma sum_power2_eq_zero_iff:
   233   "x\<twosuperior> + y\<twosuperior> = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   234   unfolding power2_eq_square by (rule sum_squares_eq_zero_iff)
   235 
   236 lemma sum_power2_le_zero_iff:
   237   "x\<twosuperior> + y\<twosuperior> \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   238   unfolding power2_eq_square by (rule sum_squares_le_zero_iff)
   239 
   240 lemma sum_power2_gt_zero_iff:
   241   "0 < x\<twosuperior> + y\<twosuperior> \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   242   unfolding power2_eq_square by (rule sum_squares_gt_zero_iff)
   243 
   244 end
   245 
   246 lemma power2_sum:
   247   fixes x y :: "'a::number_ring"
   248   shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
   249   by (simp add: ring_distribs power2_eq_square)
   250 
   251 lemma power2_diff:
   252   fixes x y :: "'a::number_ring"
   253   shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
   254   by (simp add: ring_distribs power2_eq_square)
   255 
   256 
   257 subsection {* Predicate for negative binary numbers *}
   258 
   259 definition neg  :: "int \<Rightarrow> bool" where
   260   "neg Z \<longleftrightarrow> Z < 0"
   261 
   262 lemma not_neg_int [simp]: "~ neg (of_nat n)"
   263 by (simp add: neg_def)
   264 
   265 lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
   266 by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
   267 
   268 lemmas neg_eq_less_0 = neg_def
   269 
   270 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
   271 by (simp add: neg_def linorder_not_less)
   272 
   273 text{*To simplify inequalities when Numeral1 can get simplified to 1*}
   274 
   275 lemma not_neg_0: "~ neg 0"
   276 by (simp add: One_int_def neg_def)
   277 
   278 lemma not_neg_1: "~ neg 1"
   279 by (simp add: neg_def linorder_not_less zero_le_one)
   280 
   281 lemma neg_nat: "neg z ==> nat z = 0"
   282 by (simp add: neg_def order_less_imp_le) 
   283 
   284 lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
   285 by (simp add: linorder_not_less neg_def)
   286 
   287 text {*
   288   If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
   289   @{term Numeral0} IS @{term "number_of Pls"}
   290 *}
   291 
   292 lemma not_neg_number_of_Pls: "~ neg (number_of Int.Pls)"
   293   by (simp add: neg_def)
   294 
   295 lemma neg_number_of_Min: "neg (number_of Int.Min)"
   296   by (simp add: neg_def)
   297 
   298 lemma neg_number_of_Bit0:
   299   "neg (number_of (Int.Bit0 w)) = neg (number_of w)"
   300   by (simp add: neg_def)
   301 
   302 lemma neg_number_of_Bit1:
   303   "neg (number_of (Int.Bit1 w)) = neg (number_of w)"
   304   by (simp add: neg_def)
   305 
   306 lemmas neg_simps [simp] =
   307   not_neg_0 not_neg_1
   308   not_neg_number_of_Pls neg_number_of_Min
   309   neg_number_of_Bit0 neg_number_of_Bit1
   310 
   311 
   312 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
   313 
   314 declare nat_0 [simp] nat_1 [simp]
   315 
   316 lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
   317 by (simp add: nat_number_of_def)
   318 
   319 lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
   320 by (simp add: nat_number_of_def)
   321 
   322 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
   323 by (simp add: nat_1 nat_number_of_def)
   324 
   325 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
   326 by (simp add: nat_numeral_1_eq_1)
   327 
   328 
   329 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
   330 
   331 lemma int_nat_number_of [simp]:
   332      "int (number_of v) =  
   333          (if neg (number_of v :: int) then 0  
   334           else (number_of v :: int))"
   335   unfolding nat_number_of_def number_of_is_id neg_def
   336   by simp
   337 
   338 
   339 subsubsection{*Successor *}
   340 
   341 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
   342 apply (rule sym)
   343 apply (simp add: nat_eq_iff int_Suc)
   344 done
   345 
   346 lemma Suc_nat_number_of_add:
   347      "Suc (number_of v + n) =  
   348         (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)"
   349   unfolding nat_number_of_def number_of_is_id neg_def numeral_simps
   350   by (simp add: Suc_nat_eq_nat_zadd1 add_ac)
   351 
   352 lemma Suc_nat_number_of [simp]:
   353      "Suc (number_of v) =  
   354         (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
   355 apply (cut_tac n = 0 in Suc_nat_number_of_add)
   356 apply (simp cong del: if_weak_cong)
   357 done
   358 
   359 
   360 subsubsection{*Addition *}
   361 
   362 lemma add_nat_number_of [simp]:
   363      "(number_of v :: nat) + number_of v' =  
   364          (if v < Int.Pls then number_of v'  
   365           else if v' < Int.Pls then number_of v  
   366           else number_of (v + v'))"
   367   unfolding nat_number_of_def number_of_is_id numeral_simps
   368   by (simp add: nat_add_distrib)
   369 
   370 lemma nat_number_of_add_1 [simp]:
   371   "number_of v + (1::nat) =
   372     (if v < Int.Pls then 1 else number_of (Int.succ v))"
   373   unfolding nat_number_of_def number_of_is_id numeral_simps
   374   by (simp add: nat_add_distrib)
   375 
   376 lemma nat_1_add_number_of [simp]:
   377   "(1::nat) + number_of v =
   378     (if v < Int.Pls then 1 else number_of (Int.succ v))"
   379   unfolding nat_number_of_def number_of_is_id numeral_simps
   380   by (simp add: nat_add_distrib)
   381 
   382 lemma nat_1_add_1 [simp]: "1 + 1 = (2::nat)"
   383   by (rule int_int_eq [THEN iffD1]) simp
   384 
   385 
   386 subsubsection{*Subtraction *}
   387 
   388 lemma diff_nat_eq_if:
   389      "nat z - nat z' =  
   390         (if neg z' then nat z   
   391          else let d = z-z' in     
   392               if neg d then 0 else nat d)"
   393 by (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
   394 
   395 
   396 lemma diff_nat_number_of [simp]: 
   397      "(number_of v :: nat) - number_of v' =  
   398         (if v' < Int.Pls then number_of v  
   399          else let d = number_of (v + uminus v') in     
   400               if neg d then 0 else nat d)"
   401   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def
   402   by auto
   403 
   404 lemma nat_number_of_diff_1 [simp]:
   405   "number_of v - (1::nat) =
   406     (if v \<le> Int.Pls then 0 else number_of (Int.pred v))"
   407   unfolding nat_number_of_def number_of_is_id numeral_simps
   408   by auto
   409 
   410 
   411 subsubsection{*Multiplication *}
   412 
   413 lemma mult_nat_number_of [simp]:
   414      "(number_of v :: nat) * number_of v' =  
   415        (if v < Int.Pls then 0 else number_of (v * v'))"
   416   unfolding nat_number_of_def number_of_is_id numeral_simps
   417   by (simp add: nat_mult_distrib)
   418 
   419 
   420 subsubsection{*Quotient *}
   421 
   422 lemma div_nat_number_of [simp]:
   423      "(number_of v :: nat)  div  number_of v' =  
   424           (if neg (number_of v :: int) then 0  
   425            else nat (number_of v div number_of v'))"
   426   unfolding nat_number_of_def number_of_is_id neg_def
   427   by (simp add: nat_div_distrib)
   428 
   429 lemma one_div_nat_number_of [simp]:
   430      "Suc 0 div number_of v' = nat (1 div number_of v')" 
   431 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
   432 
   433 
   434 subsubsection{*Remainder *}
   435 
   436 lemma mod_nat_number_of [simp]:
   437      "(number_of v :: nat)  mod  number_of v' =  
   438         (if neg (number_of v :: int) then 0  
   439          else if neg (number_of v' :: int) then number_of v  
   440          else nat (number_of v mod number_of v'))"
   441   unfolding nat_number_of_def number_of_is_id neg_def
   442   by (simp add: nat_mod_distrib)
   443 
   444 lemma one_mod_nat_number_of [simp]:
   445      "Suc 0 mod number_of v' =  
   446         (if neg (number_of v' :: int) then Suc 0
   447          else nat (1 mod number_of v'))"
   448 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
   449 
   450 
   451 subsubsection{* Divisibility *}
   452 
   453 lemmas dvd_eq_mod_eq_0_number_of =
   454   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
   455 
   456 declare dvd_eq_mod_eq_0_number_of [simp]
   457 
   458 
   459 subsection{*Comparisons*}
   460 
   461 subsubsection{*Equals (=) *}
   462 
   463 lemma eq_nat_nat_iff:
   464      "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
   465 by (auto elim!: nonneg_eq_int)
   466 
   467 lemma eq_nat_number_of [simp]:
   468      "((number_of v :: nat) = number_of v') =  
   469       (if neg (number_of v :: int) then (number_of v' :: int) \<le> 0
   470        else if neg (number_of v' :: int) then (number_of v :: int) = 0
   471        else v = v')"
   472   unfolding nat_number_of_def number_of_is_id neg_def
   473   by auto
   474 
   475 
   476 subsubsection{*Less-than (<) *}
   477 
   478 lemma less_nat_number_of [simp]:
   479   "(number_of v :: nat) < number_of v' \<longleftrightarrow>
   480     (if v < v' then Int.Pls < v' else False)"
   481   unfolding nat_number_of_def number_of_is_id numeral_simps
   482   by auto
   483 
   484 
   485 subsubsection{*Less-than-or-equal *}
   486 
   487 lemma le_nat_number_of [simp]:
   488   "(number_of v :: nat) \<le> number_of v' \<longleftrightarrow>
   489     (if v \<le> v' then True else v \<le> Int.Pls)"
   490   unfolding nat_number_of_def number_of_is_id numeral_simps
   491   by auto
   492 
   493 (*Maps #n to n for n = 0, 1, 2*)
   494 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
   495 
   496 
   497 subsection{*Powers with Numeric Exponents*}
   498 
   499 text{*Squares of literal numerals will be evaluated.*}
   500 lemmas power2_eq_square_number_of [simp] =
   501     power2_eq_square [of "number_of w", standard]
   502 
   503 
   504 text{*Simprules for comparisons where common factors can be cancelled.*}
   505 lemmas zero_compare_simps =
   506     add_strict_increasing add_strict_increasing2 add_increasing
   507     zero_le_mult_iff zero_le_divide_iff 
   508     zero_less_mult_iff zero_less_divide_iff 
   509     mult_le_0_iff divide_le_0_iff 
   510     mult_less_0_iff divide_less_0_iff 
   511     zero_le_power2 power2_less_0
   512 
   513 subsubsection{*Nat *}
   514 
   515 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
   516 by (simp add: numerals)
   517 
   518 (*Expresses a natural number constant as the Suc of another one.
   519   NOT suitable for rewriting because n recurs in the condition.*)
   520 lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
   521 
   522 subsubsection{*Arith *}
   523 
   524 lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
   525 by (simp add: numerals)
   526 
   527 lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
   528 by (simp add: numerals)
   529 
   530 (* These two can be useful when m = number_of... *)
   531 
   532 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
   533   unfolding One_nat_def by (cases m) simp_all
   534 
   535 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
   536   unfolding One_nat_def by (cases m) simp_all
   537 
   538 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
   539   unfolding One_nat_def by (cases m) simp_all
   540 
   541 
   542 subsection{*Comparisons involving (0::nat) *}
   543 
   544 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
   545 
   546 lemma eq_number_of_0 [simp]:
   547   "number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls"
   548   unfolding nat_number_of_def number_of_is_id numeral_simps
   549   by auto
   550 
   551 lemma eq_0_number_of [simp]:
   552   "(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls"
   553 by (rule trans [OF eq_sym_conv eq_number_of_0])
   554 
   555 lemma less_0_number_of [simp]:
   556    "(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v"
   557   unfolding nat_number_of_def number_of_is_id numeral_simps
   558   by simp
   559 
   560 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
   561 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
   562 
   563 
   564 
   565 subsection{*Comparisons involving  @{term Suc} *}
   566 
   567 lemma eq_number_of_Suc [simp]:
   568      "(number_of v = Suc n) =  
   569         (let pv = number_of (Int.pred v) in  
   570          if neg pv then False else nat pv = n)"
   571 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   572                   number_of_pred nat_number_of_def 
   573             split add: split_if)
   574 apply (rule_tac x = "number_of v" in spec)
   575 apply (auto simp add: nat_eq_iff)
   576 done
   577 
   578 lemma Suc_eq_number_of [simp]:
   579      "(Suc n = number_of v) =  
   580         (let pv = number_of (Int.pred v) in  
   581          if neg pv then False else nat pv = n)"
   582 by (rule trans [OF eq_sym_conv eq_number_of_Suc])
   583 
   584 lemma less_number_of_Suc [simp]:
   585      "(number_of v < Suc n) =  
   586         (let pv = number_of (Int.pred v) in  
   587          if neg pv then True else nat pv < n)"
   588 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
   589                   number_of_pred nat_number_of_def  
   590             split add: split_if)
   591 apply (rule_tac x = "number_of v" in spec)
   592 apply (auto simp add: nat_less_iff)
   593 done
   594 
   595 lemma less_Suc_number_of [simp]:
   596      "(Suc n < number_of v) =  
   597         (let pv = number_of (Int.pred v) in  
   598          if neg pv then False else n < nat pv)"
   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: zless_nat_eq_int_zless)
   604 done
   605 
   606 lemma le_number_of_Suc [simp]:
   607      "(number_of v <= Suc n) =  
   608         (let pv = number_of (Int.pred v) in  
   609          if neg pv then True else nat pv <= n)"
   610 by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
   611 
   612 lemma le_Suc_number_of [simp]:
   613      "(Suc n <= number_of v) =  
   614         (let pv = number_of (Int.pred v) in  
   615          if neg pv then False else n <= nat pv)"
   616 by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
   617 
   618 
   619 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
   620 by auto
   621 
   622 
   623 
   624 subsection{*Max and Min Combined with @{term Suc} *}
   625 
   626 lemma max_number_of_Suc [simp]:
   627      "max (Suc n) (number_of v) =  
   628         (let pv = number_of (Int.pred v) in  
   629          if neg pv then Suc n else Suc(max n (nat pv)))"
   630 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   631             split add: split_if nat.split)
   632 apply (rule_tac x = "number_of v" in spec) 
   633 apply auto
   634 done
   635  
   636 lemma max_Suc_number_of [simp]:
   637      "max (number_of v) (Suc n) =  
   638         (let pv = number_of (Int.pred v) in  
   639          if neg pv then Suc n else Suc(max (nat pv) n))"
   640 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   641             split add: split_if nat.split)
   642 apply (rule_tac x = "number_of v" in spec) 
   643 apply auto
   644 done
   645  
   646 lemma min_number_of_Suc [simp]:
   647      "min (Suc n) (number_of v) =  
   648         (let pv = number_of (Int.pred v) in  
   649          if neg pv then 0 else Suc(min n (nat pv)))"
   650 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   651             split add: split_if nat.split)
   652 apply (rule_tac x = "number_of v" in spec) 
   653 apply auto
   654 done
   655  
   656 lemma min_Suc_number_of [simp]:
   657      "min (number_of v) (Suc n) =  
   658         (let pv = number_of (Int.pred v) in  
   659          if neg pv then 0 else Suc(min (nat pv) n))"
   660 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
   661             split add: split_if nat.split)
   662 apply (rule_tac x = "number_of v" in spec) 
   663 apply auto
   664 done
   665  
   666 subsection{*Literal arithmetic involving powers*}
   667 
   668 lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
   669 apply (induct "n")
   670 apply (simp_all (no_asm_simp) add: nat_mult_distrib)
   671 done
   672 
   673 lemma power_nat_number_of:
   674      "(number_of v :: nat) ^ n =  
   675        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
   676 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
   677          split add: split_if cong: imp_cong)
   678 
   679 
   680 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
   681 declare power_nat_number_of_number_of [simp]
   682 
   683 
   684 
   685 text{*For arbitrary rings*}
   686 
   687 lemma power_number_of_even:
   688   fixes z :: "'a::number_ring"
   689   shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
   690 unfolding Let_def nat_number_of_def number_of_Bit0
   691 apply (rule_tac x = "number_of w" in spec, clarify)
   692 apply (case_tac " (0::int) <= x")
   693 apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
   694 done
   695 
   696 lemma power_number_of_odd:
   697   fixes z :: "'a::number_ring"
   698   shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
   699      then (let w = z ^ (number_of w) in z * w * w) else 1)"
   700 unfolding Let_def nat_number_of_def number_of_Bit1
   701 apply (rule_tac x = "number_of w" in spec, auto)
   702 apply (simp only: nat_add_distrib nat_mult_distrib)
   703 apply simp
   704 apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
   705 done
   706 
   707 lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
   708 lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
   709 
   710 lemmas power_number_of_even_number_of [simp] =
   711     power_number_of_even [of "number_of v", standard]
   712 
   713 lemmas power_number_of_odd_number_of [simp] =
   714     power_number_of_odd [of "number_of v", standard]
   715 
   716 
   717 (* Enable arith to deal with div/mod k where k is a numeral: *)
   718 declare split_div[of _ _ "number_of k", standard, arith_split]
   719 declare split_mod[of _ _ "number_of k", standard, arith_split]
   720 
   721 lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
   722   by (simp add: number_of_Pls nat_number_of_def)
   723 
   724 lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
   725   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
   726   done
   727 
   728 lemma nat_number_of_Bit0:
   729     "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
   730   unfolding nat_number_of_def number_of_is_id numeral_simps Let_def
   731   by auto
   732 
   733 lemma nat_number_of_Bit1:
   734   "number_of (Int.Bit1 w) =
   735     (if neg (number_of w :: int) then 0
   736      else let n = number_of w in Suc (n + n))"
   737   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def Let_def
   738   by auto
   739 
   740 lemmas nat_number =
   741   nat_number_of_Pls nat_number_of_Min
   742   nat_number_of_Bit0 nat_number_of_Bit1
   743 
   744 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
   745   by (simp add: Let_def)
   746 
   747 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring})"
   748   by (simp only: number_of_Min power_minus1_even)
   749 
   750 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring})"
   751   by (simp only: number_of_Min power_minus1_odd)
   752 
   753 
   754 subsection{*Literal arithmetic and @{term of_nat}*}
   755 
   756 lemma of_nat_double:
   757      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
   758 by (simp only: mult_2 nat_add_distrib of_nat_add) 
   759 
   760 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
   761 by (simp only: nat_number_of_def)
   762 
   763 lemma of_nat_number_of_lemma:
   764      "of_nat (number_of v :: nat) =  
   765          (if 0 \<le> (number_of v :: int) 
   766           then (number_of v :: 'a :: number_ring)
   767           else 0)"
   768 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
   769 
   770 lemma of_nat_number_of_eq [simp]:
   771      "of_nat (number_of v :: nat) =  
   772          (if neg (number_of v :: int) then 0  
   773           else (number_of v :: 'a :: number_ring))"
   774 by (simp only: of_nat_number_of_lemma neg_def, simp) 
   775 
   776 
   777 subsection {*Lemmas for the Combination and Cancellation Simprocs*}
   778 
   779 lemma nat_number_of_add_left:
   780      "number_of v + (number_of v' + (k::nat)) =  
   781          (if neg (number_of v :: int) then number_of v' + k  
   782           else if neg (number_of v' :: int) then number_of v + k  
   783           else number_of (v + v') + k)"
   784   unfolding nat_number_of_def number_of_is_id neg_def
   785   by auto
   786 
   787 lemma nat_number_of_mult_left:
   788      "number_of v * (number_of v' * (k::nat)) =  
   789          (if v < Int.Pls then 0
   790           else number_of (v * v') * k)"
   791 by simp
   792 
   793 
   794 subsubsection{*For @{text combine_numerals}*}
   795 
   796 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
   797 by (simp add: add_mult_distrib)
   798 
   799 
   800 subsubsection{*For @{text cancel_numerals}*}
   801 
   802 lemma nat_diff_add_eq1:
   803      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
   804 by (simp split add: nat_diff_split add: add_mult_distrib)
   805 
   806 lemma nat_diff_add_eq2:
   807      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
   808 by (simp split add: nat_diff_split add: add_mult_distrib)
   809 
   810 lemma nat_eq_add_iff1:
   811      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
   812 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   813 
   814 lemma nat_eq_add_iff2:
   815      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
   816 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   817 
   818 lemma nat_less_add_iff1:
   819      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
   820 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   821 
   822 lemma nat_less_add_iff2:
   823      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
   824 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   825 
   826 lemma nat_le_add_iff1:
   827      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
   828 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   829 
   830 lemma nat_le_add_iff2:
   831      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
   832 by (auto split add: nat_diff_split simp add: add_mult_distrib)
   833 
   834 
   835 subsubsection{*For @{text cancel_numeral_factors} *}
   836 
   837 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
   838 by auto
   839 
   840 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
   841 by auto
   842 
   843 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
   844 by auto
   845 
   846 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
   847 by auto
   848 
   849 lemma nat_mult_dvd_cancel_disj[simp]:
   850   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
   851 by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
   852 
   853 lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
   854 by(auto)
   855 
   856 
   857 subsubsection{*For @{text cancel_factor} *}
   858 
   859 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
   860 by auto
   861 
   862 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
   863 by auto
   864 
   865 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
   866 by auto
   867 
   868 lemma nat_mult_div_cancel_disj[simp]:
   869      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
   870 by (simp add: nat_mult_div_cancel1)
   871 
   872 
   873 subsection {* Simprocs for the Naturals *}
   874 
   875 use "Tools/nat_numeral_simprocs.ML"
   876 
   877 declaration {*
   878 let
   879 
   880 val less_eq_rules = @{thms ring_distribs} @
   881   [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1}, @{thm nat_0}, @{thm nat_1},
   882    @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
   883    @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
   884    @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
   885    @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
   886    @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
   887    @{thm mult_Suc}, @{thm mult_Suc_right},
   888    @{thm add_Suc}, @{thm add_Suc_right},
   889    @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
   890    @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of}, @{thm if_True}, @{thm if_False}];
   891 
   892 val simprocs = Nat_Numeral_Simprocs.combine_numerals :: Nat_Numeral_Simprocs.cancel_numerals;
   893 
   894 in
   895 
   896 K (Lin_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
   897   {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
   898     inj_thms = inj_thms, lessD = lessD, neqE = neqE,
   899     simpset = simpset addsimps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
   900       addsimps less_eq_rules
   901       addsimprocs simprocs}))
   902 
   903 end
   904 *}
   905 
   906 
   907 subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
   908 
   909 text{*Where K above is a literal*}
   910 
   911 lemma Suc_diff_eq_diff_pred: "Numeral0 < n ==> Suc m - n = m - (n - Numeral1)"
   912 by (simp add: numeral_0_eq_0 numeral_1_eq_1 split add: nat_diff_split)
   913 
   914 text {*Now just instantiating @{text n} to @{text "number_of v"} does
   915   the right simplification, but with some redundant inequality
   916   tests.*}
   917 lemma neg_number_of_pred_iff_0:
   918   "neg (number_of (Int.pred v)::int) = (number_of v = (0::nat))"
   919 apply (subgoal_tac "neg (number_of (Int.pred v)) = (number_of v < Suc 0) ")
   920 apply (simp only: less_Suc_eq_le le_0_eq)
   921 apply (subst less_number_of_Suc, simp)
   922 done
   923 
   924 text{*No longer required as a simprule because of the @{text inverse_fold}
   925    simproc*}
   926 lemma Suc_diff_number_of:
   927      "Int.Pls < v ==>
   928       Suc m - (number_of v) = m - (number_of (Int.pred v))"
   929 apply (subst Suc_diff_eq_diff_pred)
   930 apply simp
   931 apply (simp del: nat_numeral_1_eq_1)
   932 apply (auto simp only: diff_nat_number_of less_0_number_of [symmetric]
   933                         neg_number_of_pred_iff_0)
   934 done
   935 
   936 lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
   937 by (simp add: numerals split add: nat_diff_split)
   938 
   939 
   940 subsubsection{*For @{term nat_case} and @{term nat_rec}*}
   941 
   942 lemma nat_case_number_of [simp]:
   943      "nat_case a f (number_of v) =
   944         (let pv = number_of (Int.pred v) in
   945          if neg pv then a else f (nat pv))"
   946 by (simp split add: nat.split add: Let_def neg_number_of_pred_iff_0)
   947 
   948 lemma nat_case_add_eq_if [simp]:
   949      "nat_case a f ((number_of v) + n) =
   950        (let pv = number_of (Int.pred v) in
   951          if neg pv then nat_case a f n else f (nat pv + n))"
   952 apply (subst add_eq_if)
   953 apply (simp split add: nat.split
   954             del: nat_numeral_1_eq_1
   955             add: nat_numeral_1_eq_1 [symmetric]
   956                  numeral_1_eq_Suc_0 [symmetric]
   957                  neg_number_of_pred_iff_0)
   958 done
   959 
   960 lemma nat_rec_number_of [simp]:
   961      "nat_rec a f (number_of v) =
   962         (let pv = number_of (Int.pred v) in
   963          if neg pv then a else f (nat pv) (nat_rec a f (nat pv)))"
   964 apply (case_tac " (number_of v) ::nat")
   965 apply (simp_all (no_asm_simp) add: Let_def neg_number_of_pred_iff_0)
   966 apply (simp split add: split_if_asm)
   967 done
   968 
   969 lemma nat_rec_add_eq_if [simp]:
   970      "nat_rec a f (number_of v + n) =
   971         (let pv = number_of (Int.pred v) in
   972          if neg pv then nat_rec a f n
   973                    else f (nat pv + n) (nat_rec a f (nat pv + n)))"
   974 apply (subst add_eq_if)
   975 apply (simp split add: nat.split
   976             del: nat_numeral_1_eq_1
   977             add: nat_numeral_1_eq_1 [symmetric]
   978                  numeral_1_eq_Suc_0 [symmetric]
   979                  neg_number_of_pred_iff_0)
   980 done
   981 
   982 
   983 subsubsection{*Various Other Lemmas*}
   984 
   985 text {*Evens and Odds, for Mutilated Chess Board*}
   986 
   987 text{*Lemmas for specialist use, NOT as default simprules*}
   988 lemma nat_mult_2: "2 * z = (z+z::nat)"
   989 proof -
   990   have "2*z = (1 + 1)*z" by simp
   991   also have "... = z+z" by (simp add: left_distrib)
   992   finally show ?thesis .
   993 qed
   994 
   995 lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
   996 by (subst mult_commute, rule nat_mult_2)
   997 
   998 text{*Case analysis on @{term "n<2"}*}
   999 lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
  1000 by arith
  1001 
  1002 lemma div2_Suc_Suc [simp]: "Suc(Suc m) div 2 = Suc (m div 2)"
  1003 by arith
  1004 
  1005 lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
  1006 by (simp add: nat_mult_2 [symmetric])
  1007 
  1008 lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
  1009 apply (subgoal_tac "m mod 2 < 2")
  1010 apply (erule less_2_cases [THEN disjE])
  1011 apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
  1012 done
  1013 
  1014 lemma mod2_gr_0 [simp]: "!!m::nat. (0 < m mod 2) = (m mod 2 = 1)"
  1015 apply (subgoal_tac "m mod 2 < 2")
  1016 apply (force simp del: mod_less_divisor, simp)
  1017 done
  1018 
  1019 text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
  1020 
  1021 lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
  1022 by simp
  1023 
  1024 lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
  1025 by simp
  1026 
  1027 text{*Can be used to eliminate long strings of Sucs, but not by default*}
  1028 lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
  1029 by simp
  1030 
  1031 
  1032 text{*These lemmas collapse some needless occurrences of Suc:
  1033     at least three Sucs, since two and fewer are rewritten back to Suc again!
  1034     We already have some rules to simplify operands smaller than 3.*}
  1035 
  1036 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
  1037 by (simp add: Suc3_eq_add_3)
  1038 
  1039 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
  1040 by (simp add: Suc3_eq_add_3)
  1041 
  1042 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
  1043 by (simp add: Suc3_eq_add_3)
  1044 
  1045 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
  1046 by (simp add: Suc3_eq_add_3)
  1047 
  1048 lemmas Suc_div_eq_add3_div_number_of =
  1049     Suc_div_eq_add3_div [of _ "number_of v", standard]
  1050 declare Suc_div_eq_add3_div_number_of [simp]
  1051 
  1052 lemmas Suc_mod_eq_add3_mod_number_of =
  1053     Suc_mod_eq_add3_mod [of _ "number_of v", standard]
  1054 declare Suc_mod_eq_add3_mod_number_of [simp]
  1055 
  1056 end