src/HOL/Nat_Numeral.thy
author huffman
Sun Mar 25 20:15:39 2012 +0200 (2012-03-25 ago)
changeset 47108 2a1953f0d20d
parent 46026 83caa4f4bd56
child 47192 0c0501cb6da6
permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
haftmann@30925
     1
(*  Title:      HOL/Nat_Numeral.thy
wenzelm@23164
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@23164
     3
    Copyright   1999  University of Cambridge
wenzelm@23164
     4
*)
wenzelm@23164
     5
haftmann@30925
     6
header {* Binary numerals for the natural numbers *}
wenzelm@23164
     7
haftmann@30925
     8
theory Nat_Numeral
haftmann@33296
     9
imports Int
wenzelm@23164
    10
begin
wenzelm@23164
    11
haftmann@31014
    12
subsection {* Numerals for natural numbers *}
haftmann@31014
    13
wenzelm@23164
    14
text {*
wenzelm@23164
    15
  Arithmetic for naturals is reduced to that for the non-negative integers.
wenzelm@23164
    16
*}
wenzelm@23164
    17
haftmann@31014
    18
subsection {* Special case: squares and cubes *}
haftmann@31014
    19
haftmann@31014
    20
lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
huffman@47108
    21
  by (simp add: nat_number(2-4))
haftmann@31014
    22
haftmann@31014
    23
lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
huffman@47108
    24
  by (simp add: nat_number(2-4))
haftmann@31014
    25
haftmann@31014
    26
context power
haftmann@30960
    27
begin
haftmann@30960
    28
wenzelm@23164
    29
abbreviation (xsymbols)
haftmann@30960
    30
  power2 :: "'a \<Rightarrow> 'a"  ("(_\<twosuperior>)" [1000] 999) where
haftmann@30960
    31
  "x\<twosuperior> \<equiv> x ^ 2"
wenzelm@23164
    32
wenzelm@23164
    33
notation (latex output)
huffman@29401
    34
  power2  ("(_\<twosuperior>)" [1000] 999)
wenzelm@23164
    35
wenzelm@23164
    36
notation (HTML output)
huffman@29401
    37
  power2  ("(_\<twosuperior>)" [1000] 999)
wenzelm@23164
    38
haftmann@30960
    39
end
haftmann@30960
    40
haftmann@31014
    41
context monoid_mult
haftmann@31014
    42
begin
haftmann@31014
    43
haftmann@31014
    44
lemma power2_eq_square: "a\<twosuperior> = a * a"
haftmann@31014
    45
  by (simp add: numeral_2_eq_2)
haftmann@31014
    46
haftmann@31014
    47
lemma power3_eq_cube: "a ^ 3 = a * a * a"
haftmann@31014
    48
  by (simp add: numeral_3_eq_3 mult_assoc)
haftmann@31014
    49
haftmann@31014
    50
lemma power_even_eq:
haftmann@31014
    51
  "a ^ (2*n) = (a ^ n) ^ 2"
haftmann@35047
    52
  by (subst mult_commute) (simp add: power_mult)
haftmann@31014
    53
haftmann@31014
    54
lemma power_odd_eq:
haftmann@31014
    55
  "a ^ Suc (2*n) = a * (a ^ n) ^ 2"
haftmann@31014
    56
  by (simp add: power_even_eq)
haftmann@31014
    57
haftmann@31014
    58
end
haftmann@31014
    59
haftmann@31014
    60
context semiring_1
haftmann@31014
    61
begin
haftmann@31014
    62
haftmann@31014
    63
lemma zero_power2 [simp]: "0\<twosuperior> = 0"
haftmann@31014
    64
  by (simp add: power2_eq_square)
haftmann@31014
    65
haftmann@31014
    66
lemma one_power2 [simp]: "1\<twosuperior> = 1"
haftmann@31014
    67
  by (simp add: power2_eq_square)
haftmann@31014
    68
haftmann@31014
    69
end
haftmann@31014
    70
huffman@36823
    71
context ring_1
haftmann@31014
    72
begin
haftmann@31014
    73
haftmann@31014
    74
lemma power2_minus [simp]:
haftmann@31014
    75
  "(- a)\<twosuperior> = a\<twosuperior>"
haftmann@31014
    76
  by (simp add: power2_eq_square)
haftmann@31014
    77
haftmann@31014
    78
lemma power_minus1_even [simp]:
huffman@47108
    79
  "-1 ^ (2*n) = 1"
haftmann@31014
    80
proof (induct n)
haftmann@31014
    81
  case 0 show ?case by simp
haftmann@31014
    82
next
huffman@47108
    83
  case (Suc n) then show ?case by (simp add: power_add power2_eq_square)
haftmann@31014
    84
qed
haftmann@31014
    85
haftmann@31014
    86
lemma power_minus1_odd:
huffman@47108
    87
  "-1 ^ Suc (2*n) = -1"
haftmann@31014
    88
  by simp
haftmann@31014
    89
haftmann@31014
    90
lemma power_minus_even [simp]:
haftmann@31014
    91
  "(-a) ^ (2*n) = a ^ (2*n)"
huffman@47108
    92
  by (simp add: power_minus [of a])
haftmann@31014
    93
haftmann@31014
    94
end
haftmann@31014
    95
huffman@36823
    96
context ring_1_no_zero_divisors
huffman@36823
    97
begin
huffman@36823
    98
huffman@36823
    99
lemma zero_eq_power2 [simp]:
huffman@36823
   100
  "a\<twosuperior> = 0 \<longleftrightarrow> a = 0"
huffman@36823
   101
  unfolding power2_eq_square by simp
huffman@36823
   102
huffman@36964
   103
lemma power2_eq_1_iff:
huffman@36823
   104
  "a\<twosuperior> = 1 \<longleftrightarrow> a = 1 \<or> a = - 1"
huffman@36964
   105
  unfolding power2_eq_square by (rule square_eq_1_iff)
huffman@36823
   106
huffman@36823
   107
end
huffman@36823
   108
huffman@44345
   109
context idom
huffman@44345
   110
begin
huffman@44345
   111
huffman@44345
   112
lemma power2_eq_iff: "x\<twosuperior> = y\<twosuperior> \<longleftrightarrow> x = y \<or> x = - y"
huffman@44345
   113
  unfolding power2_eq_square by (rule square_eq_iff)
huffman@44345
   114
huffman@44345
   115
end
huffman@44345
   116
huffman@35631
   117
context linordered_ring
haftmann@31014
   118
begin
haftmann@31014
   119
haftmann@31014
   120
lemma sum_squares_ge_zero:
haftmann@31014
   121
  "0 \<le> x * x + y * y"
haftmann@31014
   122
  by (intro add_nonneg_nonneg zero_le_square)
haftmann@31014
   123
haftmann@31014
   124
lemma not_sum_squares_lt_zero:
haftmann@31014
   125
  "\<not> x * x + y * y < 0"
haftmann@31014
   126
  by (simp add: not_less sum_squares_ge_zero)
haftmann@31014
   127
huffman@35631
   128
end
huffman@35631
   129
huffman@35631
   130
context linordered_ring_strict
huffman@35631
   131
begin
huffman@35631
   132
haftmann@31014
   133
lemma sum_squares_eq_zero_iff:
haftmann@31014
   134
  "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
haftmann@31034
   135
  by (simp add: add_nonneg_eq_0_iff)
haftmann@31014
   136
haftmann@31014
   137
lemma sum_squares_le_zero_iff:
haftmann@31014
   138
  "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
haftmann@31014
   139
  by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
haftmann@31014
   140
haftmann@31014
   141
lemma sum_squares_gt_zero_iff:
haftmann@31014
   142
  "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
huffman@35631
   143
  by (simp add: not_le [symmetric] sum_squares_le_zero_iff)
haftmann@31014
   144
haftmann@31014
   145
end
haftmann@31014
   146
haftmann@35028
   147
context linordered_semidom
haftmann@31014
   148
begin
haftmann@31014
   149
haftmann@31014
   150
lemma power2_le_imp_le:
haftmann@31014
   151
  "x\<twosuperior> \<le> y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
haftmann@31014
   152
  unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
haftmann@31014
   153
haftmann@31014
   154
lemma power2_less_imp_less:
haftmann@31014
   155
  "x\<twosuperior> < y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
haftmann@31014
   156
  by (rule power_less_imp_less_base)
haftmann@31014
   157
haftmann@31014
   158
lemma power2_eq_imp_eq:
haftmann@31014
   159
  "x\<twosuperior> = y\<twosuperior> \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
haftmann@31014
   160
  unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
haftmann@31014
   161
haftmann@31014
   162
end
haftmann@31014
   163
haftmann@35028
   164
context linordered_idom
haftmann@31014
   165
begin
haftmann@31014
   166
haftmann@31014
   167
lemma zero_le_power2 [simp]:
haftmann@31014
   168
  "0 \<le> a\<twosuperior>"
haftmann@31014
   169
  by (simp add: power2_eq_square)
haftmann@31014
   170
haftmann@31014
   171
lemma zero_less_power2 [simp]:
haftmann@31014
   172
  "0 < a\<twosuperior> \<longleftrightarrow> a \<noteq> 0"
haftmann@31014
   173
  by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
haftmann@31014
   174
haftmann@31014
   175
lemma power2_less_0 [simp]:
haftmann@31014
   176
  "\<not> a\<twosuperior> < 0"
haftmann@31014
   177
  by (force simp add: power2_eq_square mult_less_0_iff) 
haftmann@31014
   178
haftmann@31014
   179
lemma abs_power2 [simp]:
haftmann@31014
   180
  "abs (a\<twosuperior>) = a\<twosuperior>"
haftmann@31014
   181
  by (simp add: power2_eq_square abs_mult abs_mult_self)
haftmann@31014
   182
haftmann@31014
   183
lemma power2_abs [simp]:
haftmann@31014
   184
  "(abs a)\<twosuperior> = a\<twosuperior>"
haftmann@31014
   185
  by (simp add: power2_eq_square abs_mult_self)
haftmann@31014
   186
haftmann@31014
   187
lemma odd_power_less_zero:
haftmann@31014
   188
  "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
haftmann@31014
   189
proof (induct n)
haftmann@31014
   190
  case 0
haftmann@31014
   191
  then show ?case by simp
haftmann@31014
   192
next
haftmann@31014
   193
  case (Suc n)
haftmann@31014
   194
  have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
haftmann@31014
   195
    by (simp add: mult_ac power_add power2_eq_square)
haftmann@31014
   196
  thus ?case
haftmann@31014
   197
    by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
haftmann@31014
   198
qed
haftmann@31014
   199
haftmann@31014
   200
lemma odd_0_le_power_imp_0_le:
haftmann@31014
   201
  "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
haftmann@31014
   202
  using odd_power_less_zero [of a n]
haftmann@31014
   203
    by (force simp add: linorder_not_less [symmetric]) 
haftmann@31014
   204
haftmann@31014
   205
lemma zero_le_even_power'[simp]:
haftmann@31014
   206
  "0 \<le> a ^ (2*n)"
haftmann@31014
   207
proof (induct n)
haftmann@31014
   208
  case 0
huffman@35216
   209
    show ?case by simp
haftmann@31014
   210
next
haftmann@31014
   211
  case (Suc n)
haftmann@31014
   212
    have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
haftmann@31014
   213
      by (simp add: mult_ac power_add power2_eq_square)
haftmann@31014
   214
    thus ?case
haftmann@31014
   215
      by (simp add: Suc zero_le_mult_iff)
haftmann@31014
   216
qed
haftmann@31014
   217
haftmann@31014
   218
lemma sum_power2_ge_zero:
haftmann@31014
   219
  "0 \<le> x\<twosuperior> + y\<twosuperior>"
haftmann@31014
   220
  unfolding power2_eq_square by (rule sum_squares_ge_zero)
haftmann@31014
   221
haftmann@31014
   222
lemma not_sum_power2_lt_zero:
haftmann@31014
   223
  "\<not> x\<twosuperior> + y\<twosuperior> < 0"
haftmann@31014
   224
  unfolding power2_eq_square by (rule not_sum_squares_lt_zero)
haftmann@31014
   225
haftmann@31014
   226
lemma sum_power2_eq_zero_iff:
haftmann@31014
   227
  "x\<twosuperior> + y\<twosuperior> = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
haftmann@31014
   228
  unfolding power2_eq_square by (rule sum_squares_eq_zero_iff)
haftmann@31014
   229
haftmann@31014
   230
lemma sum_power2_le_zero_iff:
haftmann@31014
   231
  "x\<twosuperior> + y\<twosuperior> \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
haftmann@31014
   232
  unfolding power2_eq_square by (rule sum_squares_le_zero_iff)
haftmann@31014
   233
haftmann@31014
   234
lemma sum_power2_gt_zero_iff:
haftmann@31014
   235
  "0 < x\<twosuperior> + y\<twosuperior> \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
haftmann@31014
   236
  unfolding power2_eq_square by (rule sum_squares_gt_zero_iff)
haftmann@31014
   237
haftmann@31014
   238
end
haftmann@31014
   239
haftmann@31014
   240
lemma power2_sum:
huffman@47108
   241
  fixes x y :: "'a::comm_semiring_1"
haftmann@31014
   242
  shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
huffman@47108
   243
  by (simp add: algebra_simps power2_eq_square mult_2_right)
haftmann@31014
   244
haftmann@31014
   245
lemma power2_diff:
huffman@47108
   246
  fixes x y :: "'a::comm_ring_1"
haftmann@31014
   247
  shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
haftmann@33296
   248
  by (simp add: ring_distribs power2_eq_square mult_2) (rule mult_commute)
haftmann@31014
   249
wenzelm@23164
   250
wenzelm@23164
   251
subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
wenzelm@23164
   252
huffman@35216
   253
declare nat_1 [simp]
wenzelm@23164
   254
huffman@47108
   255
lemma nat_neg_numeral [simp]: "nat (neg_numeral w) = 0"
haftmann@36719
   256
  by simp
haftmann@36719
   257
haftmann@46026
   258
lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
huffman@47108
   259
  by simp
wenzelm@23164
   260
wenzelm@23164
   261
wenzelm@23164
   262
subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
wenzelm@23164
   263
huffman@47108
   264
lemma int_numeral: "int (numeral v) = numeral v"
huffman@47108
   265
  by (rule of_nat_numeral) (* already simp *)
huffman@23307
   266
huffman@43531
   267
lemma nonneg_int_cases:
huffman@43531
   268
  fixes k :: int assumes "0 \<le> k" obtains n where "k = of_nat n"
huffman@43531
   269
  using assms by (cases k, simp, simp)
wenzelm@23164
   270
wenzelm@23164
   271
subsubsection{*Successor *}
wenzelm@23164
   272
wenzelm@23164
   273
lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
wenzelm@23164
   274
apply (rule sym)
huffman@44766
   275
apply (simp add: nat_eq_iff)
wenzelm@23164
   276
done
wenzelm@23164
   277
wenzelm@23164
   278
lemma Suc_nat_number_of_add:
huffman@47108
   279
  "Suc (numeral v + n) = numeral (v + Num.One) + n"
huffman@47108
   280
  by simp
huffman@30081
   281
huffman@47108
   282
lemma Suc_numeral [simp]:
huffman@47108
   283
  "Suc (numeral v) = numeral (v + Num.One)"
huffman@47108
   284
  by simp
huffman@30081
   285
wenzelm@23164
   286
wenzelm@23164
   287
subsubsection{*Subtraction *}
wenzelm@23164
   288
wenzelm@23164
   289
lemma diff_nat_eq_if:
wenzelm@23164
   290
     "nat z - nat z' =  
huffman@47108
   291
        (if z' < 0 then nat z   
wenzelm@23164
   292
         else let d = z-z' in     
huffman@47108
   293
              if d < 0 then 0 else nat d)"
huffman@47108
   294
by (simp add: Let_def nat_diff_distrib [symmetric])
wenzelm@23164
   295
huffman@47108
   296
(* Int.nat_diff_distrib has too-strong premises *)
huffman@47108
   297
lemma nat_diff_distrib': "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x - y) = nat x - nat y"
huffman@47108
   298
apply (rule int_int_eq [THEN iffD1], clarsimp)
huffman@47108
   299
apply (subst zdiff_int [symmetric])
huffman@47108
   300
apply (rule nat_mono, simp_all)
huffman@47108
   301
done
wenzelm@23164
   302
huffman@47108
   303
lemma diff_nat_numeral [simp]: 
huffman@47108
   304
  "(numeral v :: nat) - numeral v' = nat (numeral v - numeral v')"
huffman@47108
   305
  by (simp only: nat_diff_distrib' zero_le_numeral nat_numeral)
wenzelm@23164
   306
huffman@47108
   307
lemma nat_numeral_diff_1 [simp]:
huffman@47108
   308
  "numeral v - (1::nat) = nat (numeral v - 1)"
huffman@47108
   309
  using diff_nat_numeral [of v Num.One] by simp
huffman@43531
   310
wenzelm@23164
   311
wenzelm@23164
   312
subsection{*Comparisons*}
wenzelm@23164
   313
huffman@47108
   314
(*Maps #n to n for n = 1, 2*)
huffman@47108
   315
lemmas numerals = numeral_1_eq_1 [where 'a=nat] numeral_2_eq_2
wenzelm@23164
   316
wenzelm@23164
   317
wenzelm@23164
   318
subsection{*Powers with Numeric Exponents*}
wenzelm@23164
   319
wenzelm@23164
   320
text{*Squares of literal numerals will be evaluated.*}
huffman@47108
   321
(* FIXME: replace with more general rules for powers of numerals *)
huffman@47108
   322
lemmas power2_eq_square_numeral [simp] =
huffman@47108
   323
    power2_eq_square [of "numeral w"] for w
wenzelm@23164
   324
wenzelm@23164
   325
wenzelm@23164
   326
text{*Simprules for comparisons where common factors can be cancelled.*}
wenzelm@23164
   327
lemmas zero_compare_simps =
wenzelm@23164
   328
    add_strict_increasing add_strict_increasing2 add_increasing
wenzelm@23164
   329
    zero_le_mult_iff zero_le_divide_iff 
wenzelm@23164
   330
    zero_less_mult_iff zero_less_divide_iff 
wenzelm@23164
   331
    mult_le_0_iff divide_le_0_iff 
wenzelm@23164
   332
    mult_less_0_iff divide_less_0_iff 
wenzelm@23164
   333
    zero_le_power2 power2_less_0
wenzelm@23164
   334
wenzelm@23164
   335
subsubsection{*Nat *}
wenzelm@23164
   336
wenzelm@23164
   337
lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
huffman@35216
   338
by simp
wenzelm@23164
   339
wenzelm@23164
   340
(*Expresses a natural number constant as the Suc of another one.
huffman@47108
   341
  NOT suitable for rewriting because n recurs on the right-hand side.*)
huffman@47108
   342
lemmas expand_Suc = Suc_pred' [of "numeral v", OF zero_less_numeral] for v
wenzelm@23164
   343
wenzelm@23164
   344
subsubsection{*Arith *}
wenzelm@23164
   345
nipkow@31790
   346
lemma Suc_eq_plus1: "Suc n = n + 1"
huffman@35216
   347
  unfolding One_nat_def by simp
wenzelm@23164
   348
nipkow@31790
   349
lemma Suc_eq_plus1_left: "Suc n = 1 + n"
huffman@35216
   350
  unfolding One_nat_def by simp
wenzelm@23164
   351
huffman@47108
   352
(* These two can be useful when m = numeral... *)
wenzelm@23164
   353
wenzelm@23164
   354
lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
huffman@30079
   355
  unfolding One_nat_def by (cases m) simp_all
wenzelm@23164
   356
wenzelm@23164
   357
lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
huffman@30079
   358
  unfolding One_nat_def by (cases m) simp_all
wenzelm@23164
   359
wenzelm@23164
   360
lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
huffman@30079
   361
  unfolding One_nat_def by (cases m) simp_all
wenzelm@23164
   362
wenzelm@23164
   363
wenzelm@23164
   364
subsection{*Comparisons involving  @{term Suc} *}
wenzelm@23164
   365
huffman@47108
   366
lemma eq_numeral_Suc [simp]: "numeral v = Suc n \<longleftrightarrow> nat (numeral v - 1) = n"
huffman@47108
   367
  by (subst expand_Suc, simp only: nat.inject nat_numeral_diff_1)
wenzelm@23164
   368
huffman@47108
   369
lemma Suc_eq_numeral [simp]: "Suc n = numeral v \<longleftrightarrow> n = nat (numeral v - 1)"
huffman@47108
   370
  by (subst expand_Suc, simp only: nat.inject nat_numeral_diff_1)
wenzelm@23164
   371
huffman@47108
   372
lemma less_numeral_Suc [simp]: "numeral v < Suc n \<longleftrightarrow> nat (numeral v - 1) < n"
huffman@47108
   373
  by (subst expand_Suc, simp only: Suc_less_eq nat_numeral_diff_1)
wenzelm@23164
   374
huffman@47108
   375
lemma less_Suc_numeral [simp]: "Suc n < numeral v \<longleftrightarrow> n < nat (numeral v - 1)"
huffman@47108
   376
  by (subst expand_Suc, simp only: Suc_less_eq nat_numeral_diff_1)
wenzelm@23164
   377
huffman@47108
   378
lemma le_numeral_Suc [simp]: "numeral v \<le> Suc n \<longleftrightarrow> nat (numeral v - 1) \<le> n"
huffman@47108
   379
  by (subst expand_Suc, simp only: Suc_le_mono nat_numeral_diff_1)
wenzelm@23164
   380
huffman@47108
   381
lemma le_Suc_numeral [simp]: "Suc n \<le> numeral v \<longleftrightarrow> n \<le> nat (numeral v - 1)"
huffman@47108
   382
  by (subst expand_Suc, simp only: Suc_le_mono nat_numeral_diff_1)
wenzelm@23164
   383
wenzelm@23164
   384
wenzelm@23164
   385
subsection{*Max and Min Combined with @{term Suc} *}
wenzelm@23164
   386
huffman@47108
   387
lemma max_Suc_numeral [simp]:
huffman@47108
   388
  "max (Suc n) (numeral v) = Suc (max n (nat (numeral v - 1)))"
huffman@47108
   389
  by (subst expand_Suc, simp only: max_Suc_Suc nat_numeral_diff_1)
huffman@47108
   390
huffman@47108
   391
lemma max_numeral_Suc [simp]:
huffman@47108
   392
  "max (numeral v) (Suc n) = Suc (max (nat (numeral v - 1)) n)"
huffman@47108
   393
  by (subst expand_Suc, simp only: max_Suc_Suc nat_numeral_diff_1)
huffman@47108
   394
huffman@47108
   395
lemma min_Suc_numeral [simp]:
huffman@47108
   396
  "min (Suc n) (numeral v) = Suc (min n (nat (numeral v - 1)))"
huffman@47108
   397
  by (subst expand_Suc, simp only: min_Suc_Suc nat_numeral_diff_1)
huffman@47108
   398
huffman@47108
   399
lemma min_numeral_Suc [simp]:
huffman@47108
   400
  "min (numeral v) (Suc n) = Suc (min (nat (numeral v - 1)) n)"
huffman@47108
   401
  by (subst expand_Suc, simp only: min_Suc_Suc nat_numeral_diff_1)
wenzelm@23164
   402
 
wenzelm@23164
   403
subsection{*Literal arithmetic involving powers*}
wenzelm@23164
   404
huffman@47108
   405
(* TODO: replace with more generic rule for powers of numerals *)
huffman@47108
   406
lemma power_nat_numeral:
huffman@47108
   407
  "(numeral v :: nat) ^ n = nat ((numeral v :: int) ^ n)"
huffman@47108
   408
  by (simp only: nat_power_eq zero_le_numeral nat_numeral)
wenzelm@23164
   409
huffman@47108
   410
lemmas power_nat_numeral_numeral = power_nat_numeral [of _ "numeral w"] for w
huffman@47108
   411
declare power_nat_numeral_numeral [simp]
wenzelm@23164
   412
wenzelm@23164
   413
huffman@23294
   414
text{*For arbitrary rings*}
wenzelm@23164
   415
huffman@47108
   416
lemma power_numeral_even:
huffman@43526
   417
  fixes z :: "'a::monoid_mult"
huffman@47108
   418
  shows "z ^ numeral (Num.Bit0 w) = (let w = z ^ (numeral w) in w * w)"
huffman@47108
   419
  unfolding numeral_Bit0 power_add Let_def ..
wenzelm@23164
   420
huffman@47108
   421
lemma power_numeral_odd:
huffman@43526
   422
  fixes z :: "'a::monoid_mult"
huffman@47108
   423
  shows "z ^ numeral (Num.Bit1 w) = (let w = z ^ (numeral w) in z * w * w)"
huffman@47108
   424
  unfolding numeral_Bit1 One_nat_def add_Suc_right add_0_right
huffman@47108
   425
  unfolding power_Suc power_add Let_def mult_assoc ..
wenzelm@23164
   426
huffman@47108
   427
lemmas zpower_numeral_even = power_numeral_even [where 'a=int]
huffman@47108
   428
lemmas zpower_numeral_odd = power_numeral_odd [where 'a=int]
wenzelm@23164
   429
huffman@47108
   430
lemmas power_numeral_even_numeral [simp] =
huffman@47108
   431
    power_numeral_even [of "numeral v"] for v
wenzelm@23164
   432
huffman@47108
   433
lemmas power_numeral_odd_numeral [simp] =
huffman@47108
   434
    power_numeral_odd [of "numeral v"] for v
wenzelm@23164
   435
huffman@47108
   436
lemma nat_numeral_Bit0:
huffman@47108
   437
  "numeral (Num.Bit0 w) = (let n::nat = numeral w in n + n)"
huffman@47108
   438
  unfolding numeral_Bit0 Let_def ..
huffman@26086
   439
huffman@47108
   440
lemma nat_numeral_Bit1:
huffman@47108
   441
  "numeral (Num.Bit1 w) = (let n = numeral w in Suc (n + n))"
huffman@47108
   442
  unfolding numeral_Bit1 Let_def by simp
wenzelm@23164
   443
nipkow@40077
   444
lemmas eval_nat_numeral =
huffman@47108
   445
  nat_numeral_Bit0 nat_numeral_Bit1
huffman@35216
   446
haftmann@36699
   447
lemmas nat_arith =
huffman@47108
   448
  diff_nat_numeral
haftmann@36699
   449
haftmann@36716
   450
lemmas semiring_norm =
huffman@47108
   451
  Let_def arith_simps nat_arith rel_simps
huffman@47108
   452
  if_False if_True
huffman@47108
   453
  add_0 add_Suc add_numeral_left
huffman@47108
   454
  add_neg_numeral_left mult_numeral_left
haftmann@36716
   455
  numeral_1_eq_1 [symmetric] Suc_eq_plus1
huffman@47108
   456
  eq_numeral_iff_iszero not_iszero_Numeral1
haftmann@36716
   457
wenzelm@23164
   458
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
haftmann@33296
   459
  by (fact Let_def)
wenzelm@23164
   460
huffman@47108
   461
lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::ring_1)"
huffman@47108
   462
  by (fact power_minus1_even) (* FIXME: duplicate *)
wenzelm@23164
   463
huffman@47108
   464
lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::ring_1)"
huffman@47108
   465
  by (fact power_minus1_odd) (* FIXME: duplicate *)
haftmann@33296
   466
wenzelm@23164
   467
wenzelm@23164
   468
subsection{*Literal arithmetic and @{term of_nat}*}
wenzelm@23164
   469
wenzelm@23164
   470
lemma of_nat_double:
wenzelm@23164
   471
     "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
wenzelm@23164
   472
by (simp only: mult_2 nat_add_distrib of_nat_add) 
wenzelm@23164
   473
wenzelm@23164
   474
haftmann@30652
   475
subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
haftmann@30652
   476
haftmann@30652
   477
text{*Where K above is a literal*}
haftmann@30652
   478
huffman@47108
   479
lemma Suc_diff_eq_diff_pred: "0 < n ==> Suc m - n = m - (n - Numeral1)"
huffman@35216
   480
by (simp split: nat_diff_split)
haftmann@30652
   481
haftmann@30652
   482
text{*No longer required as a simprule because of the @{text inverse_fold}
haftmann@30652
   483
   simproc*}
huffman@47108
   484
lemma Suc_diff_numeral: "Suc m - (numeral v) = m - (numeral v - 1)"
huffman@47108
   485
  by (subst expand_Suc, simp only: diff_Suc_Suc)
haftmann@30652
   486
haftmann@30652
   487
lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
huffman@35216
   488
by (simp split: nat_diff_split)
haftmann@30652
   489
haftmann@30652
   490
haftmann@30652
   491
subsubsection{*For @{term nat_case} and @{term nat_rec}*}
haftmann@30652
   492
huffman@47108
   493
lemma nat_case_numeral [simp]:
huffman@47108
   494
  "nat_case a f (numeral v) = (let pv = nat (numeral v - 1) in f pv)"
huffman@47108
   495
  by (subst expand_Suc, simp only: nat.cases nat_numeral_diff_1 Let_def)
haftmann@30652
   496
haftmann@30652
   497
lemma nat_case_add_eq_if [simp]:
huffman@47108
   498
  "nat_case a f ((numeral v) + n) = (let pv = nat (numeral v - 1) in f (pv + n))"
huffman@47108
   499
  by (subst expand_Suc, simp only: nat.cases nat_numeral_diff_1 Let_def add_Suc)
haftmann@30652
   500
huffman@47108
   501
lemma nat_rec_numeral [simp]:
huffman@47108
   502
  "nat_rec a f (numeral v) = (let pv = nat (numeral v - 1) in f pv (nat_rec a f pv))"
huffman@47108
   503
  by (subst expand_Suc, simp only: nat_rec_Suc nat_numeral_diff_1 Let_def)
haftmann@30652
   504
haftmann@30652
   505
lemma nat_rec_add_eq_if [simp]:
huffman@47108
   506
  "nat_rec a f (numeral v + n) =
huffman@47108
   507
    (let pv = nat (numeral v - 1) in f (pv + n) (nat_rec a f (pv + n)))"
huffman@47108
   508
  by (subst expand_Suc, simp only: nat_rec_Suc nat_numeral_diff_1 Let_def add_Suc)
haftmann@30652
   509
haftmann@30652
   510
haftmann@30652
   511
subsubsection{*Various Other Lemmas*}
haftmann@30652
   512
nipkow@31080
   513
lemma card_UNIV_bool[simp]: "card (UNIV :: bool set) = 2"
nipkow@31080
   514
by(simp add: UNIV_bool)
nipkow@31080
   515
haftmann@30652
   516
text {*Evens and Odds, for Mutilated Chess Board*}
haftmann@30652
   517
haftmann@30652
   518
text{*Lemmas for specialist use, NOT as default simprules*}
haftmann@30652
   519
lemma nat_mult_2: "2 * z = (z+z::nat)"
huffman@47108
   520
by (rule mult_2) (* FIXME: duplicate *)
haftmann@30652
   521
haftmann@30652
   522
lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
huffman@47108
   523
by (rule mult_2_right) (* FIXME: duplicate *)
haftmann@30652
   524
haftmann@30652
   525
text{*Case analysis on @{term "n<2"}*}
haftmann@30652
   526
lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
huffman@47108
   527
by (auto simp add: numeral_2_eq_2)
haftmann@30652
   528
haftmann@30652
   529
text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
haftmann@30652
   530
haftmann@30652
   531
lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
haftmann@30652
   532
by simp
haftmann@30652
   533
haftmann@30652
   534
lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
haftmann@30652
   535
by simp
haftmann@30652
   536
haftmann@30652
   537
text{*Can be used to eliminate long strings of Sucs, but not by default*}
haftmann@30652
   538
lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
haftmann@30652
   539
by simp
haftmann@30652
   540
huffman@47108
   541
text{*Legacy theorems*}
huffman@47108
   542
huffman@47108
   543
lemmas nat_1_add_1 = one_add_one [where 'a=nat]
huffman@47108
   544
huffman@31096
   545
end