author | haftmann |
Wed, 02 Apr 2008 15:58:32 +0200 | |
changeset 26513 | 6f306c8c2c54 |
parent 25965 | 05df64f786a4 |
child 26732 | 6ea9de67e576 |
permissions | -rw-r--r-- |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
1 |
(* Title: HOL/Library/Rational.thy |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
2 |
ID: $Id$ |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
4 |
*) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
5 |
|
14691 | 6 |
header {* Rational numbers *} |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
7 |
|
15131 | 8 |
theory Rational |
25762 | 9 |
imports "~~/src/HOL/Library/Abstract_Rat" |
16417 | 10 |
uses ("rat_arith.ML") |
15131 | 11 |
begin |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
12 |
|
18913 | 13 |
subsection {* Rational numbers *} |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
14 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
15 |
subsubsection {* Equivalence of fractions *} |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
16 |
|
19765 | 17 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20522
diff
changeset
|
18 |
fraction :: "(int \<times> int) set" where |
19765 | 19 |
"fraction = {x. snd x \<noteq> 0}" |
18913 | 20 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20522
diff
changeset
|
21 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20522
diff
changeset
|
22 |
ratrel :: "((int \<times> int) \<times> (int \<times> int)) set" where |
19765 | 23 |
"ratrel = {(x,y). snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x}" |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
24 |
|
18913 | 25 |
lemma fraction_iff [simp]: "(x \<in> fraction) = (snd x \<noteq> 0)" |
26 |
by (simp add: fraction_def) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
27 |
|
18913 | 28 |
lemma ratrel_iff [simp]: |
29 |
"((x,y) \<in> ratrel) = |
|
30 |
(snd x \<noteq> 0 \<and> snd y \<noteq> 0 \<and> fst x * snd y = fst y * snd x)" |
|
31 |
by (simp add: ratrel_def) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
32 |
|
18913 | 33 |
lemma refl_ratrel: "refl fraction ratrel" |
34 |
by (auto simp add: refl_def fraction_def ratrel_def) |
|
35 |
||
36 |
lemma sym_ratrel: "sym ratrel" |
|
37 |
by (simp add: ratrel_def sym_def) |
|
38 |
||
39 |
lemma trans_ratrel_lemma: |
|
40 |
assumes 1: "a * b' = a' * b" |
|
41 |
assumes 2: "a' * b'' = a'' * b'" |
|
42 |
assumes 3: "b' \<noteq> (0::int)" |
|
43 |
shows "a * b'' = a'' * b" |
|
44 |
proof - |
|
45 |
have "b' * (a * b'') = b'' * (a * b')" by simp |
|
46 |
also note 1 |
|
47 |
also have "b'' * (a' * b) = b * (a' * b'')" by simp |
|
48 |
also note 2 |
|
49 |
also have "b * (a'' * b') = b' * (a'' * b)" by simp |
|
50 |
finally have "b' * (a * b'') = b' * (a'' * b)" . |
|
51 |
with 3 show "a * b'' = a'' * b" by simp |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
52 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
53 |
|
18913 | 54 |
lemma trans_ratrel: "trans ratrel" |
55 |
by (auto simp add: trans_def elim: trans_ratrel_lemma) |
|
56 |
||
57 |
lemma equiv_ratrel: "equiv fraction ratrel" |
|
58 |
by (rule equiv.intro [OF refl_ratrel sym_ratrel trans_ratrel]) |
|
59 |
||
60 |
lemmas equiv_ratrel_iff [iff] = eq_equiv_class_iff [OF equiv_ratrel] |
|
61 |
||
62 |
lemma equiv_ratrel_iff2: |
|
63 |
"\<lbrakk>snd x \<noteq> 0; snd y \<noteq> 0\<rbrakk> |
|
64 |
\<Longrightarrow> (ratrel `` {x} = ratrel `` {y}) = ((x,y) \<in> ratrel)" |
|
65 |
by (rule eq_equiv_class_iff [OF equiv_ratrel], simp_all) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
66 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
67 |
|
18913 | 68 |
subsubsection {* The type of rational numbers *} |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
69 |
|
18913 | 70 |
typedef (Rat) rat = "fraction//ratrel" |
71 |
proof |
|
72 |
have "(0,1) \<in> fraction" by (simp add: fraction_def) |
|
73 |
thus "ratrel``{(0,1)} \<in> fraction//ratrel" by (rule quotientI) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
74 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
75 |
|
18913 | 76 |
lemma ratrel_in_Rat [simp]: "snd x \<noteq> 0 \<Longrightarrow> ratrel``{x} \<in> Rat" |
77 |
by (simp add: Rat_def quotientI) |
|
78 |
||
79 |
declare Abs_Rat_inject [simp] Abs_Rat_inverse [simp] |
|
80 |
||
81 |
||
19765 | 82 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20522
diff
changeset
|
83 |
Fract :: "int \<Rightarrow> int \<Rightarrow> rat" where |
24198 | 84 |
[code func del]: "Fract a b = Abs_Rat (ratrel``{(a,b)})" |
85 |
||
86 |
lemma Fract_zero: |
|
87 |
"Fract k 0 = Fract l 0" |
|
88 |
by (simp add: Fract_def ratrel_def) |
|
18913 | 89 |
|
90 |
theorem Rat_cases [case_names Fract, cases type: rat]: |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20522
diff
changeset
|
91 |
"(!!a b. q = Fract a b ==> b \<noteq> 0 ==> C) ==> C" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20522
diff
changeset
|
92 |
by (cases q) (clarsimp simp add: Fract_def Rat_def fraction_def quotient_def) |
18913 | 93 |
|
94 |
theorem Rat_induct [case_names Fract, induct type: rat]: |
|
95 |
"(!!a b. b \<noteq> 0 ==> P (Fract a b)) ==> P q" |
|
96 |
by (cases q) simp |
|
97 |
||
98 |
||
99 |
subsubsection {* Congruence lemmas *} |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
100 |
|
18913 | 101 |
lemma add_congruent2: |
102 |
"(\<lambda>x y. ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)}) |
|
103 |
respects2 ratrel" |
|
104 |
apply (rule equiv_ratrel [THEN congruent2_commuteI]) |
|
105 |
apply (simp_all add: left_distrib) |
|
106 |
done |
|
107 |
||
108 |
lemma minus_congruent: |
|
109 |
"(\<lambda>x. ratrel``{(- fst x, snd x)}) respects ratrel" |
|
110 |
by (simp add: congruent_def) |
|
111 |
||
112 |
lemma mult_congruent2: |
|
113 |
"(\<lambda>x y. ratrel``{(fst x * fst y, snd x * snd y)}) respects2 ratrel" |
|
114 |
by (rule equiv_ratrel [THEN congruent2_commuteI], simp_all) |
|
115 |
||
116 |
lemma inverse_congruent: |
|
117 |
"(\<lambda>x. ratrel``{if fst x=0 then (0,1) else (snd x, fst x)}) respects ratrel" |
|
118 |
by (auto simp add: congruent_def mult_commute) |
|
119 |
||
120 |
lemma le_congruent2: |
|
18982 | 121 |
"(\<lambda>x y. {(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)}) |
18913 | 122 |
respects2 ratrel" |
123 |
proof (clarsimp simp add: congruent2_def) |
|
124 |
fix a b a' b' c d c' d'::int |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
125 |
assume neq: "b \<noteq> 0" "b' \<noteq> 0" "d \<noteq> 0" "d' \<noteq> 0" |
18913 | 126 |
assume eq1: "a * b' = a' * b" |
127 |
assume eq2: "c * d' = c' * d" |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
128 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
129 |
let ?le = "\<lambda>a b c d. ((a * d) * (b * d) \<le> (c * b) * (b * d))" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
130 |
{ |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
131 |
fix a b c d x :: int assume x: "x \<noteq> 0" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
132 |
have "?le a b c d = ?le (a * x) (b * x) c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
133 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
134 |
from x have "0 < x * x" by (auto simp add: zero_less_mult_iff) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
135 |
hence "?le a b c d = |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
136 |
((a * d) * (b * d) * (x * x) \<le> (c * b) * (b * d) * (x * x))" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
137 |
by (simp add: mult_le_cancel_right) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
138 |
also have "... = ?le (a * x) (b * x) c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
139 |
by (simp add: mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
140 |
finally show ?thesis . |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
141 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
142 |
} note le_factor = this |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
143 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
144 |
let ?D = "b * d" and ?D' = "b' * d'" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
145 |
from neq have D: "?D \<noteq> 0" by simp |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
146 |
from neq have "?D' \<noteq> 0" by simp |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
147 |
hence "?le a b c d = ?le (a * ?D') (b * ?D') c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
148 |
by (rule le_factor) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
149 |
also have "... = ((a * b') * ?D * ?D' * d * d' \<le> (c * d') * ?D * ?D' * b * b')" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
150 |
by (simp add: mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
151 |
also have "... = ((a' * b) * ?D * ?D' * d * d' \<le> (c' * d) * ?D * ?D' * b * b')" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
152 |
by (simp only: eq1 eq2) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
153 |
also have "... = ?le (a' * ?D) (b' * ?D) c' d'" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
154 |
by (simp add: mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
155 |
also from D have "... = ?le a' b' c' d'" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
156 |
by (rule le_factor [symmetric]) |
18913 | 157 |
finally show "?le a b c d = ?le a' b' c' d'" . |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
158 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
159 |
|
18913 | 160 |
lemmas UN_ratrel = UN_equiv_class [OF equiv_ratrel] |
161 |
lemmas UN_ratrel2 = UN_equiv_class2 [OF equiv_ratrel equiv_ratrel] |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
162 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
163 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
164 |
subsubsection {* Standard operations on rational numbers *} |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
165 |
|
25762 | 166 |
instantiation rat :: "{zero, one, plus, minus, uminus, times, inverse, ord, abs, sgn}" |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
167 |
begin |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
168 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
169 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
170 |
Zero_rat_def [code func del]: "0 = Fract 0 1" |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
171 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
172 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
173 |
One_rat_def [code func del]: "1 = Fract 1 1" |
18913 | 174 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
175 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
176 |
add_rat_def [code func del]: |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
177 |
"q + r = |
18913 | 178 |
Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r. |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
179 |
ratrel``{(fst x * snd y + fst y * snd x, snd x * snd y)})" |
18913 | 180 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
181 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
182 |
minus_rat_def [code func del]: |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
183 |
"- q = Abs_Rat (\<Union>x \<in> Rep_Rat q. ratrel``{(- fst x, snd x)})" |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
184 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
185 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
186 |
diff_rat_def [code func del]: "q - r = q + - (r::rat)" |
18913 | 187 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
188 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
189 |
mult_rat_def [code func del]: |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
190 |
"q * r = |
18913 | 191 |
Abs_Rat (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r. |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
192 |
ratrel``{(fst x * fst y, snd x * snd y)})" |
18913 | 193 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
194 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
195 |
inverse_rat_def [code func del]: |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
196 |
"inverse q = |
18913 | 197 |
Abs_Rat (\<Union>x \<in> Rep_Rat q. |
198 |
ratrel``{if fst x=0 then (0,1) else (snd x, fst x)})" |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
199 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
200 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
201 |
divide_rat_def [code func del]: "q / r = q * inverse (r::rat)" |
18913 | 202 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
203 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
204 |
le_rat_def [code func del]: |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
205 |
"q \<le> r \<longleftrightarrow> contents (\<Union>x \<in> Rep_Rat q. \<Union>y \<in> Rep_Rat r. |
18982 | 206 |
{(fst x * snd y)*(snd x * snd y) \<le> (fst y * snd x)*(snd x * snd y)})" |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
207 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
208 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
209 |
less_rat_def [code func del]: "z < (w::rat) \<longleftrightarrow> z \<le> w \<and> z \<noteq> w" |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
210 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
211 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
212 |
abs_rat_def: "\<bar>q\<bar> = (if q < 0 then -q else (q::rat))" |
18913 | 213 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
214 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
215 |
sgn_rat_def: "sgn (q::rat) = (if q=0 then 0 else if 0<q then 1 else - 1)" |
18913 | 216 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
217 |
instance .. |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
218 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
219 |
end |
24506 | 220 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
221 |
instantiation rat :: power |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
222 |
begin |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
223 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
224 |
primrec power_rat |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
225 |
where |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
226 |
rat_power_0: "q ^ 0 = (1\<Colon>rat)" |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
227 |
| rat_power_Suc: "q ^ (Suc n) = (q\<Colon>rat) * (q ^ n)" |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
228 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
229 |
instance .. |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
230 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
231 |
end |
20522 | 232 |
|
18913 | 233 |
theorem eq_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
234 |
(Fract a b = Fract c d) = (a * d = c * b)" |
|
235 |
by (simp add: Fract_def) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
236 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
237 |
theorem add_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
238 |
Fract a b + Fract c d = Fract (a * d + c * b) (b * d)" |
18913 | 239 |
by (simp add: Fract_def add_rat_def add_congruent2 UN_ratrel2) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
240 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
241 |
theorem minus_rat: "b \<noteq> 0 ==> -(Fract a b) = Fract (-a) b" |
18913 | 242 |
by (simp add: Fract_def minus_rat_def minus_congruent UN_ratrel) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
243 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
244 |
theorem diff_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
245 |
Fract a b - Fract c d = Fract (a * d - c * b) (b * d)" |
18913 | 246 |
by (simp add: diff_rat_def add_rat minus_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
247 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
248 |
theorem mult_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
249 |
Fract a b * Fract c d = Fract (a * c) (b * d)" |
18913 | 250 |
by (simp add: Fract_def mult_rat_def mult_congruent2 UN_ratrel2) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
251 |
|
18913 | 252 |
theorem inverse_rat: "a \<noteq> 0 ==> b \<noteq> 0 ==> |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
253 |
inverse (Fract a b) = Fract b a" |
18913 | 254 |
by (simp add: Fract_def inverse_rat_def inverse_congruent UN_ratrel) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
255 |
|
18913 | 256 |
theorem divide_rat: "c \<noteq> 0 ==> b \<noteq> 0 ==> d \<noteq> 0 ==> |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
257 |
Fract a b / Fract c d = Fract (a * d) (b * c)" |
18913 | 258 |
by (simp add: divide_rat_def inverse_rat mult_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
259 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
260 |
theorem le_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
261 |
(Fract a b \<le> Fract c d) = ((a * d) * (b * d) \<le> (c * b) * (b * d))" |
18982 | 262 |
by (simp add: Fract_def le_rat_def le_congruent2 UN_ratrel2) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
263 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
264 |
theorem less_rat: "b \<noteq> 0 ==> d \<noteq> 0 ==> |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
265 |
(Fract a b < Fract c d) = ((a * d) * (b * d) < (c * b) * (b * d))" |
18913 | 266 |
by (simp add: less_rat_def le_rat eq_rat order_less_le) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
267 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
268 |
theorem abs_rat: "b \<noteq> 0 ==> \<bar>Fract a b\<bar> = Fract \<bar>a\<bar> \<bar>b\<bar>" |
23879 | 269 |
by (simp add: abs_rat_def minus_rat Zero_rat_def less_rat eq_rat) |
14691 | 270 |
(auto simp add: mult_less_0_iff zero_less_mult_iff order_le_less |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
271 |
split: abs_split) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
272 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
273 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
274 |
subsubsection {* The ordered field of rational numbers *} |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
275 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
276 |
instance rat :: field |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
277 |
proof |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
278 |
fix q r s :: rat |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
279 |
show "(q + r) + s = q + (r + s)" |
18913 | 280 |
by (induct q, induct r, induct s) |
281 |
(simp add: add_rat add_ac mult_ac int_distrib) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
282 |
show "q + r = r + q" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
283 |
by (induct q, induct r) (simp add: add_rat add_ac mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
284 |
show "0 + q = q" |
23879 | 285 |
by (induct q) (simp add: Zero_rat_def add_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
286 |
show "(-q) + q = 0" |
23879 | 287 |
by (induct q) (simp add: Zero_rat_def minus_rat add_rat eq_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
288 |
show "q - r = q + (-r)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
289 |
by (induct q, induct r) (simp add: add_rat minus_rat diff_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
290 |
show "(q * r) * s = q * (r * s)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
291 |
by (induct q, induct r, induct s) (simp add: mult_rat mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
292 |
show "q * r = r * q" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
293 |
by (induct q, induct r) (simp add: mult_rat mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
294 |
show "1 * q = q" |
23879 | 295 |
by (induct q) (simp add: One_rat_def mult_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
296 |
show "(q + r) * s = q * s + r * s" |
14691 | 297 |
by (induct q, induct r, induct s) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
298 |
(simp add: add_rat mult_rat eq_rat int_distrib) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
299 |
show "q \<noteq> 0 ==> inverse q * q = 1" |
23879 | 300 |
by (induct q) (simp add: inverse_rat mult_rat One_rat_def Zero_rat_def eq_rat) |
14430
5cb24165a2e1
new material from Avigad, and simplified treatment of division by 0
paulson
parents:
14421
diff
changeset
|
301 |
show "q / r = q * inverse r" |
14691 | 302 |
by (simp add: divide_rat_def) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
303 |
show "0 \<noteq> (1::rat)" |
23879 | 304 |
by (simp add: Zero_rat_def One_rat_def eq_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
305 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
306 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
307 |
instance rat :: linorder |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
308 |
proof |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
309 |
fix q r s :: rat |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
310 |
{ |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
311 |
assume "q \<le> r" and "r \<le> s" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
312 |
show "q \<le> s" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
313 |
proof (insert prems, induct q, induct r, induct s) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
314 |
fix a b c d e f :: int |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
315 |
assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
316 |
assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract e f" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
317 |
show "Fract a b \<le> Fract e f" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
318 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
319 |
from neq obtain bb: "0 < b * b" and dd: "0 < d * d" and ff: "0 < f * f" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
320 |
by (auto simp add: zero_less_mult_iff linorder_neq_iff) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
321 |
have "(a * d) * (b * d) * (f * f) \<le> (c * b) * (b * d) * (f * f)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
322 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
323 |
from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
324 |
by (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
325 |
with ff show ?thesis by (simp add: mult_le_cancel_right) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
326 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
327 |
also have "... = (c * f) * (d * f) * (b * b)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
328 |
by (simp only: mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
329 |
also have "... \<le> (e * d) * (d * f) * (b * b)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
330 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
331 |
from neq 2 have "(c * f) * (d * f) \<le> (e * d) * (d * f)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
332 |
by (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
333 |
with bb show ?thesis by (simp add: mult_le_cancel_right) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
334 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
335 |
finally have "(a * f) * (b * f) * (d * d) \<le> e * b * (b * f) * (d * d)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
336 |
by (simp only: mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
337 |
with dd have "(a * f) * (b * f) \<le> (e * b) * (b * f)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
338 |
by (simp add: mult_le_cancel_right) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
339 |
with neq show ?thesis by (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
340 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
341 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
342 |
next |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
343 |
assume "q \<le> r" and "r \<le> q" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
344 |
show "q = r" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
345 |
proof (insert prems, induct q, induct r) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
346 |
fix a b c d :: int |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
347 |
assume neq: "b \<noteq> 0" "d \<noteq> 0" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
348 |
assume 1: "Fract a b \<le> Fract c d" and 2: "Fract c d \<le> Fract a b" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
349 |
show "Fract a b = Fract c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
350 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
351 |
from neq 1 have "(a * d) * (b * d) \<le> (c * b) * (b * d)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
352 |
by (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
353 |
also have "... \<le> (a * d) * (b * d)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
354 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
355 |
from neq 2 have "(c * b) * (d * b) \<le> (a * d) * (d * b)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
356 |
by (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
357 |
thus ?thesis by (simp only: mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
358 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
359 |
finally have "(a * d) * (b * d) = (c * b) * (b * d)" . |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
360 |
moreover from neq have "b * d \<noteq> 0" by simp |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
361 |
ultimately have "a * d = c * b" by simp |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
362 |
with neq show ?thesis by (simp add: eq_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
363 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
364 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
365 |
next |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
366 |
show "q \<le> q" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
367 |
by (induct q) (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
368 |
show "(q < r) = (q \<le> r \<and> q \<noteq> r)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
369 |
by (simp only: less_rat_def) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
370 |
show "q \<le> r \<or> r \<le> q" |
18913 | 371 |
by (induct q, induct r) |
372 |
(simp add: le_rat mult_commute, rule linorder_linear) |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
373 |
} |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
374 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
375 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
376 |
instantiation rat :: distrib_lattice |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
377 |
begin |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
378 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
379 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
380 |
"(inf \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = min" |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
381 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
382 |
definition |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
383 |
"(sup \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat) = max" |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
384 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
385 |
instance |
22456 | 386 |
by default (auto simp add: min_max.sup_inf_distrib1 inf_rat_def sup_rat_def) |
387 |
||
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
388 |
end |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
389 |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
390 |
instance rat :: ordered_field |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
391 |
proof |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
392 |
fix q r s :: rat |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
393 |
show "q \<le> r ==> s + q \<le> s + r" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
394 |
proof (induct q, induct r, induct s) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
395 |
fix a b c d e f :: int |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
396 |
assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
397 |
assume le: "Fract a b \<le> Fract c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
398 |
show "Fract e f + Fract a b \<le> Fract e f + Fract c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
399 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
400 |
let ?F = "f * f" from neq have F: "0 < ?F" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
401 |
by (auto simp add: zero_less_mult_iff) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
402 |
from neq le have "(a * d) * (b * d) \<le> (c * b) * (b * d)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
403 |
by (simp add: le_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
404 |
with F have "(a * d) * (b * d) * ?F * ?F \<le> (c * b) * (b * d) * ?F * ?F" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
405 |
by (simp add: mult_le_cancel_right) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
406 |
with neq show ?thesis by (simp add: add_rat le_rat mult_ac int_distrib) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
407 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
408 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
409 |
show "q < r ==> 0 < s ==> s * q < s * r" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
410 |
proof (induct q, induct r, induct s) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
411 |
fix a b c d e f :: int |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
412 |
assume neq: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
413 |
assume le: "Fract a b < Fract c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
414 |
assume gt: "0 < Fract e f" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
415 |
show "Fract e f * Fract a b < Fract e f * Fract c d" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
416 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
417 |
let ?E = "e * f" and ?F = "f * f" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
418 |
from neq gt have "0 < ?E" |
23879 | 419 |
by (auto simp add: Zero_rat_def less_rat le_rat order_less_le eq_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
420 |
moreover from neq have "0 < ?F" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
421 |
by (auto simp add: zero_less_mult_iff) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
422 |
moreover from neq le have "(a * d) * (b * d) < (c * b) * (b * d)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
423 |
by (simp add: less_rat) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
424 |
ultimately have "(a * d) * (b * d) * ?E * ?F < (c * b) * (b * d) * ?E * ?F" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
425 |
by (simp add: mult_less_cancel_right) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
426 |
with neq show ?thesis |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
427 |
by (simp add: less_rat mult_rat mult_ac) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
428 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
429 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
430 |
show "\<bar>q\<bar> = (if q < 0 then -q else q)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
431 |
by (simp only: abs_rat_def) |
24506 | 432 |
qed (auto simp: sgn_rat_def) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
433 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
434 |
instance rat :: division_by_zero |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
435 |
proof |
18913 | 436 |
show "inverse 0 = (0::rat)" |
23879 | 437 |
by (simp add: Zero_rat_def Fract_def inverse_rat_def |
18913 | 438 |
inverse_congruent UN_ratrel) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
439 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
440 |
|
20522 | 441 |
instance rat :: recpower |
442 |
proof |
|
443 |
fix q :: rat |
|
444 |
fix n :: nat |
|
445 |
show "q ^ 0 = 1" by simp |
|
446 |
show "q ^ (Suc n) = q * (q ^ n)" by simp |
|
447 |
qed |
|
448 |
||
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
449 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
450 |
subsection {* Various Other Results *} |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
451 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
452 |
lemma minus_rat_cancel [simp]: "b \<noteq> 0 ==> Fract (-a) (-b) = Fract a b" |
18913 | 453 |
by (simp add: eq_rat) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
454 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
455 |
theorem Rat_induct_pos [case_names Fract, induct type: rat]: |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
456 |
assumes step: "!!a b. 0 < b ==> P (Fract a b)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
457 |
shows "P q" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
458 |
proof (cases q) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
459 |
have step': "!!a b. b < 0 ==> P (Fract a b)" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
460 |
proof - |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
461 |
fix a::int and b::int |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
462 |
assume b: "b < 0" |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
463 |
hence "0 < -b" by simp |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
464 |
hence "P (Fract (-a) (-b))" by (rule step) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
465 |
thus "P (Fract a b)" by (simp add: order_less_imp_not_eq [OF b]) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
466 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
467 |
case (Fract a b) |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
468 |
thus "P q" by (force simp add: linorder_neq_iff step step') |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
469 |
qed |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
470 |
|
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
471 |
lemma zero_less_Fract_iff: |
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
472 |
"0 < b ==> (0 < Fract a b) = (0 < a)" |
23879 | 473 |
by (simp add: Zero_rat_def less_rat order_less_imp_not_eq2 zero_less_mult_iff) |
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
474 |
|
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
475 |
lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1" |
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
476 |
apply (insert add_rat [of concl: m n 1 1]) |
23879 | 477 |
apply (simp add: One_rat_def [symmetric]) |
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
478 |
done |
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
479 |
|
23429 | 480 |
lemma of_nat_rat: "of_nat k = Fract (of_nat k) 1" |
23879 | 481 |
by (induct k) (simp_all add: Zero_rat_def One_rat_def add_rat) |
23429 | 482 |
|
483 |
lemma of_int_rat: "of_int k = Fract k 1" |
|
484 |
by (cases k rule: int_diff_cases, simp add: of_nat_rat diff_rat) |
|
485 |
||
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
486 |
lemma Fract_of_nat_eq: "Fract (of_nat k) 1 = of_nat k" |
23429 | 487 |
by (rule of_nat_rat [symmetric]) |
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
488 |
|
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
489 |
lemma Fract_of_int_eq: "Fract k 1 = of_int k" |
23429 | 490 |
by (rule of_int_rat [symmetric]) |
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
491 |
|
24198 | 492 |
lemma Fract_of_int_quotient: "Fract k l = (if l = 0 then Fract 1 0 else of_int k / of_int l)" |
493 |
by (auto simp add: Fract_zero Fract_of_int_eq [symmetric] divide_rat) |
|
494 |
||
14378
69c4d5997669
generic of_nat and of_int functions, and generalization of iszero
paulson
parents:
14365
diff
changeset
|
495 |
|
14691 | 496 |
subsection {* Numerals and Arithmetic *} |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
14378
diff
changeset
|
497 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
498 |
instantiation rat :: number_ring |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
499 |
begin |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
14378
diff
changeset
|
500 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
501 |
definition |
25965 | 502 |
rat_number_of_def [code func del]: "number_of w = (of_int w \<Colon> rat)" |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
503 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
504 |
instance |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
505 |
by default (simp add: rat_number_of_def) |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
506 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
507 |
end |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
14378
diff
changeset
|
508 |
|
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
14378
diff
changeset
|
509 |
use "rat_arith.ML" |
24075 | 510 |
declaration {* K rat_arith_setup *} |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
14378
diff
changeset
|
511 |
|
23342 | 512 |
|
513 |
subsection {* Embedding from Rationals to other Fields *} |
|
514 |
||
24198 | 515 |
class field_char_0 = field + ring_char_0 |
23342 | 516 |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25502
diff
changeset
|
517 |
instance ordered_field < field_char_0 .. |
23342 | 518 |
|
519 |
definition |
|
520 |
of_rat :: "rat \<Rightarrow> 'a::field_char_0" |
|
521 |
where |
|
24198 | 522 |
[code func del]: "of_rat q = contents (\<Union>(a,b) \<in> Rep_Rat q. {of_int a / of_int b})" |
23342 | 523 |
|
524 |
lemma of_rat_congruent: |
|
525 |
"(\<lambda>(a, b). {of_int a / of_int b::'a::field_char_0}) respects ratrel" |
|
526 |
apply (rule congruent.intro) |
|
527 |
apply (clarsimp simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq) |
|
528 |
apply (simp only: of_int_mult [symmetric]) |
|
529 |
done |
|
530 |
||
531 |
lemma of_rat_rat: |
|
532 |
"b \<noteq> 0 \<Longrightarrow> of_rat (Fract a b) = of_int a / of_int b" |
|
533 |
unfolding Fract_def of_rat_def |
|
534 |
by (simp add: UN_ratrel of_rat_congruent) |
|
535 |
||
536 |
lemma of_rat_0 [simp]: "of_rat 0 = 0" |
|
537 |
by (simp add: Zero_rat_def of_rat_rat) |
|
538 |
||
539 |
lemma of_rat_1 [simp]: "of_rat 1 = 1" |
|
540 |
by (simp add: One_rat_def of_rat_rat) |
|
541 |
||
542 |
lemma of_rat_add: "of_rat (a + b) = of_rat a + of_rat b" |
|
543 |
by (induct a, induct b, simp add: add_rat of_rat_rat add_frac_eq) |
|
544 |
||
23343 | 545 |
lemma of_rat_minus: "of_rat (- a) = - of_rat a" |
546 |
by (induct a, simp add: minus_rat of_rat_rat) |
|
547 |
||
548 |
lemma of_rat_diff: "of_rat (a - b) = of_rat a - of_rat b" |
|
549 |
by (simp only: diff_minus of_rat_add of_rat_minus) |
|
550 |
||
23342 | 551 |
lemma of_rat_mult: "of_rat (a * b) = of_rat a * of_rat b" |
552 |
apply (induct a, induct b, simp add: mult_rat of_rat_rat) |
|
553 |
apply (simp add: divide_inverse nonzero_inverse_mult_distrib mult_ac) |
|
554 |
done |
|
555 |
||
556 |
lemma nonzero_of_rat_inverse: |
|
557 |
"a \<noteq> 0 \<Longrightarrow> of_rat (inverse a) = inverse (of_rat a)" |
|
23343 | 558 |
apply (rule inverse_unique [symmetric]) |
559 |
apply (simp add: of_rat_mult [symmetric]) |
|
23342 | 560 |
done |
561 |
||
562 |
lemma of_rat_inverse: |
|
563 |
"(of_rat (inverse a)::'a::{field_char_0,division_by_zero}) = |
|
564 |
inverse (of_rat a)" |
|
565 |
by (cases "a = 0", simp_all add: nonzero_of_rat_inverse) |
|
566 |
||
567 |
lemma nonzero_of_rat_divide: |
|
568 |
"b \<noteq> 0 \<Longrightarrow> of_rat (a / b) = of_rat a / of_rat b" |
|
569 |
by (simp add: divide_inverse of_rat_mult nonzero_of_rat_inverse) |
|
570 |
||
571 |
lemma of_rat_divide: |
|
572 |
"(of_rat (a / b)::'a::{field_char_0,division_by_zero}) |
|
573 |
= of_rat a / of_rat b" |
|
574 |
by (cases "b = 0", simp_all add: nonzero_of_rat_divide) |
|
575 |
||
23343 | 576 |
lemma of_rat_power: |
577 |
"(of_rat (a ^ n)::'a::{field_char_0,recpower}) = of_rat a ^ n" |
|
578 |
by (induct n) (simp_all add: of_rat_mult power_Suc) |
|
579 |
||
580 |
lemma of_rat_eq_iff [simp]: "(of_rat a = of_rat b) = (a = b)" |
|
581 |
apply (induct a, induct b) |
|
582 |
apply (simp add: of_rat_rat eq_rat) |
|
583 |
apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq) |
|
584 |
apply (simp only: of_int_mult [symmetric] of_int_eq_iff) |
|
585 |
done |
|
586 |
||
587 |
lemmas of_rat_eq_0_iff [simp] = of_rat_eq_iff [of _ 0, simplified] |
|
588 |
||
589 |
lemma of_rat_eq_id [simp]: "of_rat = (id :: rat \<Rightarrow> rat)" |
|
590 |
proof |
|
591 |
fix a |
|
592 |
show "of_rat a = id a" |
|
593 |
by (induct a) |
|
594 |
(simp add: of_rat_rat divide_rat Fract_of_int_eq [symmetric]) |
|
595 |
qed |
|
596 |
||
597 |
text{*Collapse nested embeddings*} |
|
598 |
lemma of_rat_of_nat_eq [simp]: "of_rat (of_nat n) = of_nat n" |
|
599 |
by (induct n) (simp_all add: of_rat_add) |
|
600 |
||
601 |
lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z" |
|
23365 | 602 |
by (cases z rule: int_diff_cases, simp add: of_rat_diff) |
23343 | 603 |
|
604 |
lemma of_rat_number_of_eq [simp]: |
|
605 |
"of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})" |
|
606 |
by (simp add: number_of_eq) |
|
607 |
||
23879 | 608 |
lemmas zero_rat = Zero_rat_def |
609 |
lemmas one_rat = One_rat_def |
|
610 |
||
24198 | 611 |
abbreviation |
612 |
rat_of_nat :: "nat \<Rightarrow> rat" |
|
613 |
where |
|
614 |
"rat_of_nat \<equiv> of_nat" |
|
615 |
||
616 |
abbreviation |
|
617 |
rat_of_int :: "int \<Rightarrow> rat" |
|
618 |
where |
|
619 |
"rat_of_int \<equiv> of_int" |
|
620 |
||
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
621 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
622 |
subsection {* Implementation of rational numbers as pairs of integers *} |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
623 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
624 |
definition |
24622 | 625 |
Rational :: "int \<times> int \<Rightarrow> rat" |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
626 |
where |
24622 | 627 |
"Rational = INum" |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
628 |
|
24622 | 629 |
code_datatype Rational |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
630 |
|
24622 | 631 |
lemma Rational_simp: |
632 |
"Rational (k, l) = rat_of_int k / rat_of_int l" |
|
633 |
unfolding Rational_def INum_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
634 |
|
24622 | 635 |
lemma Rational_zero [simp]: "Rational 0\<^sub>N = 0" |
636 |
by (simp add: Rational_simp) |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
637 |
|
24622 | 638 |
lemma Rational_lit [simp]: "Rational i\<^sub>N = rat_of_int i" |
639 |
by (simp add: Rational_simp) |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
640 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
641 |
lemma zero_rat_code [code, code unfold]: |
24622 | 642 |
"0 = Rational 0\<^sub>N" by simp |
25965 | 643 |
declare zero_rat_code [symmetric, code post] |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
644 |
|
25965 | 645 |
lemma one_rat_code [code, code unfold]: |
24622 | 646 |
"1 = Rational 1\<^sub>N" by simp |
25965 | 647 |
declare one_rat_code [symmetric, code post] |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
648 |
|
25965 | 649 |
lemma [code unfold, symmetric, code post]: |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
650 |
"number_of k = rat_of_int (number_of k)" |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
651 |
by (simp add: number_of_is_id rat_number_of_def) |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
652 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
653 |
definition |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
654 |
[code func del]: "Fract' (b\<Colon>bool) k l = Fract k l" |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
655 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
656 |
lemma [code]: |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
657 |
"Fract k l = Fract' (l \<noteq> 0) k l" |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
658 |
unfolding Fract'_def .. |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
659 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
660 |
lemma [code]: |
24622 | 661 |
"Fract' True k l = (if l \<noteq> 0 then Rational (k, l) else Fract 1 0)" |
662 |
by (simp add: Fract'_def Rational_simp Fract_of_int_quotient [of k l]) |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
663 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
664 |
lemma [code]: |
24622 | 665 |
"of_rat (Rational (k, l)) = (if l \<noteq> 0 then of_int k / of_int l else 0)" |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
666 |
by (cases "l = 0") |
24622 | 667 |
(auto simp add: Rational_simp of_rat_rat [simplified Fract_of_int_quotient [of k l], symmetric]) |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
668 |
|
26513 | 669 |
instantiation rat :: eq |
670 |
begin |
|
671 |
||
672 |
definition [code func del]: "eq (r\<Colon>rat) s \<longleftrightarrow> r = s" |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
673 |
|
26513 | 674 |
instance by default (simp add: eq_rat_def) |
675 |
||
676 |
lemma rat_eq_code [code]: "eq (Rational x) (Rational y) \<longleftrightarrow> eq (normNum x) (normNum y)" |
|
677 |
unfolding Rational_def INum_normNum_iff eq .. |
|
678 |
||
679 |
end |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
680 |
|
24622 | 681 |
lemma rat_less_eq_code [code]: "Rational x \<le> Rational y \<longleftrightarrow> normNum x \<le>\<^sub>N normNum y" |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
682 |
proof - |
24622 | 683 |
have "normNum x \<le>\<^sub>N normNum y \<longleftrightarrow> Rational (normNum x) \<le> Rational (normNum y)" |
684 |
by (simp add: Rational_def del: normNum) |
|
685 |
also have "\<dots> = (Rational x \<le> Rational y)" by (simp add: Rational_def) |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
686 |
finally show ?thesis by simp |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
687 |
qed |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
688 |
|
24622 | 689 |
lemma rat_less_code [code]: "Rational x < Rational y \<longleftrightarrow> normNum x <\<^sub>N normNum y" |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
690 |
proof - |
24622 | 691 |
have "normNum x <\<^sub>N normNum y \<longleftrightarrow> Rational (normNum x) < Rational (normNum y)" |
692 |
by (simp add: Rational_def del: normNum) |
|
693 |
also have "\<dots> = (Rational x < Rational y)" by (simp add: Rational_def) |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
694 |
finally show ?thesis by simp |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
695 |
qed |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
696 |
|
24622 | 697 |
lemma rat_add_code [code]: "Rational x + Rational y = Rational (x +\<^sub>N y)" |
698 |
unfolding Rational_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
699 |
|
24622 | 700 |
lemma rat_mul_code [code]: "Rational x * Rational y = Rational (x *\<^sub>N y)" |
701 |
unfolding Rational_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
702 |
|
24622 | 703 |
lemma rat_neg_code [code]: "- Rational x = Rational (~\<^sub>N x)" |
704 |
unfolding Rational_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
705 |
|
24622 | 706 |
lemma rat_sub_code [code]: "Rational x - Rational y = Rational (x -\<^sub>N y)" |
707 |
unfolding Rational_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
708 |
|
24622 | 709 |
lemma rat_inv_code [code]: "inverse (Rational x) = Rational (Ninv x)" |
710 |
unfolding Rational_def Ninv divide_rat_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
711 |
|
24622 | 712 |
lemma rat_div_code [code]: "Rational x / Rational y = Rational (x \<div>\<^sub>N y)" |
713 |
unfolding Rational_def by simp |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
714 |
|
24622 | 715 |
text {* Setup for SML code generator *} |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
716 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
717 |
types_code |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
718 |
rat ("(int */ int)") |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
719 |
attach (term_of) {* |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
720 |
fun term_of_rat (p, q) = |
24622 | 721 |
let |
24661 | 722 |
val rT = Type ("Rational.rat", []) |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
723 |
in |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
724 |
if q = 1 orelse p = 0 then HOLogic.mk_number rT p |
25885 | 725 |
else @{term "op / \<Colon> rat \<Rightarrow> rat \<Rightarrow> rat"} $ |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
726 |
HOLogic.mk_number rT p $ HOLogic.mk_number rT q |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
727 |
end; |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
728 |
*} |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
729 |
attach (test) {* |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
730 |
fun gen_rat i = |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
731 |
let |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
732 |
val p = random_range 0 i; |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
733 |
val q = random_range 1 (i + 1); |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
734 |
val g = Integer.gcd p q; |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24622
diff
changeset
|
735 |
val p' = p div g; |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24622
diff
changeset
|
736 |
val q' = q div g; |
25885 | 737 |
val r = (if one_of [true, false] then p' else ~ p', |
738 |
if p' = 0 then 0 else q') |
|
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
739 |
in |
25885 | 740 |
(r, fn () => term_of_rat r) |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
741 |
end; |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
742 |
*} |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
743 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
744 |
consts_code |
24622 | 745 |
Rational ("(_)") |
24533
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
746 |
|
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
747 |
consts_code |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
748 |
"of_int :: int \<Rightarrow> rat" ("\<module>rat'_of'_int") |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
749 |
attach {* |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
750 |
fun rat_of_int 0 = (0, 0) |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
751 |
| rat_of_int i = (i, 1); |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
752 |
*} |
fe1f93f6a15a
Added code generator setup (taken from Library/Executable_Rat.thy,
berghofe
parents:
24506
diff
changeset
|
753 |
|
14365
3d4df8c166ae
replacing HOL/Real/PRat, PNat by the rational number development
paulson
parents:
diff
changeset
|
754 |
end |