463 finally show ?thesis . |
463 finally show ?thesis . |
464 qed |
464 qed |
465 |
465 |
466 theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
466 theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
467 (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))" |
467 (Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))" |
468 by (simp add: less_rat_def le_rat eq_rat int_less_le) |
468 by (simp add: less_rat_def le_rat eq_rat order_less_le) |
469 |
469 |
470 theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>" |
470 theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>" |
471 by (simp add: abs_rat_def minus_rat zero_rat less_rat eq_rat) |
471 by (simp add: abs_rat_def minus_rat zero_rat less_rat eq_rat) |
472 (auto simp add: mult_less_0_iff zero_less_mult_iff int_le_less |
472 (auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less |
473 split: abs_split) |
473 split: abs_split) |
474 |
474 |
475 |
475 |
476 subsubsection {* The ordered field of rational numbers *} |
476 subsubsection {* The ordered field of rational numbers *} |
477 |
477 |
617 assume gt: "0 < Fract e f" |
617 assume gt: "0 < Fract e f" |
618 show "Fract e f * Fract a b < Fract e f * Fract c d" |
618 show "Fract e f * Fract a b < Fract e f * Fract c d" |
619 proof - |
619 proof - |
620 let ?E = "e * f" and ?F = "f * f" |
620 let ?E = "e * f" and ?F = "f * f" |
621 from neq gt have "0 < ?E" |
621 from neq gt have "0 < ?E" |
622 by (auto simp add: zero_rat less_rat le_rat int_less_le eq_rat) |
622 by (auto simp add: zero_rat less_rat le_rat order_less_le eq_rat) |
623 moreover from neq have "0 < ?F" |
623 moreover from neq have "0 < ?F" |
624 by (auto simp add: zero_less_mult_iff) |
624 by (auto simp add: zero_less_mult_iff) |
625 moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)" |
625 moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)" |
626 by (simp add: less_rat) |
626 by (simp add: less_rat) |
627 ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F" |
627 ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F" |
638 proof |
638 proof |
639 fix x :: rat |
639 fix x :: rat |
640 show "inverse 0 = (0::rat)" by (simp add: inverse_rat_def) |
640 show "inverse 0 = (0::rat)" by (simp add: inverse_rat_def) |
641 show "x/0 = 0" by (simp add: divide_rat_def inverse_rat_def) |
641 show "x/0 = 0" by (simp add: divide_rat_def inverse_rat_def) |
642 qed |
642 qed |
643 |
|
644 |
|
645 subsection {* Embedding integers: The Injection @{term rat} *} |
|
646 |
|
647 constdefs |
|
648 rat :: "int => rat" (* FIXME generalize int to any numeric subtype (?) *) |
|
649 "rat z == Fract z 1" |
|
650 int_set :: "rat set" ("\<int>") (* FIXME generalize rat to any numeric supertype (?) *) |
|
651 "\<int> == range rat" |
|
652 |
|
653 lemma int_set_cases [case_names rat, cases set: int_set]: |
|
654 "q \<in> \<int> ==> (!!z. q = rat z ==> C) ==> C" |
|
655 proof (unfold int_set_def) |
|
656 assume "!!z. q = rat z ==> C" |
|
657 assume "q \<in> range rat" thus C .. |
|
658 qed |
|
659 |
|
660 lemma int_set_induct [case_names rat, induct set: int_set]: |
|
661 "q \<in> \<int> ==> (!!z. P (rat z)) ==> P q" |
|
662 by (rule int_set_cases) auto |
|
663 |
|
664 lemma rat_of_int_zero [simp]: "rat (0::int) = (0::rat)" |
|
665 by (simp add: rat_def zero_rat [symmetric]) |
|
666 |
|
667 lemma rat_of_int_one [simp]: "rat (1::int) = (1::rat)" |
|
668 by (simp add: rat_def one_rat [symmetric]) |
|
669 |
|
670 lemma rat_of_int_add_distrib [simp]: "rat (x + y) = rat (x::int) + rat y" |
|
671 by (simp add: rat_def add_rat) |
|
672 |
|
673 lemma rat_of_int_minus_distrib [simp]: "rat (-x) = -rat (x::int)" |
|
674 by (simp add: rat_def minus_rat) |
|
675 |
|
676 lemma rat_of_int_diff_distrib [simp]: "rat (x - y) = rat (x::int) - rat y" |
|
677 by (simp add: rat_def diff_rat) |
|
678 |
|
679 lemma rat_of_int_mult_distrib [simp]: "rat (x * y) = rat (x::int) * rat y" |
|
680 by (simp add: rat_def mult_rat) |
|
681 |
|
682 lemma rat_inject [simp]: "(rat z = rat w) = (z = w)" |
|
683 proof |
|
684 assume "rat z = rat w" |
|
685 hence "Fract z 1 = Fract w 1" by (unfold rat_def) |
|
686 hence "\<lfloor>fract z 1\<rfloor> = \<lfloor>fract w 1\<rfloor>" .. |
|
687 thus "z = w" by auto |
|
688 next |
|
689 assume "z = w" |
|
690 thus "rat z = rat w" by simp |
|
691 qed |
|
692 |
|
693 |
|
694 lemma rat_of_int_zero_cancel [simp]: "(rat x = 0) = (x = 0)" |
|
695 proof - |
|
696 have "(rat x = 0) = (rat x = rat 0)" by simp |
|
697 also have "... = (x = 0)" by (rule rat_inject) |
|
698 finally show ?thesis . |
|
699 qed |
|
700 |
|
701 lemma rat_of_int_less_iff [simp]: "rat (x::int) < rat y = (x < y)" |
|
702 by (simp add: rat_def less_rat) |
|
703 |
|
704 lemma rat_of_int_le_iff [simp]: "(rat (x::int) \<le> rat y) = (x \<le> y)" |
|
705 by (simp add: linorder_not_less [symmetric]) |
|
706 |
|
707 lemma zero_less_rat_of_int_iff [simp]: "(0 < rat y) = (0 < y)" |
|
708 by (insert rat_of_int_less_iff [of 0 y], simp) |
|
709 |
643 |
710 |
644 |
711 subsection {* Various Other Results *} |
645 subsection {* Various Other Results *} |
712 |
646 |
713 lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b" |
647 lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b" |
731 |
665 |
732 lemma zero_less_Fract_iff: |
666 lemma zero_less_Fract_iff: |
733 "0 < b ==> (0 < Fract a b) = (0 < a)" |
667 "0 < b ==> (0 < Fract a b) = (0 < a)" |
734 by (simp add: zero_rat less_rat order_less_imp_not_eq2 zero_less_mult_iff) |
668 by (simp add: zero_rat less_rat order_less_imp_not_eq2 zero_less_mult_iff) |
735 |
669 |
|
670 lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1" |
|
671 apply (insert add_rat [of concl: m n 1 1]) |
|
672 apply (simp add: one_rat [symmetric]) |
|
673 done |
|
674 |
|
675 lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k" |
|
676 apply (induct k) |
|
677 apply (simp add: zero_rat) |
|
678 apply (simp add: Fract_add_one) |
|
679 done |
|
680 |
|
681 lemma Fract_of_int_eq: "Fract k 1 = of_int k" |
|
682 proof (cases k rule: int_cases) |
|
683 case (nonneg n) |
|
684 thus ?thesis by (simp add: int_eq_of_nat Fract_of_nat_eq) |
|
685 next |
|
686 case (neg n) |
|
687 hence "Fract k 1 = - (Fract (of_nat (Suc n)) 1)" |
|
688 by (simp only: minus_rat int_eq_of_nat) |
|
689 also have "... = - (of_nat (Suc n))" |
|
690 by (simp only: Fract_of_nat_eq) |
|
691 finally show ?thesis |
|
692 by (simp add: only: prems int_eq_of_nat of_int_minus of_int_of_nat_eq) |
|
693 qed |
|
694 |
|
695 |
736 end |
696 end |