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