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