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