src/HOL/Integ/NatBin.thy
author paulson
Tue Jun 28 15:27:45 2005 +0200 (2005-06-28)
changeset 16587 b34c8aa657a5
parent 16413 47ffc49c7d7b
child 16642 849ec3962b55
permissions -rw-r--r--
Constant "If" is now local
paulson@7032
     1
(*  Title:      HOL/NatBin.thy
paulson@7032
     2
    ID:         $Id$
paulson@7032
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@7032
     4
    Copyright   1999  University of Cambridge
wenzelm@12838
     5
*)
paulson@7032
     6
wenzelm@12838
     7
header {* Binary arithmetic for the natural numbers *}
paulson@7032
     8
nipkow@15131
     9
theory NatBin
nipkow@15140
    10
imports IntDiv
nipkow@15131
    11
begin
paulson@7032
    12
wenzelm@12838
    13
text {*
paulson@14273
    14
  Arithmetic for naturals is reduced to that for the non-negative integers.
wenzelm@12838
    15
*}
wenzelm@12838
    16
wenzelm@12838
    17
instance nat :: number ..
wenzelm@12838
    18
wenzelm@12838
    19
defs (overloaded)
paulson@14273
    20
  nat_number_of_def:
paulson@14273
    21
     "(number_of::bin => nat) v == nat ((number_of :: bin => int) v)"
wenzelm@12838
    22
paulson@14272
    23
paulson@14353
    24
subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
paulson@14272
    25
paulson@14273
    26
declare nat_0 [simp] nat_1 [simp]
paulson@14273
    27
paulson@14273
    28
lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
paulson@14273
    29
by (simp add: nat_number_of_def)
paulson@14272
    30
paulson@14387
    31
lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
paulson@14273
    32
by (simp add: nat_number_of_def)
paulson@14272
    33
paulson@14387
    34
lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
paulson@14273
    35
by (simp add: nat_1 nat_number_of_def)
paulson@14272
    36
paulson@14272
    37
lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
paulson@14387
    38
by (simp add: nat_numeral_1_eq_1)
paulson@14272
    39
paulson@14272
    40
lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
paulson@14272
    41
apply (unfold nat_number_of_def)
paulson@14272
    42
apply (rule nat_2)
paulson@14272
    43
done
paulson@14272
    44
paulson@14272
    45
paulson@14273
    46
text{*Distributive laws for type @{text nat}.  The others are in theory
paulson@14273
    47
   @{text IntArith}, but these require div and mod to be defined for type
paulson@14273
    48
   "int".  They also need some of the lemmas proved above.*}
paulson@14272
    49
paulson@14272
    50
lemma nat_div_distrib: "(0::int) <= z ==> nat (z div z') = nat z div nat z'"
paulson@14272
    51
apply (case_tac "0 <= z'")
paulson@14272
    52
apply (auto simp add: div_nonneg_neg_le0 DIVISION_BY_ZERO_DIV)
paulson@14273
    53
apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
paulson@14272
    54
apply (auto elim!: nonneg_eq_int)
paulson@14272
    55
apply (rename_tac m m')
paulson@14272
    56
apply (subgoal_tac "0 <= int m div int m'")
paulson@14387
    57
 prefer 2 apply (simp add: nat_numeral_0_eq_0 pos_imp_zdiv_nonneg_iff) 
paulson@14273
    58
apply (rule inj_int [THEN injD], simp)
paulson@14272
    59
apply (rule_tac r = "int (m mod m') " in quorem_div)
paulson@14273
    60
 prefer 2 apply force
paulson@14387
    61
apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0 zadd_int 
paulson@14273
    62
                 zmult_int)
paulson@14272
    63
done
paulson@14272
    64
paulson@14272
    65
(*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*)
paulson@14273
    66
lemma nat_mod_distrib:
paulson@14273
    67
     "[| (0::int) <= z;  0 <= z' |] ==> nat (z mod z') = nat z mod nat z'"
paulson@14273
    68
apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
paulson@14272
    69
apply (auto elim!: nonneg_eq_int)
paulson@14272
    70
apply (rename_tac m m')
paulson@14272
    71
apply (subgoal_tac "0 <= int m mod int m'")
paulson@14387
    72
 prefer 2 apply (simp add: nat_less_iff nat_numeral_0_eq_0 pos_mod_sign) 
paulson@14273
    73
apply (rule inj_int [THEN injD], simp)
paulson@14272
    74
apply (rule_tac q = "int (m div m') " in quorem_mod)
paulson@14273
    75
 prefer 2 apply force
paulson@14387
    76
apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0 zadd_int zmult_int)
paulson@14272
    77
done
paulson@14272
    78
paulson@16413
    79
text{*Suggested by Matthias Daum*}
paulson@16413
    80
lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
paulson@16413
    81
apply (subgoal_tac "nat x div nat k < nat x") 
paulson@16413
    82
 apply (simp add: nat_div_distrib [symmetric])
paulson@16413
    83
apply (rule Divides.div_less_dividend, simp_all) 
paulson@16413
    84
done
paulson@16413
    85
paulson@14272
    86
paulson@14353
    87
subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
paulson@14272
    88
paulson@14272
    89
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14387
    90
lemma int_nat_number_of [simp]:
paulson@14273
    91
     "int (number_of v :: nat) =  
paulson@14378
    92
         (if neg (number_of v :: int) then 0  
paulson@14272
    93
          else (number_of v :: int))"
paulson@14272
    94
by (simp del: nat_number_of
paulson@14272
    95
	 add: neg_nat nat_number_of_def not_neg_nat add_assoc)
paulson@14272
    96
paulson@14272
    97
paulson@14390
    98
subsubsection{*Successor *}
paulson@14272
    99
paulson@14272
   100
lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
paulson@14272
   101
apply (rule sym)
paulson@14273
   102
apply (simp add: nat_eq_iff int_Suc)
paulson@14272
   103
done
paulson@14272
   104
paulson@14273
   105
lemma Suc_nat_number_of_add:
paulson@14273
   106
     "Suc (number_of v + n) =  
paulson@14378
   107
        (if neg (number_of v :: int) then 1+n else number_of (bin_succ v) + n)" 
paulson@14272
   108
by (simp del: nat_number_of 
paulson@14272
   109
         add: nat_number_of_def neg_nat
paulson@14272
   110
              Suc_nat_eq_nat_zadd1 number_of_succ) 
paulson@14272
   111
paulson@14387
   112
lemma Suc_nat_number_of [simp]:
paulson@14273
   113
     "Suc (number_of v) =  
paulson@14378
   114
        (if neg (number_of v :: int) then 1 else number_of (bin_succ v))"
paulson@14273
   115
apply (cut_tac n = 0 in Suc_nat_number_of_add)
paulson@14272
   116
apply (simp cong del: if_weak_cong)
paulson@14272
   117
done
paulson@14272
   118
paulson@14272
   119
paulson@14390
   120
subsubsection{*Addition *}
paulson@14272
   121
paulson@14272
   122
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14387
   123
lemma add_nat_number_of [simp]:
paulson@14273
   124
     "(number_of v :: nat) + number_of v' =  
paulson@14378
   125
         (if neg (number_of v :: int) then number_of v'  
paulson@14378
   126
          else if neg (number_of v' :: int) then number_of v  
paulson@14272
   127
          else number_of (bin_add v v'))"
paulson@14272
   128
by (force dest!: neg_nat
paulson@14272
   129
          simp del: nat_number_of
paulson@14272
   130
          simp add: nat_number_of_def nat_add_distrib [symmetric]) 
paulson@14272
   131
paulson@14272
   132
paulson@14390
   133
subsubsection{*Subtraction *}
paulson@14272
   134
paulson@14273
   135
lemma diff_nat_eq_if:
paulson@14273
   136
     "nat z - nat z' =  
paulson@14272
   137
        (if neg z' then nat z   
paulson@14272
   138
         else let d = z-z' in     
paulson@14272
   139
              if neg d then 0 else nat d)"
paulson@14273
   140
apply (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
paulson@14273
   141
apply (simp add: diff_is_0_eq nat_le_eq_zle)
paulson@14272
   142
done
paulson@14272
   143
paulson@14387
   144
lemma diff_nat_number_of [simp]: 
paulson@14272
   145
     "(number_of v :: nat) - number_of v' =  
paulson@14378
   146
        (if neg (number_of v' :: int) then number_of v  
paulson@14272
   147
         else let d = number_of (bin_add v (bin_minus v')) in     
paulson@14272
   148
              if neg d then 0 else nat d)"
paulson@14272
   149
by (simp del: nat_number_of add: diff_nat_eq_if nat_number_of_def) 
paulson@14272
   150
paulson@14272
   151
paulson@14272
   152
paulson@14390
   153
subsubsection{*Multiplication *}
paulson@14272
   154
paulson@14387
   155
lemma mult_nat_number_of [simp]:
paulson@14273
   156
     "(number_of v :: nat) * number_of v' =  
paulson@14378
   157
       (if neg (number_of v :: int) then 0 else number_of (bin_mult v v'))"
paulson@14272
   158
by (force dest!: neg_nat
paulson@14272
   159
          simp del: nat_number_of
paulson@14272
   160
          simp add: nat_number_of_def nat_mult_distrib [symmetric]) 
paulson@14272
   161
paulson@14272
   162
paulson@14272
   163
paulson@14390
   164
subsubsection{*Quotient *}
paulson@14272
   165
paulson@14387
   166
lemma div_nat_number_of [simp]:
paulson@14273
   167
     "(number_of v :: nat)  div  number_of v' =  
paulson@14378
   168
          (if neg (number_of v :: int) then 0  
paulson@14272
   169
           else nat (number_of v div number_of v'))"
paulson@14272
   170
by (force dest!: neg_nat
paulson@14272
   171
          simp del: nat_number_of
paulson@14272
   172
          simp add: nat_number_of_def nat_div_distrib [symmetric]) 
paulson@14272
   173
paulson@14387
   174
lemma one_div_nat_number_of [simp]:
paulson@14387
   175
     "(Suc 0)  div  number_of v' = (nat (1 div number_of v'))" 
paulson@14387
   176
by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
paulson@14272
   177
paulson@14272
   178
paulson@14390
   179
subsubsection{*Remainder *}
paulson@14272
   180
paulson@14387
   181
lemma mod_nat_number_of [simp]:
paulson@14273
   182
     "(number_of v :: nat)  mod  number_of v' =  
paulson@14378
   183
        (if neg (number_of v :: int) then 0  
paulson@14378
   184
         else if neg (number_of v' :: int) then number_of v  
paulson@14272
   185
         else nat (number_of v mod number_of v'))"
paulson@14272
   186
by (force dest!: neg_nat
paulson@14272
   187
          simp del: nat_number_of
paulson@14272
   188
          simp add: nat_number_of_def nat_mod_distrib [symmetric]) 
paulson@14272
   189
paulson@14387
   190
lemma one_mod_nat_number_of [simp]:
paulson@14387
   191
     "(Suc 0)  mod  number_of v' =  
paulson@14387
   192
        (if neg (number_of v' :: int) then Suc 0
paulson@14387
   193
         else nat (1 mod number_of v'))"
paulson@14387
   194
by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
paulson@14387
   195
paulson@14387
   196
paulson@14272
   197
paulson@14272
   198
ML
paulson@14272
   199
{*
paulson@14272
   200
val nat_number_of_def = thm"nat_number_of_def";
paulson@14272
   201
paulson@14272
   202
val nat_number_of = thm"nat_number_of";
paulson@14387
   203
val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
paulson@14387
   204
val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
paulson@14272
   205
val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
paulson@14272
   206
val numeral_2_eq_2 = thm"numeral_2_eq_2";
paulson@14272
   207
val nat_div_distrib = thm"nat_div_distrib";
paulson@14272
   208
val nat_mod_distrib = thm"nat_mod_distrib";
paulson@14272
   209
val int_nat_number_of = thm"int_nat_number_of";
paulson@14272
   210
val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
paulson@14272
   211
val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
paulson@14272
   212
val Suc_nat_number_of = thm"Suc_nat_number_of";
paulson@14272
   213
val add_nat_number_of = thm"add_nat_number_of";
paulson@14272
   214
val diff_nat_eq_if = thm"diff_nat_eq_if";
paulson@14272
   215
val diff_nat_number_of = thm"diff_nat_number_of";
paulson@14272
   216
val mult_nat_number_of = thm"mult_nat_number_of";
paulson@14272
   217
val div_nat_number_of = thm"div_nat_number_of";
paulson@14272
   218
val mod_nat_number_of = thm"mod_nat_number_of";
paulson@14272
   219
*}
paulson@14272
   220
paulson@14272
   221
paulson@14390
   222
subsection{*Comparisons*}
paulson@14272
   223
paulson@14390
   224
subsubsection{*Equals (=) *}
paulson@14272
   225
paulson@14273
   226
lemma eq_nat_nat_iff:
paulson@14273
   227
     "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
paulson@14273
   228
by (auto elim!: nonneg_eq_int)
paulson@14272
   229
paulson@14272
   230
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14390
   231
lemma eq_nat_number_of [simp]:
paulson@14273
   232
     "((number_of v :: nat) = number_of v') =  
paulson@14378
   233
      (if neg (number_of v :: int) then (iszero (number_of v' :: int) | neg (number_of v' :: int))  
paulson@14378
   234
       else if neg (number_of v' :: int) then iszero (number_of v :: int)  
paulson@14378
   235
       else iszero (number_of (bin_add v (bin_minus v')) :: int))"
paulson@14272
   236
apply (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
paulson@14272
   237
                  eq_nat_nat_iff eq_number_of_eq nat_0 iszero_def
paulson@14273
   238
            split add: split_if cong add: imp_cong)
paulson@14272
   239
apply (simp only: nat_eq_iff nat_eq_iff2)
paulson@14272
   240
apply (simp add: not_neg_eq_ge_0 [symmetric])
paulson@14272
   241
done
paulson@14272
   242
paulson@14272
   243
paulson@14390
   244
subsubsection{*Less-than (<) *}
paulson@14272
   245
paulson@14272
   246
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14390
   247
lemma less_nat_number_of [simp]:
paulson@14273
   248
     "((number_of v :: nat) < number_of v') =  
paulson@14378
   249
         (if neg (number_of v :: int) then neg (number_of (bin_minus v') :: int)  
paulson@14378
   250
          else neg (number_of (bin_add v (bin_minus v')) :: int))"
paulson@14390
   251
by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
paulson@14272
   252
                nat_less_eq_zless less_number_of_eq_neg zless_nat_eq_int_zless
paulson@14390
   253
         cong add: imp_cong, simp) 
paulson@14272
   254
paulson@14390
   255
paulson@14272
   256
paulson@14272
   257
paulson@14272
   258
(*Maps #n to n for n = 0, 1, 2*)
paulson@14387
   259
lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
paulson@14272
   260
paulson@14272
   261
paulson@15234
   262
subsection{*Powers with Numeric Exponents*}
paulson@14353
   263
paulson@14353
   264
text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}.
paulson@14353
   265
We cannot prove general results about the numeral @{term "-1"}, so we have to
paulson@14353
   266
use @{term "- 1"} instead.*}
paulson@14353
   267
paulson@15003
   268
lemma power2_eq_square: "(a::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = a * a"
paulson@14353
   269
  by (simp add: numeral_2_eq_2 Power.power_Suc)
paulson@14353
   270
paulson@15003
   271
lemma [simp]: "(0::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = 0"
paulson@14353
   272
  by (simp add: power2_eq_square)
paulson@14353
   273
paulson@15003
   274
lemma [simp]: "(1::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = 1"
paulson@14353
   275
  by (simp add: power2_eq_square)
paulson@14353
   276
paulson@14353
   277
text{*Squares of literal numerals will be evaluated.*}
paulson@14353
   278
declare power2_eq_square [of "number_of w", standard, simp]
paulson@14353
   279
paulson@15003
   280
lemma zero_le_power2 [simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})"
paulson@14353
   281
  by (simp add: power2_eq_square zero_le_square)
paulson@14353
   282
paulson@14353
   283
lemma zero_less_power2 [simp]:
paulson@15003
   284
     "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))"
paulson@14353
   285
  by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
paulson@14353
   286
paulson@15234
   287
lemma power2_less_0 [simp]:
paulson@15234
   288
  fixes a :: "'a::{ordered_idom,recpower}"
paulson@15234
   289
  shows "~ (a\<twosuperior> < 0)"
paulson@15234
   290
by (force simp add: power2_eq_square mult_less_0_iff) 
paulson@15234
   291
paulson@14353
   292
lemma zero_eq_power2 [simp]:
paulson@15003
   293
     "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))"
paulson@14353
   294
  by (force simp add: power2_eq_square mult_eq_0_iff)
paulson@14353
   295
paulson@14353
   296
lemma abs_power2 [simp]:
paulson@15003
   297
     "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})"
paulson@14353
   298
  by (simp add: power2_eq_square abs_mult abs_mult_self)
paulson@14353
   299
paulson@14353
   300
lemma power2_abs [simp]:
paulson@15003
   301
     "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})"
paulson@14353
   302
  by (simp add: power2_eq_square abs_mult_self)
paulson@14353
   303
paulson@14353
   304
lemma power2_minus [simp]:
paulson@15003
   305
     "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})"
paulson@14353
   306
  by (simp add: power2_eq_square)
paulson@14353
   307
paulson@15003
   308
lemma power_minus1_even: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})"
paulson@15251
   309
apply (induct "n")
paulson@14353
   310
apply (auto simp add: power_Suc power_add)
paulson@14353
   311
done
paulson@14353
   312
paulson@15003
   313
lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2"
paulson@14443
   314
by (simp add: power_mult power_mult_distrib power2_eq_square)
paulson@14443
   315
paulson@14443
   316
lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2"
paulson@14443
   317
by (simp add: power_even_eq) 
paulson@14443
   318
paulson@14353
   319
lemma power_minus_even [simp]:
paulson@15003
   320
     "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)"
paulson@14353
   321
by (simp add: power_minus1_even power_minus [of a]) 
paulson@14353
   322
paulson@14353
   323
lemma zero_le_even_power:
paulson@15003
   324
     "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)"
paulson@14353
   325
proof (induct "n")
paulson@14353
   326
  case 0
paulson@14353
   327
    show ?case by (simp add: zero_le_one)
paulson@14353
   328
next
paulson@14353
   329
  case (Suc n)
paulson@14353
   330
    have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
paulson@14353
   331
      by (simp add: mult_ac power_add power2_eq_square)
paulson@14353
   332
    thus ?case
paulson@14353
   333
      by (simp add: prems zero_le_square zero_le_mult_iff)
paulson@14353
   334
qed
paulson@14353
   335
paulson@14353
   336
lemma odd_power_less_zero:
paulson@15003
   337
     "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0"
paulson@14353
   338
proof (induct "n")
paulson@14353
   339
  case 0
paulson@14353
   340
    show ?case by (simp add: Power.power_Suc)
paulson@14353
   341
next
paulson@14353
   342
  case (Suc n)
paulson@14353
   343
    have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)" 
paulson@14353
   344
      by (simp add: mult_ac power_add power2_eq_square Power.power_Suc)
paulson@14353
   345
    thus ?case
paulson@14353
   346
      by (simp add: prems mult_less_0_iff mult_neg)
paulson@14353
   347
qed
paulson@14353
   348
paulson@14353
   349
lemma odd_0_le_power_imp_0_le:
paulson@15003
   350
     "0 \<le> a  ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})"
paulson@14353
   351
apply (insert odd_power_less_zero [of a n]) 
paulson@14353
   352
apply (force simp add: linorder_not_less [symmetric]) 
paulson@14353
   353
done
paulson@14353
   354
paulson@15234
   355
text{*Simprules for comparisons where common factors can be cancelled.*}
paulson@15234
   356
lemmas zero_compare_simps =
paulson@15234
   357
    add_strict_increasing add_strict_increasing2 add_increasing
paulson@15234
   358
    zero_le_mult_iff zero_le_divide_iff 
paulson@15234
   359
    zero_less_mult_iff zero_less_divide_iff 
paulson@15234
   360
    mult_le_0_iff divide_le_0_iff 
paulson@15234
   361
    mult_less_0_iff divide_less_0_iff 
paulson@15234
   362
    zero_le_power2 power2_less_0
paulson@14353
   363
paulson@14390
   364
subsubsection{*Nat *}
paulson@14272
   365
paulson@14272
   366
lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
paulson@14273
   367
by (simp add: numerals)
paulson@14272
   368
paulson@14272
   369
(*Expresses a natural number constant as the Suc of another one.
paulson@14272
   370
  NOT suitable for rewriting because n recurs in the condition.*)
paulson@14272
   371
lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
paulson@14272
   372
paulson@14390
   373
subsubsection{*Arith *}
paulson@14272
   374
paulson@14272
   375
lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
paulson@14273
   376
by (simp add: numerals)
paulson@14272
   377
paulson@14467
   378
lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
paulson@14467
   379
by (simp add: numerals)
paulson@14467
   380
paulson@14272
   381
(* These two can be useful when m = number_of... *)
paulson@14272
   382
paulson@14272
   383
lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
paulson@14272
   384
apply (case_tac "m")
paulson@14272
   385
apply (simp_all add: numerals)
paulson@14272
   386
done
paulson@14272
   387
paulson@14272
   388
lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
paulson@14272
   389
apply (case_tac "m")
paulson@14272
   390
apply (simp_all add: numerals)
paulson@14272
   391
done
paulson@14272
   392
paulson@14272
   393
lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
paulson@14272
   394
apply (case_tac "m")
paulson@14272
   395
apply (simp_all add: numerals)
paulson@14272
   396
done
paulson@14272
   397
paulson@14272
   398
paulson@14390
   399
subsection{*Comparisons involving (0::nat) *}
paulson@14272
   400
paulson@14390
   401
text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
paulson@14390
   402
paulson@14390
   403
lemma eq_number_of_0 [simp]:
paulson@14273
   404
     "(number_of v = (0::nat)) =  
paulson@14378
   405
      (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
paulson@14390
   406
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
paulson@14272
   407
paulson@14390
   408
lemma eq_0_number_of [simp]:
paulson@14273
   409
     "((0::nat) = number_of v) =  
paulson@14378
   410
      (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
paulson@14390
   411
by (rule trans [OF eq_sym_conv eq_number_of_0])
paulson@14272
   412
paulson@14390
   413
lemma less_0_number_of [simp]:
paulson@14378
   414
     "((0::nat) < number_of v) = neg (number_of (bin_minus v) :: int)"
paulson@14387
   415
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
paulson@14272
   416
paulson@14272
   417
paulson@14378
   418
lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
paulson@14387
   419
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
paulson@14272
   420
paulson@14272
   421
paulson@14272
   422
paulson@14390
   423
subsection{*Comparisons involving Suc *}
paulson@14272
   424
paulson@14273
   425
lemma eq_number_of_Suc [simp]:
paulson@14273
   426
     "(number_of v = Suc n) =  
paulson@14272
   427
        (let pv = number_of (bin_pred v) in  
paulson@14272
   428
         if neg pv then False else nat pv = n)"
paulson@14272
   429
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
paulson@14272
   430
                  number_of_pred nat_number_of_def 
paulson@14273
   431
            split add: split_if)
paulson@14272
   432
apply (rule_tac x = "number_of v" in spec)
paulson@14272
   433
apply (auto simp add: nat_eq_iff)
paulson@14272
   434
done
paulson@14272
   435
paulson@14273
   436
lemma Suc_eq_number_of [simp]:
paulson@14273
   437
     "(Suc n = number_of v) =  
paulson@14272
   438
        (let pv = number_of (bin_pred v) in  
paulson@14272
   439
         if neg pv then False else nat pv = n)"
paulson@14390
   440
by (rule trans [OF eq_sym_conv eq_number_of_Suc])
paulson@14272
   441
paulson@14273
   442
lemma less_number_of_Suc [simp]:
paulson@14273
   443
     "(number_of v < Suc n) =  
paulson@14272
   444
        (let pv = number_of (bin_pred v) in  
paulson@14272
   445
         if neg pv then True else nat pv < n)"
paulson@14272
   446
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
paulson@14272
   447
                  number_of_pred nat_number_of_def  
paulson@14273
   448
            split add: split_if)
paulson@14272
   449
apply (rule_tac x = "number_of v" in spec)
paulson@14272
   450
apply (auto simp add: nat_less_iff)
paulson@14272
   451
done
paulson@14272
   452
paulson@14273
   453
lemma less_Suc_number_of [simp]:
paulson@14273
   454
     "(Suc n < number_of v) =  
paulson@14272
   455
        (let pv = number_of (bin_pred v) in  
paulson@14272
   456
         if neg pv then False else n < nat pv)"
paulson@14272
   457
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
paulson@14272
   458
                  number_of_pred nat_number_of_def
paulson@14273
   459
            split add: split_if)
paulson@14272
   460
apply (rule_tac x = "number_of v" in spec)
paulson@14272
   461
apply (auto simp add: zless_nat_eq_int_zless)
paulson@14272
   462
done
paulson@14272
   463
paulson@14273
   464
lemma le_number_of_Suc [simp]:
paulson@14273
   465
     "(number_of v <= Suc n) =  
paulson@14272
   466
        (let pv = number_of (bin_pred v) in  
paulson@14272
   467
         if neg pv then True else nat pv <= n)"
paulson@14390
   468
by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
paulson@14272
   469
paulson@14273
   470
lemma le_Suc_number_of [simp]:
paulson@14273
   471
     "(Suc n <= number_of v) =  
paulson@14272
   472
        (let pv = number_of (bin_pred v) in  
paulson@14272
   473
         if neg pv then False else n <= nat pv)"
paulson@14390
   474
by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
paulson@14272
   475
paulson@14272
   476
paulson@14272
   477
(* Push int(.) inwards: *)
paulson@14272
   478
declare zadd_int [symmetric, simp]
paulson@14272
   479
paulson@14272
   480
lemma lemma1: "(m+m = n+n) = (m = (n::int))"
paulson@14273
   481
by auto
paulson@14272
   482
paulson@14272
   483
lemma lemma2: "m+m ~= (1::int) + (n + n)"
paulson@14272
   484
apply auto
paulson@14272
   485
apply (drule_tac f = "%x. x mod 2" in arg_cong)
paulson@14273
   486
apply (simp add: zmod_zadd1_eq)
paulson@14272
   487
done
paulson@14272
   488
paulson@14273
   489
lemma eq_number_of_BIT_BIT:
paulson@14273
   490
     "((number_of (v BIT x) ::int) = number_of (w BIT y)) =  
paulson@14272
   491
      (x=y & (((number_of v) ::int) = number_of w))"
paulson@15620
   492
apply (simp only: number_of_BIT lemma1 lemma2 eq_commute
obua@14738
   493
               OrderedGroup.add_left_cancel add_assoc OrderedGroup.add_0
paulson@15620
   494
            split add: bit.split) 
paulson@15620
   495
apply simp
paulson@15620
   496
done
paulson@14272
   497
paulson@14273
   498
lemma eq_number_of_BIT_Pls:
paulson@15013
   499
     "((number_of (v BIT x) ::int) = Numeral0) =  
paulson@15620
   500
      (x=bit.B0 & (((number_of v) ::int) = Numeral0))"
paulson@14272
   501
apply (simp only: simp_thms  add: number_of_BIT number_of_Pls eq_commute
paulson@15620
   502
            split add: bit.split cong: imp_cong)
paulson@14273
   503
apply (rule_tac x = "number_of v" in spec, safe)
paulson@14272
   504
apply (simp_all (no_asm_use))
paulson@14272
   505
apply (drule_tac f = "%x. x mod 2" in arg_cong)
paulson@14273
   506
apply (simp add: zmod_zadd1_eq)
paulson@14272
   507
done
paulson@14272
   508
paulson@14273
   509
lemma eq_number_of_BIT_Min:
paulson@15013
   510
     "((number_of (v BIT x) ::int) = number_of Numeral.Min) =  
paulson@15620
   511
      (x=bit.B1 & (((number_of v) ::int) = number_of Numeral.Min))"
paulson@14272
   512
apply (simp only: simp_thms  add: number_of_BIT number_of_Min eq_commute
paulson@15620
   513
            split add: bit.split cong: imp_cong)
paulson@14273
   514
apply (rule_tac x = "number_of v" in spec, auto)
paulson@14273
   515
apply (drule_tac f = "%x. x mod 2" in arg_cong, auto)
paulson@14272
   516
done
paulson@14272
   517
paulson@15013
   518
lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Numeral.Min"
paulson@14273
   519
by auto
paulson@14272
   520
paulson@14272
   521
paulson@14272
   522
paulson@14390
   523
subsection{*Literal arithmetic involving powers*}
paulson@14272
   524
paulson@14272
   525
lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
paulson@15251
   526
apply (induct "n")
paulson@14272
   527
apply (simp_all (no_asm_simp) add: nat_mult_distrib)
paulson@14272
   528
done
paulson@14272
   529
paulson@14273
   530
lemma power_nat_number_of:
paulson@14273
   531
     "(number_of v :: nat) ^ n =  
paulson@14378
   532
       (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
paulson@14272
   533
by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
paulson@14272
   534
         split add: split_if cong: imp_cong)
paulson@14272
   535
paulson@14272
   536
paulson@14272
   537
declare power_nat_number_of [of _ "number_of w", standard, simp]
paulson@14272
   538
paulson@14272
   539
paulson@14390
   540
text{*For the integers*}
paulson@14272
   541
paulson@14273
   542
lemma zpower_number_of_even:
paulson@15620
   543
     "(z::int) ^ number_of (w BIT bit.B0) =  
paulson@14272
   544
      (let w = z ^ (number_of w) in  w*w)"
paulson@14272
   545
apply (simp del: nat_number_of  add: nat_number_of_def number_of_BIT Let_def)
paulson@14272
   546
apply (simp only: number_of_add) 
paulson@14273
   547
apply (rule_tac x = "number_of w" in spec, clarify)
paulson@14272
   548
apply (case_tac " (0::int) <= x")
paulson@14443
   549
apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
paulson@14272
   550
done
paulson@14272
   551
paulson@14273
   552
lemma zpower_number_of_odd:
paulson@15620
   553
     "(z::int) ^ number_of (w BIT bit.B1) =  
paulson@14272
   554
          (if (0::int) <= number_of w                    
paulson@14272
   555
           then (let w = z ^ (number_of w) in  z*w*w)    
paulson@14272
   556
           else 1)"
paulson@14272
   557
apply (simp del: nat_number_of  add: nat_number_of_def number_of_BIT Let_def)
paulson@14387
   558
apply (simp only: number_of_add nat_numeral_1_eq_1 not_neg_eq_ge_0 neg_eq_less_0) 
paulson@14273
   559
apply (rule_tac x = "number_of w" in spec, clarify)
paulson@14443
   560
apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat)
paulson@14272
   561
done
paulson@14272
   562
paulson@14272
   563
declare zpower_number_of_even [of "number_of v", standard, simp]
paulson@14272
   564
declare zpower_number_of_odd  [of "number_of v", standard, simp]
paulson@14272
   565
paulson@14272
   566
paulson@14272
   567
paulson@14272
   568
ML
paulson@14272
   569
{*
paulson@14272
   570
val numerals = thms"numerals";
paulson@14272
   571
val numeral_ss = simpset() addsimps numerals;
paulson@14272
   572
paulson@14272
   573
val nat_bin_arith_setup =
paulson@14272
   574
 [Fast_Arith.map_data 
nipkow@15921
   575
   (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
paulson@14272
   576
     {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
paulson@14272
   577
      inj_thms = inj_thms,
nipkow@15921
   578
      lessD = lessD, neqE = neqE,
paulson@14272
   579
      simpset = simpset addsimps [Suc_nat_number_of, int_nat_number_of,
paulson@14272
   580
                                  not_neg_number_of_Pls,
paulson@14272
   581
                                  neg_number_of_Min,neg_number_of_BIT]})]
paulson@14272
   582
*}
paulson@14272
   583
wenzelm@12838
   584
setup nat_bin_arith_setup
wenzelm@12838
   585
nipkow@13189
   586
(* Enable arith to deal with div/mod k where k is a numeral: *)
nipkow@13189
   587
declare split_div[of _ _ "number_of k", standard, arith_split]
nipkow@13189
   588
declare split_mod[of _ _ "number_of k", standard, arith_split]
nipkow@13154
   589
paulson@15013
   590
lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
wenzelm@12838
   591
  by (simp add: number_of_Pls nat_number_of_def)
wenzelm@12838
   592
paulson@15013
   593
lemma nat_number_of_Min: "number_of Numeral.Min = (0::nat)"
wenzelm@12838
   594
  apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
wenzelm@12838
   595
  apply (simp add: neg_nat)
wenzelm@12838
   596
  done
paulson@7032
   597
paulson@15620
   598
lemma nat_number_of_BIT_1:
paulson@15620
   599
  "number_of (w BIT bit.B1) =
paulson@14378
   600
    (if neg (number_of w :: int) then 0
wenzelm@12838
   601
     else let n = number_of w in Suc (n + n))"
wenzelm@12838
   602
  apply (simp only: nat_number_of_def Let_def split: split_if)
wenzelm@12838
   603
  apply (intro conjI impI)
wenzelm@12838
   604
   apply (simp add: neg_nat neg_number_of_BIT)
wenzelm@12838
   605
  apply (rule int_int_eq [THEN iffD1])
wenzelm@12838
   606
  apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
paulson@15620
   607
  apply (simp only: number_of_BIT zadd_assoc split: bit.split)
paulson@15620
   608
  apply simp
wenzelm@12838
   609
  done
paulson@7032
   610
paulson@15620
   611
lemma nat_number_of_BIT_0:
paulson@15620
   612
    "number_of (w BIT bit.B0) = (let n::nat = number_of w in n + n)"
wenzelm@12838
   613
  apply (simp only: nat_number_of_def Let_def)
paulson@14378
   614
  apply (cases "neg (number_of w :: int)")
wenzelm@12838
   615
   apply (simp add: neg_nat neg_number_of_BIT)
wenzelm@12838
   616
  apply (rule int_int_eq [THEN iffD1])
wenzelm@12838
   617
  apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
paulson@15620
   618
  apply (simp only: number_of_BIT zadd_assoc)
paulson@15620
   619
  apply simp
wenzelm@12838
   620
  done
wenzelm@12838
   621
wenzelm@13043
   622
lemmas nat_number =
wenzelm@12838
   623
  nat_number_of_Pls nat_number_of_Min
paulson@15620
   624
  nat_number_of_BIT_1 nat_number_of_BIT_0
wenzelm@12838
   625
wenzelm@12838
   626
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
wenzelm@12838
   627
  by (simp add: Let_def)
nipkow@10574
   628
paulson@15003
   629
lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})"
paulson@14443
   630
by (simp add: power_mult); 
paulson@14443
   631
paulson@15003
   632
lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})"
paulson@14443
   633
by (simp add: power_mult power_Suc); 
paulson@14443
   634
berghofe@12440
   635
paulson@14390
   636
subsection{*Literal arithmetic and @{term of_nat}*}
paulson@14390
   637
paulson@14390
   638
lemma of_nat_double:
paulson@14390
   639
     "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
paulson@14390
   640
by (simp only: mult_2 nat_add_distrib of_nat_add) 
paulson@14390
   641
paulson@14390
   642
lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
paulson@14390
   643
by (simp only:  nat_number_of_def, simp)
paulson@14390
   644
paulson@14390
   645
lemma of_nat_number_of_lemma:
paulson@14390
   646
     "of_nat (number_of v :: nat) =  
paulson@14390
   647
         (if 0 \<le> (number_of v :: int) 
paulson@14390
   648
          then (number_of v :: 'a :: number_ring)
paulson@14390
   649
          else 0)"
paulson@15013
   650
by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
paulson@14390
   651
paulson@14390
   652
lemma of_nat_number_of_eq [simp]:
paulson@14390
   653
     "of_nat (number_of v :: nat) =  
paulson@14390
   654
         (if neg (number_of v :: int) then 0  
paulson@14390
   655
          else (number_of v :: 'a :: number_ring))"
paulson@14390
   656
by (simp only: of_nat_number_of_lemma neg_def, simp) 
paulson@14390
   657
paulson@14390
   658
paulson@14273
   659
subsection {*Lemmas for the Combination and Cancellation Simprocs*}
paulson@14273
   660
paulson@14273
   661
lemma nat_number_of_add_left:
paulson@14273
   662
     "number_of v + (number_of v' + (k::nat)) =  
paulson@14378
   663
         (if neg (number_of v :: int) then number_of v' + k  
paulson@14378
   664
          else if neg (number_of v' :: int) then number_of v + k  
paulson@14273
   665
          else number_of (bin_add v v') + k)"
paulson@14390
   666
by simp
paulson@14273
   667
paulson@14430
   668
lemma nat_number_of_mult_left:
paulson@14430
   669
     "number_of v * (number_of v' * (k::nat)) =  
paulson@14430
   670
         (if neg (number_of v :: int) then 0
paulson@14430
   671
          else number_of (bin_mult v v') * k)"
paulson@14430
   672
by simp
paulson@14430
   673
paulson@14273
   674
paulson@14390
   675
subsubsection{*For @{text combine_numerals}*}
paulson@14273
   676
paulson@14273
   677
lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
paulson@14273
   678
by (simp add: add_mult_distrib)
paulson@14273
   679
paulson@14273
   680
paulson@14390
   681
subsubsection{*For @{text cancel_numerals}*}
paulson@14273
   682
paulson@14273
   683
lemma nat_diff_add_eq1:
paulson@14273
   684
     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
paulson@14273
   685
by (simp split add: nat_diff_split add: add_mult_distrib)
paulson@14273
   686
paulson@14273
   687
lemma nat_diff_add_eq2:
paulson@14273
   688
     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
paulson@14273
   689
by (simp split add: nat_diff_split add: add_mult_distrib)
paulson@14273
   690
paulson@14273
   691
lemma nat_eq_add_iff1:
paulson@14273
   692
     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
paulson@14273
   693
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   694
paulson@14273
   695
lemma nat_eq_add_iff2:
paulson@14273
   696
     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
paulson@14273
   697
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   698
paulson@14273
   699
lemma nat_less_add_iff1:
paulson@14273
   700
     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
paulson@14273
   701
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   702
paulson@14273
   703
lemma nat_less_add_iff2:
paulson@14273
   704
     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
paulson@14273
   705
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   706
paulson@14273
   707
lemma nat_le_add_iff1:
paulson@14273
   708
     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
paulson@14273
   709
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   710
paulson@14273
   711
lemma nat_le_add_iff2:
paulson@14273
   712
     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
paulson@14273
   713
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   714
paulson@14273
   715
paulson@14390
   716
subsubsection{*For @{text cancel_numeral_factors} *}
paulson@14273
   717
paulson@14273
   718
lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
paulson@14273
   719
by auto
paulson@14273
   720
paulson@14273
   721
lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
paulson@14273
   722
by auto
paulson@14273
   723
paulson@14273
   724
lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
paulson@14273
   725
by auto
paulson@14273
   726
paulson@14273
   727
lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
paulson@14273
   728
by auto
paulson@14273
   729
paulson@14273
   730
paulson@14390
   731
subsubsection{*For @{text cancel_factor} *}
paulson@14273
   732
paulson@14273
   733
lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
paulson@14273
   734
by auto
paulson@14273
   735
paulson@14273
   736
lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
paulson@14273
   737
by auto
paulson@14273
   738
paulson@14273
   739
lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
paulson@14273
   740
by auto
paulson@14273
   741
paulson@14273
   742
lemma nat_mult_div_cancel_disj:
paulson@14273
   743
     "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
paulson@14273
   744
by (simp add: nat_mult_div_cancel1)
paulson@14273
   745
paulson@14353
   746
paulson@14273
   747
ML
paulson@14273
   748
{*
paulson@14353
   749
val eq_nat_nat_iff = thm"eq_nat_nat_iff";
paulson@14353
   750
val eq_nat_number_of = thm"eq_nat_number_of";
paulson@14353
   751
val less_nat_number_of = thm"less_nat_number_of";
paulson@14353
   752
val power2_eq_square = thm "power2_eq_square";
paulson@14353
   753
val zero_le_power2 = thm "zero_le_power2";
paulson@14353
   754
val zero_less_power2 = thm "zero_less_power2";
paulson@14353
   755
val zero_eq_power2 = thm "zero_eq_power2";
paulson@14353
   756
val abs_power2 = thm "abs_power2";
paulson@14353
   757
val power2_abs = thm "power2_abs";
paulson@14353
   758
val power2_minus = thm "power2_minus";
paulson@14353
   759
val power_minus1_even = thm "power_minus1_even";
paulson@14353
   760
val power_minus_even = thm "power_minus_even";
paulson@14353
   761
val zero_le_even_power = thm "zero_le_even_power";
paulson@14353
   762
val odd_power_less_zero = thm "odd_power_less_zero";
paulson@14353
   763
val odd_0_le_power_imp_0_le = thm "odd_0_le_power_imp_0_le";
paulson@14353
   764
paulson@14353
   765
val Suc_pred' = thm"Suc_pred'";
paulson@14353
   766
val expand_Suc = thm"expand_Suc";
paulson@14353
   767
val Suc_eq_add_numeral_1 = thm"Suc_eq_add_numeral_1";
paulson@14467
   768
val Suc_eq_add_numeral_1_left = thm"Suc_eq_add_numeral_1_left";
paulson@14353
   769
val add_eq_if = thm"add_eq_if";
paulson@14353
   770
val mult_eq_if = thm"mult_eq_if";
paulson@14353
   771
val power_eq_if = thm"power_eq_if";
paulson@14353
   772
val eq_number_of_0 = thm"eq_number_of_0";
paulson@14353
   773
val eq_0_number_of = thm"eq_0_number_of";
paulson@14353
   774
val less_0_number_of = thm"less_0_number_of";
paulson@14353
   775
val neg_imp_number_of_eq_0 = thm"neg_imp_number_of_eq_0";
paulson@14353
   776
val eq_number_of_Suc = thm"eq_number_of_Suc";
paulson@14353
   777
val Suc_eq_number_of = thm"Suc_eq_number_of";
paulson@14353
   778
val less_number_of_Suc = thm"less_number_of_Suc";
paulson@14353
   779
val less_Suc_number_of = thm"less_Suc_number_of";
paulson@14353
   780
val le_number_of_Suc = thm"le_number_of_Suc";
paulson@14353
   781
val le_Suc_number_of = thm"le_Suc_number_of";
paulson@14353
   782
val eq_number_of_BIT_BIT = thm"eq_number_of_BIT_BIT";
paulson@14353
   783
val eq_number_of_BIT_Pls = thm"eq_number_of_BIT_Pls";
paulson@14353
   784
val eq_number_of_BIT_Min = thm"eq_number_of_BIT_Min";
paulson@14353
   785
val eq_number_of_Pls_Min = thm"eq_number_of_Pls_Min";
paulson@14390
   786
val of_nat_number_of_eq = thm"of_nat_number_of_eq";
paulson@14353
   787
val nat_power_eq = thm"nat_power_eq";
paulson@14353
   788
val power_nat_number_of = thm"power_nat_number_of";
paulson@14353
   789
val zpower_number_of_even = thm"zpower_number_of_even";
paulson@14353
   790
val zpower_number_of_odd = thm"zpower_number_of_odd";
paulson@14353
   791
val nat_number_of_Pls = thm"nat_number_of_Pls";
paulson@14353
   792
val nat_number_of_Min = thm"nat_number_of_Min";
paulson@14353
   793
val Let_Suc = thm"Let_Suc";
paulson@14353
   794
paulson@14353
   795
val nat_number = thms"nat_number";
paulson@14353
   796
paulson@14273
   797
val nat_number_of_add_left = thm"nat_number_of_add_left";
paulson@14430
   798
val nat_number_of_mult_left = thm"nat_number_of_mult_left";
paulson@14273
   799
val left_add_mult_distrib = thm"left_add_mult_distrib";
paulson@14273
   800
val nat_diff_add_eq1 = thm"nat_diff_add_eq1";
paulson@14273
   801
val nat_diff_add_eq2 = thm"nat_diff_add_eq2";
paulson@14273
   802
val nat_eq_add_iff1 = thm"nat_eq_add_iff1";
paulson@14273
   803
val nat_eq_add_iff2 = thm"nat_eq_add_iff2";
paulson@14273
   804
val nat_less_add_iff1 = thm"nat_less_add_iff1";
paulson@14273
   805
val nat_less_add_iff2 = thm"nat_less_add_iff2";
paulson@14273
   806
val nat_le_add_iff1 = thm"nat_le_add_iff1";
paulson@14273
   807
val nat_le_add_iff2 = thm"nat_le_add_iff2";
paulson@14273
   808
val nat_mult_le_cancel1 = thm"nat_mult_le_cancel1";
paulson@14273
   809
val nat_mult_less_cancel1 = thm"nat_mult_less_cancel1";
paulson@14273
   810
val nat_mult_eq_cancel1 = thm"nat_mult_eq_cancel1";
paulson@14273
   811
val nat_mult_div_cancel1 = thm"nat_mult_div_cancel1";
paulson@14273
   812
val nat_mult_le_cancel_disj = thm"nat_mult_le_cancel_disj";
paulson@14273
   813
val nat_mult_less_cancel_disj = thm"nat_mult_less_cancel_disj";
paulson@14273
   814
val nat_mult_eq_cancel_disj = thm"nat_mult_eq_cancel_disj";
paulson@14273
   815
val nat_mult_div_cancel_disj = thm"nat_mult_div_cancel_disj";
paulson@14353
   816
paulson@14353
   817
val power_minus_even = thm"power_minus_even";
paulson@14353
   818
val zero_le_even_power = thm"zero_le_even_power";
paulson@14273
   819
*}
paulson@14273
   820
paulson@14273
   821
berghofe@12440
   822
subsection {* Configuration of the code generator *}
berghofe@12440
   823
berghofe@12933
   824
ML {*
berghofe@12933
   825
infix 7 `*;
berghofe@12933
   826
infix 6 `+;
berghofe@12933
   827
berghofe@12933
   828
val op `* = op * : int * int -> int;
berghofe@12933
   829
val op `+ = op + : int * int -> int;
berghofe@12933
   830
val `~ = ~ : int -> int;
berghofe@12933
   831
*}
berghofe@12933
   832
berghofe@12440
   833
types_code
berghofe@12440
   834
  "int" ("int")
berghofe@12440
   835
berghofe@14194
   836
constdefs
berghofe@14194
   837
  int_aux :: "int \<Rightarrow> nat \<Rightarrow> int"
berghofe@14194
   838
  "int_aux i n == (i + int n)"
berghofe@14194
   839
  nat_aux :: "nat \<Rightarrow> int \<Rightarrow> nat"
berghofe@14194
   840
  "nat_aux n i == (n + nat i)"
berghofe@12440
   841
berghofe@14194
   842
lemma [code]:
berghofe@14194
   843
  "int_aux i 0 = i"
berghofe@14194
   844
  "int_aux i (Suc n) = int_aux (i + 1) n" -- {* tail recursive *}
berghofe@14194
   845
  "int n = int_aux 0 n"
berghofe@14194
   846
  by (simp add: int_aux_def)+
berghofe@14194
   847
berghofe@14194
   848
lemma [code]: "nat_aux n i = (if i <= 0 then n else nat_aux (Suc n) (i - 1))"
berghofe@14194
   849
  by (simp add: nat_aux_def Suc_nat_eq_nat_zadd1) -- {* tail recursive *}
berghofe@14194
   850
lemma [code]: "nat i = nat_aux 0 i"
berghofe@14194
   851
  by (simp add: nat_aux_def)
berghofe@12440
   852
berghofe@12440
   853
consts_code
berghofe@12440
   854
  "0" :: "int"                  ("0")
berghofe@12440
   855
  "1" :: "int"                  ("1")
berghofe@12933
   856
  "uminus" :: "int => int"      ("`~")
berghofe@12933
   857
  "op +" :: "int => int => int" ("(_ `+/ _)")
berghofe@12933
   858
  "op *" :: "int => int => int" ("(_ `*/ _)")
nipkow@15440
   859
(* Fails for 0:
berghofe@15129
   860
  "op div" :: "int => int => int" ("(_ div/ _)")
berghofe@15129
   861
  "op mod" :: "int => int => int" ("(_ mod/ _)")
nipkow@15440
   862
*)
paulson@14378
   863
  "op <" :: "int => int => bool" ("(_ </ _)")
paulson@14378
   864
  "op <=" :: "int => int => bool" ("(_ <=/ _)")
berghofe@12440
   865
  "neg"                         ("(_ < 0)")
berghofe@12440
   866
berghofe@14417
   867
ML {*
berghofe@14417
   868
fun number_of_codegen thy gr s b (Const ("Numeral.number_of",
berghofe@14417
   869
      Type ("fun", [_, Type ("IntDef.int", [])])) $ bin) =
paulson@15965
   870
        (SOME (gr, Pretty.str (IntInf.toString (HOLogic.dest_binum bin)))
skalberg@15531
   871
        handle TERM _ => NONE)
berghofe@14417
   872
  | number_of_codegen thy gr s b (Const ("Numeral.number_of",
berghofe@14417
   873
      Type ("fun", [_, Type ("nat", [])])) $ bin) =
skalberg@15531
   874
        SOME (Codegen.invoke_codegen thy s b (gr,
berghofe@14417
   875
          Const ("IntDef.nat", HOLogic.intT --> HOLogic.natT) $
berghofe@14417
   876
            (Const ("Numeral.number_of", HOLogic.binT --> HOLogic.intT) $ bin)))
skalberg@15531
   877
  | number_of_codegen _ _ _ _ _ = NONE;
berghofe@14417
   878
*}
berghofe@14417
   879
berghofe@14417
   880
setup {* [Codegen.add_codegen "number_of_codegen" number_of_codegen] *}
berghofe@14417
   881
paulson@7032
   882
end