src/HOL/IntDef.thy
author huffman
Wed Jun 06 20:49:04 2007 +0200 (2007-06-06)
changeset 23282 dfc459989d24
parent 23276 a70934b63910
child 23299 292b1cbd05dc
permissions -rw-r--r--
add axclass semiring_char_0 for types where of_nat is injective
wenzelm@23164
     1
(*  Title:      IntDef.thy
wenzelm@23164
     2
    ID:         $Id$
wenzelm@23164
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@23164
     4
    Copyright   1996  University of Cambridge
wenzelm@23164
     5
wenzelm@23164
     6
*)
wenzelm@23164
     7
wenzelm@23164
     8
header{*The Integers as Equivalence Classes over Pairs of Natural Numbers*} 
wenzelm@23164
     9
wenzelm@23164
    10
theory IntDef
wenzelm@23164
    11
imports Equiv_Relations Nat
wenzelm@23164
    12
begin
wenzelm@23164
    13
wenzelm@23164
    14
text {* the equivalence relation underlying the integers *}
wenzelm@23164
    15
wenzelm@23164
    16
definition
wenzelm@23164
    17
  intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set"
wenzelm@23164
    18
where
wenzelm@23164
    19
  "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
wenzelm@23164
    20
wenzelm@23164
    21
typedef (Integ)
wenzelm@23164
    22
  int = "UNIV//intrel"
wenzelm@23164
    23
  by (auto simp add: quotient_def)
wenzelm@23164
    24
wenzelm@23164
    25
definition
wenzelm@23164
    26
  int :: "nat \<Rightarrow> int"
wenzelm@23164
    27
where
wenzelm@23164
    28
  [code func del]: "int m = Abs_Integ (intrel `` {(m, 0)})"
wenzelm@23164
    29
wenzelm@23164
    30
instance int :: zero
wenzelm@23164
    31
  Zero_int_def: "0 \<equiv> int 0" ..
wenzelm@23164
    32
wenzelm@23164
    33
instance int :: one
wenzelm@23164
    34
  One_int_def: "1 \<equiv> int 1" ..
wenzelm@23164
    35
wenzelm@23164
    36
instance int :: plus
wenzelm@23164
    37
  add_int_def: "z + w \<equiv> Abs_Integ
wenzelm@23164
    38
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u, v) \<in> Rep_Integ w.
wenzelm@23164
    39
      intrel `` {(x + u, y + v)})" ..
wenzelm@23164
    40
wenzelm@23164
    41
instance int :: minus
wenzelm@23164
    42
  minus_int_def:
wenzelm@23164
    43
    "- z \<equiv> Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
wenzelm@23164
    44
  diff_int_def:  "z - w \<equiv> z + (-w)" ..
wenzelm@23164
    45
wenzelm@23164
    46
instance int :: times
wenzelm@23164
    47
  mult_int_def: "z * w \<equiv>  Abs_Integ
wenzelm@23164
    48
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
wenzelm@23164
    49
      intrel `` {(x*u + y*v, x*v + y*u)})" ..
wenzelm@23164
    50
wenzelm@23164
    51
instance int :: ord
wenzelm@23164
    52
  le_int_def:
wenzelm@23164
    53
   "z \<le> w \<equiv> \<exists>x y u v. x+v \<le> u+y \<and> (x, y) \<in> Rep_Integ z \<and> (u, v) \<in> Rep_Integ w"
wenzelm@23164
    54
  less_int_def: "z < w \<equiv> z \<le> w \<and> z \<noteq> w" ..
wenzelm@23164
    55
wenzelm@23164
    56
lemmas [code func del] = Zero_int_def One_int_def add_int_def
wenzelm@23164
    57
  minus_int_def mult_int_def le_int_def less_int_def
wenzelm@23164
    58
wenzelm@23164
    59
wenzelm@23164
    60
subsection{*Construction of the Integers*}
wenzelm@23164
    61
wenzelm@23164
    62
subsubsection{*Preliminary Lemmas about the Equivalence Relation*}
wenzelm@23164
    63
wenzelm@23164
    64
lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
wenzelm@23164
    65
by (simp add: intrel_def)
wenzelm@23164
    66
wenzelm@23164
    67
lemma equiv_intrel: "equiv UNIV intrel"
wenzelm@23164
    68
by (simp add: intrel_def equiv_def refl_def sym_def trans_def)
wenzelm@23164
    69
wenzelm@23164
    70
text{*Reduces equality of equivalence classes to the @{term intrel} relation:
wenzelm@23164
    71
  @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
wenzelm@23164
    72
lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
wenzelm@23164
    73
wenzelm@23164
    74
text{*All equivalence classes belong to set of representatives*}
wenzelm@23164
    75
lemma [simp]: "intrel``{(x,y)} \<in> Integ"
wenzelm@23164
    76
by (auto simp add: Integ_def intrel_def quotient_def)
wenzelm@23164
    77
wenzelm@23164
    78
text{*Reduces equality on abstractions to equality on representatives:
wenzelm@23164
    79
  @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
wenzelm@23164
    80
declare Abs_Integ_inject [simp]  Abs_Integ_inverse [simp]
wenzelm@23164
    81
wenzelm@23164
    82
text{*Case analysis on the representation of an integer as an equivalence
wenzelm@23164
    83
      class of pairs of naturals.*}
wenzelm@23164
    84
lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
wenzelm@23164
    85
     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
wenzelm@23164
    86
apply (rule Abs_Integ_cases [of z]) 
wenzelm@23164
    87
apply (auto simp add: Integ_def quotient_def) 
wenzelm@23164
    88
done
wenzelm@23164
    89
wenzelm@23164
    90
wenzelm@23164
    91
subsubsection{*@{term int}: Embedding the Naturals into the Integers*}
wenzelm@23164
    92
wenzelm@23164
    93
lemma inj_int: "inj int"
wenzelm@23164
    94
by (simp add: inj_on_def int_def)
wenzelm@23164
    95
wenzelm@23164
    96
lemma int_int_eq [iff]: "(int m = int n) = (m = n)"
wenzelm@23164
    97
by (fast elim!: inj_int [THEN injD])
wenzelm@23164
    98
wenzelm@23164
    99
wenzelm@23164
   100
subsubsection{*Integer Unary Negation*}
wenzelm@23164
   101
wenzelm@23164
   102
lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
wenzelm@23164
   103
proof -
wenzelm@23164
   104
  have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
wenzelm@23164
   105
    by (simp add: congruent_def) 
wenzelm@23164
   106
  thus ?thesis
wenzelm@23164
   107
    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
wenzelm@23164
   108
qed
wenzelm@23164
   109
wenzelm@23164
   110
lemma zminus_zminus: "- (- z) = (z::int)"
wenzelm@23164
   111
  by (cases z) (simp add: minus)
wenzelm@23164
   112
wenzelm@23164
   113
lemma zminus_0: "- 0 = (0::int)"
wenzelm@23164
   114
  by (simp add: int_def Zero_int_def minus)
wenzelm@23164
   115
wenzelm@23164
   116
wenzelm@23164
   117
subsection{*Integer Addition*}
wenzelm@23164
   118
wenzelm@23164
   119
lemma add:
wenzelm@23164
   120
     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
wenzelm@23164
   121
      Abs_Integ (intrel``{(x+u, y+v)})"
wenzelm@23164
   122
proof -
wenzelm@23164
   123
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z) 
wenzelm@23164
   124
        respects2 intrel"
wenzelm@23164
   125
    by (simp add: congruent2_def)
wenzelm@23164
   126
  thus ?thesis
wenzelm@23164
   127
    by (simp add: add_int_def UN_UN_split_split_eq
wenzelm@23164
   128
                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
wenzelm@23164
   129
qed
wenzelm@23164
   130
wenzelm@23164
   131
lemma zminus_zadd_distrib: "- (z + w) = (- z) + (- w::int)"
wenzelm@23164
   132
  by (cases z, cases w) (simp add: minus add)
wenzelm@23164
   133
wenzelm@23164
   134
lemma zadd_commute: "(z::int) + w = w + z"
wenzelm@23164
   135
  by (cases z, cases w) (simp add: add_ac add)
wenzelm@23164
   136
wenzelm@23164
   137
lemma zadd_assoc: "((z1::int) + z2) + z3 = z1 + (z2 + z3)"
wenzelm@23164
   138
  by (cases z1, cases z2, cases z3) (simp add: add add_assoc)
wenzelm@23164
   139
wenzelm@23164
   140
(*For AC rewriting*)
wenzelm@23164
   141
lemma zadd_left_commute: "x + (y + z) = y + ((x + z)  ::int)"
wenzelm@23164
   142
  apply (rule mk_left_commute [of "op +"])
wenzelm@23164
   143
  apply (rule zadd_assoc)
wenzelm@23164
   144
  apply (rule zadd_commute)
wenzelm@23164
   145
  done
wenzelm@23164
   146
wenzelm@23164
   147
lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
wenzelm@23164
   148
wenzelm@23164
   149
lemmas zmult_ac = OrderedGroup.mult_ac
wenzelm@23164
   150
wenzelm@23164
   151
lemma zadd_int: "(int m) + (int n) = int (m + n)"
wenzelm@23164
   152
  by (simp add: int_def add)
wenzelm@23164
   153
wenzelm@23164
   154
lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
wenzelm@23164
   155
  by (simp add: zadd_int zadd_assoc [symmetric])
wenzelm@23164
   156
wenzelm@23164
   157
(*also for the instance declaration int :: comm_monoid_add*)
wenzelm@23164
   158
lemma zadd_0: "(0::int) + z = z"
wenzelm@23164
   159
apply (simp add: Zero_int_def int_def)
wenzelm@23164
   160
apply (cases z, simp add: add)
wenzelm@23164
   161
done
wenzelm@23164
   162
wenzelm@23164
   163
lemma zadd_0_right: "z + (0::int) = z"
wenzelm@23164
   164
by (rule trans [OF zadd_commute zadd_0])
wenzelm@23164
   165
wenzelm@23164
   166
lemma zadd_zminus_inverse2: "(- z) + z = (0::int)"
wenzelm@23164
   167
by (cases z, simp add: int_def Zero_int_def minus add)
wenzelm@23164
   168
wenzelm@23164
   169
wenzelm@23164
   170
subsection{*Integer Multiplication*}
wenzelm@23164
   171
wenzelm@23164
   172
text{*Congruence property for multiplication*}
wenzelm@23164
   173
lemma mult_congruent2:
wenzelm@23164
   174
     "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
wenzelm@23164
   175
      respects2 intrel"
wenzelm@23164
   176
apply (rule equiv_intrel [THEN congruent2_commuteI])
wenzelm@23164
   177
 apply (force simp add: mult_ac, clarify) 
wenzelm@23164
   178
apply (simp add: congruent_def mult_ac)  
wenzelm@23164
   179
apply (rename_tac u v w x y z)
wenzelm@23164
   180
apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
wenzelm@23164
   181
apply (simp add: mult_ac)
wenzelm@23164
   182
apply (simp add: add_mult_distrib [symmetric])
wenzelm@23164
   183
done
wenzelm@23164
   184
wenzelm@23164
   185
wenzelm@23164
   186
lemma mult:
wenzelm@23164
   187
     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
wenzelm@23164
   188
      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
wenzelm@23164
   189
by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
wenzelm@23164
   190
              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
wenzelm@23164
   191
wenzelm@23164
   192
lemma zmult_zminus: "(- z) * w = - (z * (w::int))"
wenzelm@23164
   193
by (cases z, cases w, simp add: minus mult add_ac)
wenzelm@23164
   194
wenzelm@23164
   195
lemma zmult_commute: "(z::int) * w = w * z"
wenzelm@23164
   196
by (cases z, cases w, simp add: mult add_ac mult_ac)
wenzelm@23164
   197
wenzelm@23164
   198
lemma zmult_assoc: "((z1::int) * z2) * z3 = z1 * (z2 * z3)"
wenzelm@23164
   199
by (cases z1, cases z2, cases z3, simp add: mult add_mult_distrib2 mult_ac)
wenzelm@23164
   200
wenzelm@23164
   201
lemma zadd_zmult_distrib: "((z1::int) + z2) * w = (z1 * w) + (z2 * w)"
wenzelm@23164
   202
by (cases z1, cases z2, cases w, simp add: add mult add_mult_distrib2 mult_ac)
wenzelm@23164
   203
wenzelm@23164
   204
lemma zadd_zmult_distrib2: "(w::int) * (z1 + z2) = (w * z1) + (w * z2)"
wenzelm@23164
   205
by (simp add: zmult_commute [of w] zadd_zmult_distrib)
wenzelm@23164
   206
wenzelm@23164
   207
lemma zdiff_zmult_distrib: "((z1::int) - z2) * w = (z1 * w) - (z2 * w)"
wenzelm@23164
   208
by (simp add: diff_int_def zadd_zmult_distrib zmult_zminus)
wenzelm@23164
   209
wenzelm@23164
   210
lemma zdiff_zmult_distrib2: "(w::int) * (z1 - z2) = (w * z1) - (w * z2)"
wenzelm@23164
   211
by (simp add: zmult_commute [of w] zdiff_zmult_distrib)
wenzelm@23164
   212
wenzelm@23164
   213
lemmas int_distrib =
wenzelm@23164
   214
  zadd_zmult_distrib zadd_zmult_distrib2
wenzelm@23164
   215
  zdiff_zmult_distrib zdiff_zmult_distrib2
wenzelm@23164
   216
wenzelm@23164
   217
lemma int_mult: "int (m * n) = (int m) * (int n)"
wenzelm@23164
   218
by (simp add: int_def mult)
wenzelm@23164
   219
wenzelm@23164
   220
text{*Compatibility binding*}
wenzelm@23164
   221
lemmas zmult_int = int_mult [symmetric]
wenzelm@23164
   222
wenzelm@23164
   223
lemma zmult_1: "(1::int) * z = z"
wenzelm@23164
   224
by (cases z, simp add: One_int_def int_def mult)
wenzelm@23164
   225
wenzelm@23164
   226
lemma zmult_1_right: "z * (1::int) = z"
wenzelm@23164
   227
by (rule trans [OF zmult_commute zmult_1])
wenzelm@23164
   228
wenzelm@23164
   229
wenzelm@23164
   230
text{*The integers form a @{text comm_ring_1}*}
wenzelm@23164
   231
instance int :: comm_ring_1
wenzelm@23164
   232
proof
wenzelm@23164
   233
  fix i j k :: int
wenzelm@23164
   234
  show "(i + j) + k = i + (j + k)" by (simp add: zadd_assoc)
wenzelm@23164
   235
  show "i + j = j + i" by (simp add: zadd_commute)
wenzelm@23164
   236
  show "0 + i = i" by (rule zadd_0)
wenzelm@23164
   237
  show "- i + i = 0" by (rule zadd_zminus_inverse2)
wenzelm@23164
   238
  show "i - j = i + (-j)" by (simp add: diff_int_def)
wenzelm@23164
   239
  show "(i * j) * k = i * (j * k)" by (rule zmult_assoc)
wenzelm@23164
   240
  show "i * j = j * i" by (rule zmult_commute)
wenzelm@23164
   241
  show "1 * i = i" by (rule zmult_1) 
wenzelm@23164
   242
  show "(i + j) * k = i * k + j * k" by (simp add: int_distrib)
wenzelm@23164
   243
  show "0 \<noteq> (1::int)"
wenzelm@23164
   244
    by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
wenzelm@23164
   245
qed
wenzelm@23164
   246
wenzelm@23164
   247
wenzelm@23164
   248
subsection{*The @{text "\<le>"} Ordering*}
wenzelm@23164
   249
wenzelm@23164
   250
lemma le:
wenzelm@23164
   251
  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
wenzelm@23164
   252
by (force simp add: le_int_def)
wenzelm@23164
   253
wenzelm@23164
   254
lemma zle_refl: "w \<le> (w::int)"
wenzelm@23164
   255
by (cases w, simp add: le)
wenzelm@23164
   256
wenzelm@23164
   257
lemma zle_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::int)"
wenzelm@23164
   258
by (cases i, cases j, cases k, simp add: le)
wenzelm@23164
   259
wenzelm@23164
   260
lemma zle_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::int)"
wenzelm@23164
   261
by (cases w, cases z, simp add: le)
wenzelm@23164
   262
wenzelm@23164
   263
instance int :: order
wenzelm@23164
   264
  by intro_classes
wenzelm@23164
   265
    (assumption |
wenzelm@23164
   266
      rule zle_refl zle_trans zle_anti_sym less_int_def [THEN meta_eq_to_obj_eq])+
wenzelm@23164
   267
wenzelm@23164
   268
lemma zle_linear: "(z::int) \<le> w \<or> w \<le> z"
wenzelm@23164
   269
by (cases z, cases w) (simp add: le linorder_linear)
wenzelm@23164
   270
wenzelm@23164
   271
instance int :: linorder
wenzelm@23164
   272
  by intro_classes (rule zle_linear)
wenzelm@23164
   273
wenzelm@23164
   274
lemmas zless_linear = linorder_less_linear [where 'a = int]
wenzelm@23164
   275
wenzelm@23164
   276
wenzelm@23164
   277
lemma int_eq_0_conv [simp]: "(int n = 0) = (n = 0)"
wenzelm@23164
   278
by (simp add: Zero_int_def)
wenzelm@23164
   279
wenzelm@23164
   280
lemma zless_int [simp]: "(int m < int n) = (m<n)"
wenzelm@23164
   281
by (simp add: le add int_def linorder_not_le [symmetric]) 
wenzelm@23164
   282
wenzelm@23164
   283
lemma int_less_0_conv [simp]: "~ (int k < 0)"
wenzelm@23164
   284
by (simp add: Zero_int_def)
wenzelm@23164
   285
wenzelm@23164
   286
lemma zero_less_int_conv [simp]: "(0 < int n) = (0 < n)"
wenzelm@23164
   287
by (simp add: Zero_int_def)
wenzelm@23164
   288
wenzelm@23164
   289
lemma int_0_less_1: "0 < (1::int)"
wenzelm@23164
   290
by (simp only: Zero_int_def One_int_def One_nat_def zless_int)
wenzelm@23164
   291
wenzelm@23164
   292
lemma int_0_neq_1 [simp]: "0 \<noteq> (1::int)"
wenzelm@23164
   293
by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
wenzelm@23164
   294
wenzelm@23164
   295
lemma zle_int [simp]: "(int m \<le> int n) = (m\<le>n)"
wenzelm@23164
   296
by (simp add: linorder_not_less [symmetric])
wenzelm@23164
   297
wenzelm@23164
   298
lemma zero_zle_int [simp]: "(0 \<le> int n)"
wenzelm@23164
   299
by (simp add: Zero_int_def)
wenzelm@23164
   300
wenzelm@23164
   301
lemma int_le_0_conv [simp]: "(int n \<le> 0) = (n = 0)"
wenzelm@23164
   302
by (simp add: Zero_int_def)
wenzelm@23164
   303
wenzelm@23164
   304
lemma int_0 [simp]: "int 0 = (0::int)"
wenzelm@23164
   305
by (simp add: Zero_int_def)
wenzelm@23164
   306
wenzelm@23164
   307
lemma int_1 [simp]: "int 1 = 1"
wenzelm@23164
   308
by (simp add: One_int_def)
wenzelm@23164
   309
wenzelm@23164
   310
lemma int_Suc0_eq_1: "int (Suc 0) = 1"
wenzelm@23164
   311
by (simp add: One_int_def One_nat_def)
wenzelm@23164
   312
wenzelm@23164
   313
lemma int_Suc: "int (Suc m) = 1 + (int m)"
wenzelm@23164
   314
by (simp add: One_int_def zadd_int)
wenzelm@23164
   315
wenzelm@23164
   316
wenzelm@23164
   317
subsection{*Monotonicity results*}
wenzelm@23164
   318
wenzelm@23164
   319
lemma zadd_left_mono: "i \<le> j ==> k + i \<le> k + (j::int)"
wenzelm@23164
   320
by (cases i, cases j, cases k, simp add: le add)
wenzelm@23164
   321
wenzelm@23164
   322
lemma zadd_strict_right_mono: "i < j ==> i + k < j + (k::int)"
wenzelm@23164
   323
apply (cases i, cases j, cases k)
wenzelm@23164
   324
apply (simp add: linorder_not_le [where 'a = int, symmetric]
wenzelm@23164
   325
                 linorder_not_le [where 'a = nat]  le add)
wenzelm@23164
   326
done
wenzelm@23164
   327
wenzelm@23164
   328
lemma zadd_zless_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::int)"
wenzelm@23164
   329
by (rule order_less_le_trans [OF zadd_strict_right_mono zadd_left_mono])
wenzelm@23164
   330
wenzelm@23164
   331
wenzelm@23164
   332
subsection{*Strict Monotonicity of Multiplication*}
wenzelm@23164
   333
wenzelm@23164
   334
text{*strict, in 1st argument; proof is by induction on k>0*}
wenzelm@23164
   335
lemma zmult_zless_mono2_lemma:
wenzelm@23164
   336
     "i<j ==> 0<k ==> int k * i < int k * j"
wenzelm@23164
   337
apply (induct "k", simp)
wenzelm@23164
   338
apply (simp add: int_Suc)
wenzelm@23164
   339
apply (case_tac "k=0")
wenzelm@23164
   340
apply (simp_all add: zadd_zmult_distrib int_Suc0_eq_1 order_le_less)
wenzelm@23164
   341
apply (simp add: zadd_zless_mono int_Suc0_eq_1 order_le_less)
wenzelm@23164
   342
done
wenzelm@23164
   343
wenzelm@23164
   344
lemma zero_le_imp_eq_int: "0 \<le> k ==> \<exists>n. k = int n"
wenzelm@23164
   345
apply (cases k)
wenzelm@23164
   346
apply (auto simp add: le add int_def Zero_int_def)
wenzelm@23164
   347
apply (rule_tac x="x-y" in exI, simp)
wenzelm@23164
   348
done
wenzelm@23164
   349
wenzelm@23164
   350
lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
wenzelm@23164
   351
apply (frule order_less_imp_le [THEN zero_le_imp_eq_int])
wenzelm@23164
   352
apply (auto simp add: zmult_zless_mono2_lemma)
wenzelm@23164
   353
done
wenzelm@23164
   354
wenzelm@23164
   355
instance int :: minus
wenzelm@23164
   356
  zabs_def: "\<bar>i\<Colon>int\<bar> \<equiv> if i < 0 then - i else i" ..
wenzelm@23164
   357
wenzelm@23164
   358
instance int :: distrib_lattice
wenzelm@23164
   359
  "inf \<equiv> min"
wenzelm@23164
   360
  "sup \<equiv> max"
wenzelm@23164
   361
  by intro_classes
wenzelm@23164
   362
    (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
wenzelm@23164
   363
wenzelm@23164
   364
text{*The integers form an ordered @{text comm_ring_1}*}
wenzelm@23164
   365
instance int :: ordered_idom
wenzelm@23164
   366
proof
wenzelm@23164
   367
  fix i j k :: int
wenzelm@23164
   368
  show "i \<le> j ==> k + i \<le> k + j" by (rule zadd_left_mono)
wenzelm@23164
   369
  show "i < j ==> 0 < k ==> k * i < k * j" by (rule zmult_zless_mono2)
wenzelm@23164
   370
  show "\<bar>i\<bar> = (if i < 0 then -i else i)" by (simp only: zabs_def)
wenzelm@23164
   371
qed
wenzelm@23164
   372
wenzelm@23164
   373
lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
wenzelm@23164
   374
apply (cases w, cases z) 
wenzelm@23164
   375
apply (simp add: linorder_not_le [symmetric] le int_def add One_int_def)
wenzelm@23164
   376
done
wenzelm@23164
   377
wenzelm@23164
   378
subsection{*Magnitide of an Integer, as a Natural Number: @{term nat}*}
wenzelm@23164
   379
wenzelm@23164
   380
definition
wenzelm@23164
   381
  nat :: "int \<Rightarrow> nat"
wenzelm@23164
   382
where
wenzelm@23164
   383
  [code func del]: "nat z = contents (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
wenzelm@23164
   384
wenzelm@23164
   385
lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
wenzelm@23164
   386
proof -
wenzelm@23164
   387
  have "(\<lambda>(x,y). {x-y}) respects intrel"
wenzelm@23164
   388
    by (simp add: congruent_def) arith
wenzelm@23164
   389
  thus ?thesis
wenzelm@23164
   390
    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
wenzelm@23164
   391
qed
wenzelm@23164
   392
wenzelm@23164
   393
lemma nat_int [simp]: "nat(int n) = n"
wenzelm@23164
   394
by (simp add: nat int_def) 
wenzelm@23164
   395
wenzelm@23164
   396
lemma nat_zero [simp]: "nat 0 = 0"
wenzelm@23164
   397
by (simp only: Zero_int_def nat_int)
wenzelm@23164
   398
wenzelm@23164
   399
lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
wenzelm@23164
   400
by (cases z, simp add: nat le int_def Zero_int_def)
wenzelm@23164
   401
wenzelm@23164
   402
corollary nat_0_le: "0 \<le> z ==> int (nat z) = z"
wenzelm@23164
   403
by simp
wenzelm@23164
   404
wenzelm@23164
   405
lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
wenzelm@23164
   406
by (cases z, simp add: nat le int_def Zero_int_def)
wenzelm@23164
   407
wenzelm@23164
   408
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
wenzelm@23164
   409
apply (cases w, cases z) 
wenzelm@23164
   410
apply (simp add: nat le linorder_not_le [symmetric] int_def Zero_int_def, arith)
wenzelm@23164
   411
done
wenzelm@23164
   412
wenzelm@23164
   413
text{*An alternative condition is @{term "0 \<le> w"} *}
wenzelm@23164
   414
corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
wenzelm@23164
   415
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
wenzelm@23164
   416
wenzelm@23164
   417
corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
wenzelm@23164
   418
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
wenzelm@23164
   419
wenzelm@23164
   420
lemma zless_nat_conj: "(nat w < nat z) = (0 < z & w < z)"
wenzelm@23164
   421
apply (cases w, cases z) 
wenzelm@23164
   422
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
wenzelm@23164
   423
done
wenzelm@23164
   424
wenzelm@23164
   425
lemma nonneg_eq_int: "[| 0 \<le> z;  !!m. z = int m ==> P |] ==> P"
wenzelm@23164
   426
by (blast dest: nat_0_le sym)
wenzelm@23164
   427
wenzelm@23164
   428
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
wenzelm@23164
   429
by (cases w, simp add: nat le int_def Zero_int_def, arith)
wenzelm@23164
   430
wenzelm@23164
   431
corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
wenzelm@23164
   432
by (simp only: eq_commute [of m] nat_eq_iff) 
wenzelm@23164
   433
wenzelm@23164
   434
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)"
wenzelm@23164
   435
apply (cases w)
wenzelm@23164
   436
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
wenzelm@23164
   437
done
wenzelm@23164
   438
wenzelm@23164
   439
lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)"
wenzelm@23164
   440
by (auto simp add: nat_eq_iff2)
wenzelm@23164
   441
wenzelm@23164
   442
lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
wenzelm@23164
   443
by (insert zless_nat_conj [of 0], auto)
wenzelm@23164
   444
wenzelm@23164
   445
lemma nat_add_distrib:
wenzelm@23164
   446
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
wenzelm@23164
   447
by (cases z, cases z', simp add: nat add le int_def Zero_int_def)
wenzelm@23164
   448
wenzelm@23164
   449
lemma nat_diff_distrib:
wenzelm@23164
   450
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
wenzelm@23164
   451
by (cases z, cases z', 
wenzelm@23164
   452
    simp add: nat add minus diff_minus le int_def Zero_int_def)
wenzelm@23164
   453
wenzelm@23164
   454
wenzelm@23164
   455
lemma nat_zminus_int [simp]: "nat (- (int n)) = 0"
wenzelm@23164
   456
by (simp add: int_def minus nat Zero_int_def) 
wenzelm@23164
   457
wenzelm@23164
   458
lemma zless_nat_eq_int_zless: "(m < nat z) = (int m < z)"
wenzelm@23164
   459
by (cases z, simp add: nat le int_def  linorder_not_le [symmetric], arith)
wenzelm@23164
   460
wenzelm@23164
   461
wenzelm@23164
   462
subsection{*Lemmas about the Function @{term int} and Orderings*}
wenzelm@23164
   463
wenzelm@23164
   464
lemma negative_zless_0: "- (int (Suc n)) < 0"
wenzelm@23164
   465
by (simp add: order_less_le)
wenzelm@23164
   466
wenzelm@23164
   467
lemma negative_zless [iff]: "- (int (Suc n)) < int m"
wenzelm@23164
   468
by (rule negative_zless_0 [THEN order_less_le_trans], simp)
wenzelm@23164
   469
wenzelm@23164
   470
lemma negative_zle_0: "- int n \<le> 0"
wenzelm@23164
   471
by (simp add: minus_le_iff)
wenzelm@23164
   472
wenzelm@23164
   473
lemma negative_zle [iff]: "- int n \<le> int m"
wenzelm@23164
   474
by (rule order_trans [OF negative_zle_0 zero_zle_int])
wenzelm@23164
   475
wenzelm@23164
   476
lemma not_zle_0_negative [simp]: "~ (0 \<le> - (int (Suc n)))"
wenzelm@23164
   477
by (subst le_minus_iff, simp)
wenzelm@23164
   478
wenzelm@23164
   479
lemma int_zle_neg: "(int n \<le> - int m) = (n = 0 & m = 0)"
wenzelm@23164
   480
by (simp add: int_def le minus Zero_int_def) 
wenzelm@23164
   481
wenzelm@23164
   482
lemma not_int_zless_negative [simp]: "~ (int n < - int m)"
wenzelm@23164
   483
by (simp add: linorder_not_less)
wenzelm@23164
   484
wenzelm@23164
   485
lemma negative_eq_positive [simp]: "(- int n = int m) = (n = 0 & m = 0)"
wenzelm@23164
   486
by (force simp add: order_eq_iff [of "- int n"] int_zle_neg)
wenzelm@23164
   487
wenzelm@23164
   488
lemma zle_iff_zadd: "(w \<le> z) = (\<exists>n. z = w + int n)"
wenzelm@23164
   489
proof (cases w, cases z, simp add: le add int_def)
wenzelm@23164
   490
  fix a b c d
wenzelm@23164
   491
  assume "w = Abs_Integ (intrel `` {(a,b)})" "z = Abs_Integ (intrel `` {(c,d)})"
wenzelm@23164
   492
  show "(a+d \<le> c+b) = (\<exists>n. c+b = a+n+d)"
wenzelm@23164
   493
  proof
wenzelm@23164
   494
    assume "a + d \<le> c + b" 
wenzelm@23164
   495
    thus "\<exists>n. c + b = a + n + d" 
wenzelm@23164
   496
      by (auto intro!: exI [where x="c+b - (a+d)"])
wenzelm@23164
   497
  next    
wenzelm@23164
   498
    assume "\<exists>n. c + b = a + n + d" 
wenzelm@23164
   499
    then obtain n where "c + b = a + n + d" ..
wenzelm@23164
   500
    thus "a + d \<le> c + b" by arith
wenzelm@23164
   501
  qed
wenzelm@23164
   502
qed
wenzelm@23164
   503
wenzelm@23164
   504
lemma abs_int_eq [simp]: "abs (int m) = int m"
wenzelm@23164
   505
by (simp add: abs_if)
wenzelm@23164
   506
wenzelm@23164
   507
text{*This version is proved for all ordered rings, not just integers!
wenzelm@23164
   508
      It is proved here because attribute @{text arith_split} is not available
wenzelm@23164
   509
      in theory @{text Ring_and_Field}.
wenzelm@23164
   510
      But is it really better than just rewriting with @{text abs_if}?*}
wenzelm@23164
   511
lemma abs_split [arith_split]:
wenzelm@23164
   512
     "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
wenzelm@23164
   513
by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
wenzelm@23164
   514
wenzelm@23164
   515
wenzelm@23164
   516
subsection {* Constants @{term neg} and @{term iszero} *}
wenzelm@23164
   517
wenzelm@23164
   518
definition
wenzelm@23164
   519
  neg  :: "'a\<Colon>ordered_idom \<Rightarrow> bool"
wenzelm@23164
   520
where
wenzelm@23164
   521
  [code inline]: "neg Z \<longleftrightarrow> Z < 0"
wenzelm@23164
   522
wenzelm@23164
   523
definition (*for simplifying equalities*)
huffman@23276
   524
  iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
wenzelm@23164
   525
where
wenzelm@23164
   526
  "iszero z \<longleftrightarrow> z = 0"
wenzelm@23164
   527
wenzelm@23164
   528
lemma not_neg_int [simp]: "~ neg(int n)"
wenzelm@23164
   529
by (simp add: neg_def)
wenzelm@23164
   530
wenzelm@23164
   531
lemma neg_zminus_int [simp]: "neg(- (int (Suc n)))"
wenzelm@23164
   532
by (simp add: neg_def neg_less_0_iff_less)
wenzelm@23164
   533
wenzelm@23164
   534
lemmas neg_eq_less_0 = neg_def
wenzelm@23164
   535
wenzelm@23164
   536
lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
wenzelm@23164
   537
by (simp add: neg_def linorder_not_less)
wenzelm@23164
   538
wenzelm@23164
   539
wenzelm@23164
   540
subsection{*To simplify inequalities when Numeral1 can get simplified to 1*}
wenzelm@23164
   541
wenzelm@23164
   542
lemma not_neg_0: "~ neg 0"
wenzelm@23164
   543
by (simp add: One_int_def neg_def)
wenzelm@23164
   544
wenzelm@23164
   545
lemma not_neg_1: "~ neg 1"
wenzelm@23164
   546
by (simp add: neg_def linorder_not_less zero_le_one)
wenzelm@23164
   547
wenzelm@23164
   548
lemma iszero_0: "iszero 0"
wenzelm@23164
   549
by (simp add: iszero_def)
wenzelm@23164
   550
wenzelm@23164
   551
lemma not_iszero_1: "~ iszero 1"
wenzelm@23164
   552
by (simp add: iszero_def eq_commute)
wenzelm@23164
   553
wenzelm@23164
   554
lemma neg_nat: "neg z ==> nat z = 0"
wenzelm@23164
   555
by (simp add: neg_def order_less_imp_le) 
wenzelm@23164
   556
wenzelm@23164
   557
lemma not_neg_nat: "~ neg z ==> int (nat z) = z"
wenzelm@23164
   558
by (simp add: linorder_not_less neg_def)
wenzelm@23164
   559
wenzelm@23164
   560
wenzelm@23164
   561
subsection{*The Set of Natural Numbers*}
wenzelm@23164
   562
wenzelm@23164
   563
constdefs
huffman@23276
   564
  Nats  :: "'a::semiring_1 set"
wenzelm@23164
   565
  "Nats == range of_nat"
wenzelm@23164
   566
wenzelm@23164
   567
notation (xsymbols)
wenzelm@23164
   568
  Nats  ("\<nat>")
wenzelm@23164
   569
wenzelm@23164
   570
lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
wenzelm@23164
   571
by (simp add: Nats_def)
wenzelm@23164
   572
wenzelm@23164
   573
lemma Nats_0 [simp]: "0 \<in> Nats"
wenzelm@23164
   574
apply (simp add: Nats_def)
wenzelm@23164
   575
apply (rule range_eqI)
wenzelm@23164
   576
apply (rule of_nat_0 [symmetric])
wenzelm@23164
   577
done
wenzelm@23164
   578
wenzelm@23164
   579
lemma Nats_1 [simp]: "1 \<in> Nats"
wenzelm@23164
   580
apply (simp add: Nats_def)
wenzelm@23164
   581
apply (rule range_eqI)
wenzelm@23164
   582
apply (rule of_nat_1 [symmetric])
wenzelm@23164
   583
done
wenzelm@23164
   584
wenzelm@23164
   585
lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
wenzelm@23164
   586
apply (auto simp add: Nats_def)
wenzelm@23164
   587
apply (rule range_eqI)
wenzelm@23164
   588
apply (rule of_nat_add [symmetric])
wenzelm@23164
   589
done
wenzelm@23164
   590
wenzelm@23164
   591
lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
wenzelm@23164
   592
apply (auto simp add: Nats_def)
wenzelm@23164
   593
apply (rule range_eqI)
wenzelm@23164
   594
apply (rule of_nat_mult [symmetric])
wenzelm@23164
   595
done
wenzelm@23164
   596
wenzelm@23164
   597
text{*Agreement with the specific embedding for the integers*}
wenzelm@23164
   598
lemma int_eq_of_nat: "int = (of_nat :: nat => int)"
wenzelm@23164
   599
proof
wenzelm@23164
   600
  fix n
wenzelm@23164
   601
  show "int n = of_nat n"  by (induct n, simp_all add: int_Suc add_ac)
wenzelm@23164
   602
qed
wenzelm@23164
   603
wenzelm@23164
   604
lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
wenzelm@23164
   605
proof
wenzelm@23164
   606
  fix n
wenzelm@23164
   607
  show "of_nat n = id n"  by (induct n, simp_all)
wenzelm@23164
   608
qed
wenzelm@23164
   609
wenzelm@23164
   610
wenzelm@23164
   611
subsection{*Embedding of the Integers into any @{text ring_1}:
wenzelm@23164
   612
@{term of_int}*}
wenzelm@23164
   613
wenzelm@23164
   614
constdefs
wenzelm@23164
   615
   of_int :: "int => 'a::ring_1"
wenzelm@23164
   616
   "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
wenzelm@23164
   617
wenzelm@23164
   618
wenzelm@23164
   619
lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
wenzelm@23164
   620
proof -
wenzelm@23164
   621
  have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
wenzelm@23164
   622
    by (simp add: congruent_def compare_rls of_nat_add [symmetric]
wenzelm@23164
   623
            del: of_nat_add) 
wenzelm@23164
   624
  thus ?thesis
wenzelm@23164
   625
    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
wenzelm@23164
   626
qed
wenzelm@23164
   627
wenzelm@23164
   628
lemma of_int_0 [simp]: "of_int 0 = 0"
wenzelm@23164
   629
by (simp add: of_int Zero_int_def int_def)
wenzelm@23164
   630
wenzelm@23164
   631
lemma of_int_1 [simp]: "of_int 1 = 1"
wenzelm@23164
   632
by (simp add: of_int One_int_def int_def)
wenzelm@23164
   633
wenzelm@23164
   634
lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
wenzelm@23164
   635
by (cases w, cases z, simp add: compare_rls of_int add)
wenzelm@23164
   636
wenzelm@23164
   637
lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
wenzelm@23164
   638
by (cases z, simp add: compare_rls of_int minus)
wenzelm@23164
   639
wenzelm@23164
   640
lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z"
wenzelm@23164
   641
by (simp add: diff_minus)
wenzelm@23164
   642
wenzelm@23164
   643
lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
wenzelm@23164
   644
apply (cases w, cases z)
wenzelm@23164
   645
apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
wenzelm@23164
   646
                 mult add_ac)
wenzelm@23164
   647
done
wenzelm@23164
   648
wenzelm@23164
   649
lemma of_int_le_iff [simp]:
wenzelm@23164
   650
     "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
wenzelm@23164
   651
apply (cases w)
wenzelm@23164
   652
apply (cases z)
wenzelm@23164
   653
apply (simp add: compare_rls of_int le diff_int_def add minus
wenzelm@23164
   654
                 of_nat_add [symmetric]   del: of_nat_add)
wenzelm@23164
   655
done
wenzelm@23164
   656
wenzelm@23164
   657
text{*Special cases where either operand is zero*}
wenzelm@23164
   658
lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified]
wenzelm@23164
   659
lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified]
wenzelm@23164
   660
wenzelm@23164
   661
wenzelm@23164
   662
lemma of_int_less_iff [simp]:
wenzelm@23164
   663
     "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
wenzelm@23164
   664
by (simp add: linorder_not_le [symmetric])
wenzelm@23164
   665
wenzelm@23164
   666
text{*Special cases where either operand is zero*}
wenzelm@23164
   667
lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified]
wenzelm@23164
   668
lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified]
wenzelm@23164
   669
wenzelm@23164
   670
text{*Class for unital rings with characteristic zero.
wenzelm@23164
   671
 Includes non-ordered rings like the complex numbers.*}
huffman@23282
   672
axclass ring_char_0 < ring_1, semiring_char_0
wenzelm@23164
   673
wenzelm@23164
   674
lemma of_int_eq_iff [simp]:
wenzelm@23164
   675
     "(of_int w = (of_int z::'a::ring_char_0)) = (w = z)"
huffman@23282
   676
apply (cases w, cases z, simp add: of_int)
huffman@23282
   677
apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq)
huffman@23282
   678
apply (simp only: of_nat_add [symmetric] of_nat_eq_iff)
huffman@23282
   679
done
wenzelm@23164
   680
wenzelm@23164
   681
text{*Every @{text ordered_idom} has characteristic zero.*}
huffman@23282
   682
instance ordered_idom < ring_char_0 ..
wenzelm@23164
   683
wenzelm@23164
   684
text{*Special cases where either operand is zero*}
wenzelm@23164
   685
lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified]
wenzelm@23164
   686
lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified]
wenzelm@23164
   687
wenzelm@23164
   688
lemma of_int_eq_id [simp]: "of_int = (id :: int => int)"
wenzelm@23164
   689
proof
wenzelm@23164
   690
  fix z
wenzelm@23164
   691
  show "of_int z = id z"  
wenzelm@23164
   692
    by (cases z)
wenzelm@23164
   693
      (simp add: of_int add minus int_eq_of_nat [symmetric] int_def diff_minus)
wenzelm@23164
   694
qed
wenzelm@23164
   695
wenzelm@23164
   696
wenzelm@23164
   697
subsection{*The Set of Integers*}
wenzelm@23164
   698
wenzelm@23164
   699
constdefs
wenzelm@23164
   700
  Ints  :: "'a::ring_1 set"
wenzelm@23164
   701
  "Ints == range of_int"
wenzelm@23164
   702
wenzelm@23164
   703
notation (xsymbols)
wenzelm@23164
   704
  Ints  ("\<int>")
wenzelm@23164
   705
wenzelm@23164
   706
lemma Ints_0 [simp]: "0 \<in> Ints"
wenzelm@23164
   707
apply (simp add: Ints_def)
wenzelm@23164
   708
apply (rule range_eqI)
wenzelm@23164
   709
apply (rule of_int_0 [symmetric])
wenzelm@23164
   710
done
wenzelm@23164
   711
wenzelm@23164
   712
lemma Ints_1 [simp]: "1 \<in> Ints"
wenzelm@23164
   713
apply (simp add: Ints_def)
wenzelm@23164
   714
apply (rule range_eqI)
wenzelm@23164
   715
apply (rule of_int_1 [symmetric])
wenzelm@23164
   716
done
wenzelm@23164
   717
wenzelm@23164
   718
lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints"
wenzelm@23164
   719
apply (auto simp add: Ints_def)
wenzelm@23164
   720
apply (rule range_eqI)
wenzelm@23164
   721
apply (rule of_int_add [symmetric])
wenzelm@23164
   722
done
wenzelm@23164
   723
wenzelm@23164
   724
lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints"
wenzelm@23164
   725
apply (auto simp add: Ints_def)
wenzelm@23164
   726
apply (rule range_eqI)
wenzelm@23164
   727
apply (rule of_int_minus [symmetric])
wenzelm@23164
   728
done
wenzelm@23164
   729
wenzelm@23164
   730
lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints"
wenzelm@23164
   731
apply (auto simp add: Ints_def)
wenzelm@23164
   732
apply (rule range_eqI)
wenzelm@23164
   733
apply (rule of_int_diff [symmetric])
wenzelm@23164
   734
done
wenzelm@23164
   735
wenzelm@23164
   736
lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints"
wenzelm@23164
   737
apply (auto simp add: Ints_def)
wenzelm@23164
   738
apply (rule range_eqI)
wenzelm@23164
   739
apply (rule of_int_mult [symmetric])
wenzelm@23164
   740
done
wenzelm@23164
   741
wenzelm@23164
   742
text{*Collapse nested embeddings*}
wenzelm@23164
   743
lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
wenzelm@23164
   744
by (induct n, auto)
wenzelm@23164
   745
wenzelm@23164
   746
lemma of_int_int_eq [simp]: "of_int (int n) = of_nat n"
wenzelm@23164
   747
by (simp add: int_eq_of_nat)
wenzelm@23164
   748
wenzelm@23164
   749
lemma Ints_cases [cases set: Ints]:
wenzelm@23164
   750
  assumes "q \<in> \<int>"
wenzelm@23164
   751
  obtains (of_int) z where "q = of_int z"
wenzelm@23164
   752
  unfolding Ints_def
wenzelm@23164
   753
proof -
wenzelm@23164
   754
  from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
wenzelm@23164
   755
  then obtain z where "q = of_int z" ..
wenzelm@23164
   756
  then show thesis ..
wenzelm@23164
   757
qed
wenzelm@23164
   758
wenzelm@23164
   759
lemma Ints_induct [case_names of_int, induct set: Ints]:
wenzelm@23164
   760
  "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q"
wenzelm@23164
   761
  by (rule Ints_cases) auto
wenzelm@23164
   762
wenzelm@23164
   763
wenzelm@23164
   764
(* int (Suc n) = 1 + int n *)
wenzelm@23164
   765
wenzelm@23164
   766
wenzelm@23164
   767
wenzelm@23164
   768
subsection{*More Properties of @{term setsum} and  @{term setprod}*}
wenzelm@23164
   769
wenzelm@23164
   770
text{*By Jeremy Avigad*}
wenzelm@23164
   771
wenzelm@23164
   772
wenzelm@23164
   773
lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))"
wenzelm@23164
   774
  apply (cases "finite A")
wenzelm@23164
   775
  apply (erule finite_induct, auto)
wenzelm@23164
   776
  done
wenzelm@23164
   777
wenzelm@23164
   778
lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))"
wenzelm@23164
   779
  apply (cases "finite A")
wenzelm@23164
   780
  apply (erule finite_induct, auto)
wenzelm@23164
   781
  done
wenzelm@23164
   782
wenzelm@23164
   783
lemma int_setsum: "int (setsum f A) = (\<Sum>x\<in>A. int(f x))"
wenzelm@23164
   784
  by (simp add: int_eq_of_nat of_nat_setsum)
wenzelm@23164
   785
wenzelm@23164
   786
lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))"
wenzelm@23164
   787
  apply (cases "finite A")
wenzelm@23164
   788
  apply (erule finite_induct, auto)
wenzelm@23164
   789
  done
wenzelm@23164
   790
wenzelm@23164
   791
lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))"
wenzelm@23164
   792
  apply (cases "finite A")
wenzelm@23164
   793
  apply (erule finite_induct, auto)
wenzelm@23164
   794
  done
wenzelm@23164
   795
wenzelm@23164
   796
lemma int_setprod: "int (setprod f A) = (\<Prod>x\<in>A. int(f x))"
wenzelm@23164
   797
  by (simp add: int_eq_of_nat of_nat_setprod)
wenzelm@23164
   798
wenzelm@23164
   799
lemma setprod_nonzero_nat:
wenzelm@23164
   800
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0"
wenzelm@23164
   801
  by (rule setprod_nonzero, auto)
wenzelm@23164
   802
wenzelm@23164
   803
lemma setprod_zero_eq_nat:
wenzelm@23164
   804
    "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)"
wenzelm@23164
   805
  by (rule setprod_zero_eq, auto)
wenzelm@23164
   806
wenzelm@23164
   807
lemma setprod_nonzero_int:
wenzelm@23164
   808
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0"
wenzelm@23164
   809
  by (rule setprod_nonzero, auto)
wenzelm@23164
   810
wenzelm@23164
   811
lemma setprod_zero_eq_int:
wenzelm@23164
   812
    "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)"
wenzelm@23164
   813
  by (rule setprod_zero_eq, auto)
wenzelm@23164
   814
wenzelm@23164
   815
wenzelm@23164
   816
subsection {* Further properties *}
wenzelm@23164
   817
wenzelm@23164
   818
text{*Now we replace the case analysis rule by a more conventional one:
wenzelm@23164
   819
whether an integer is negative or not.*}
wenzelm@23164
   820
wenzelm@23164
   821
lemma zless_iff_Suc_zadd:
wenzelm@23164
   822
    "(w < z) = (\<exists>n. z = w + int(Suc n))"
wenzelm@23164
   823
apply (cases z, cases w)
wenzelm@23164
   824
apply (auto simp add: le add int_def linorder_not_le [symmetric]) 
wenzelm@23164
   825
apply (rename_tac a b c d) 
wenzelm@23164
   826
apply (rule_tac x="a+d - Suc(c+b)" in exI) 
wenzelm@23164
   827
apply arith
wenzelm@23164
   828
done
wenzelm@23164
   829
wenzelm@23164
   830
lemma negD: "x<0 ==> \<exists>n. x = - (int (Suc n))"
wenzelm@23164
   831
apply (cases x)
wenzelm@23164
   832
apply (auto simp add: le minus Zero_int_def int_def order_less_le) 
wenzelm@23164
   833
apply (rule_tac x="y - Suc x" in exI, arith)
wenzelm@23164
   834
done
wenzelm@23164
   835
wenzelm@23164
   836
theorem int_cases [cases type: int, case_names nonneg neg]:
wenzelm@23164
   837
     "[|!! n. z = int n ==> P;  !! n. z =  - (int (Suc n)) ==> P |] ==> P"
wenzelm@23164
   838
apply (cases "z < 0", blast dest!: negD)
wenzelm@23164
   839
apply (simp add: linorder_not_less)
wenzelm@23164
   840
apply (blast dest: nat_0_le [THEN sym])
wenzelm@23164
   841
done
wenzelm@23164
   842
wenzelm@23164
   843
theorem int_induct [induct type: int, case_names nonneg neg]:
wenzelm@23164
   844
     "[|!! n. P (int n);  !!n. P (- (int (Suc n))) |] ==> P z"
wenzelm@23164
   845
  by (cases z) auto
wenzelm@23164
   846
wenzelm@23164
   847
text{*Contributed by Brian Huffman*}
wenzelm@23164
   848
theorem int_diff_cases [case_names diff]:
wenzelm@23164
   849
assumes prem: "!!m n. z = int m - int n ==> P" shows "P"
wenzelm@23164
   850
 apply (rule_tac z=z in int_cases)
wenzelm@23164
   851
  apply (rule_tac m=n and n=0 in prem, simp)
wenzelm@23164
   852
 apply (rule_tac m=0 and n="Suc n" in prem, simp)
wenzelm@23164
   853
done
wenzelm@23164
   854
wenzelm@23164
   855
lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z"
wenzelm@23164
   856
apply (cases z)
wenzelm@23164
   857
apply (simp_all add: not_zle_0_negative del: int_Suc)
wenzelm@23164
   858
done
wenzelm@23164
   859
wenzelm@23164
   860
lemmas zless_le = less_int_def [THEN meta_eq_to_obj_eq]
wenzelm@23164
   861
wenzelm@23164
   862
lemmas [simp] = int_Suc
wenzelm@23164
   863
wenzelm@23164
   864
wenzelm@23164
   865
subsection {* Legacy ML bindings *}
wenzelm@23164
   866
wenzelm@23164
   867
ML {*
wenzelm@23164
   868
val of_nat_0 = @{thm of_nat_0};
wenzelm@23164
   869
val of_nat_1 = @{thm of_nat_1};
wenzelm@23164
   870
val of_nat_Suc = @{thm of_nat_Suc};
wenzelm@23164
   871
val of_nat_add = @{thm of_nat_add};
wenzelm@23164
   872
val of_nat_mult = @{thm of_nat_mult};
wenzelm@23164
   873
val of_int_0 = @{thm of_int_0};
wenzelm@23164
   874
val of_int_1 = @{thm of_int_1};
wenzelm@23164
   875
val of_int_add = @{thm of_int_add};
wenzelm@23164
   876
val of_int_mult = @{thm of_int_mult};
wenzelm@23164
   877
val int_eq_of_nat = @{thm int_eq_of_nat};
wenzelm@23164
   878
val zle_int = @{thm zle_int};
wenzelm@23164
   879
val int_int_eq = @{thm int_int_eq};
wenzelm@23164
   880
val diff_int_def = @{thm diff_int_def};
wenzelm@23164
   881
val zadd_ac = @{thms zadd_ac};
wenzelm@23164
   882
val zless_int = @{thm zless_int};
wenzelm@23164
   883
val zadd_int = @{thm zadd_int};
wenzelm@23164
   884
val zmult_int = @{thm zmult_int};
wenzelm@23164
   885
val nat_0_le = @{thm nat_0_le};
wenzelm@23164
   886
val int_0 = @{thm int_0};
wenzelm@23164
   887
val int_1 = @{thm int_1};
wenzelm@23164
   888
val abs_split = @{thm abs_split};
wenzelm@23164
   889
*}
wenzelm@23164
   890
wenzelm@23164
   891
end