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