src/HOL/Integ/IntDef.thy
changeset 14738 83f1a514dcb4
parent 14691 e1eedc8cad37
child 14740 c8e1937110c2
equal deleted inserted replaced
14737:77ea79aed99d 14738:83f1a514dcb4
   151   apply (rule zadd_commute)
   151   apply (rule zadd_commute)
   152   done
   152   done
   153 
   153 
   154 lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
   154 lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
   155 
   155 
   156 lemmas zmult_ac = Ring_and_Field.mult_ac
   156 lemmas zmult_ac = OrderedGroup.mult_ac
   157 
   157 
   158 lemma zadd_int: "(int m) + (int n) = int (m + n)"
   158 lemma zadd_int: "(int m) + (int n) = int (m + n)"
   159 by (simp add: int_def add)
   159 by (simp add: int_def add)
   160 
   160 
   161 lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
   161 lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
   162 by (simp add: zadd_int zadd_assoc [symmetric])
   162 by (simp add: zadd_int zadd_assoc [symmetric])
   163 
   163 
   164 lemma int_Suc: "int (Suc m) = 1 + (int m)"
   164 lemma int_Suc: "int (Suc m) = 1 + (int m)"
   165 by (simp add: One_int_def zadd_int)
   165 by (simp add: One_int_def zadd_int)
   166 
   166 
   167 (*also for the instance declaration int :: plus_ac0*)
   167 (*also for the instance declaration int :: comm_monoid_add*)
   168 lemma zadd_0: "(0::int) + z = z"
   168 lemma zadd_0: "(0::int) + z = z"
   169 apply (simp add: Zero_int_def int_def)
   169 apply (simp add: Zero_int_def int_def)
   170 apply (cases z, simp add: add)
   170 apply (cases z, simp add: add)
   171 done
   171 done
   172 
   172 
   233 
   233 
   234 lemma zmult_1_right: "z * (1::int) = z"
   234 lemma zmult_1_right: "z * (1::int) = z"
   235 by (rule trans [OF zmult_commute zmult_1])
   235 by (rule trans [OF zmult_commute zmult_1])
   236 
   236 
   237 
   237 
   238 text{*The Integers Form A Ring*}
   238 text{*The Integers Form A comm_ring_1*}
   239 instance int :: ring
   239 instance int :: comm_ring_1
   240 proof
   240 proof
   241   fix i j k :: int
   241   fix i j k :: int
   242   show "(i + j) + k = i + (j + k)" by (simp add: zadd_assoc)
   242   show "(i + j) + k = i + (j + k)" by (simp add: zadd_assoc)
   243   show "i + j = j + i" by (simp add: zadd_commute)
   243   show "i + j = j + i" by (simp add: zadd_commute)
   244   show "0 + i = i" by (rule zadd_0)
   244   show "0 + i = i" by (rule zadd_0)
   366 
   366 
   367 defs (overloaded)
   367 defs (overloaded)
   368     zabs_def:  "abs(i::int) == if i < 0 then -i else i"
   368     zabs_def:  "abs(i::int) == if i < 0 then -i else i"
   369 
   369 
   370 
   370 
   371 text{*The Integers Form an Ordered Ring*}
   371 text{*The Integers Form an Ordered comm_ring_1*}
   372 instance int :: ordered_ring
   372 instance int :: ordered_idom
   373 proof
   373 proof
   374   fix i j k :: int
   374   fix i j k :: int
   375   show "i \<le> j ==> k + i \<le> k + j" by (rule zadd_left_mono)
   375   show "i \<le> j ==> k + i \<le> k + j" by (rule zadd_left_mono)
   376   show "i < j ==> 0 < k ==> k * i < k * j" by (rule zmult_zless_mono2)
   376   show "i < j ==> 0 < k ==> k * i < k * j" by (rule zmult_zless_mono2)
   377   show "\<bar>i\<bar> = (if i < 0 then -i else i)" by (simp only: zabs_def)
   377   show "\<bar>i\<bar> = (if i < 0 then -i else i)" by (simp only: zabs_def)
   508 text{*This version is proved for all ordered rings, not just integers!
   508 text{*This version is proved for all ordered rings, not just integers!
   509       It is proved here because attribute @{text arith_split} is not available
   509       It is proved here because attribute @{text arith_split} is not available
   510       in theory @{text Ring_and_Field}.
   510       in theory @{text Ring_and_Field}.
   511       But is it really better than just rewriting with @{text abs_if}?*}
   511       But is it really better than just rewriting with @{text abs_if}?*}
   512 lemma abs_split [arith_split]:
   512 lemma abs_split [arith_split]:
   513      "P(abs(a::'a::ordered_ring)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
   513      "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
   514 by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
   514 by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
   515 
   515 
   516 
   516 
   517 
   517 
   518 subsection{*The Constants @{term neg} and @{term iszero}*}
   518 subsection{*The Constants @{term neg} and @{term iszero}*}
   519 
   519 
   520 constdefs
   520 constdefs
   521 
   521 
   522   neg   :: "'a::ordered_ring => bool"
   522   neg   :: "'a::ordered_idom => bool"
   523   "neg(Z) == Z < 0"
   523   "neg(Z) == Z < 0"
   524 
   524 
   525   (*For simplifying equalities*)
   525   (*For simplifying equalities*)
   526   iszero :: "'a::semiring => bool"
   526   iszero :: "'a::comm_semiring_1_cancel => bool"
   527   "iszero z == z = (0)"
   527   "iszero z == z = (0)"
   528 
   528 
   529 
   529 
   530 lemma not_neg_int [simp]: "~ neg(int n)"
   530 lemma not_neg_int [simp]: "~ neg(int n)"
   531 by (simp add: neg_def)
   531 by (simp add: neg_def)
   558 
   558 
   559 lemma not_neg_nat: "~ neg z ==> int (nat z) = z"
   559 lemma not_neg_nat: "~ neg z ==> int (nat z) = z"
   560 by (simp add: linorder_not_less neg_def)
   560 by (simp add: linorder_not_less neg_def)
   561 
   561 
   562 
   562 
   563 subsection{*Embedding of the Naturals into any Semiring: @{term of_nat}*}
   563 subsection{*Embedding of the Naturals into any comm_semiring_1_cancel: @{term of_nat}*}
   564 
   564 
   565 consts of_nat :: "nat => 'a::semiring"
   565 consts of_nat :: "nat => 'a::comm_semiring_1_cancel"
   566 
   566 
   567 primrec
   567 primrec
   568   of_nat_0:   "of_nat 0 = 0"
   568   of_nat_0:   "of_nat 0 = 0"
   569   of_nat_Suc: "of_nat (Suc m) = of_nat m + 1"
   569   of_nat_Suc: "of_nat (Suc m) = of_nat m + 1"
   570 
   570 
   579 lemma of_nat_mult [simp]: "of_nat (m*n) = of_nat m * of_nat n"
   579 lemma of_nat_mult [simp]: "of_nat (m*n) = of_nat m * of_nat n"
   580 apply (induct m)
   580 apply (induct m)
   581 apply (simp_all add: mult_ac add_ac right_distrib)
   581 apply (simp_all add: mult_ac add_ac right_distrib)
   582 done
   582 done
   583 
   583 
   584 lemma zero_le_imp_of_nat: "0 \<le> (of_nat m::'a::ordered_semiring)"
   584 lemma zero_le_imp_of_nat: "0 \<le> (of_nat m::'a::ordered_semidom)"
   585 apply (induct m, simp_all)
   585 apply (induct m, simp_all)
   586 apply (erule order_trans)
   586 apply (erule order_trans)
   587 apply (rule less_add_one [THEN order_less_imp_le])
   587 apply (rule less_add_one [THEN order_less_imp_le])
   588 done
   588 done
   589 
   589 
   590 lemma less_imp_of_nat_less:
   590 lemma less_imp_of_nat_less:
   591      "m < n ==> of_nat m < (of_nat n::'a::ordered_semiring)"
   591      "m < n ==> of_nat m < (of_nat n::'a::ordered_semidom)"
   592 apply (induct m n rule: diff_induct, simp_all)
   592 apply (induct m n rule: diff_induct, simp_all)
   593 apply (insert add_le_less_mono [OF zero_le_imp_of_nat zero_less_one], force)
   593 apply (insert add_le_less_mono [OF zero_le_imp_of_nat zero_less_one], force)
   594 done
   594 done
   595 
   595 
   596 lemma of_nat_less_imp_less:
   596 lemma of_nat_less_imp_less:
   597      "of_nat m < (of_nat n::'a::ordered_semiring) ==> m < n"
   597      "of_nat m < (of_nat n::'a::ordered_semidom) ==> m < n"
   598 apply (induct m n rule: diff_induct, simp_all)
   598 apply (induct m n rule: diff_induct, simp_all)
   599 apply (insert zero_le_imp_of_nat)
   599 apply (insert zero_le_imp_of_nat)
   600 apply (force simp add: linorder_not_less [symmetric])
   600 apply (force simp add: linorder_not_less [symmetric])
   601 done
   601 done
   602 
   602 
   603 lemma of_nat_less_iff [simp]:
   603 lemma of_nat_less_iff [simp]:
   604      "(of_nat m < (of_nat n::'a::ordered_semiring)) = (m<n)"
   604      "(of_nat m < (of_nat n::'a::ordered_semidom)) = (m<n)"
   605 by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
   605 by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
   606 
   606 
   607 text{*Special cases where either operand is zero*}
   607 text{*Special cases where either operand is zero*}
   608 declare of_nat_less_iff [of 0, simplified, simp]
   608 declare of_nat_less_iff [of 0, simplified, simp]
   609 declare of_nat_less_iff [of _ 0, simplified, simp]
   609 declare of_nat_less_iff [of _ 0, simplified, simp]
   610 
   610 
   611 lemma of_nat_le_iff [simp]:
   611 lemma of_nat_le_iff [simp]:
   612      "(of_nat m \<le> (of_nat n::'a::ordered_semiring)) = (m \<le> n)"
   612      "(of_nat m \<le> (of_nat n::'a::ordered_semidom)) = (m \<le> n)"
   613 by (simp add: linorder_not_less [symmetric])
   613 by (simp add: linorder_not_less [symmetric])
   614 
   614 
   615 text{*Special cases where either operand is zero*}
   615 text{*Special cases where either operand is zero*}
   616 declare of_nat_le_iff [of 0, simplified, simp]
   616 declare of_nat_le_iff [of 0, simplified, simp]
   617 declare of_nat_le_iff [of _ 0, simplified, simp]
   617 declare of_nat_le_iff [of _ 0, simplified, simp]
   618 
   618 
   619 text{*The ordering on the semiring is necessary to exclude the possibility of
   619 text{*The ordering on the comm_semiring_1_cancel is necessary to exclude the possibility of
   620 a finite field, which indeed wraps back to zero.*}
   620 a finite field, which indeed wraps back to zero.*}
   621 lemma of_nat_eq_iff [simp]:
   621 lemma of_nat_eq_iff [simp]:
   622      "(of_nat m = (of_nat n::'a::ordered_semiring)) = (m = n)"
   622      "(of_nat m = (of_nat n::'a::ordered_semidom)) = (m = n)"
   623 by (simp add: order_eq_iff)
   623 by (simp add: order_eq_iff)
   624 
   624 
   625 text{*Special cases where either operand is zero*}
   625 text{*Special cases where either operand is zero*}
   626 declare of_nat_eq_iff [of 0, simplified, simp]
   626 declare of_nat_eq_iff [of 0, simplified, simp]
   627 declare of_nat_eq_iff [of _ 0, simplified, simp]
   627 declare of_nat_eq_iff [of _ 0, simplified, simp]
   628 
   628 
   629 lemma of_nat_diff [simp]:
   629 lemma of_nat_diff [simp]:
   630      "n \<le> m ==> of_nat (m - n) = of_nat m - (of_nat n :: 'a::ring)"
   630      "n \<le> m ==> of_nat (m - n) = of_nat m - (of_nat n :: 'a::comm_ring_1)"
   631 by (simp del: of_nat_add
   631 by (simp del: of_nat_add
   632 	 add: compare_rls of_nat_add [symmetric] split add: nat_diff_split)
   632 	 add: compare_rls of_nat_add [symmetric] split add: nat_diff_split)
   633 
   633 
   634 
   634 
   635 subsection{*The Set of Natural Numbers*}
   635 subsection{*The Set of Natural Numbers*}
   636 
   636 
   637 constdefs
   637 constdefs
   638    Nats  :: "'a::semiring set"
   638    Nats  :: "'a::comm_semiring_1_cancel set"
   639     "Nats == range of_nat"
   639     "Nats == range of_nat"
   640 
   640 
   641 syntax (xsymbols)    Nats :: "'a set"   ("\<nat>")
   641 syntax (xsymbols)    Nats :: "'a set"   ("\<nat>")
   642 
   642 
   643 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
   643 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
   679   fix n
   679   fix n
   680   show "of_nat n = id n"  by (induct n, simp_all)
   680   show "of_nat n = id n"  by (induct n, simp_all)
   681 qed
   681 qed
   682 
   682 
   683 
   683 
   684 subsection{*Embedding of the Integers into any Ring: @{term of_int}*}
   684 subsection{*Embedding of the Integers into any comm_ring_1: @{term of_int}*}
   685 
   685 
   686 constdefs
   686 constdefs
   687    of_int :: "int => 'a::ring"
   687    of_int :: "int => 'a::comm_ring_1"
   688    "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
   688    "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
   689 
   689 
   690 
   690 
   691 lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
   691 lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
   692 proof -
   692 proof -
   717 apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
   717 apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
   718                  mult add_ac)
   718                  mult add_ac)
   719 done
   719 done
   720 
   720 
   721 lemma of_int_le_iff [simp]:
   721 lemma of_int_le_iff [simp]:
   722      "(of_int w \<le> (of_int z::'a::ordered_ring)) = (w \<le> z)"
   722      "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
   723 apply (cases w)
   723 apply (cases w)
   724 apply (cases z)
   724 apply (cases z)
   725 apply (simp add: compare_rls of_int le diff_int_def add minus
   725 apply (simp add: compare_rls of_int le diff_int_def add minus
   726                  of_nat_add [symmetric]   del: of_nat_add)
   726                  of_nat_add [symmetric]   del: of_nat_add)
   727 done
   727 done
   729 text{*Special cases where either operand is zero*}
   729 text{*Special cases where either operand is zero*}
   730 declare of_int_le_iff [of 0, simplified, simp]
   730 declare of_int_le_iff [of 0, simplified, simp]
   731 declare of_int_le_iff [of _ 0, simplified, simp]
   731 declare of_int_le_iff [of _ 0, simplified, simp]
   732 
   732 
   733 lemma of_int_less_iff [simp]:
   733 lemma of_int_less_iff [simp]:
   734      "(of_int w < (of_int z::'a::ordered_ring)) = (w < z)"
   734      "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
   735 by (simp add: linorder_not_le [symmetric])
   735 by (simp add: linorder_not_le [symmetric])
   736 
   736 
   737 text{*Special cases where either operand is zero*}
   737 text{*Special cases where either operand is zero*}
   738 declare of_int_less_iff [of 0, simplified, simp]
   738 declare of_int_less_iff [of 0, simplified, simp]
   739 declare of_int_less_iff [of _ 0, simplified, simp]
   739 declare of_int_less_iff [of _ 0, simplified, simp]
   740 
   740 
   741 text{*The ordering on the ring is necessary. See @{text of_nat_eq_iff} above.*}
   741 text{*The ordering on the comm_ring_1 is necessary. See @{text of_nat_eq_iff} above.*}
   742 lemma of_int_eq_iff [simp]:
   742 lemma of_int_eq_iff [simp]:
   743      "(of_int w = (of_int z::'a::ordered_ring)) = (w = z)"
   743      "(of_int w = (of_int z::'a::ordered_idom)) = (w = z)"
   744 by (simp add: order_eq_iff)
   744 by (simp add: order_eq_iff)
   745 
   745 
   746 text{*Special cases where either operand is zero*}
   746 text{*Special cases where either operand is zero*}
   747 declare of_int_eq_iff [of 0, simplified, simp]
   747 declare of_int_eq_iff [of 0, simplified, simp]
   748 declare of_int_eq_iff [of _ 0, simplified, simp]
   748 declare of_int_eq_iff [of _ 0, simplified, simp]
   757 
   757 
   758 
   758 
   759 subsection{*The Set of Integers*}
   759 subsection{*The Set of Integers*}
   760 
   760 
   761 constdefs
   761 constdefs
   762    Ints  :: "'a::ring set"
   762    Ints  :: "'a::comm_ring_1 set"
   763     "Ints == range of_int"
   763     "Ints == range of_int"
   764 
   764 
   765 
   765 
   766 syntax (xsymbols)
   766 syntax (xsymbols)
   767   Ints      :: "'a set"                   ("\<int>")
   767   Ints      :: "'a set"                   ("\<int>")