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