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