src/HOL/Int.thy
author huffman
Wed Dec 03 20:45:42 2008 -0800 (2008-12-03)
changeset 28962 f603183f7a5c
parent 28958 74c60b78969c
child 28967 3bdb1eae352c
permissions -rw-r--r--
enable le_bin_simps and less_bin_simps for simplifying inequalities on numerals
haftmann@25919
     1
(*  Title:      Int.thy
haftmann@25919
     2
    ID:         $Id$
haftmann@25919
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
haftmann@25919
     4
                Tobias Nipkow, Florian Haftmann, TU Muenchen
haftmann@25919
     5
    Copyright   1994  University of Cambridge
haftmann@25919
     6
haftmann@25919
     7
*)
haftmann@25919
     8
haftmann@25919
     9
header {* The Integers as Equivalence Classes over Pairs of Natural Numbers *} 
haftmann@25919
    10
haftmann@25919
    11
theory Int
krauss@26748
    12
imports Equiv_Relations Nat Wellfounded
haftmann@25919
    13
uses
haftmann@25919
    14
  ("Tools/numeral.ML")
haftmann@25919
    15
  ("Tools/numeral_syntax.ML")
haftmann@25919
    16
  ("~~/src/Provers/Arith/assoc_fold.ML")
haftmann@25919
    17
  "~~/src/Provers/Arith/cancel_numerals.ML"
haftmann@25919
    18
  "~~/src/Provers/Arith/combine_numerals.ML"
haftmann@28952
    19
  ("Tools/int_arith.ML")
haftmann@25919
    20
begin
haftmann@25919
    21
haftmann@25919
    22
subsection {* The equivalence relation underlying the integers *}
haftmann@25919
    23
haftmann@28661
    24
definition intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set" where
haftmann@28562
    25
  [code del]: "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
haftmann@25919
    26
haftmann@25919
    27
typedef (Integ)
haftmann@25919
    28
  int = "UNIV//intrel"
haftmann@25919
    29
  by (auto simp add: quotient_def)
haftmann@25919
    30
haftmann@25919
    31
instantiation int :: "{zero, one, plus, minus, uminus, times, ord, abs, sgn}"
haftmann@25919
    32
begin
haftmann@25919
    33
haftmann@25919
    34
definition
haftmann@28562
    35
  Zero_int_def [code del]: "0 = Abs_Integ (intrel `` {(0, 0)})"
haftmann@25919
    36
haftmann@25919
    37
definition
haftmann@28562
    38
  One_int_def [code del]: "1 = Abs_Integ (intrel `` {(1, 0)})"
haftmann@25919
    39
haftmann@25919
    40
definition
haftmann@28562
    41
  add_int_def [code del]: "z + w = Abs_Integ
haftmann@25919
    42
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u, v) \<in> Rep_Integ w.
haftmann@25919
    43
      intrel `` {(x + u, y + v)})"
haftmann@25919
    44
haftmann@25919
    45
definition
haftmann@28562
    46
  minus_int_def [code del]:
haftmann@25919
    47
    "- z = Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
haftmann@25919
    48
haftmann@25919
    49
definition
haftmann@28562
    50
  diff_int_def [code del]:  "z - w = z + (-w \<Colon> int)"
haftmann@25919
    51
haftmann@25919
    52
definition
haftmann@28562
    53
  mult_int_def [code del]: "z * w = Abs_Integ
haftmann@25919
    54
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
haftmann@25919
    55
      intrel `` {(x*u + y*v, x*v + y*u)})"
haftmann@25919
    56
haftmann@25919
    57
definition
haftmann@28562
    58
  le_int_def [code del]:
haftmann@25919
    59
   "z \<le> w \<longleftrightarrow> (\<exists>x y u v. x+v \<le> u+y \<and> (x, y) \<in> Rep_Integ z \<and> (u, v) \<in> Rep_Integ w)"
haftmann@25919
    60
haftmann@25919
    61
definition
haftmann@28562
    62
  less_int_def [code del]: "(z\<Colon>int) < w \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
haftmann@25919
    63
haftmann@25919
    64
definition
haftmann@25919
    65
  zabs_def: "\<bar>i\<Colon>int\<bar> = (if i < 0 then - i else i)"
haftmann@25919
    66
haftmann@25919
    67
definition
haftmann@25919
    68
  zsgn_def: "sgn (i\<Colon>int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
haftmann@25919
    69
haftmann@25919
    70
instance ..
haftmann@25919
    71
haftmann@25919
    72
end
haftmann@25919
    73
haftmann@25919
    74
haftmann@25919
    75
subsection{*Construction of the Integers*}
haftmann@25919
    76
haftmann@25919
    77
lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
haftmann@25919
    78
by (simp add: intrel_def)
haftmann@25919
    79
haftmann@25919
    80
lemma equiv_intrel: "equiv UNIV intrel"
haftmann@25919
    81
by (simp add: intrel_def equiv_def refl_def sym_def trans_def)
haftmann@25919
    82
haftmann@25919
    83
text{*Reduces equality of equivalence classes to the @{term intrel} relation:
haftmann@25919
    84
  @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
haftmann@25919
    85
lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
haftmann@25919
    86
haftmann@25919
    87
text{*All equivalence classes belong to set of representatives*}
haftmann@25919
    88
lemma [simp]: "intrel``{(x,y)} \<in> Integ"
haftmann@25919
    89
by (auto simp add: Integ_def intrel_def quotient_def)
haftmann@25919
    90
haftmann@25919
    91
text{*Reduces equality on abstractions to equality on representatives:
haftmann@25919
    92
  @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
haftmann@25919
    93
declare Abs_Integ_inject [simp,noatp]  Abs_Integ_inverse [simp,noatp]
haftmann@25919
    94
haftmann@25919
    95
text{*Case analysis on the representation of an integer as an equivalence
haftmann@25919
    96
      class of pairs of naturals.*}
haftmann@25919
    97
lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
haftmann@25919
    98
     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
haftmann@25919
    99
apply (rule Abs_Integ_cases [of z]) 
haftmann@25919
   100
apply (auto simp add: Integ_def quotient_def) 
haftmann@25919
   101
done
haftmann@25919
   102
haftmann@25919
   103
haftmann@25919
   104
subsection {* Arithmetic Operations *}
haftmann@25919
   105
haftmann@25919
   106
lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
haftmann@25919
   107
proof -
haftmann@25919
   108
  have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
haftmann@25919
   109
    by (simp add: congruent_def) 
haftmann@25919
   110
  thus ?thesis
haftmann@25919
   111
    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
haftmann@25919
   112
qed
haftmann@25919
   113
haftmann@25919
   114
lemma add:
haftmann@25919
   115
     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
haftmann@25919
   116
      Abs_Integ (intrel``{(x+u, y+v)})"
haftmann@25919
   117
proof -
haftmann@25919
   118
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z) 
haftmann@25919
   119
        respects2 intrel"
haftmann@25919
   120
    by (simp add: congruent2_def)
haftmann@25919
   121
  thus ?thesis
haftmann@25919
   122
    by (simp add: add_int_def UN_UN_split_split_eq
haftmann@25919
   123
                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
haftmann@25919
   124
qed
haftmann@25919
   125
haftmann@25919
   126
text{*Congruence property for multiplication*}
haftmann@25919
   127
lemma mult_congruent2:
haftmann@25919
   128
     "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
haftmann@25919
   129
      respects2 intrel"
haftmann@25919
   130
apply (rule equiv_intrel [THEN congruent2_commuteI])
haftmann@25919
   131
 apply (force simp add: mult_ac, clarify) 
haftmann@25919
   132
apply (simp add: congruent_def mult_ac)  
haftmann@25919
   133
apply (rename_tac u v w x y z)
haftmann@25919
   134
apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
haftmann@25919
   135
apply (simp add: mult_ac)
haftmann@25919
   136
apply (simp add: add_mult_distrib [symmetric])
haftmann@25919
   137
done
haftmann@25919
   138
haftmann@25919
   139
lemma mult:
haftmann@25919
   140
     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
haftmann@25919
   141
      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
haftmann@25919
   142
by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
haftmann@25919
   143
              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
haftmann@25919
   144
haftmann@25919
   145
text{*The integers form a @{text comm_ring_1}*}
haftmann@25919
   146
instance int :: comm_ring_1
haftmann@25919
   147
proof
haftmann@25919
   148
  fix i j k :: int
haftmann@25919
   149
  show "(i + j) + k = i + (j + k)"
haftmann@25919
   150
    by (cases i, cases j, cases k) (simp add: add add_assoc)
haftmann@25919
   151
  show "i + j = j + i" 
haftmann@25919
   152
    by (cases i, cases j) (simp add: add_ac add)
haftmann@25919
   153
  show "0 + i = i"
haftmann@25919
   154
    by (cases i) (simp add: Zero_int_def add)
haftmann@25919
   155
  show "- i + i = 0"
haftmann@25919
   156
    by (cases i) (simp add: Zero_int_def minus add)
haftmann@25919
   157
  show "i - j = i + - j"
haftmann@25919
   158
    by (simp add: diff_int_def)
haftmann@25919
   159
  show "(i * j) * k = i * (j * k)"
haftmann@25919
   160
    by (cases i, cases j, cases k) (simp add: mult ring_simps)
haftmann@25919
   161
  show "i * j = j * i"
haftmann@25919
   162
    by (cases i, cases j) (simp add: mult ring_simps)
haftmann@25919
   163
  show "1 * i = i"
haftmann@25919
   164
    by (cases i) (simp add: One_int_def mult)
haftmann@25919
   165
  show "(i + j) * k = i * k + j * k"
haftmann@25919
   166
    by (cases i, cases j, cases k) (simp add: add mult ring_simps)
haftmann@25919
   167
  show "0 \<noteq> (1::int)"
haftmann@25919
   168
    by (simp add: Zero_int_def One_int_def)
haftmann@25919
   169
qed
haftmann@25919
   170
haftmann@25919
   171
lemma int_def: "of_nat m = Abs_Integ (intrel `` {(m, 0)})"
haftmann@25919
   172
by (induct m, simp_all add: Zero_int_def One_int_def add)
haftmann@25919
   173
haftmann@25919
   174
haftmann@25919
   175
subsection {* The @{text "\<le>"} Ordering *}
haftmann@25919
   176
haftmann@25919
   177
lemma le:
haftmann@25919
   178
  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
haftmann@25919
   179
by (force simp add: le_int_def)
haftmann@25919
   180
haftmann@25919
   181
lemma less:
haftmann@25919
   182
  "(Abs_Integ(intrel``{(x,y)}) < Abs_Integ(intrel``{(u,v)})) = (x+v < u+y)"
haftmann@25919
   183
by (simp add: less_int_def le order_less_le)
haftmann@25919
   184
haftmann@25919
   185
instance int :: linorder
haftmann@25919
   186
proof
haftmann@25919
   187
  fix i j k :: int
haftmann@27682
   188
  show antisym: "i \<le> j \<Longrightarrow> j \<le> i \<Longrightarrow> i = j"
haftmann@27682
   189
    by (cases i, cases j) (simp add: le)
haftmann@27682
   190
  show "(i < j) = (i \<le> j \<and> \<not> j \<le> i)"
haftmann@27682
   191
    by (auto simp add: less_int_def dest: antisym) 
haftmann@25919
   192
  show "i \<le> i"
haftmann@25919
   193
    by (cases i) (simp add: le)
haftmann@25919
   194
  show "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k"
haftmann@25919
   195
    by (cases i, cases j, cases k) (simp add: le)
haftmann@25919
   196
  show "i \<le> j \<or> j \<le> i"
haftmann@25919
   197
    by (cases i, cases j) (simp add: le linorder_linear)
haftmann@25919
   198
qed
haftmann@25919
   199
haftmann@25919
   200
instantiation int :: distrib_lattice
haftmann@25919
   201
begin
haftmann@25919
   202
haftmann@25919
   203
definition
haftmann@25919
   204
  "(inf \<Colon> int \<Rightarrow> int \<Rightarrow> int) = min"
haftmann@25919
   205
haftmann@25919
   206
definition
haftmann@25919
   207
  "(sup \<Colon> int \<Rightarrow> int \<Rightarrow> int) = max"
haftmann@25919
   208
haftmann@25919
   209
instance
haftmann@25919
   210
  by intro_classes
haftmann@25919
   211
    (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
haftmann@25919
   212
haftmann@25919
   213
end
haftmann@25919
   214
haftmann@25919
   215
instance int :: pordered_cancel_ab_semigroup_add
haftmann@25919
   216
proof
haftmann@25919
   217
  fix i j k :: int
haftmann@25919
   218
  show "i \<le> j \<Longrightarrow> k + i \<le> k + j"
haftmann@25919
   219
    by (cases i, cases j, cases k) (simp add: le add)
haftmann@25919
   220
qed
haftmann@25919
   221
haftmann@25961
   222
haftmann@25919
   223
text{*Strict Monotonicity of Multiplication*}
haftmann@25919
   224
haftmann@25919
   225
text{*strict, in 1st argument; proof is by induction on k>0*}
haftmann@25919
   226
lemma zmult_zless_mono2_lemma:
haftmann@25919
   227
     "(i::int)<j ==> 0<k ==> of_nat k * i < of_nat k * j"
haftmann@25919
   228
apply (induct "k", simp)
haftmann@25919
   229
apply (simp add: left_distrib)
haftmann@25919
   230
apply (case_tac "k=0")
haftmann@25919
   231
apply (simp_all add: add_strict_mono)
haftmann@25919
   232
done
haftmann@25919
   233
haftmann@25919
   234
lemma zero_le_imp_eq_int: "(0::int) \<le> k ==> \<exists>n. k = of_nat n"
haftmann@25919
   235
apply (cases k)
haftmann@25919
   236
apply (auto simp add: le add int_def Zero_int_def)
haftmann@25919
   237
apply (rule_tac x="x-y" in exI, simp)
haftmann@25919
   238
done
haftmann@25919
   239
haftmann@25919
   240
lemma zero_less_imp_eq_int: "(0::int) < k ==> \<exists>n>0. k = of_nat n"
haftmann@25919
   241
apply (cases k)
haftmann@25919
   242
apply (simp add: less int_def Zero_int_def)
haftmann@25919
   243
apply (rule_tac x="x-y" in exI, simp)
haftmann@25919
   244
done
haftmann@25919
   245
haftmann@25919
   246
lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
haftmann@25919
   247
apply (drule zero_less_imp_eq_int)
haftmann@25919
   248
apply (auto simp add: zmult_zless_mono2_lemma)
haftmann@25919
   249
done
haftmann@25919
   250
haftmann@25919
   251
text{*The integers form an ordered integral domain*}
haftmann@25919
   252
instance int :: ordered_idom
haftmann@25919
   253
proof
haftmann@25919
   254
  fix i j k :: int
haftmann@25919
   255
  show "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
haftmann@25919
   256
    by (rule zmult_zless_mono2)
haftmann@25919
   257
  show "\<bar>i\<bar> = (if i < 0 then -i else i)"
haftmann@25919
   258
    by (simp only: zabs_def)
haftmann@25919
   259
  show "sgn (i\<Colon>int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
haftmann@25919
   260
    by (simp only: zsgn_def)
haftmann@25919
   261
qed
haftmann@25919
   262
haftmann@25961
   263
instance int :: lordered_ring
haftmann@25961
   264
proof  
haftmann@25961
   265
  fix k :: int
haftmann@25961
   266
  show "abs k = sup k (- k)"
haftmann@25961
   267
    by (auto simp add: sup_int_def zabs_def max_def less_minus_self_iff [symmetric])
haftmann@25961
   268
qed
haftmann@25961
   269
haftmann@25919
   270
lemma zless_imp_add1_zle: "w < z \<Longrightarrow> w + (1\<Colon>int) \<le> z"
haftmann@25919
   271
apply (cases w, cases z) 
haftmann@25919
   272
apply (simp add: less le add One_int_def)
haftmann@25919
   273
done
haftmann@25919
   274
haftmann@25919
   275
lemma zless_iff_Suc_zadd:
haftmann@25919
   276
  "(w \<Colon> int) < z \<longleftrightarrow> (\<exists>n. z = w + of_nat (Suc n))"
haftmann@25919
   277
apply (cases z, cases w)
haftmann@25919
   278
apply (auto simp add: less add int_def)
haftmann@25919
   279
apply (rename_tac a b c d) 
haftmann@25919
   280
apply (rule_tac x="a+d - Suc(c+b)" in exI) 
haftmann@25919
   281
apply arith
haftmann@25919
   282
done
haftmann@25919
   283
haftmann@25919
   284
lemmas int_distrib =
haftmann@25919
   285
  left_distrib [of "z1::int" "z2" "w", standard]
haftmann@25919
   286
  right_distrib [of "w::int" "z1" "z2", standard]
haftmann@25919
   287
  left_diff_distrib [of "z1::int" "z2" "w", standard]
haftmann@25919
   288
  right_diff_distrib [of "w::int" "z1" "z2", standard]
haftmann@25919
   289
haftmann@25919
   290
haftmann@25919
   291
subsection {* Embedding of the Integers into any @{text ring_1}: @{text of_int}*}
haftmann@25919
   292
haftmann@25919
   293
context ring_1
haftmann@25919
   294
begin
haftmann@25919
   295
haftmann@25919
   296
definition
haftmann@25919
   297
  of_int :: "int \<Rightarrow> 'a"
haftmann@25919
   298
where
haftmann@28562
   299
  [code del]: "of_int z = contents (\<Union>(i, j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
haftmann@25919
   300
haftmann@25919
   301
lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
haftmann@25919
   302
proof -
haftmann@25919
   303
  have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
haftmann@25919
   304
    by (simp add: congruent_def compare_rls of_nat_add [symmetric]
haftmann@25919
   305
            del: of_nat_add) 
haftmann@25919
   306
  thus ?thesis
haftmann@25919
   307
    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
haftmann@25919
   308
qed
haftmann@25919
   309
haftmann@25919
   310
lemma of_int_0 [simp]: "of_int 0 = 0"
haftmann@25919
   311
  by (simp add: of_int Zero_int_def)
haftmann@25919
   312
haftmann@25919
   313
lemma of_int_1 [simp]: "of_int 1 = 1"
haftmann@25919
   314
  by (simp add: of_int One_int_def)
haftmann@25919
   315
haftmann@25919
   316
lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
haftmann@25919
   317
  by (cases w, cases z, simp add: compare_rls of_int OrderedGroup.compare_rls add)
haftmann@25919
   318
haftmann@25919
   319
lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
haftmann@25919
   320
  by (cases z, simp add: compare_rls of_int minus)
haftmann@25919
   321
haftmann@25919
   322
lemma of_int_diff [simp]: "of_int (w - z) = of_int w - of_int z"
haftmann@25919
   323
  by (simp add: OrderedGroup.diff_minus diff_minus)
haftmann@25919
   324
haftmann@25919
   325
lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
haftmann@25919
   326
apply (cases w, cases z)
haftmann@25919
   327
apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
haftmann@25919
   328
                 mult add_ac of_nat_mult)
haftmann@25919
   329
done
haftmann@25919
   330
haftmann@25919
   331
text{*Collapse nested embeddings*}
haftmann@25919
   332
lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
haftmann@25919
   333
  by (induct n) auto
haftmann@25919
   334
haftmann@25919
   335
end
haftmann@25919
   336
haftmann@25919
   337
context ordered_idom
haftmann@25919
   338
begin
haftmann@25919
   339
haftmann@25919
   340
lemma of_int_le_iff [simp]:
haftmann@25919
   341
  "of_int w \<le> of_int z \<longleftrightarrow> w \<le> z"
haftmann@25919
   342
  by (cases w, cases z, simp add: of_int le minus compare_rls of_nat_add [symmetric] del: of_nat_add)
haftmann@25919
   343
haftmann@25919
   344
text{*Special cases where either operand is zero*}
haftmann@25919
   345
lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified]
haftmann@25919
   346
lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified]
haftmann@25919
   347
haftmann@25919
   348
lemma of_int_less_iff [simp]:
haftmann@25919
   349
  "of_int w < of_int z \<longleftrightarrow> w < z"
haftmann@25919
   350
  by (simp add: not_le [symmetric] linorder_not_le [symmetric])
haftmann@25919
   351
haftmann@25919
   352
text{*Special cases where either operand is zero*}
haftmann@25919
   353
lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified]
haftmann@25919
   354
lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified]
haftmann@25919
   355
haftmann@25919
   356
end
haftmann@25919
   357
haftmann@25919
   358
text{*Class for unital rings with characteristic zero.
haftmann@25919
   359
 Includes non-ordered rings like the complex numbers.*}
haftmann@25919
   360
class ring_char_0 = ring_1 + semiring_char_0
haftmann@25919
   361
begin
haftmann@25919
   362
haftmann@25919
   363
lemma of_int_eq_iff [simp]:
haftmann@25919
   364
   "of_int w = of_int z \<longleftrightarrow> w = z"
haftmann@25919
   365
apply (cases w, cases z, simp add: of_int)
haftmann@25919
   366
apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq)
haftmann@25919
   367
apply (simp only: of_nat_add [symmetric] of_nat_eq_iff)
haftmann@25919
   368
done
haftmann@25919
   369
haftmann@25919
   370
text{*Special cases where either operand is zero*}
haftmann@25919
   371
lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified]
haftmann@25919
   372
lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified]
haftmann@25919
   373
haftmann@25919
   374
end
haftmann@25919
   375
haftmann@25919
   376
text{*Every @{text ordered_idom} has characteristic zero.*}
haftmann@25919
   377
subclass (in ordered_idom) ring_char_0 by intro_locales
haftmann@25919
   378
haftmann@25919
   379
lemma of_int_eq_id [simp]: "of_int = id"
haftmann@25919
   380
proof
haftmann@25919
   381
  fix z show "of_int z = id z"
haftmann@25919
   382
    by (cases z) (simp add: of_int add minus int_def diff_minus)
haftmann@25919
   383
qed
haftmann@25919
   384
haftmann@25919
   385
haftmann@25919
   386
subsection {* Magnitude of an Integer, as a Natural Number: @{text nat} *}
haftmann@25919
   387
haftmann@25919
   388
definition
haftmann@25919
   389
  nat :: "int \<Rightarrow> nat"
haftmann@25919
   390
where
haftmann@28562
   391
  [code del]: "nat z = contents (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
haftmann@25919
   392
haftmann@25919
   393
lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
haftmann@25919
   394
proof -
haftmann@25919
   395
  have "(\<lambda>(x,y). {x-y}) respects intrel"
haftmann@25919
   396
    by (simp add: congruent_def) arith
haftmann@25919
   397
  thus ?thesis
haftmann@25919
   398
    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
haftmann@25919
   399
qed
haftmann@25919
   400
haftmann@25919
   401
lemma nat_int [simp]: "nat (of_nat n) = n"
haftmann@25919
   402
by (simp add: nat int_def)
haftmann@25919
   403
haftmann@25919
   404
lemma nat_zero [simp]: "nat 0 = 0"
haftmann@25919
   405
by (simp add: Zero_int_def nat)
haftmann@25919
   406
haftmann@25919
   407
lemma int_nat_eq [simp]: "of_nat (nat z) = (if 0 \<le> z then z else 0)"
haftmann@25919
   408
by (cases z, simp add: nat le int_def Zero_int_def)
haftmann@25919
   409
haftmann@25919
   410
corollary nat_0_le: "0 \<le> z ==> of_nat (nat z) = z"
haftmann@25919
   411
by simp
haftmann@25919
   412
haftmann@25919
   413
lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
haftmann@25919
   414
by (cases z, simp add: nat le Zero_int_def)
haftmann@25919
   415
haftmann@25919
   416
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
haftmann@25919
   417
apply (cases w, cases z) 
haftmann@25919
   418
apply (simp add: nat le linorder_not_le [symmetric] Zero_int_def, arith)
haftmann@25919
   419
done
haftmann@25919
   420
haftmann@25919
   421
text{*An alternative condition is @{term "0 \<le> w"} *}
haftmann@25919
   422
corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
haftmann@25919
   423
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
haftmann@25919
   424
haftmann@25919
   425
corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
haftmann@25919
   426
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
haftmann@25919
   427
haftmann@25919
   428
lemma zless_nat_conj [simp]: "(nat w < nat z) = (0 < z & w < z)"
haftmann@25919
   429
apply (cases w, cases z) 
haftmann@25919
   430
apply (simp add: nat le Zero_int_def linorder_not_le [symmetric], arith)
haftmann@25919
   431
done
haftmann@25919
   432
haftmann@25919
   433
lemma nonneg_eq_int:
haftmann@25919
   434
  fixes z :: int
haftmann@25919
   435
  assumes "0 \<le> z" and "\<And>m. z = of_nat m \<Longrightarrow> P"
haftmann@25919
   436
  shows P
haftmann@25919
   437
  using assms by (blast dest: nat_0_le sym)
haftmann@25919
   438
haftmann@25919
   439
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = of_nat m else m=0)"
haftmann@25919
   440
by (cases w, simp add: nat le int_def Zero_int_def, arith)
haftmann@25919
   441
haftmann@25919
   442
corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = of_nat m else m=0)"
haftmann@25919
   443
by (simp only: eq_commute [of m] nat_eq_iff)
haftmann@25919
   444
haftmann@25919
   445
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < of_nat m)"
haftmann@25919
   446
apply (cases w)
haftmann@25919
   447
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
haftmann@25919
   448
done
haftmann@25919
   449
haftmann@25919
   450
lemma int_eq_iff: "(of_nat m = z) = (m = nat z & 0 \<le> z)"
haftmann@25919
   451
by (auto simp add: nat_eq_iff2)
haftmann@25919
   452
haftmann@25919
   453
lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
haftmann@25919
   454
by (insert zless_nat_conj [of 0], auto)
haftmann@25919
   455
haftmann@25919
   456
lemma nat_add_distrib:
haftmann@25919
   457
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
haftmann@25919
   458
by (cases z, cases z', simp add: nat add le Zero_int_def)
haftmann@25919
   459
haftmann@25919
   460
lemma nat_diff_distrib:
haftmann@25919
   461
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
haftmann@25919
   462
by (cases z, cases z', 
haftmann@25919
   463
    simp add: nat add minus diff_minus le Zero_int_def)
haftmann@25919
   464
haftmann@25919
   465
lemma nat_zminus_int [simp]: "nat (- (of_nat n)) = 0"
haftmann@25919
   466
by (simp add: int_def minus nat Zero_int_def) 
haftmann@25919
   467
haftmann@25919
   468
lemma zless_nat_eq_int_zless: "(m < nat z) = (of_nat m < z)"
haftmann@25919
   469
by (cases z, simp add: nat less int_def, arith)
haftmann@25919
   470
haftmann@25919
   471
context ring_1
haftmann@25919
   472
begin
haftmann@25919
   473
haftmann@25919
   474
lemma of_nat_nat: "0 \<le> z \<Longrightarrow> of_nat (nat z) = of_int z"
haftmann@25919
   475
  by (cases z rule: eq_Abs_Integ)
haftmann@25919
   476
   (simp add: nat le of_int Zero_int_def of_nat_diff)
haftmann@25919
   477
haftmann@25919
   478
end
haftmann@25919
   479
haftmann@25919
   480
haftmann@25919
   481
subsection{*Lemmas about the Function @{term of_nat} and Orderings*}
haftmann@25919
   482
haftmann@25919
   483
lemma negative_zless_0: "- (of_nat (Suc n)) < (0 \<Colon> int)"
haftmann@25919
   484
by (simp add: order_less_le del: of_nat_Suc)
haftmann@25919
   485
haftmann@25919
   486
lemma negative_zless [iff]: "- (of_nat (Suc n)) < (of_nat m \<Colon> int)"
haftmann@25919
   487
by (rule negative_zless_0 [THEN order_less_le_trans], simp)
haftmann@25919
   488
haftmann@25919
   489
lemma negative_zle_0: "- of_nat n \<le> (0 \<Colon> int)"
haftmann@25919
   490
by (simp add: minus_le_iff)
haftmann@25919
   491
haftmann@25919
   492
lemma negative_zle [iff]: "- of_nat n \<le> (of_nat m \<Colon> int)"
haftmann@25919
   493
by (rule order_trans [OF negative_zle_0 of_nat_0_le_iff])
haftmann@25919
   494
haftmann@25919
   495
lemma not_zle_0_negative [simp]: "~ (0 \<le> - (of_nat (Suc n) \<Colon> int))"
haftmann@25919
   496
by (subst le_minus_iff, simp del: of_nat_Suc)
haftmann@25919
   497
haftmann@25919
   498
lemma int_zle_neg: "((of_nat n \<Colon> int) \<le> - of_nat m) = (n = 0 & m = 0)"
haftmann@25919
   499
by (simp add: int_def le minus Zero_int_def)
haftmann@25919
   500
haftmann@25919
   501
lemma not_int_zless_negative [simp]: "~ ((of_nat n \<Colon> int) < - of_nat m)"
haftmann@25919
   502
by (simp add: linorder_not_less)
haftmann@25919
   503
haftmann@25919
   504
lemma negative_eq_positive [simp]: "((- of_nat n \<Colon> int) = of_nat m) = (n = 0 & m = 0)"
haftmann@25919
   505
by (force simp add: order_eq_iff [of "- of_nat n"] int_zle_neg)
haftmann@25919
   506
haftmann@25919
   507
lemma zle_iff_zadd: "(w\<Colon>int) \<le> z \<longleftrightarrow> (\<exists>n. z = w + of_nat n)"
haftmann@25919
   508
proof -
haftmann@25919
   509
  have "(w \<le> z) = (0 \<le> z - w)"
haftmann@25919
   510
    by (simp only: le_diff_eq add_0_left)
haftmann@25919
   511
  also have "\<dots> = (\<exists>n. z - w = of_nat n)"
haftmann@25919
   512
    by (auto elim: zero_le_imp_eq_int)
haftmann@25919
   513
  also have "\<dots> = (\<exists>n. z = w + of_nat n)"
haftmann@25919
   514
    by (simp only: group_simps)
haftmann@25919
   515
  finally show ?thesis .
haftmann@25919
   516
qed
haftmann@25919
   517
haftmann@25919
   518
lemma zadd_int_left: "of_nat m + (of_nat n + z) = of_nat (m + n) + (z\<Colon>int)"
haftmann@25919
   519
by simp
haftmann@25919
   520
haftmann@25919
   521
lemma int_Suc0_eq_1: "of_nat (Suc 0) = (1\<Colon>int)"
haftmann@25919
   522
by simp
haftmann@25919
   523
haftmann@25919
   524
text{*This version is proved for all ordered rings, not just integers!
haftmann@25919
   525
      It is proved here because attribute @{text arith_split} is not available
haftmann@25919
   526
      in theory @{text Ring_and_Field}.
haftmann@25919
   527
      But is it really better than just rewriting with @{text abs_if}?*}
haftmann@25919
   528
lemma abs_split [arith_split,noatp]:
haftmann@25919
   529
     "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
haftmann@25919
   530
by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
haftmann@25919
   531
haftmann@25919
   532
lemma negD: "(x \<Colon> int) < 0 \<Longrightarrow> \<exists>n. x = - (of_nat (Suc n))"
haftmann@25919
   533
apply (cases x)
haftmann@25919
   534
apply (auto simp add: le minus Zero_int_def int_def order_less_le)
haftmann@25919
   535
apply (rule_tac x="y - Suc x" in exI, arith)
haftmann@25919
   536
done
haftmann@25919
   537
haftmann@25919
   538
haftmann@25919
   539
subsection {* Cases and induction *}
haftmann@25919
   540
haftmann@25919
   541
text{*Now we replace the case analysis rule by a more conventional one:
haftmann@25919
   542
whether an integer is negative or not.*}
haftmann@25919
   543
haftmann@25919
   544
theorem int_cases [cases type: int, case_names nonneg neg]:
haftmann@25919
   545
  "[|!! n. (z \<Colon> int) = of_nat n ==> P;  !! n. z =  - (of_nat (Suc n)) ==> P |] ==> P"
haftmann@25919
   546
apply (cases "z < 0", blast dest!: negD)
haftmann@25919
   547
apply (simp add: linorder_not_less del: of_nat_Suc)
haftmann@25919
   548
apply auto
haftmann@25919
   549
apply (blast dest: nat_0_le [THEN sym])
haftmann@25919
   550
done
haftmann@25919
   551
haftmann@25919
   552
theorem int_induct [induct type: int, case_names nonneg neg]:
haftmann@25919
   553
     "[|!! n. P (of_nat n \<Colon> int);  !!n. P (- (of_nat (Suc n))) |] ==> P z"
haftmann@25919
   554
  by (cases z rule: int_cases) auto
haftmann@25919
   555
haftmann@25919
   556
text{*Contributed by Brian Huffman*}
haftmann@25919
   557
theorem int_diff_cases:
haftmann@25919
   558
  obtains (diff) m n where "(z\<Colon>int) = of_nat m - of_nat n"
haftmann@25919
   559
apply (cases z rule: eq_Abs_Integ)
haftmann@25919
   560
apply (rule_tac m=x and n=y in diff)
haftmann@25919
   561
apply (simp add: int_def diff_def minus add)
haftmann@25919
   562
done
haftmann@25919
   563
haftmann@25919
   564
haftmann@25919
   565
subsection {* Binary representation *}
haftmann@25919
   566
haftmann@25919
   567
text {*
haftmann@25919
   568
  This formalization defines binary arithmetic in terms of the integers
haftmann@25919
   569
  rather than using a datatype. This avoids multiple representations (leading
haftmann@25919
   570
  zeroes, etc.)  See @{text "ZF/Tools/twos-compl.ML"}, function @{text
haftmann@25919
   571
  int_of_binary}, for the numerical interpretation.
haftmann@25919
   572
haftmann@25919
   573
  The representation expects that @{text "(m mod 2)"} is 0 or 1,
haftmann@25919
   574
  even if m is negative;
haftmann@25919
   575
  For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
haftmann@25919
   576
  @{text "-5 = (-3)*2 + 1"}.
haftmann@25919
   577
  
haftmann@25919
   578
  This two's complement binary representation derives from the paper 
haftmann@25919
   579
  "An Efficient Representation of Arithmetic for Term Rewriting" by
haftmann@25919
   580
  Dave Cohen and Phil Watson, Rewriting Techniques and Applications,
haftmann@25919
   581
  Springer LNCS 488 (240-251), 1991.
haftmann@25919
   582
*}
haftmann@25919
   583
huffman@28958
   584
subsubsection {* The constructors @{term Bit0}, @{term Bit1}, @{term Pls} and @{term Min} *}
huffman@28958
   585
haftmann@25919
   586
definition
haftmann@25919
   587
  Pls :: int where
haftmann@28562
   588
  [code del]: "Pls = 0"
haftmann@25919
   589
haftmann@25919
   590
definition
haftmann@25919
   591
  Min :: int where
haftmann@28562
   592
  [code del]: "Min = - 1"
haftmann@25919
   593
haftmann@25919
   594
definition
huffman@26086
   595
  Bit0 :: "int \<Rightarrow> int" where
haftmann@28562
   596
  [code del]: "Bit0 k = k + k"
huffman@26086
   597
huffman@26086
   598
definition
huffman@26086
   599
  Bit1 :: "int \<Rightarrow> int" where
haftmann@28562
   600
  [code del]: "Bit1 k = 1 + k + k"
haftmann@25919
   601
haftmann@25919
   602
class number = type + -- {* for numeric types: nat, int, real, \dots *}
haftmann@25919
   603
  fixes number_of :: "int \<Rightarrow> 'a"
haftmann@25919
   604
haftmann@25919
   605
use "Tools/numeral.ML"
haftmann@25919
   606
haftmann@25919
   607
syntax
haftmann@25919
   608
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
haftmann@25919
   609
haftmann@25919
   610
use "Tools/numeral_syntax.ML"
haftmann@25919
   611
setup NumeralSyntax.setup
haftmann@25919
   612
haftmann@25919
   613
abbreviation
haftmann@25919
   614
  "Numeral0 \<equiv> number_of Pls"
haftmann@25919
   615
haftmann@25919
   616
abbreviation
huffman@26086
   617
  "Numeral1 \<equiv> number_of (Bit1 Pls)"
haftmann@25919
   618
haftmann@25919
   619
lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)"
haftmann@25919
   620
  -- {* Unfold all @{text let}s involving constants *}
haftmann@25919
   621
  unfolding Let_def ..
haftmann@25919
   622
haftmann@25919
   623
definition
haftmann@25919
   624
  succ :: "int \<Rightarrow> int" where
haftmann@28562
   625
  [code del]: "succ k = k + 1"
haftmann@25919
   626
haftmann@25919
   627
definition
haftmann@25919
   628
  pred :: "int \<Rightarrow> int" where
haftmann@28562
   629
  [code del]: "pred k = k - 1"
haftmann@25919
   630
haftmann@25919
   631
lemmas
haftmann@25919
   632
  max_number_of [simp] = max_def
haftmann@25919
   633
    [of "number_of u" "number_of v", standard, simp]
haftmann@25919
   634
and
haftmann@25919
   635
  min_number_of [simp] = min_def 
haftmann@25919
   636
    [of "number_of u" "number_of v", standard, simp]
haftmann@25919
   637
  -- {* unfolding @{text minx} and @{text max} on numerals *}
haftmann@25919
   638
haftmann@25919
   639
lemmas numeral_simps = 
huffman@26086
   640
  succ_def pred_def Pls_def Min_def Bit0_def Bit1_def
haftmann@25919
   641
haftmann@25919
   642
text {* Removal of leading zeroes *}
haftmann@25919
   643
huffman@26086
   644
lemma Bit0_Pls [simp, code post]:
huffman@26086
   645
  "Bit0 Pls = Pls"
haftmann@25919
   646
  unfolding numeral_simps by simp
haftmann@25919
   647
huffman@26086
   648
lemma Bit1_Min [simp, code post]:
huffman@26086
   649
  "Bit1 Min = Min"
haftmann@25919
   650
  unfolding numeral_simps by simp
haftmann@25919
   651
huffman@26075
   652
lemmas normalize_bin_simps =
huffman@26086
   653
  Bit0_Pls Bit1_Min
huffman@26075
   654
haftmann@25919
   655
huffman@28958
   656
subsubsection {* Successor and predecessor functions *}
huffman@28958
   657
huffman@28958
   658
text {* Successor *}
huffman@28958
   659
huffman@28958
   660
lemma succ_Pls:
huffman@26086
   661
  "succ Pls = Bit1 Pls"
haftmann@25919
   662
  unfolding numeral_simps by simp
haftmann@25919
   663
huffman@28958
   664
lemma succ_Min:
haftmann@25919
   665
  "succ Min = Pls"
haftmann@25919
   666
  unfolding numeral_simps by simp
haftmann@25919
   667
huffman@28958
   668
lemma succ_Bit0:
huffman@26086
   669
  "succ (Bit0 k) = Bit1 k"
haftmann@25919
   670
  unfolding numeral_simps by simp
haftmann@25919
   671
huffman@28958
   672
lemma succ_Bit1:
huffman@26086
   673
  "succ (Bit1 k) = Bit0 (succ k)"
haftmann@25919
   674
  unfolding numeral_simps by simp
haftmann@25919
   675
huffman@28958
   676
lemmas succ_bin_simps [simp] =
huffman@26086
   677
  succ_Pls succ_Min succ_Bit0 succ_Bit1
huffman@26075
   678
huffman@28958
   679
text {* Predecessor *}
huffman@28958
   680
huffman@28958
   681
lemma pred_Pls:
haftmann@25919
   682
  "pred Pls = Min"
haftmann@25919
   683
  unfolding numeral_simps by simp
haftmann@25919
   684
huffman@28958
   685
lemma pred_Min:
huffman@26086
   686
  "pred Min = Bit0 Min"
haftmann@25919
   687
  unfolding numeral_simps by simp
haftmann@25919
   688
huffman@28958
   689
lemma pred_Bit0:
huffman@26086
   690
  "pred (Bit0 k) = Bit1 (pred k)"
haftmann@25919
   691
  unfolding numeral_simps by simp 
haftmann@25919
   692
huffman@28958
   693
lemma pred_Bit1:
huffman@26086
   694
  "pred (Bit1 k) = Bit0 k"
huffman@26086
   695
  unfolding numeral_simps by simp
huffman@26086
   696
huffman@28958
   697
lemmas pred_bin_simps [simp] =
huffman@26086
   698
  pred_Pls pred_Min pred_Bit0 pred_Bit1
huffman@26075
   699
huffman@28958
   700
huffman@28958
   701
subsubsection {* Binary arithmetic *}
huffman@28958
   702
huffman@28958
   703
text {* Addition *}
huffman@28958
   704
huffman@28958
   705
lemma add_Pls:
huffman@28958
   706
  "Pls + k = k"
huffman@28958
   707
  unfolding numeral_simps by simp
huffman@28958
   708
huffman@28958
   709
lemma add_Min:
huffman@28958
   710
  "Min + k = pred k"
huffman@28958
   711
  unfolding numeral_simps by simp
huffman@28958
   712
huffman@28958
   713
lemma add_Bit0_Bit0:
huffman@28958
   714
  "(Bit0 k) + (Bit0 l) = Bit0 (k + l)"
huffman@28958
   715
  unfolding numeral_simps by simp
huffman@28958
   716
huffman@28958
   717
lemma add_Bit0_Bit1:
huffman@28958
   718
  "(Bit0 k) + (Bit1 l) = Bit1 (k + l)"
huffman@28958
   719
  unfolding numeral_simps by simp
huffman@28958
   720
huffman@28958
   721
lemma add_Bit1_Bit0:
huffman@28958
   722
  "(Bit1 k) + (Bit0 l) = Bit1 (k + l)"
huffman@28958
   723
  unfolding numeral_simps by simp
huffman@28958
   724
huffman@28958
   725
lemma add_Bit1_Bit1:
huffman@28958
   726
  "(Bit1 k) + (Bit1 l) = Bit0 (k + succ l)"
huffman@28958
   727
  unfolding numeral_simps by simp
huffman@28958
   728
huffman@28958
   729
lemma add_Pls_right:
huffman@28958
   730
  "k + Pls = k"
huffman@28958
   731
  unfolding numeral_simps by simp
huffman@28958
   732
huffman@28958
   733
lemma add_Min_right:
huffman@28958
   734
  "k + Min = pred k"
huffman@28958
   735
  unfolding numeral_simps by simp
huffman@28958
   736
huffman@28958
   737
lemmas add_bin_simps [simp] =
huffman@28958
   738
  add_Pls add_Min add_Pls_right add_Min_right
huffman@28958
   739
  add_Bit0_Bit0 add_Bit0_Bit1 add_Bit1_Bit0 add_Bit1_Bit1
huffman@28958
   740
huffman@28958
   741
text {* Negation *}
huffman@28958
   742
huffman@28958
   743
lemma minus_Pls:
haftmann@25919
   744
  "- Pls = Pls"
huffman@28958
   745
  unfolding numeral_simps by simp
huffman@28958
   746
huffman@28958
   747
lemma minus_Min:
huffman@26086
   748
  "- Min = Bit1 Pls"
huffman@28958
   749
  unfolding numeral_simps by simp
huffman@28958
   750
huffman@28958
   751
lemma minus_Bit0:
huffman@26086
   752
  "- (Bit0 k) = Bit0 (- k)"
huffman@28958
   753
  unfolding numeral_simps by simp
huffman@28958
   754
huffman@28958
   755
lemma minus_Bit1:
huffman@26086
   756
  "- (Bit1 k) = Bit1 (pred (- k))"
huffman@26086
   757
  unfolding numeral_simps by simp
haftmann@25919
   758
huffman@28958
   759
lemmas minus_bin_simps [simp] =
huffman@26086
   760
  minus_Pls minus_Min minus_Bit0 minus_Bit1
huffman@26075
   761
huffman@28958
   762
text {* Subtraction *}
huffman@28958
   763
huffman@28958
   764
lemma diff_Pls:
huffman@28958
   765
  "Pls - k = - k"
huffman@28958
   766
  unfolding numeral_simps by simp
huffman@28958
   767
huffman@28958
   768
lemma diff_Min:
huffman@28958
   769
  "Min - k = pred (- k)"
huffman@28958
   770
  unfolding numeral_simps by simp
huffman@28958
   771
huffman@28958
   772
lemma diff_Bit0_Bit0:
huffman@28958
   773
  "(Bit0 k) - (Bit0 l) = Bit0 (k - l)"
huffman@28958
   774
  unfolding numeral_simps by simp
huffman@28958
   775
huffman@28958
   776
lemma diff_Bit0_Bit1:
huffman@28958
   777
  "(Bit0 k) - (Bit1 l) = Bit1 (pred k - l)"
huffman@28958
   778
  unfolding numeral_simps by simp
huffman@28958
   779
huffman@28958
   780
lemma diff_Bit1_Bit0:
huffman@28958
   781
  "(Bit1 k) - (Bit0 l) = Bit1 (k - l)"
haftmann@25919
   782
  unfolding numeral_simps by simp
haftmann@25919
   783
huffman@28958
   784
lemma diff_Bit1_Bit1:
huffman@28958
   785
  "(Bit1 k) - (Bit1 l) = Bit0 (k - l)"
haftmann@25919
   786
  unfolding numeral_simps by simp
haftmann@25919
   787
huffman@28958
   788
lemma diff_Pls_right:
huffman@28958
   789
  "k - Pls = k"
huffman@28958
   790
  unfolding numeral_simps by simp
huffman@28958
   791
huffman@28958
   792
lemma diff_Min_right:
huffman@28958
   793
  "k - Min = succ k"
haftmann@25919
   794
  unfolding numeral_simps by simp
haftmann@25919
   795
huffman@28958
   796
lemmas diff_bin_simps [simp] =
huffman@28958
   797
  diff_Pls diff_Min diff_Pls_right diff_Min_right
huffman@28958
   798
  diff_Bit0_Bit0 diff_Bit0_Bit1 diff_Bit1_Bit0 diff_Bit1_Bit1
huffman@28958
   799
huffman@28958
   800
text {* Multiplication *}
huffman@28958
   801
huffman@28958
   802
lemma mult_Pls:
huffman@28958
   803
  "Pls * w = Pls"
huffman@26086
   804
  unfolding numeral_simps by simp
haftmann@25919
   805
huffman@28958
   806
lemma mult_Min:
haftmann@25919
   807
  "Min * k = - k"
haftmann@25919
   808
  unfolding numeral_simps by simp
haftmann@25919
   809
huffman@28958
   810
lemma mult_Bit0:
huffman@26086
   811
  "(Bit0 k) * l = Bit0 (k * l)"
huffman@26086
   812
  unfolding numeral_simps int_distrib by simp
haftmann@25919
   813
huffman@28958
   814
lemma mult_Bit1:
huffman@26086
   815
  "(Bit1 k) * l = (Bit0 (k * l)) + l"
huffman@28958
   816
  unfolding numeral_simps int_distrib by simp
huffman@28958
   817
huffman@28958
   818
lemmas mult_bin_simps [simp] =
huffman@26086
   819
  mult_Pls mult_Min mult_Bit0 mult_Bit1
huffman@26075
   820
haftmann@25919
   821
huffman@28958
   822
subsubsection {* Binary comparisons *}
huffman@28958
   823
huffman@28958
   824
text {* Preliminaries *}
huffman@28958
   825
huffman@28958
   826
lemma even_less_0_iff:
huffman@28958
   827
  "a + a < 0 \<longleftrightarrow> a < (0::'a::ordered_idom)"
huffman@28958
   828
proof -
huffman@28958
   829
  have "a + a < 0 \<longleftrightarrow> (1+1)*a < 0" by (simp add: left_distrib)
huffman@28958
   830
  also have "(1+1)*a < 0 \<longleftrightarrow> a < 0"
huffman@28958
   831
    by (simp add: mult_less_0_iff zero_less_two 
huffman@28958
   832
                  order_less_not_sym [OF zero_less_two])
huffman@28958
   833
  finally show ?thesis .
huffman@28958
   834
qed
huffman@28958
   835
huffman@28958
   836
lemma le_imp_0_less: 
huffman@28958
   837
  assumes le: "0 \<le> z"
huffman@28958
   838
  shows "(0::int) < 1 + z"
huffman@28958
   839
proof -
huffman@28958
   840
  have "0 \<le> z" by fact
huffman@28958
   841
  also have "... < z + 1" by (rule less_add_one) 
huffman@28958
   842
  also have "... = 1 + z" by (simp add: add_ac)
huffman@28958
   843
  finally show "0 < 1 + z" .
huffman@28958
   844
qed
huffman@28958
   845
huffman@28958
   846
lemma odd_less_0_iff:
huffman@28958
   847
  "(1 + z + z < 0) = (z < (0::int))"
huffman@28958
   848
proof (cases z rule: int_cases)
huffman@28958
   849
  case (nonneg n)
huffman@28958
   850
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
huffman@28958
   851
                             le_imp_0_less [THEN order_less_imp_le])  
huffman@28958
   852
next
huffman@28958
   853
  case (neg n)
huffman@28958
   854
  thus ?thesis by (simp del: of_nat_Suc of_nat_add
huffman@28958
   855
    add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])
huffman@28958
   856
qed
huffman@28958
   857
huffman@28958
   858
lemma neg_simps:
huffman@28958
   859
  "Pls < 0 \<longleftrightarrow> False"
huffman@28958
   860
  "Min < 0 \<longleftrightarrow> True"
huffman@28958
   861
  "Bit0 w < 0 \<longleftrightarrow> w < 0"
huffman@28958
   862
  "Bit1 w < 0 \<longleftrightarrow> w < 0"
huffman@28958
   863
  unfolding numeral_simps
huffman@28958
   864
  by (simp_all add: even_less_0_iff odd_less_0_iff)
huffman@28958
   865
huffman@28958
   866
lemma less_bin_lemma: "k < l \<longleftrightarrow> k - l < (0::int)"
huffman@28958
   867
  by simp
huffman@28958
   868
huffman@28958
   869
lemma le_iff_pred_less: "k \<le> l \<longleftrightarrow> pred k < l"
huffman@28958
   870
  unfolding numeral_simps
huffman@28958
   871
  proof
huffman@28958
   872
    have "k - 1 < k" by simp
huffman@28958
   873
    also assume "k \<le> l"
huffman@28958
   874
    finally show "k - 1 < l" .
huffman@28958
   875
  next
huffman@28958
   876
    assume "k - 1 < l"
huffman@28958
   877
    hence "(k - 1) + 1 \<le> l" by (rule zless_imp_add1_zle)
huffman@28958
   878
    thus "k \<le> l" by simp
huffman@28958
   879
  qed
huffman@28958
   880
huffman@28958
   881
lemma succ_pred: "succ (pred x) = x"
huffman@28958
   882
  unfolding numeral_simps by simp
huffman@28958
   883
huffman@28958
   884
text {* Less-than *}
huffman@28958
   885
huffman@28958
   886
lemma less_bin_simps [simp]:
huffman@28958
   887
  "Pls < Pls \<longleftrightarrow> False"
huffman@28958
   888
  "Pls < Min \<longleftrightarrow> False"
huffman@28958
   889
  "Pls < Bit0 k \<longleftrightarrow> Pls < k"
huffman@28958
   890
  "Pls < Bit1 k \<longleftrightarrow> Pls \<le> k"
huffman@28958
   891
  "Min < Pls \<longleftrightarrow> True"
huffman@28958
   892
  "Min < Min \<longleftrightarrow> False"
huffman@28958
   893
  "Min < Bit0 k \<longleftrightarrow> Min < k"
huffman@28958
   894
  "Min < Bit1 k \<longleftrightarrow> Min < k"
huffman@28958
   895
  "Bit0 k < Pls \<longleftrightarrow> k < Pls"
huffman@28958
   896
  "Bit0 k < Min \<longleftrightarrow> k \<le> Min"
huffman@28958
   897
  "Bit1 k < Pls \<longleftrightarrow> k < Pls"
huffman@28958
   898
  "Bit1 k < Min \<longleftrightarrow> k < Min"
huffman@28958
   899
  "Bit0 k < Bit0 l \<longleftrightarrow> k < l"
huffman@28958
   900
  "Bit0 k < Bit1 l \<longleftrightarrow> k \<le> l"
huffman@28958
   901
  "Bit1 k < Bit0 l \<longleftrightarrow> k < l"
huffman@28958
   902
  "Bit1 k < Bit1 l \<longleftrightarrow> k < l"
huffman@28958
   903
  unfolding le_iff_pred_less
huffman@28958
   904
    less_bin_lemma [of Pls]
huffman@28958
   905
    less_bin_lemma [of Min]
huffman@28958
   906
    less_bin_lemma [of "k"]
huffman@28958
   907
    less_bin_lemma [of "Bit0 k"]
huffman@28958
   908
    less_bin_lemma [of "Bit1 k"]
huffman@28958
   909
    less_bin_lemma [of "pred Pls"]
huffman@28958
   910
    less_bin_lemma [of "pred k"]
huffman@28958
   911
  by (simp_all add: neg_simps succ_pred)
huffman@28958
   912
huffman@28958
   913
text {* Less-than-or-equal *}
huffman@28958
   914
huffman@28958
   915
lemma le_bin_simps [simp]:
huffman@28958
   916
  "Pls \<le> Pls \<longleftrightarrow> True"
huffman@28958
   917
  "Pls \<le> Min \<longleftrightarrow> False"
huffman@28958
   918
  "Pls \<le> Bit0 k \<longleftrightarrow> Pls \<le> k"
huffman@28958
   919
  "Pls \<le> Bit1 k \<longleftrightarrow> Pls \<le> k"
huffman@28958
   920
  "Min \<le> Pls \<longleftrightarrow> True"
huffman@28958
   921
  "Min \<le> Min \<longleftrightarrow> True"
huffman@28958
   922
  "Min \<le> Bit0 k \<longleftrightarrow> Min < k"
huffman@28958
   923
  "Min \<le> Bit1 k \<longleftrightarrow> Min \<le> k"
huffman@28958
   924
  "Bit0 k \<le> Pls \<longleftrightarrow> k \<le> Pls"
huffman@28958
   925
  "Bit0 k \<le> Min \<longleftrightarrow> k \<le> Min"
huffman@28958
   926
  "Bit1 k \<le> Pls \<longleftrightarrow> k < Pls"
huffman@28958
   927
  "Bit1 k \<le> Min \<longleftrightarrow> k \<le> Min"
huffman@28958
   928
  "Bit0 k \<le> Bit0 l \<longleftrightarrow> k \<le> l"
huffman@28958
   929
  "Bit0 k \<le> Bit1 l \<longleftrightarrow> k \<le> l"
huffman@28958
   930
  "Bit1 k \<le> Bit0 l \<longleftrightarrow> k < l"
huffman@28958
   931
  "Bit1 k \<le> Bit1 l \<longleftrightarrow> k \<le> l"
huffman@28958
   932
  unfolding not_less [symmetric]
huffman@28958
   933
  by (simp_all add: not_le)
huffman@28958
   934
huffman@28958
   935
text {* Equality *}
huffman@28958
   936
huffman@28958
   937
lemma eq_bin_simps [simp]:
huffman@28958
   938
  "Pls = Pls \<longleftrightarrow> True"
huffman@28958
   939
  "Pls = Min \<longleftrightarrow> False"
huffman@28958
   940
  "Pls = Bit0 l \<longleftrightarrow> Pls = l"
huffman@28958
   941
  "Pls = Bit1 l \<longleftrightarrow> False"
huffman@28958
   942
  "Min = Pls \<longleftrightarrow> False"
huffman@28958
   943
  "Min = Min \<longleftrightarrow> True"
huffman@28958
   944
  "Min = Bit0 l \<longleftrightarrow> False"
huffman@28958
   945
  "Min = Bit1 l \<longleftrightarrow> Min = l"
huffman@28958
   946
  "Bit0 k = Pls \<longleftrightarrow> k = Pls"
huffman@28958
   947
  "Bit0 k = Min \<longleftrightarrow> False"
huffman@28958
   948
  "Bit1 k = Pls \<longleftrightarrow> False"
huffman@28958
   949
  "Bit1 k = Min \<longleftrightarrow> k = Min"
huffman@28958
   950
  "Bit0 k = Bit0 l \<longleftrightarrow> k = l"
huffman@28958
   951
  "Bit0 k = Bit1 l \<longleftrightarrow> False"
huffman@28958
   952
  "Bit1 k = Bit0 l \<longleftrightarrow> False"
huffman@28958
   953
  "Bit1 k = Bit1 l \<longleftrightarrow> k = l"
huffman@28958
   954
  unfolding order_eq_iff [where 'a=int]
huffman@28958
   955
  by (simp_all add: not_less)
huffman@28958
   956
huffman@28958
   957
haftmann@25919
   958
subsection {* Converting Numerals to Rings: @{term number_of} *}
haftmann@25919
   959
haftmann@25919
   960
class number_ring = number + comm_ring_1 +
haftmann@25919
   961
  assumes number_of_eq: "number_of k = of_int k"
haftmann@25919
   962
haftmann@25919
   963
text {* self-embedding of the integers *}
haftmann@25919
   964
haftmann@25919
   965
instantiation int :: number_ring
haftmann@25919
   966
begin
haftmann@25919
   967
haftmann@28724
   968
definition int_number_of_def [code del]:
haftmann@28724
   969
  "number_of w = (of_int w \<Colon> int)"
haftmann@25919
   970
haftmann@28724
   971
instance proof
haftmann@28724
   972
qed (simp only: int_number_of_def)
haftmann@25919
   973
haftmann@25919
   974
end
haftmann@25919
   975
haftmann@25919
   976
lemma number_of_is_id:
haftmann@25919
   977
  "number_of (k::int) = k"
haftmann@25919
   978
  unfolding int_number_of_def by simp
haftmann@25919
   979
haftmann@25919
   980
lemma number_of_succ:
haftmann@25919
   981
  "number_of (succ k) = (1 + number_of k ::'a::number_ring)"
haftmann@25919
   982
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
   983
haftmann@25919
   984
lemma number_of_pred:
haftmann@25919
   985
  "number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"
haftmann@25919
   986
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
   987
haftmann@25919
   988
lemma number_of_minus:
haftmann@25919
   989
  "number_of (uminus w) = (- (number_of w)::'a::number_ring)"
huffman@28958
   990
  unfolding number_of_eq by (rule of_int_minus)
haftmann@25919
   991
haftmann@25919
   992
lemma number_of_add:
haftmann@25919
   993
  "number_of (v + w) = (number_of v + number_of w::'a::number_ring)"
huffman@28958
   994
  unfolding number_of_eq by (rule of_int_add)
huffman@28958
   995
huffman@28958
   996
lemma number_of_diff:
huffman@28958
   997
  "number_of (v - w) = (number_of v - number_of w::'a::number_ring)"
huffman@28958
   998
  unfolding number_of_eq by (rule of_int_diff)
haftmann@25919
   999
haftmann@25919
  1000
lemma number_of_mult:
haftmann@25919
  1001
  "number_of (v * w) = (number_of v * number_of w::'a::number_ring)"
huffman@28958
  1002
  unfolding number_of_eq by (rule of_int_mult)
haftmann@25919
  1003
haftmann@25919
  1004
text {*
haftmann@25919
  1005
  The correctness of shifting.
haftmann@25919
  1006
  But it doesn't seem to give a measurable speed-up.
haftmann@25919
  1007
*}
haftmann@25919
  1008
huffman@26086
  1009
lemma double_number_of_Bit0:
huffman@26086
  1010
  "(1 + 1) * number_of w = (number_of (Bit0 w) ::'a::number_ring)"
haftmann@25919
  1011
  unfolding number_of_eq numeral_simps left_distrib by simp
haftmann@25919
  1012
haftmann@25919
  1013
text {*
haftmann@25919
  1014
  Converting numerals 0 and 1 to their abstract versions.
haftmann@25919
  1015
*}
haftmann@25919
  1016
haftmann@25919
  1017
lemma numeral_0_eq_0 [simp]:
haftmann@25919
  1018
  "Numeral0 = (0::'a::number_ring)"
haftmann@25919
  1019
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1020
haftmann@25919
  1021
lemma numeral_1_eq_1 [simp]:
haftmann@25919
  1022
  "Numeral1 = (1::'a::number_ring)"
haftmann@25919
  1023
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1024
haftmann@25919
  1025
text {*
haftmann@25919
  1026
  Special-case simplification for small constants.
haftmann@25919
  1027
*}
haftmann@25919
  1028
haftmann@25919
  1029
text{*
haftmann@25919
  1030
  Unary minus for the abstract constant 1. Cannot be inserted
haftmann@25919
  1031
  as a simprule until later: it is @{text number_of_Min} re-oriented!
haftmann@25919
  1032
*}
haftmann@25919
  1033
haftmann@25919
  1034
lemma numeral_m1_eq_minus_1:
haftmann@25919
  1035
  "(-1::'a::number_ring) = - 1"
haftmann@25919
  1036
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1037
haftmann@25919
  1038
lemma mult_minus1 [simp]:
haftmann@25919
  1039
  "-1 * z = -(z::'a::number_ring)"
haftmann@25919
  1040
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1041
haftmann@25919
  1042
lemma mult_minus1_right [simp]:
haftmann@25919
  1043
  "z * -1 = -(z::'a::number_ring)"
haftmann@25919
  1044
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1045
haftmann@25919
  1046
(*Negation of a coefficient*)
haftmann@25919
  1047
lemma minus_number_of_mult [simp]:
haftmann@25919
  1048
   "- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"
haftmann@25919
  1049
   unfolding number_of_eq by simp
haftmann@25919
  1050
haftmann@25919
  1051
text {* Subtraction *}
haftmann@25919
  1052
haftmann@25919
  1053
lemma diff_number_of_eq:
haftmann@25919
  1054
  "number_of v - number_of w =
haftmann@25919
  1055
    (number_of (v + uminus w)::'a::number_ring)"
haftmann@25919
  1056
  unfolding number_of_eq by simp
haftmann@25919
  1057
haftmann@25919
  1058
lemma number_of_Pls:
haftmann@25919
  1059
  "number_of Pls = (0::'a::number_ring)"
haftmann@25919
  1060
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1061
haftmann@25919
  1062
lemma number_of_Min:
haftmann@25919
  1063
  "number_of Min = (- 1::'a::number_ring)"
haftmann@25919
  1064
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1065
huffman@26086
  1066
lemma number_of_Bit0:
huffman@26086
  1067
  "number_of (Bit0 w) = (0::'a::number_ring) + (number_of w) + (number_of w)"
huffman@26086
  1068
  unfolding number_of_eq numeral_simps by simp
huffman@26086
  1069
huffman@26086
  1070
lemma number_of_Bit1:
huffman@26086
  1071
  "number_of (Bit1 w) = (1::'a::number_ring) + (number_of w) + (number_of w)"
huffman@26086
  1072
  unfolding number_of_eq numeral_simps by simp
haftmann@25919
  1073
haftmann@25919
  1074
huffman@28958
  1075
subsubsection {* Equality of Binary Numbers *}
haftmann@25919
  1076
haftmann@25919
  1077
text {* First version by Norbert Voelker *}
haftmann@25919
  1078
haftmann@25919
  1079
definition
haftmann@25919
  1080
  neg  :: "'a\<Colon>ordered_idom \<Rightarrow> bool"
haftmann@25919
  1081
where
haftmann@25919
  1082
  "neg Z \<longleftrightarrow> Z < 0"
haftmann@25919
  1083
haftmann@25919
  1084
definition (*for simplifying equalities*)
haftmann@25919
  1085
  iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
haftmann@25919
  1086
where
haftmann@25919
  1087
  "iszero z \<longleftrightarrow> z = 0"
haftmann@25919
  1088
haftmann@25919
  1089
lemma not_neg_int [simp]: "~ neg (of_nat n)"
haftmann@25919
  1090
by (simp add: neg_def)
haftmann@25919
  1091
haftmann@25919
  1092
lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
haftmann@25919
  1093
by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
haftmann@25919
  1094
haftmann@25919
  1095
lemmas neg_eq_less_0 = neg_def
haftmann@25919
  1096
haftmann@25919
  1097
lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
haftmann@25919
  1098
by (simp add: neg_def linorder_not_less)
haftmann@25919
  1099
haftmann@25919
  1100
text{*To simplify inequalities when Numeral1 can get simplified to 1*}
haftmann@25919
  1101
haftmann@25919
  1102
lemma not_neg_0: "~ neg 0"
haftmann@25919
  1103
by (simp add: One_int_def neg_def)
haftmann@25919
  1104
haftmann@25919
  1105
lemma not_neg_1: "~ neg 1"
haftmann@25919
  1106
by (simp add: neg_def linorder_not_less zero_le_one)
haftmann@25919
  1107
haftmann@25919
  1108
lemma iszero_0: "iszero 0"
haftmann@25919
  1109
by (simp add: iszero_def)
haftmann@25919
  1110
haftmann@25919
  1111
lemma not_iszero_1: "~ iszero 1"
haftmann@25919
  1112
by (simp add: iszero_def eq_commute)
haftmann@25919
  1113
haftmann@25919
  1114
lemma neg_nat: "neg z ==> nat z = 0"
haftmann@25919
  1115
by (simp add: neg_def order_less_imp_le) 
haftmann@25919
  1116
haftmann@25919
  1117
lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
haftmann@25919
  1118
by (simp add: linorder_not_less neg_def)
haftmann@25919
  1119
haftmann@25919
  1120
lemma eq_number_of_eq:
haftmann@25919
  1121
  "((number_of x::'a::number_ring) = number_of y) =
haftmann@25919
  1122
   iszero (number_of (x + uminus y) :: 'a)"
haftmann@25919
  1123
  unfolding iszero_def number_of_add number_of_minus
haftmann@25919
  1124
  by (simp add: compare_rls)
haftmann@25919
  1125
haftmann@25919
  1126
lemma iszero_number_of_Pls:
haftmann@25919
  1127
  "iszero ((number_of Pls)::'a::number_ring)"
haftmann@25919
  1128
  unfolding iszero_def numeral_0_eq_0 ..
haftmann@25919
  1129
haftmann@25919
  1130
lemma nonzero_number_of_Min:
haftmann@25919
  1131
  "~ iszero ((number_of Min)::'a::number_ring)"
haftmann@25919
  1132
  unfolding iszero_def numeral_m1_eq_minus_1 by simp
haftmann@25919
  1133
haftmann@25919
  1134
huffman@28958
  1135
subsubsection {* Comparisons, for Ordered Rings *}
haftmann@25919
  1136
haftmann@25919
  1137
lemmas double_eq_0_iff = double_zero
haftmann@25919
  1138
haftmann@25919
  1139
lemma odd_nonzero:
haftmann@25919
  1140
  "1 + z + z \<noteq> (0::int)";
haftmann@25919
  1141
proof (cases z rule: int_cases)
haftmann@25919
  1142
  case (nonneg n)
haftmann@25919
  1143
  have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
haftmann@25919
  1144
  thus ?thesis using  le_imp_0_less [OF le]
haftmann@25919
  1145
    by (auto simp add: add_assoc) 
haftmann@25919
  1146
next
haftmann@25919
  1147
  case (neg n)
haftmann@25919
  1148
  show ?thesis
haftmann@25919
  1149
  proof
haftmann@25919
  1150
    assume eq: "1 + z + z = 0"
haftmann@25919
  1151
    have "(0::int) < 1 + (of_nat n + of_nat n)"
haftmann@25919
  1152
      by (simp add: le_imp_0_less add_increasing) 
haftmann@25919
  1153
    also have "... = - (1 + z + z)" 
haftmann@25919
  1154
      by (simp add: neg add_assoc [symmetric]) 
haftmann@25919
  1155
    also have "... = 0" by (simp add: eq) 
haftmann@25919
  1156
    finally have "0<0" ..
haftmann@25919
  1157
    thus False by blast
haftmann@25919
  1158
  qed
haftmann@25919
  1159
qed
haftmann@25919
  1160
huffman@26086
  1161
lemma iszero_number_of_Bit0:
huffman@26086
  1162
  "iszero (number_of (Bit0 w)::'a) = 
huffman@26086
  1163
   iszero (number_of w::'a::{ring_char_0,number_ring})"
haftmann@25919
  1164
proof -
haftmann@25919
  1165
  have "(of_int w + of_int w = (0::'a)) \<Longrightarrow> (w = 0)"
haftmann@25919
  1166
  proof -
haftmann@25919
  1167
    assume eq: "of_int w + of_int w = (0::'a)"
haftmann@25919
  1168
    then have "of_int (w + w) = (of_int 0 :: 'a)" by simp
haftmann@25919
  1169
    then have "w + w = 0" by (simp only: of_int_eq_iff)
haftmann@25919
  1170
    then show "w = 0" by (simp only: double_eq_0_iff)
haftmann@25919
  1171
  qed
huffman@26086
  1172
  thus ?thesis
huffman@26086
  1173
    by (auto simp add: iszero_def number_of_eq numeral_simps)
huffman@26086
  1174
qed
huffman@26086
  1175
huffman@26086
  1176
lemma iszero_number_of_Bit1:
huffman@26086
  1177
  "~ iszero (number_of (Bit1 w)::'a::{ring_char_0,number_ring})"
huffman@26086
  1178
proof -
huffman@26086
  1179
  have "1 + of_int w + of_int w \<noteq> (0::'a)"
haftmann@25919
  1180
  proof
haftmann@25919
  1181
    assume eq: "1 + of_int w + of_int w = (0::'a)"
haftmann@25919
  1182
    hence "of_int (1 + w + w) = (of_int 0 :: 'a)" by simp 
haftmann@25919
  1183
    hence "1 + w + w = 0" by (simp only: of_int_eq_iff)
haftmann@25919
  1184
    with odd_nonzero show False by blast
haftmann@25919
  1185
  qed
huffman@26086
  1186
  thus ?thesis
huffman@26086
  1187
    by (auto simp add: iszero_def number_of_eq numeral_simps)
haftmann@25919
  1188
qed
haftmann@25919
  1189
haftmann@25919
  1190
huffman@28958
  1191
subsubsection {* The Less-Than Relation *}
haftmann@25919
  1192
haftmann@25919
  1193
lemma less_number_of_eq_neg:
haftmann@25919
  1194
  "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
haftmann@25919
  1195
  = neg (number_of (x + uminus y) :: 'a)"
haftmann@25919
  1196
apply (subst less_iff_diff_less_0) 
haftmann@25919
  1197
apply (simp add: neg_def diff_minus number_of_add number_of_minus)
haftmann@25919
  1198
done
haftmann@25919
  1199
haftmann@25919
  1200
text {*
haftmann@25919
  1201
  If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
haftmann@25919
  1202
  @{term Numeral0} IS @{term "number_of Pls"}
haftmann@25919
  1203
*}
haftmann@25919
  1204
haftmann@25919
  1205
lemma not_neg_number_of_Pls:
haftmann@25919
  1206
  "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
haftmann@25919
  1207
  by (simp add: neg_def numeral_0_eq_0)
haftmann@25919
  1208
haftmann@25919
  1209
lemma neg_number_of_Min:
haftmann@25919
  1210
  "neg (number_of Min ::'a::{ordered_idom,number_ring})"
haftmann@25919
  1211
  by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
haftmann@25919
  1212
haftmann@25919
  1213
lemma double_less_0_iff:
haftmann@25919
  1214
  "(a + a < 0) = (a < (0::'a::ordered_idom))"
haftmann@25919
  1215
proof -
haftmann@25919
  1216
  have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
haftmann@25919
  1217
  also have "... = (a < 0)"
haftmann@25919
  1218
    by (simp add: mult_less_0_iff zero_less_two 
haftmann@25919
  1219
                  order_less_not_sym [OF zero_less_two]) 
haftmann@25919
  1220
  finally show ?thesis .
haftmann@25919
  1221
qed
haftmann@25919
  1222
haftmann@25919
  1223
lemma odd_less_0:
haftmann@25919
  1224
  "(1 + z + z < 0) = (z < (0::int))";
haftmann@25919
  1225
proof (cases z rule: int_cases)
haftmann@25919
  1226
  case (nonneg n)
haftmann@25919
  1227
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
haftmann@25919
  1228
                             le_imp_0_less [THEN order_less_imp_le])  
haftmann@25919
  1229
next
haftmann@25919
  1230
  case (neg n)
haftmann@25919
  1231
  thus ?thesis by (simp del: of_nat_Suc of_nat_add
haftmann@25919
  1232
    add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])
haftmann@25919
  1233
qed
haftmann@25919
  1234
huffman@26086
  1235
lemma neg_number_of_Bit0:
huffman@26086
  1236
  "neg (number_of (Bit0 w)::'a) = 
huffman@26086
  1237
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
huffman@26086
  1238
by (simp add: neg_def number_of_eq numeral_simps double_less_0_iff)
huffman@26086
  1239
huffman@26086
  1240
lemma neg_number_of_Bit1:
huffman@26086
  1241
  "neg (number_of (Bit1 w)::'a) = 
haftmann@25919
  1242
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
haftmann@25919
  1243
proof -
haftmann@25919
  1244
  have "((1::'a) + of_int w + of_int w < 0) = (of_int (1 + w + w) < (of_int 0 :: 'a))"
haftmann@25919
  1245
    by simp
haftmann@25919
  1246
  also have "... = (w < 0)" by (simp only: of_int_less_iff odd_less_0)
haftmann@25919
  1247
  finally show ?thesis
huffman@26086
  1248
  by (simp add: neg_def number_of_eq numeral_simps)
haftmann@25919
  1249
qed
haftmann@25919
  1250
haftmann@25919
  1251
haftmann@25919
  1252
text {* Less-Than or Equals *}
haftmann@25919
  1253
haftmann@25919
  1254
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
haftmann@25919
  1255
haftmann@25919
  1256
lemmas le_number_of_eq_not_less =
haftmann@25919
  1257
  linorder_not_less [of "number_of w" "number_of v", symmetric, 
haftmann@25919
  1258
  standard]
haftmann@25919
  1259
haftmann@25919
  1260
lemma le_number_of_eq:
haftmann@25919
  1261
    "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
haftmann@25919
  1262
     = (~ (neg (number_of (y + uminus x) :: 'a)))"
haftmann@25919
  1263
by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
haftmann@25919
  1264
haftmann@25919
  1265
haftmann@25919
  1266
text {* Absolute value (@{term abs}) *}
haftmann@25919
  1267
haftmann@25919
  1268
lemma abs_number_of:
haftmann@25919
  1269
  "abs(number_of x::'a::{ordered_idom,number_ring}) =
haftmann@25919
  1270
   (if number_of x < (0::'a) then -number_of x else number_of x)"
haftmann@25919
  1271
  by (simp add: abs_if)
haftmann@25919
  1272
haftmann@25919
  1273
haftmann@25919
  1274
text {* Re-orientation of the equation nnn=x *}
haftmann@25919
  1275
haftmann@25919
  1276
lemma number_of_reorient:
haftmann@25919
  1277
  "(number_of w = x) = (x = number_of w)"
haftmann@25919
  1278
  by auto
haftmann@25919
  1279
haftmann@25919
  1280
huffman@28958
  1281
subsubsection {* Simplification of arithmetic operations on integer constants. *}
haftmann@25919
  1282
haftmann@25919
  1283
lemmas arith_extra_simps [standard, simp] =
haftmann@25919
  1284
  number_of_add [symmetric]
huffman@28958
  1285
  number_of_minus [symmetric]
huffman@28958
  1286
  numeral_m1_eq_minus_1 [symmetric]
haftmann@25919
  1287
  number_of_mult [symmetric]
haftmann@25919
  1288
  diff_number_of_eq abs_number_of 
haftmann@25919
  1289
haftmann@25919
  1290
text {*
haftmann@25919
  1291
  For making a minimal simpset, one must include these default simprules.
haftmann@25919
  1292
  Also include @{text simp_thms}.
haftmann@25919
  1293
*}
haftmann@25919
  1294
haftmann@25919
  1295
lemmas arith_simps = 
huffman@26075
  1296
  normalize_bin_simps pred_bin_simps succ_bin_simps
huffman@26075
  1297
  add_bin_simps minus_bin_simps mult_bin_simps
haftmann@25919
  1298
  abs_zero abs_one arith_extra_simps
haftmann@25919
  1299
haftmann@25919
  1300
text {* Simplification of relational operations *}
haftmann@25919
  1301
huffman@28962
  1302
lemma less_number_of [simp]:
huffman@28962
  1303
  "(number_of x::'a::{ordered_idom,number_ring}) < number_of y \<longleftrightarrow> x < y"
huffman@28962
  1304
  unfolding number_of_eq by (rule of_int_less_iff)
huffman@28962
  1305
huffman@28962
  1306
lemma le_number_of [simp]:
huffman@28962
  1307
  "(number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y \<longleftrightarrow> x \<le> y"
huffman@28962
  1308
  unfolding number_of_eq by (rule of_int_le_iff)
huffman@28962
  1309
haftmann@25919
  1310
lemmas rel_simps [simp] = 
huffman@28962
  1311
  less_number_of less_bin_simps
huffman@28962
  1312
  le_number_of le_bin_simps
haftmann@25919
  1313
  eq_number_of_eq iszero_0 nonzero_number_of_Min
huffman@26086
  1314
  iszero_number_of_Bit0 iszero_number_of_Bit1
haftmann@25919
  1315
  not_neg_number_of_Pls not_neg_0 not_neg_1 not_iszero_1
huffman@26086
  1316
  neg_number_of_Min neg_number_of_Bit0 neg_number_of_Bit1
haftmann@25919
  1317
(* iszero_number_of_Pls would never be used
haftmann@25919
  1318
   because its lhs simplifies to "iszero 0" *)
haftmann@25919
  1319
haftmann@25919
  1320
huffman@28958
  1321
subsubsection {* Simplification of arithmetic when nested to the right. *}
haftmann@25919
  1322
haftmann@25919
  1323
lemma add_number_of_left [simp]:
haftmann@25919
  1324
  "number_of v + (number_of w + z) =
haftmann@25919
  1325
   (number_of(v + w) + z::'a::number_ring)"
haftmann@25919
  1326
  by (simp add: add_assoc [symmetric])
haftmann@25919
  1327
haftmann@25919
  1328
lemma mult_number_of_left [simp]:
haftmann@25919
  1329
  "number_of v * (number_of w * z) =
haftmann@25919
  1330
   (number_of(v * w) * z::'a::number_ring)"
haftmann@25919
  1331
  by (simp add: mult_assoc [symmetric])
haftmann@25919
  1332
haftmann@25919
  1333
lemma add_number_of_diff1:
haftmann@25919
  1334
  "number_of v + (number_of w - c) = 
haftmann@25919
  1335
  number_of(v + w) - (c::'a::number_ring)"
haftmann@25919
  1336
  by (simp add: diff_minus add_number_of_left)
haftmann@25919
  1337
haftmann@25919
  1338
lemma add_number_of_diff2 [simp]:
haftmann@25919
  1339
  "number_of v + (c - number_of w) =
haftmann@25919
  1340
   number_of (v + uminus w) + (c::'a::number_ring)"
haftmann@25919
  1341
apply (subst diff_number_of_eq [symmetric])
haftmann@25919
  1342
apply (simp only: compare_rls)
haftmann@25919
  1343
done
haftmann@25919
  1344
haftmann@25919
  1345
haftmann@25919
  1346
subsection {* The Set of Integers *}
haftmann@25919
  1347
haftmann@25919
  1348
context ring_1
haftmann@25919
  1349
begin
haftmann@25919
  1350
haftmann@25919
  1351
definition
haftmann@25919
  1352
  Ints  :: "'a set"
haftmann@25919
  1353
where
haftmann@28562
  1354
  [code del]: "Ints = range of_int"
haftmann@25919
  1355
haftmann@25919
  1356
end
haftmann@25919
  1357
haftmann@25919
  1358
notation (xsymbols)
haftmann@25919
  1359
  Ints  ("\<int>")
haftmann@25919
  1360
haftmann@25919
  1361
context ring_1
haftmann@25919
  1362
begin
haftmann@25919
  1363
haftmann@25919
  1364
lemma Ints_0 [simp]: "0 \<in> \<int>"
haftmann@25919
  1365
apply (simp add: Ints_def)
haftmann@25919
  1366
apply (rule range_eqI)
haftmann@25919
  1367
apply (rule of_int_0 [symmetric])
haftmann@25919
  1368
done
haftmann@25919
  1369
haftmann@25919
  1370
lemma Ints_1 [simp]: "1 \<in> \<int>"
haftmann@25919
  1371
apply (simp add: Ints_def)
haftmann@25919
  1372
apply (rule range_eqI)
haftmann@25919
  1373
apply (rule of_int_1 [symmetric])
haftmann@25919
  1374
done
haftmann@25919
  1375
haftmann@25919
  1376
lemma Ints_add [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a + b \<in> \<int>"
haftmann@25919
  1377
apply (auto simp add: Ints_def)
haftmann@25919
  1378
apply (rule range_eqI)
haftmann@25919
  1379
apply (rule of_int_add [symmetric])
haftmann@25919
  1380
done
haftmann@25919
  1381
haftmann@25919
  1382
lemma Ints_minus [simp]: "a \<in> \<int> \<Longrightarrow> -a \<in> \<int>"
haftmann@25919
  1383
apply (auto simp add: Ints_def)
haftmann@25919
  1384
apply (rule range_eqI)
haftmann@25919
  1385
apply (rule of_int_minus [symmetric])
haftmann@25919
  1386
done
haftmann@25919
  1387
haftmann@25919
  1388
lemma Ints_mult [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a * b \<in> \<int>"
haftmann@25919
  1389
apply (auto simp add: Ints_def)
haftmann@25919
  1390
apply (rule range_eqI)
haftmann@25919
  1391
apply (rule of_int_mult [symmetric])
haftmann@25919
  1392
done
haftmann@25919
  1393
haftmann@25919
  1394
lemma Ints_cases [cases set: Ints]:
haftmann@25919
  1395
  assumes "q \<in> \<int>"
haftmann@25919
  1396
  obtains (of_int) z where "q = of_int z"
haftmann@25919
  1397
  unfolding Ints_def
haftmann@25919
  1398
proof -
haftmann@25919
  1399
  from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
haftmann@25919
  1400
  then obtain z where "q = of_int z" ..
haftmann@25919
  1401
  then show thesis ..
haftmann@25919
  1402
qed
haftmann@25919
  1403
haftmann@25919
  1404
lemma Ints_induct [case_names of_int, induct set: Ints]:
haftmann@25919
  1405
  "q \<in> \<int> \<Longrightarrow> (\<And>z. P (of_int z)) \<Longrightarrow> P q"
haftmann@25919
  1406
  by (rule Ints_cases) auto
haftmann@25919
  1407
haftmann@25919
  1408
end
haftmann@25919
  1409
haftmann@25919
  1410
lemma Ints_diff [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a-b \<in> \<int>"
haftmann@25919
  1411
apply (auto simp add: Ints_def)
haftmann@25919
  1412
apply (rule range_eqI)
haftmann@25919
  1413
apply (rule of_int_diff [symmetric])
haftmann@25919
  1414
done
haftmann@25919
  1415
haftmann@25919
  1416
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
haftmann@25919
  1417
haftmann@25919
  1418
lemma Ints_double_eq_0_iff:
haftmann@25919
  1419
  assumes in_Ints: "a \<in> Ints"
haftmann@25919
  1420
  shows "(a + a = 0) = (a = (0::'a::ring_char_0))"
haftmann@25919
  1421
proof -
haftmann@25919
  1422
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
haftmann@25919
  1423
  then obtain z where a: "a = of_int z" ..
haftmann@25919
  1424
  show ?thesis
haftmann@25919
  1425
  proof
haftmann@25919
  1426
    assume "a = 0"
haftmann@25919
  1427
    thus "a + a = 0" by simp
haftmann@25919
  1428
  next
haftmann@25919
  1429
    assume eq: "a + a = 0"
haftmann@25919
  1430
    hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)
haftmann@25919
  1431
    hence "z + z = 0" by (simp only: of_int_eq_iff)
haftmann@25919
  1432
    hence "z = 0" by (simp only: double_eq_0_iff)
haftmann@25919
  1433
    thus "a = 0" by (simp add: a)
haftmann@25919
  1434
  qed
haftmann@25919
  1435
qed
haftmann@25919
  1436
haftmann@25919
  1437
lemma Ints_odd_nonzero:
haftmann@25919
  1438
  assumes in_Ints: "a \<in> Ints"
haftmann@25919
  1439
  shows "1 + a + a \<noteq> (0::'a::ring_char_0)"
haftmann@25919
  1440
proof -
haftmann@25919
  1441
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
haftmann@25919
  1442
  then obtain z where a: "a = of_int z" ..
haftmann@25919
  1443
  show ?thesis
haftmann@25919
  1444
  proof
haftmann@25919
  1445
    assume eq: "1 + a + a = 0"
haftmann@25919
  1446
    hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)
haftmann@25919
  1447
    hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
haftmann@25919
  1448
    with odd_nonzero show False by blast
haftmann@25919
  1449
  qed
haftmann@25919
  1450
qed 
haftmann@25919
  1451
haftmann@25919
  1452
lemma Ints_number_of:
haftmann@25919
  1453
  "(number_of w :: 'a::number_ring) \<in> Ints"
haftmann@25919
  1454
  unfolding number_of_eq Ints_def by simp
haftmann@25919
  1455
haftmann@25919
  1456
lemma Ints_odd_less_0: 
haftmann@25919
  1457
  assumes in_Ints: "a \<in> Ints"
haftmann@25919
  1458
  shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))";
haftmann@25919
  1459
proof -
haftmann@25919
  1460
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
haftmann@25919
  1461
  then obtain z where a: "a = of_int z" ..
haftmann@25919
  1462
  hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
haftmann@25919
  1463
    by (simp add: a)
haftmann@25919
  1464
  also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0)
haftmann@25919
  1465
  also have "... = (a < 0)" by (simp add: a)
haftmann@25919
  1466
  finally show ?thesis .
haftmann@25919
  1467
qed
haftmann@25919
  1468
haftmann@25919
  1469
haftmann@25919
  1470
subsection {* @{term setsum} and @{term setprod} *}
haftmann@25919
  1471
haftmann@25919
  1472
text {*By Jeremy Avigad*}
haftmann@25919
  1473
haftmann@25919
  1474
lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))"
haftmann@25919
  1475
  apply (cases "finite A")
haftmann@25919
  1476
  apply (erule finite_induct, auto)
haftmann@25919
  1477
  done
haftmann@25919
  1478
haftmann@25919
  1479
lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))"
haftmann@25919
  1480
  apply (cases "finite A")
haftmann@25919
  1481
  apply (erule finite_induct, auto)
haftmann@25919
  1482
  done
haftmann@25919
  1483
haftmann@25919
  1484
lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))"
haftmann@25919
  1485
  apply (cases "finite A")
haftmann@25919
  1486
  apply (erule finite_induct, auto simp add: of_nat_mult)
haftmann@25919
  1487
  done
haftmann@25919
  1488
haftmann@25919
  1489
lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))"
haftmann@25919
  1490
  apply (cases "finite A")
haftmann@25919
  1491
  apply (erule finite_induct, auto)
haftmann@25919
  1492
  done
haftmann@25919
  1493
haftmann@25919
  1494
lemma setprod_nonzero_nat:
haftmann@25919
  1495
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0"
haftmann@25919
  1496
  by (rule setprod_nonzero, auto)
haftmann@25919
  1497
haftmann@25919
  1498
lemma setprod_zero_eq_nat:
haftmann@25919
  1499
    "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)"
haftmann@25919
  1500
  by (rule setprod_zero_eq, auto)
haftmann@25919
  1501
haftmann@25919
  1502
lemma setprod_nonzero_int:
haftmann@25919
  1503
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0"
haftmann@25919
  1504
  by (rule setprod_nonzero, auto)
haftmann@25919
  1505
haftmann@25919
  1506
lemma setprod_zero_eq_int:
haftmann@25919
  1507
    "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)"
haftmann@25919
  1508
  by (rule setprod_zero_eq, auto)
haftmann@25919
  1509
haftmann@25919
  1510
lemmas int_setsum = of_nat_setsum [where 'a=int]
haftmann@25919
  1511
lemmas int_setprod = of_nat_setprod [where 'a=int]
haftmann@25919
  1512
haftmann@25919
  1513
haftmann@25919
  1514
subsection{*Inequality Reasoning for the Arithmetic Simproc*}
haftmann@25919
  1515
haftmann@25919
  1516
lemma add_numeral_0: "Numeral0 + a = (a::'a::number_ring)"
haftmann@25919
  1517
by simp 
haftmann@25919
  1518
haftmann@25919
  1519
lemma add_numeral_0_right: "a + Numeral0 = (a::'a::number_ring)"
haftmann@25919
  1520
by simp
haftmann@25919
  1521
haftmann@25919
  1522
lemma mult_numeral_1: "Numeral1 * a = (a::'a::number_ring)"
haftmann@25919
  1523
by simp 
haftmann@25919
  1524
haftmann@25919
  1525
lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::number_ring)"
haftmann@25919
  1526
by simp
haftmann@25919
  1527
haftmann@25919
  1528
lemma divide_numeral_1: "a / Numeral1 = (a::'a::{number_ring,field})"
haftmann@25919
  1529
by simp
haftmann@25919
  1530
haftmann@25919
  1531
lemma inverse_numeral_1:
haftmann@25919
  1532
  "inverse Numeral1 = (Numeral1::'a::{number_ring,field})"
haftmann@25919
  1533
by simp
haftmann@25919
  1534
haftmann@25919
  1535
text{*Theorem lists for the cancellation simprocs. The use of binary numerals
haftmann@25919
  1536
for 0 and 1 reduces the number of special cases.*}
haftmann@25919
  1537
haftmann@25919
  1538
lemmas add_0s = add_numeral_0 add_numeral_0_right
haftmann@25919
  1539
lemmas mult_1s = mult_numeral_1 mult_numeral_1_right 
haftmann@25919
  1540
                 mult_minus1 mult_minus1_right
haftmann@25919
  1541
haftmann@25919
  1542
haftmann@25919
  1543
subsection{*Special Arithmetic Rules for Abstract 0 and 1*}
haftmann@25919
  1544
haftmann@25919
  1545
text{*Arithmetic computations are defined for binary literals, which leaves 0
haftmann@25919
  1546
and 1 as special cases. Addition already has rules for 0, but not 1.
haftmann@25919
  1547
Multiplication and unary minus already have rules for both 0 and 1.*}
haftmann@25919
  1548
haftmann@25919
  1549
haftmann@25919
  1550
lemma binop_eq: "[|f x y = g x y; x = x'; y = y'|] ==> f x' y' = g x' y'"
haftmann@25919
  1551
by simp
haftmann@25919
  1552
haftmann@25919
  1553
haftmann@25919
  1554
lemmas add_number_of_eq = number_of_add [symmetric]
haftmann@25919
  1555
haftmann@25919
  1556
text{*Allow 1 on either or both sides*}
haftmann@25919
  1557
lemma one_add_one_is_two: "1 + 1 = (2::'a::number_ring)"
haftmann@25919
  1558
by (simp del: numeral_1_eq_1 add: numeral_1_eq_1 [symmetric] add_number_of_eq)
haftmann@25919
  1559
haftmann@25919
  1560
lemmas add_special =
haftmann@25919
  1561
    one_add_one_is_two
haftmann@25919
  1562
    binop_eq [of "op +", OF add_number_of_eq numeral_1_eq_1 refl, standard]
haftmann@25919
  1563
    binop_eq [of "op +", OF add_number_of_eq refl numeral_1_eq_1, standard]
haftmann@25919
  1564
haftmann@25919
  1565
text{*Allow 1 on either or both sides (1-1 already simplifies to 0)*}
haftmann@25919
  1566
lemmas diff_special =
haftmann@25919
  1567
    binop_eq [of "op -", OF diff_number_of_eq numeral_1_eq_1 refl, standard]
haftmann@25919
  1568
    binop_eq [of "op -", OF diff_number_of_eq refl numeral_1_eq_1, standard]
haftmann@25919
  1569
haftmann@25919
  1570
text{*Allow 0 or 1 on either side with a binary numeral on the other*}
haftmann@25919
  1571
lemmas eq_special =
haftmann@25919
  1572
    binop_eq [of "op =", OF eq_number_of_eq numeral_0_eq_0 refl, standard]
haftmann@25919
  1573
    binop_eq [of "op =", OF eq_number_of_eq numeral_1_eq_1 refl, standard]
haftmann@25919
  1574
    binop_eq [of "op =", OF eq_number_of_eq refl numeral_0_eq_0, standard]
haftmann@25919
  1575
    binop_eq [of "op =", OF eq_number_of_eq refl numeral_1_eq_1, standard]
haftmann@25919
  1576
haftmann@25919
  1577
text{*Allow 0 or 1 on either side with a binary numeral on the other*}
haftmann@25919
  1578
lemmas less_special =
haftmann@25919
  1579
  binop_eq [of "op <", OF less_number_of_eq_neg numeral_0_eq_0 refl, standard]
haftmann@25919
  1580
  binop_eq [of "op <", OF less_number_of_eq_neg numeral_1_eq_1 refl, standard]
haftmann@25919
  1581
  binop_eq [of "op <", OF less_number_of_eq_neg refl numeral_0_eq_0, standard]
haftmann@25919
  1582
  binop_eq [of "op <", OF less_number_of_eq_neg refl numeral_1_eq_1, standard]
haftmann@25919
  1583
haftmann@25919
  1584
text{*Allow 0 or 1 on either side with a binary numeral on the other*}
haftmann@25919
  1585
lemmas le_special =
haftmann@25919
  1586
    binop_eq [of "op \<le>", OF le_number_of_eq numeral_0_eq_0 refl, standard]
haftmann@25919
  1587
    binop_eq [of "op \<le>", OF le_number_of_eq numeral_1_eq_1 refl, standard]
haftmann@25919
  1588
    binop_eq [of "op \<le>", OF le_number_of_eq refl numeral_0_eq_0, standard]
haftmann@25919
  1589
    binop_eq [of "op \<le>", OF le_number_of_eq refl numeral_1_eq_1, standard]
haftmann@25919
  1590
haftmann@25919
  1591
lemmas arith_special[simp] = 
haftmann@25919
  1592
       add_special diff_special eq_special less_special le_special
haftmann@25919
  1593
haftmann@25919
  1594
haftmann@25919
  1595
lemma min_max_01: "min (0::int) 1 = 0 & min (1::int) 0 = 0 &
haftmann@25919
  1596
                   max (0::int) 1 = 1 & max (1::int) 0 = 1"
haftmann@25919
  1597
by(simp add:min_def max_def)
haftmann@25919
  1598
haftmann@25919
  1599
lemmas min_max_special[simp] =
haftmann@25919
  1600
 min_max_01
haftmann@25919
  1601
 max_def[of "0::int" "number_of v", standard, simp]
haftmann@25919
  1602
 min_def[of "0::int" "number_of v", standard, simp]
haftmann@25919
  1603
 max_def[of "number_of u" "0::int", standard, simp]
haftmann@25919
  1604
 min_def[of "number_of u" "0::int", standard, simp]
haftmann@25919
  1605
 max_def[of "1::int" "number_of v", standard, simp]
haftmann@25919
  1606
 min_def[of "1::int" "number_of v", standard, simp]
haftmann@25919
  1607
 max_def[of "number_of u" "1::int", standard, simp]
haftmann@25919
  1608
 min_def[of "number_of u" "1::int", standard, simp]
haftmann@25919
  1609
haftmann@25919
  1610
text {* Legacy theorems *}
haftmann@25919
  1611
haftmann@25919
  1612
lemmas zle_int = of_nat_le_iff [where 'a=int]
haftmann@25919
  1613
lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
haftmann@25919
  1614
haftmann@25919
  1615
use "~~/src/Provers/Arith/assoc_fold.ML"
haftmann@28952
  1616
use "Tools/int_arith.ML"
haftmann@25919
  1617
declaration {* K int_arith_setup *}
haftmann@25919
  1618
haftmann@25919
  1619
haftmann@25919
  1620
subsection{*Lemmas About Small Numerals*}
haftmann@25919
  1621
haftmann@25919
  1622
lemma of_int_m1 [simp]: "of_int -1 = (-1 :: 'a :: number_ring)"
haftmann@25919
  1623
proof -
haftmann@25919
  1624
  have "(of_int -1 :: 'a) = of_int (- 1)" by simp
haftmann@25919
  1625
  also have "... = - of_int 1" by (simp only: of_int_minus)
haftmann@25919
  1626
  also have "... = -1" by simp
haftmann@25919
  1627
  finally show ?thesis .
haftmann@25919
  1628
qed
haftmann@25919
  1629
haftmann@25919
  1630
lemma abs_minus_one [simp]: "abs (-1) = (1::'a::{ordered_idom,number_ring})"
haftmann@25919
  1631
by (simp add: abs_if)
haftmann@25919
  1632
haftmann@25919
  1633
lemma abs_power_minus_one [simp]:
haftmann@25919
  1634
     "abs(-1 ^ n) = (1::'a::{ordered_idom,number_ring,recpower})"
haftmann@25919
  1635
by (simp add: power_abs)
haftmann@25919
  1636
haftmann@25919
  1637
lemma of_int_number_of_eq:
haftmann@25919
  1638
     "of_int (number_of v) = (number_of v :: 'a :: number_ring)"
haftmann@25919
  1639
by (simp add: number_of_eq) 
haftmann@25919
  1640
haftmann@25919
  1641
text{*Lemmas for specialist use, NOT as default simprules*}
haftmann@25919
  1642
lemma mult_2: "2 * z = (z+z::'a::number_ring)"
haftmann@25919
  1643
proof -
haftmann@25919
  1644
  have "2*z = (1 + 1)*z" by simp
haftmann@25919
  1645
  also have "... = z+z" by (simp add: left_distrib)
haftmann@25919
  1646
  finally show ?thesis .
haftmann@25919
  1647
qed
haftmann@25919
  1648
haftmann@25919
  1649
lemma mult_2_right: "z * 2 = (z+z::'a::number_ring)"
haftmann@25919
  1650
by (subst mult_commute, rule mult_2)
haftmann@25919
  1651
haftmann@25919
  1652
haftmann@25919
  1653
subsection{*More Inequality Reasoning*}
haftmann@25919
  1654
haftmann@25919
  1655
lemma zless_add1_eq: "(w < z + (1::int)) = (w<z | w=z)"
haftmann@25919
  1656
by arith
haftmann@25919
  1657
haftmann@25919
  1658
lemma add1_zle_eq: "(w + (1::int) \<le> z) = (w<z)"
haftmann@25919
  1659
by arith
haftmann@25919
  1660
haftmann@25919
  1661
lemma zle_diff1_eq [simp]: "(w \<le> z - (1::int)) = (w<z)"
haftmann@25919
  1662
by arith
haftmann@25919
  1663
haftmann@25919
  1664
lemma zle_add1_eq_le [simp]: "(w < z + (1::int)) = (w\<le>z)"
haftmann@25919
  1665
by arith
haftmann@25919
  1666
haftmann@25919
  1667
lemma int_one_le_iff_zero_less: "((1::int) \<le> z) = (0 < z)"
haftmann@25919
  1668
by arith
haftmann@25919
  1669
haftmann@25919
  1670
huffman@28958
  1671
subsection{*The functions @{term nat} and @{term int}*}
haftmann@25919
  1672
haftmann@25919
  1673
text{*Simplify the terms @{term "int 0"}, @{term "int(Suc 0)"} and
haftmann@25919
  1674
  @{term "w + - z"}*}
haftmann@25919
  1675
declare Zero_int_def [symmetric, simp]
haftmann@25919
  1676
declare One_int_def [symmetric, simp]
haftmann@25919
  1677
haftmann@25919
  1678
lemmas diff_int_def_symmetric = diff_int_def [symmetric, simp]
haftmann@25919
  1679
haftmann@25919
  1680
lemma nat_0: "nat 0 = 0"
haftmann@25919
  1681
by (simp add: nat_eq_iff)
haftmann@25919
  1682
haftmann@25919
  1683
lemma nat_1: "nat 1 = Suc 0"
haftmann@25919
  1684
by (subst nat_eq_iff, simp)
haftmann@25919
  1685
haftmann@25919
  1686
lemma nat_2: "nat 2 = Suc (Suc 0)"
haftmann@25919
  1687
by (subst nat_eq_iff, simp)
haftmann@25919
  1688
haftmann@25919
  1689
lemma one_less_nat_eq [simp]: "(Suc 0 < nat z) = (1 < z)"
haftmann@25919
  1690
apply (insert zless_nat_conj [of 1 z])
haftmann@25919
  1691
apply (auto simp add: nat_1)
haftmann@25919
  1692
done
haftmann@25919
  1693
haftmann@25919
  1694
text{*This simplifies expressions of the form @{term "int n = z"} where
haftmann@25919
  1695
      z is an integer literal.*}
haftmann@25919
  1696
lemmas int_eq_iff_number_of [simp] = int_eq_iff [of _ "number_of v", standard]
haftmann@25919
  1697
haftmann@25919
  1698
lemma split_nat [arith_split]:
haftmann@25919
  1699
  "P(nat(i::int)) = ((\<forall>n. i = of_nat n \<longrightarrow> P n) & (i < 0 \<longrightarrow> P 0))"
haftmann@25919
  1700
  (is "?P = (?L & ?R)")
haftmann@25919
  1701
proof (cases "i < 0")
haftmann@25919
  1702
  case True thus ?thesis by auto
haftmann@25919
  1703
next
haftmann@25919
  1704
  case False
haftmann@25919
  1705
  have "?P = ?L"
haftmann@25919
  1706
  proof
haftmann@25919
  1707
    assume ?P thus ?L using False by clarsimp
haftmann@25919
  1708
  next
haftmann@25919
  1709
    assume ?L thus ?P using False by simp
haftmann@25919
  1710
  qed
haftmann@25919
  1711
  with False show ?thesis by simp
haftmann@25919
  1712
qed
haftmann@25919
  1713
haftmann@25919
  1714
context ring_1
haftmann@25919
  1715
begin
haftmann@25919
  1716
haftmann@25919
  1717
lemma of_int_of_nat:
haftmann@25919
  1718
  "of_int k = (if k < 0 then - of_nat (nat (- k)) else of_nat (nat k))"
haftmann@25919
  1719
proof (cases "k < 0")
haftmann@25919
  1720
  case True then have "0 \<le> - k" by simp
haftmann@25919
  1721
  then have "of_nat (nat (- k)) = of_int (- k)" by (rule of_nat_nat)
haftmann@25919
  1722
  with True show ?thesis by simp
haftmann@25919
  1723
next
haftmann@25919
  1724
  case False then show ?thesis by (simp add: not_less of_nat_nat)
haftmann@25919
  1725
qed
haftmann@25919
  1726
haftmann@25919
  1727
end
haftmann@25919
  1728
haftmann@25919
  1729
lemma nat_mult_distrib:
haftmann@25919
  1730
  fixes z z' :: int
haftmann@25919
  1731
  assumes "0 \<le> z"
haftmann@25919
  1732
  shows "nat (z * z') = nat z * nat z'"
haftmann@25919
  1733
proof (cases "0 \<le> z'")
haftmann@25919
  1734
  case False with assms have "z * z' \<le> 0"
haftmann@25919
  1735
    by (simp add: not_le mult_le_0_iff)
haftmann@25919
  1736
  then have "nat (z * z') = 0" by simp
haftmann@25919
  1737
  moreover from False have "nat z' = 0" by simp
haftmann@25919
  1738
  ultimately show ?thesis by simp
haftmann@25919
  1739
next
haftmann@25919
  1740
  case True with assms have ge_0: "z * z' \<ge> 0" by (simp add: zero_le_mult_iff)
haftmann@25919
  1741
  show ?thesis
haftmann@25919
  1742
    by (rule injD [of "of_nat :: nat \<Rightarrow> int", OF inj_of_nat])
haftmann@25919
  1743
      (simp only: of_nat_mult of_nat_nat [OF True]
haftmann@25919
  1744
         of_nat_nat [OF assms] of_nat_nat [OF ge_0], simp)
haftmann@25919
  1745
qed
haftmann@25919
  1746
haftmann@25919
  1747
lemma nat_mult_distrib_neg: "z \<le> (0::int) ==> nat(z*z') = nat(-z) * nat(-z')"
haftmann@25919
  1748
apply (rule trans)
haftmann@25919
  1749
apply (rule_tac [2] nat_mult_distrib, auto)
haftmann@25919
  1750
done
haftmann@25919
  1751
haftmann@25919
  1752
lemma nat_abs_mult_distrib: "nat (abs (w * z)) = nat (abs w) * nat (abs z)"
haftmann@25919
  1753
apply (cases "z=0 | w=0")
haftmann@25919
  1754
apply (auto simp add: abs_if nat_mult_distrib [symmetric] 
haftmann@25919
  1755
                      nat_mult_distrib_neg [symmetric] mult_less_0_iff)
haftmann@25919
  1756
done
haftmann@25919
  1757
haftmann@25919
  1758
haftmann@25919
  1759
subsection "Induction principles for int"
haftmann@25919
  1760
haftmann@25919
  1761
text{*Well-founded segments of the integers*}
haftmann@25919
  1762
haftmann@25919
  1763
definition
haftmann@25919
  1764
  int_ge_less_than  ::  "int => (int * int) set"
haftmann@25919
  1765
where
haftmann@25919
  1766
  "int_ge_less_than d = {(z',z). d \<le> z' & z' < z}"
haftmann@25919
  1767
haftmann@25919
  1768
theorem wf_int_ge_less_than: "wf (int_ge_less_than d)"
haftmann@25919
  1769
proof -
haftmann@25919
  1770
  have "int_ge_less_than d \<subseteq> measure (%z. nat (z-d))"
haftmann@25919
  1771
    by (auto simp add: int_ge_less_than_def)
haftmann@25919
  1772
  thus ?thesis 
haftmann@25919
  1773
    by (rule wf_subset [OF wf_measure]) 
haftmann@25919
  1774
qed
haftmann@25919
  1775
haftmann@25919
  1776
text{*This variant looks odd, but is typical of the relations suggested
haftmann@25919
  1777
by RankFinder.*}
haftmann@25919
  1778
haftmann@25919
  1779
definition
haftmann@25919
  1780
  int_ge_less_than2 ::  "int => (int * int) set"
haftmann@25919
  1781
where
haftmann@25919
  1782
  "int_ge_less_than2 d = {(z',z). d \<le> z & z' < z}"
haftmann@25919
  1783
haftmann@25919
  1784
theorem wf_int_ge_less_than2: "wf (int_ge_less_than2 d)"
haftmann@25919
  1785
proof -
haftmann@25919
  1786
  have "int_ge_less_than2 d \<subseteq> measure (%z. nat (1+z-d))" 
haftmann@25919
  1787
    by (auto simp add: int_ge_less_than2_def)
haftmann@25919
  1788
  thus ?thesis 
haftmann@25919
  1789
    by (rule wf_subset [OF wf_measure]) 
haftmann@25919
  1790
qed
haftmann@25919
  1791
haftmann@25919
  1792
abbreviation
haftmann@25919
  1793
  int :: "nat \<Rightarrow> int"
haftmann@25919
  1794
where
haftmann@25919
  1795
  "int \<equiv> of_nat"
haftmann@25919
  1796
haftmann@25919
  1797
(* `set:int': dummy construction *)
haftmann@25919
  1798
theorem int_ge_induct [case_names base step, induct set: int]:
haftmann@25919
  1799
  fixes i :: int
haftmann@25919
  1800
  assumes ge: "k \<le> i" and
haftmann@25919
  1801
    base: "P k" and
haftmann@25919
  1802
    step: "\<And>i. k \<le> i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
haftmann@25919
  1803
  shows "P i"
haftmann@25919
  1804
proof -
haftmann@25919
  1805
  { fix n have "\<And>i::int. n = nat(i-k) \<Longrightarrow> k \<le> i \<Longrightarrow> P i"
haftmann@25919
  1806
    proof (induct n)
haftmann@25919
  1807
      case 0
haftmann@25919
  1808
      hence "i = k" by arith
haftmann@25919
  1809
      thus "P i" using base by simp
haftmann@25919
  1810
    next
haftmann@25919
  1811
      case (Suc n)
haftmann@25919
  1812
      then have "n = nat((i - 1) - k)" by arith
haftmann@25919
  1813
      moreover
haftmann@25919
  1814
      have ki1: "k \<le> i - 1" using Suc.prems by arith
haftmann@25919
  1815
      ultimately
haftmann@25919
  1816
      have "P(i - 1)" by(rule Suc.hyps)
haftmann@25919
  1817
      from step[OF ki1 this] show ?case by simp
haftmann@25919
  1818
    qed
haftmann@25919
  1819
  }
haftmann@25919
  1820
  with ge show ?thesis by fast
haftmann@25919
  1821
qed
haftmann@25919
  1822
haftmann@25928
  1823
(* `set:int': dummy construction *)
haftmann@25928
  1824
theorem int_gr_induct [case_names base step, induct set: int]:
haftmann@25919
  1825
  assumes gr: "k < (i::int)" and
haftmann@25919
  1826
        base: "P(k+1)" and
haftmann@25919
  1827
        step: "\<And>i. \<lbrakk>k < i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
haftmann@25919
  1828
  shows "P i"
haftmann@25919
  1829
apply(rule int_ge_induct[of "k + 1"])
haftmann@25919
  1830
  using gr apply arith
haftmann@25919
  1831
 apply(rule base)
haftmann@25919
  1832
apply (rule step, simp+)
haftmann@25919
  1833
done
haftmann@25919
  1834
haftmann@25919
  1835
theorem int_le_induct[consumes 1,case_names base step]:
haftmann@25919
  1836
  assumes le: "i \<le> (k::int)" and
haftmann@25919
  1837
        base: "P(k)" and
haftmann@25919
  1838
        step: "\<And>i. \<lbrakk>i \<le> k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
haftmann@25919
  1839
  shows "P i"
haftmann@25919
  1840
proof -
haftmann@25919
  1841
  { fix n have "\<And>i::int. n = nat(k-i) \<Longrightarrow> i \<le> k \<Longrightarrow> P i"
haftmann@25919
  1842
    proof (induct n)
haftmann@25919
  1843
      case 0
haftmann@25919
  1844
      hence "i = k" by arith
haftmann@25919
  1845
      thus "P i" using base by simp
haftmann@25919
  1846
    next
haftmann@25919
  1847
      case (Suc n)
haftmann@25919
  1848
      hence "n = nat(k - (i+1))" by arith
haftmann@25919
  1849
      moreover
haftmann@25919
  1850
      have ki1: "i + 1 \<le> k" using Suc.prems by arith
haftmann@25919
  1851
      ultimately
haftmann@25919
  1852
      have "P(i+1)" by(rule Suc.hyps)
haftmann@25919
  1853
      from step[OF ki1 this] show ?case by simp
haftmann@25919
  1854
    qed
haftmann@25919
  1855
  }
haftmann@25919
  1856
  with le show ?thesis by fast
haftmann@25919
  1857
qed
haftmann@25919
  1858
haftmann@25919
  1859
theorem int_less_induct [consumes 1,case_names base step]:
haftmann@25919
  1860
  assumes less: "(i::int) < k" and
haftmann@25919
  1861
        base: "P(k - 1)" and
haftmann@25919
  1862
        step: "\<And>i. \<lbrakk>i < k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
haftmann@25919
  1863
  shows "P i"
haftmann@25919
  1864
apply(rule int_le_induct[of _ "k - 1"])
haftmann@25919
  1865
  using less apply arith
haftmann@25919
  1866
 apply(rule base)
haftmann@25919
  1867
apply (rule step, simp+)
haftmann@25919
  1868
done
haftmann@25919
  1869
haftmann@25919
  1870
subsection{*Intermediate value theorems*}
haftmann@25919
  1871
haftmann@25919
  1872
lemma int_val_lemma:
haftmann@25919
  1873
     "(\<forall>i<n::nat. abs(f(i+1) - f i) \<le> 1) -->  
haftmann@25919
  1874
      f 0 \<le> k --> k \<le> f n --> (\<exists>i \<le> n. f i = (k::int))"
haftmann@27106
  1875
apply (induct n, simp)
haftmann@25919
  1876
apply (intro strip)
haftmann@25919
  1877
apply (erule impE, simp)
haftmann@25919
  1878
apply (erule_tac x = n in allE, simp)
haftmann@25919
  1879
apply (case_tac "k = f (n+1) ")
haftmann@27106
  1880
apply force
haftmann@25919
  1881
apply (erule impE)
haftmann@25919
  1882
 apply (simp add: abs_if split add: split_if_asm)
haftmann@25919
  1883
apply (blast intro: le_SucI)
haftmann@25919
  1884
done
haftmann@25919
  1885
haftmann@25919
  1886
lemmas nat0_intermed_int_val = int_val_lemma [rule_format (no_asm)]
haftmann@25919
  1887
haftmann@25919
  1888
lemma nat_intermed_int_val:
haftmann@25919
  1889
     "[| \<forall>i. m \<le> i & i < n --> abs(f(i + 1::nat) - f i) \<le> 1; m < n;  
haftmann@25919
  1890
         f m \<le> k; k \<le> f n |] ==> ? i. m \<le> i & i \<le> n & f i = (k::int)"
haftmann@25919
  1891
apply (cut_tac n = "n-m" and f = "%i. f (i+m) " and k = k 
haftmann@25919
  1892
       in int_val_lemma)
haftmann@25919
  1893
apply simp
haftmann@25919
  1894
apply (erule exE)
haftmann@25919
  1895
apply (rule_tac x = "i+m" in exI, arith)
haftmann@25919
  1896
done
haftmann@25919
  1897
haftmann@25919
  1898
haftmann@25919
  1899
subsection{*Products and 1, by T. M. Rasmussen*}
haftmann@25919
  1900
haftmann@25919
  1901
lemma zabs_less_one_iff [simp]: "(\<bar>z\<bar> < 1) = (z = (0::int))"
haftmann@25919
  1902
by arith
haftmann@25919
  1903
haftmann@25919
  1904
lemma abs_zmult_eq_1: "(\<bar>m * n\<bar> = 1) ==> \<bar>m\<bar> = (1::int)"
haftmann@25919
  1905
apply (cases "\<bar>n\<bar>=1") 
haftmann@25919
  1906
apply (simp add: abs_mult) 
haftmann@25919
  1907
apply (rule ccontr) 
haftmann@25919
  1908
apply (auto simp add: linorder_neq_iff abs_mult) 
haftmann@25919
  1909
apply (subgoal_tac "2 \<le> \<bar>m\<bar> & 2 \<le> \<bar>n\<bar>")
haftmann@25919
  1910
 prefer 2 apply arith 
haftmann@25919
  1911
apply (subgoal_tac "2*2 \<le> \<bar>m\<bar> * \<bar>n\<bar>", simp) 
haftmann@25919
  1912
apply (rule mult_mono, auto) 
haftmann@25919
  1913
done
haftmann@25919
  1914
haftmann@25919
  1915
lemma pos_zmult_eq_1_iff_lemma: "(m * n = 1) ==> m = (1::int) | m = -1"
haftmann@25919
  1916
by (insert abs_zmult_eq_1 [of m n], arith)
haftmann@25919
  1917
haftmann@25919
  1918
lemma pos_zmult_eq_1_iff: "0 < (m::int) ==> (m * n = 1) = (m = 1 & n = 1)"
haftmann@25919
  1919
apply (auto dest: pos_zmult_eq_1_iff_lemma) 
haftmann@25919
  1920
apply (simp add: mult_commute [of m]) 
haftmann@25919
  1921
apply (frule pos_zmult_eq_1_iff_lemma, auto) 
haftmann@25919
  1922
done
haftmann@25919
  1923
haftmann@25919
  1924
lemma zmult_eq_1_iff: "(m*n = (1::int)) = ((m = 1 & n = 1) | (m = -1 & n = -1))"
haftmann@25919
  1925
apply (rule iffI) 
haftmann@25919
  1926
 apply (frule pos_zmult_eq_1_iff_lemma)
haftmann@25919
  1927
 apply (simp add: mult_commute [of m]) 
haftmann@25919
  1928
 apply (frule pos_zmult_eq_1_iff_lemma, auto) 
haftmann@25919
  1929
done
haftmann@25919
  1930
haftmann@25919
  1931
(* Could be simplified but Presburger only becomes available too late *)
haftmann@25919
  1932
lemma infinite_UNIV_int: "~finite(UNIV::int set)"
haftmann@25919
  1933
proof
haftmann@25919
  1934
  assume "finite(UNIV::int set)"
haftmann@25919
  1935
  moreover have "~(EX i::int. 2*i = 1)"
haftmann@25919
  1936
    by (auto simp: pos_zmult_eq_1_iff)
haftmann@25919
  1937
  ultimately show False using finite_UNIV_inj_surj[of "%n::int. n+n"]
haftmann@25919
  1938
    by (simp add:inj_on_def surj_def) (blast intro:sym)
haftmann@25919
  1939
qed
haftmann@25919
  1940
haftmann@25919
  1941
haftmann@25961
  1942
subsection{*Integer Powers*} 
haftmann@25961
  1943
haftmann@25961
  1944
instantiation int :: recpower
haftmann@25961
  1945
begin
haftmann@25961
  1946
haftmann@25961
  1947
primrec power_int where
haftmann@25961
  1948
  "p ^ 0 = (1\<Colon>int)"
haftmann@25961
  1949
  | "p ^ (Suc n) = (p\<Colon>int) * (p ^ n)"
haftmann@25961
  1950
haftmann@25961
  1951
instance proof
haftmann@25961
  1952
  fix z :: int
haftmann@25961
  1953
  fix n :: nat
haftmann@25961
  1954
  show "z ^ 0 = 1" by simp
haftmann@25961
  1955
  show "z ^ Suc n = z * (z ^ n)" by simp
haftmann@25961
  1956
qed
haftmann@25961
  1957
haftmann@25961
  1958
end
haftmann@25961
  1959
haftmann@25961
  1960
lemma zpower_zadd_distrib: "x ^ (y + z) = ((x ^ y) * (x ^ z)::int)"
haftmann@25961
  1961
  by (rule Power.power_add)
haftmann@25961
  1962
haftmann@25961
  1963
lemma zpower_zpower: "(x ^ y) ^ z = (x ^ (y * z)::int)"
haftmann@25961
  1964
  by (rule Power.power_mult [symmetric])
haftmann@25961
  1965
haftmann@25961
  1966
lemma zero_less_zpower_abs_iff [simp]:
haftmann@25961
  1967
  "(0 < abs x ^ n) \<longleftrightarrow> (x \<noteq> (0::int) | n = 0)"
haftmann@25961
  1968
  by (induct n) (auto simp add: zero_less_mult_iff)
haftmann@25961
  1969
haftmann@25961
  1970
lemma zero_le_zpower_abs [simp]: "(0::int) \<le> abs x ^ n"
haftmann@25961
  1971
  by (induct n) (auto simp add: zero_le_mult_iff)
haftmann@25961
  1972
haftmann@25961
  1973
lemma of_int_power:
haftmann@25961
  1974
  "of_int (z ^ n) = (of_int z ^ n :: 'a::{recpower, ring_1})"
haftmann@25961
  1975
  by (induct n) (simp_all add: power_Suc)
haftmann@25961
  1976
haftmann@25961
  1977
lemma int_power: "int (m^n) = (int m) ^ n"
haftmann@25961
  1978
  by (rule of_nat_power)
haftmann@25961
  1979
haftmann@25961
  1980
lemmas zpower_int = int_power [symmetric]
haftmann@25961
  1981
haftmann@25919
  1982
subsection {* Configuration of the code generator *}
haftmann@25919
  1983
haftmann@26507
  1984
code_datatype Pls Min Bit0 Bit1 "number_of \<Colon> int \<Rightarrow> int"
haftmann@26507
  1985
haftmann@28562
  1986
lemmas pred_succ_numeral_code [code] =
haftmann@26507
  1987
  pred_bin_simps succ_bin_simps
haftmann@26507
  1988
haftmann@28562
  1989
lemmas plus_numeral_code [code] =
haftmann@26507
  1990
  add_bin_simps
haftmann@26507
  1991
  arith_extra_simps(1) [where 'a = int]
haftmann@26507
  1992
haftmann@28562
  1993
lemmas minus_numeral_code [code] =
haftmann@26507
  1994
  minus_bin_simps
haftmann@26507
  1995
  arith_extra_simps(2) [where 'a = int]
haftmann@26507
  1996
  arith_extra_simps(5) [where 'a = int]
haftmann@26507
  1997
haftmann@28562
  1998
lemmas times_numeral_code [code] =
haftmann@26507
  1999
  mult_bin_simps
haftmann@26507
  2000
  arith_extra_simps(4) [where 'a = int]
haftmann@26507
  2001
haftmann@26507
  2002
instantiation int :: eq
haftmann@26507
  2003
begin
haftmann@26507
  2004
haftmann@28562
  2005
definition [code del]: "eq_class.eq k l \<longleftrightarrow> k - l = (0\<Colon>int)"
haftmann@26507
  2006
haftmann@26507
  2007
instance by default (simp add: eq_int_def)
haftmann@26507
  2008
haftmann@26507
  2009
end
haftmann@26507
  2010
haftmann@28562
  2011
lemma eq_number_of_int_code [code]:
haftmann@26732
  2012
  "eq_class.eq (number_of k \<Colon> int) (number_of l) \<longleftrightarrow> eq_class.eq k l"
haftmann@26507
  2013
  unfolding eq_int_def number_of_is_id ..
haftmann@26507
  2014
haftmann@28562
  2015
lemma eq_int_code [code]:
haftmann@26732
  2016
  "eq_class.eq Int.Pls Int.Pls \<longleftrightarrow> True"
haftmann@26732
  2017
  "eq_class.eq Int.Pls Int.Min \<longleftrightarrow> False"
haftmann@26732
  2018
  "eq_class.eq Int.Pls (Int.Bit0 k2) \<longleftrightarrow> eq_class.eq Int.Pls k2"
haftmann@26732
  2019
  "eq_class.eq Int.Pls (Int.Bit1 k2) \<longleftrightarrow> False"
haftmann@26732
  2020
  "eq_class.eq Int.Min Int.Pls \<longleftrightarrow> False"
haftmann@26732
  2021
  "eq_class.eq Int.Min Int.Min \<longleftrightarrow> True"
haftmann@26732
  2022
  "eq_class.eq Int.Min (Int.Bit0 k2) \<longleftrightarrow> False"
haftmann@26732
  2023
  "eq_class.eq Int.Min (Int.Bit1 k2) \<longleftrightarrow> eq_class.eq Int.Min k2"
huffman@28958
  2024
  "eq_class.eq (Int.Bit0 k1) Int.Pls \<longleftrightarrow> eq_class.eq k1 Int.Pls"
haftmann@26732
  2025
  "eq_class.eq (Int.Bit1 k1) Int.Pls \<longleftrightarrow> False"
haftmann@26732
  2026
  "eq_class.eq (Int.Bit0 k1) Int.Min \<longleftrightarrow> False"
huffman@28958
  2027
  "eq_class.eq (Int.Bit1 k1) Int.Min \<longleftrightarrow> eq_class.eq k1 Int.Min"
haftmann@26732
  2028
  "eq_class.eq (Int.Bit0 k1) (Int.Bit0 k2) \<longleftrightarrow> eq_class.eq k1 k2"
haftmann@26732
  2029
  "eq_class.eq (Int.Bit0 k1) (Int.Bit1 k2) \<longleftrightarrow> False"
haftmann@26732
  2030
  "eq_class.eq (Int.Bit1 k1) (Int.Bit0 k2) \<longleftrightarrow> False"
haftmann@26732
  2031
  "eq_class.eq (Int.Bit1 k1) (Int.Bit1 k2) \<longleftrightarrow> eq_class.eq k1 k2"
huffman@28958
  2032
  unfolding eq_equals by simp_all
haftmann@25919
  2033
haftmann@28351
  2034
lemma eq_int_refl [code nbe]:
haftmann@28351
  2035
  "eq_class.eq (k::int) k \<longleftrightarrow> True"
haftmann@28351
  2036
  by (rule HOL.eq_refl)
haftmann@28351
  2037
haftmann@28562
  2038
lemma less_eq_number_of_int_code [code]:
haftmann@26507
  2039
  "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
haftmann@26507
  2040
  unfolding number_of_is_id ..
haftmann@26507
  2041
haftmann@28562
  2042
lemma less_eq_int_code [code]:
haftmann@26507
  2043
  "Int.Pls \<le> Int.Pls \<longleftrightarrow> True"
haftmann@26507
  2044
  "Int.Pls \<le> Int.Min \<longleftrightarrow> False"
haftmann@26507
  2045
  "Int.Pls \<le> Int.Bit0 k \<longleftrightarrow> Int.Pls \<le> k"
haftmann@26507
  2046
  "Int.Pls \<le> Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
haftmann@26507
  2047
  "Int.Min \<le> Int.Pls \<longleftrightarrow> True"
haftmann@26507
  2048
  "Int.Min \<le> Int.Min \<longleftrightarrow> True"
haftmann@26507
  2049
  "Int.Min \<le> Int.Bit0 k \<longleftrightarrow> Int.Min < k"
haftmann@26507
  2050
  "Int.Min \<le> Int.Bit1 k \<longleftrightarrow> Int.Min \<le> k"
haftmann@26507
  2051
  "Int.Bit0 k \<le> Int.Pls \<longleftrightarrow> k \<le> Int.Pls"
haftmann@26507
  2052
  "Int.Bit1 k \<le> Int.Pls \<longleftrightarrow> k < Int.Pls"
haftmann@26507
  2053
  "Int.Bit0 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
haftmann@26507
  2054
  "Int.Bit1 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
haftmann@26507
  2055
  "Int.Bit0 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 \<le> k2"
haftmann@26507
  2056
  "Int.Bit0 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
haftmann@26507
  2057
  "Int.Bit1 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
haftmann@26507
  2058
  "Int.Bit1 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
huffman@28958
  2059
  by simp_all
haftmann@26507
  2060
haftmann@28562
  2061
lemma less_number_of_int_code [code]:
haftmann@26507
  2062
  "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
haftmann@26507
  2063
  unfolding number_of_is_id ..
haftmann@26507
  2064
haftmann@28562
  2065
lemma less_int_code [code]:
haftmann@26507
  2066
  "Int.Pls < Int.Pls \<longleftrightarrow> False"
haftmann@26507
  2067
  "Int.Pls < Int.Min \<longleftrightarrow> False"
haftmann@26507
  2068
  "Int.Pls < Int.Bit0 k \<longleftrightarrow> Int.Pls < k"
haftmann@26507
  2069
  "Int.Pls < Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
haftmann@26507
  2070
  "Int.Min < Int.Pls \<longleftrightarrow> True"
haftmann@26507
  2071
  "Int.Min < Int.Min \<longleftrightarrow> False"
haftmann@26507
  2072
  "Int.Min < Int.Bit0 k \<longleftrightarrow> Int.Min < k"
haftmann@26507
  2073
  "Int.Min < Int.Bit1 k \<longleftrightarrow> Int.Min < k"
haftmann@26507
  2074
  "Int.Bit0 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
haftmann@26507
  2075
  "Int.Bit1 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
haftmann@26507
  2076
  "Int.Bit0 k < Int.Min \<longleftrightarrow> k \<le> Int.Min"
haftmann@26507
  2077
  "Int.Bit1 k < Int.Min \<longleftrightarrow> k < Int.Min"
haftmann@26507
  2078
  "Int.Bit0 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
haftmann@26507
  2079
  "Int.Bit0 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
haftmann@26507
  2080
  "Int.Bit1 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
haftmann@26507
  2081
  "Int.Bit1 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 < k2"
huffman@28958
  2082
  by simp_all
haftmann@25919
  2083
haftmann@25919
  2084
definition
haftmann@25919
  2085
  nat_aux :: "int \<Rightarrow> nat \<Rightarrow> nat" where
haftmann@25919
  2086
  "nat_aux i n = nat i + n"
haftmann@25919
  2087
haftmann@25919
  2088
lemma [code]:
haftmann@25919
  2089
  "nat_aux i n = (if i \<le> 0 then n else nat_aux (i - 1) (Suc n))"  -- {* tail recursive *}
haftmann@25919
  2090
  by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le
haftmann@25919
  2091
    dest: zless_imp_add1_zle)
haftmann@25919
  2092
haftmann@25919
  2093
lemma [code]: "nat i = nat_aux i 0"
haftmann@25919
  2094
  by (simp add: nat_aux_def)
haftmann@25919
  2095
haftmann@28514
  2096
hide (open) const nat_aux
haftmann@25928
  2097
haftmann@28562
  2098
lemma zero_is_num_zero [code, code inline, symmetric, code post]:
haftmann@25919
  2099
  "(0\<Colon>int) = Numeral0" 
haftmann@25919
  2100
  by simp
haftmann@25919
  2101
haftmann@28562
  2102
lemma one_is_num_one [code, code inline, symmetric, code post]:
haftmann@25919
  2103
  "(1\<Colon>int) = Numeral1" 
haftmann@25961
  2104
  by simp
haftmann@25919
  2105
haftmann@25919
  2106
code_modulename SML
haftmann@25928
  2107
  Int Integer
haftmann@25919
  2108
haftmann@25919
  2109
code_modulename OCaml
haftmann@25928
  2110
  Int Integer
haftmann@25919
  2111
haftmann@25919
  2112
code_modulename Haskell
haftmann@25928
  2113
  Int Integer
haftmann@25919
  2114
haftmann@25919
  2115
types_code
haftmann@25919
  2116
  "int" ("int")
haftmann@25919
  2117
attach (term_of) {*
haftmann@25919
  2118
val term_of_int = HOLogic.mk_number HOLogic.intT;
haftmann@25919
  2119
*}
haftmann@25919
  2120
attach (test) {*
haftmann@25919
  2121
fun gen_int i =
haftmann@25919
  2122
  let val j = one_of [~1, 1] * random_range 0 i
haftmann@25919
  2123
  in (j, fn () => term_of_int j) end;
haftmann@25919
  2124
*}
haftmann@25919
  2125
haftmann@25919
  2126
setup {*
haftmann@25919
  2127
let
haftmann@25919
  2128
haftmann@25919
  2129
fun strip_number_of (@{term "Int.number_of :: int => int"} $ t) = t
haftmann@25919
  2130
  | strip_number_of t = t;
haftmann@25919
  2131
haftmann@28537
  2132
fun numeral_codegen thy defs dep module b t gr =
haftmann@25919
  2133
  let val i = HOLogic.dest_numeral (strip_number_of t)
haftmann@25919
  2134
  in
haftmann@28537
  2135
    SOME (Codegen.str (string_of_int i),
haftmann@28537
  2136
      snd (Codegen.invoke_tycodegen thy defs dep module false HOLogic.intT gr))
haftmann@25919
  2137
  end handle TERM _ => NONE;
haftmann@25919
  2138
haftmann@25919
  2139
in
haftmann@25919
  2140
haftmann@25919
  2141
Codegen.add_codegen "numeral_codegen" numeral_codegen
haftmann@25919
  2142
haftmann@25919
  2143
end
haftmann@25919
  2144
*}
haftmann@25919
  2145
haftmann@25919
  2146
consts_code
haftmann@25919
  2147
  "number_of :: int \<Rightarrow> int"    ("(_)")
haftmann@25919
  2148
  "0 :: int"                   ("0")
haftmann@25919
  2149
  "1 :: int"                   ("1")
haftmann@25919
  2150
  "uminus :: int => int"       ("~")
haftmann@25919
  2151
  "op + :: int => int => int"  ("(_ +/ _)")
haftmann@25919
  2152
  "op * :: int => int => int"  ("(_ */ _)")
haftmann@25919
  2153
  "op \<le> :: int => int => bool" ("(_ <=/ _)")
haftmann@25919
  2154
  "op < :: int => int => bool" ("(_ </ _)")
haftmann@25919
  2155
haftmann@25919
  2156
quickcheck_params [default_type = int]
haftmann@25919
  2157
huffman@26086
  2158
hide (open) const Pls Min Bit0 Bit1 succ pred
haftmann@25919
  2159
haftmann@25919
  2160
haftmann@25919
  2161
subsection {* Legacy theorems *}
haftmann@25919
  2162
haftmann@25919
  2163
lemmas zminus_zminus = minus_minus [of "z::int", standard]
haftmann@25919
  2164
lemmas zminus_0 = minus_zero [where 'a=int]
haftmann@25919
  2165
lemmas zminus_zadd_distrib = minus_add_distrib [of "z::int" "w", standard]
haftmann@25919
  2166
lemmas zadd_commute = add_commute [of "z::int" "w", standard]
haftmann@25919
  2167
lemmas zadd_assoc = add_assoc [of "z1::int" "z2" "z3", standard]
haftmann@25919
  2168
lemmas zadd_left_commute = add_left_commute [of "x::int" "y" "z", standard]
haftmann@25919
  2169
lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
haftmann@25919
  2170
lemmas zmult_ac = OrderedGroup.mult_ac
haftmann@25919
  2171
lemmas zadd_0 = OrderedGroup.add_0_left [of "z::int", standard]
haftmann@25919
  2172
lemmas zadd_0_right = OrderedGroup.add_0_left [of "z::int", standard]
haftmann@25919
  2173
lemmas zadd_zminus_inverse2 = left_minus [of "z::int", standard]
haftmann@25919
  2174
lemmas zmult_zminus = mult_minus_left [of "z::int" "w", standard]
haftmann@25919
  2175
lemmas zmult_commute = mult_commute [of "z::int" "w", standard]
haftmann@25919
  2176
lemmas zmult_assoc = mult_assoc [of "z1::int" "z2" "z3", standard]
haftmann@25919
  2177
lemmas zadd_zmult_distrib = left_distrib [of "z1::int" "z2" "w", standard]
haftmann@25919
  2178
lemmas zadd_zmult_distrib2 = right_distrib [of "w::int" "z1" "z2", standard]
haftmann@25919
  2179
lemmas zdiff_zmult_distrib = left_diff_distrib [of "z1::int" "z2" "w", standard]
haftmann@25919
  2180
lemmas zdiff_zmult_distrib2 = right_diff_distrib [of "w::int" "z1" "z2", standard]
haftmann@25919
  2181
haftmann@25919
  2182
lemmas zmult_1 = mult_1_left [of "z::int", standard]
haftmann@25919
  2183
lemmas zmult_1_right = mult_1_right [of "z::int", standard]
haftmann@25919
  2184
haftmann@25919
  2185
lemmas zle_refl = order_refl [of "w::int", standard]
haftmann@25919
  2186
lemmas zle_trans = order_trans [where 'a=int and x="i" and y="j" and z="k", standard]
haftmann@25919
  2187
lemmas zle_anti_sym = order_antisym [of "z::int" "w", standard]
haftmann@25919
  2188
lemmas zle_linear = linorder_linear [of "z::int" "w", standard]
haftmann@25919
  2189
lemmas zless_linear = linorder_less_linear [where 'a = int]
haftmann@25919
  2190
haftmann@25919
  2191
lemmas zadd_left_mono = add_left_mono [of "i::int" "j" "k", standard]
haftmann@25919
  2192
lemmas zadd_strict_right_mono = add_strict_right_mono [of "i::int" "j" "k", standard]
haftmann@25919
  2193
lemmas zadd_zless_mono = add_less_le_mono [of "w'::int" "w" "z'" "z", standard]
haftmann@25919
  2194
haftmann@25919
  2195
lemmas int_0_less_1 = zero_less_one [where 'a=int]
haftmann@25919
  2196
lemmas int_0_neq_1 = zero_neq_one [where 'a=int]
haftmann@25919
  2197
haftmann@25919
  2198
lemmas inj_int = inj_of_nat [where 'a=int]
haftmann@25919
  2199
lemmas zadd_int = of_nat_add [where 'a=int, symmetric]
haftmann@25919
  2200
lemmas int_mult = of_nat_mult [where 'a=int]
haftmann@25919
  2201
lemmas zmult_int = of_nat_mult [where 'a=int, symmetric]
haftmann@25919
  2202
lemmas int_eq_0_conv = of_nat_eq_0_iff [where 'a=int and m="n", standard]
haftmann@25919
  2203
lemmas zless_int = of_nat_less_iff [where 'a=int]
haftmann@25919
  2204
lemmas int_less_0_conv = of_nat_less_0_iff [where 'a=int and m="k", standard]
haftmann@25919
  2205
lemmas zero_less_int_conv = of_nat_0_less_iff [where 'a=int]
haftmann@25919
  2206
lemmas zero_zle_int = of_nat_0_le_iff [where 'a=int]
haftmann@25919
  2207
lemmas int_le_0_conv = of_nat_le_0_iff [where 'a=int and m="n", standard]
haftmann@25919
  2208
lemmas int_0 = of_nat_0 [where 'a=int]
haftmann@25919
  2209
lemmas int_1 = of_nat_1 [where 'a=int]
haftmann@25919
  2210
lemmas int_Suc = of_nat_Suc [where 'a=int]
haftmann@25919
  2211
lemmas abs_int_eq = abs_of_nat [where 'a=int and n="m", standard]
haftmann@25919
  2212
lemmas of_int_int_eq = of_int_of_nat_eq [where 'a=int]
haftmann@25919
  2213
lemmas zdiff_int = of_nat_diff [where 'a=int, symmetric]
haftmann@25919
  2214
lemmas zless_le = less_int_def
haftmann@25919
  2215
lemmas int_eq_of_nat = TrueI
haftmann@25919
  2216
haftmann@25919
  2217
end