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