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