1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/Q/q".
17 include "Z/compare.ma".
19 include "higher_order_defs/functions.ma".
21 (* a fraction is a list of Z-coefficients for primes, in natural
22 order. The last coefficient must eventually be different from 0 *)
24 inductive fraction : Set \def
26 | nn: nat \to fraction
27 | cons : Z \to fraction \to fraction.
29 inductive ratio : Set \def
31 | frac : fraction \to ratio.
33 (* a rational number is either O or a ratio with a sign *)
34 inductive Q : Set \def
39 (* double elimination principles *)
40 theorem fraction_elim2:
41 \forall R:fraction \to fraction \to Prop.
42 (\forall n:nat.\forall g:fraction.R (pp n) g) \to
43 (\forall n:nat.\forall g:fraction.R (nn n) g) \to
44 (\forall x:Z.\forall f:fraction.\forall m:nat.R (cons x f) (pp m)) \to
45 (\forall x:Z.\forall f:fraction.\forall m:nat.R (cons x f) (nn m)) \to
46 (\forall x,y:Z.\forall f,g:fraction.R f g \to R (cons x f) (cons y g)) \to
47 \forall f,g:fraction. R f g.
48 intros 7.elim f.apply H.
55 (* boolean equality *)
60 [ (pp m) \Rightarrow eqb n m
61 | (nn m) \Rightarrow false
62 | (cons y g1) \Rightarrow false]
65 [ (pp m) \Rightarrow false
66 | (nn m) \Rightarrow eqb n m
67 | (cons y g1) \Rightarrow false]
68 | (cons x f1) \Rightarrow
70 [ (pp m) \Rightarrow false
71 | (nn m) \Rightarrow false
72 | (cons y g1) \Rightarrow andb (eqZb x y) (eqfb f1 g1)]].
76 \lambda f. match f with
77 [ (pp n) \Rightarrow n
78 | (nn n) \Rightarrow n
79 | (cons x f) \Rightarrow O].
82 \lambda f. match f with
83 [ (pp n) \Rightarrow (pos n)
84 | (nn n) \Rightarrow (neg n)
85 | (cons x f) \Rightarrow x].
88 \lambda f. match f with
89 [ (pp n) \Rightarrow (pp n)
90 | (nn n) \Rightarrow (nn n)
91 | (cons x f) \Rightarrow f].
93 theorem injective_pp : injective nat fraction pp.
95 change with (aux (pp x)) = (aux (pp y)).
96 apply eq_f.assumption.
99 theorem injective_nn : injective nat fraction nn.
101 change with (aux (nn x)) = (aux (nn y)).
102 apply eq_f.assumption.
105 theorem eq_cons_to_eq1: \forall f,g:fraction.\forall x,y:Z.
106 (cons x f) = (cons y g) \to x = y.
108 change with (fhd (cons x f)) = (fhd (cons y g)).
109 apply eq_f.assumption.
112 theorem eq_cons_to_eq2: \forall x,y:Z.\forall f,g:fraction.
113 (cons x f) = (cons y g) \to f = g.
115 change with (ftl (cons x f)) = (ftl (cons y g)).
116 apply eq_f.assumption.
119 theorem not_eq_pp_nn: \forall n,m:nat. \lnot (pp n) = (nn m).
120 intros.simplify. intro.
121 change with match (pp n) with
122 [ (pp n) \Rightarrow False
123 | (nn n) \Rightarrow True
124 | (cons x f) \Rightarrow True].
129 theorem not_eq_pp_cons:
130 \forall n:nat.\forall x:Z. \forall f:fraction.
131 \lnot (pp n) = (cons x f).
132 intros.simplify. intro.
133 change with match (pp n) with
134 [ (pp n) \Rightarrow False
135 | (nn n) \Rightarrow True
136 | (cons x f) \Rightarrow True].
141 theorem not_eq_nn_cons:
142 \forall n:nat.\forall x:Z. \forall f:fraction.
143 \lnot (nn n) = (cons x f).
144 intros.simplify. intro.
145 change with match (nn n) with
146 [ (pp n) \Rightarrow True
147 | (nn n) \Rightarrow False
148 | (cons x f) \Rightarrow True].
153 theorem decidable_eq_fraction: \forall f,g:fraction.
156 apply fraction_elim2 (\lambda f,g. Or (f=g) (f=g \to False)).
158 elim ((decidable_eq_nat n n1) : Or (n=n1) (n=n1 \to False)).
159 left.apply eq_f. assumption.
160 right.intro.apply H.apply injective_pp.assumption.
161 right.apply not_eq_pp_cons.
162 right.apply not_eq_pp_nn.
164 right.intro.apply not_eq_pp_nn n1 n ?.apply sym_eq. assumption.
165 right.apply not_eq_nn_cons.
166 elim ((decidable_eq_nat n n1) : Or (n=n1) (n=n1 \to False)).
167 left. apply eq_f. assumption.
168 right.intro.apply H.apply injective_nn.assumption.
169 intros.right.intro.apply not_eq_pp_cons m x f1 ?.apply sym_eq.assumption.
170 intros.right.intro.apply not_eq_nn_cons m x f1 ?.apply sym_eq.assumption.
172 elim ((decidable_eq_Z x y) : Or (x=y) (x=y \to False)).
173 left.apply eq_f2.assumption. assumption.
174 right.intro.apply H2.
175 apply eq_cons_to_eq1 f1 g1.assumption.
176 right.intro.apply H1.
177 apply eq_cons_to_eq2 x y f1 g1.assumption.
180 theorem eqfb_to_Prop: \forall f,g:fraction.
181 match (eqfb f g) with
182 [true \Rightarrow f=g
183 |false \Rightarrow \lnot f=g].
184 intros.apply fraction_elim2
185 (\lambda f,g.match (eqfb f g) with
186 [true \Rightarrow f=g
187 |false \Rightarrow \lnot f=g]).
191 intro.simplify.apply eq_f.assumption.
192 intro.simplify.intro.apply H.apply injective_pp.assumption.
193 simplify.apply not_eq_pp_cons.
194 simplify.apply not_eq_pp_nn.
197 intro.apply not_eq_pp_nn n1 n ?.apply sym_eq. assumption.
198 simplify.apply not_eq_nn_cons.
199 simplify.apply eqb_elim.
200 intro.simplify.apply eq_f.assumption.
201 intro.simplify.intro.apply H.apply injective_nn.assumption.
203 simplify.intro.apply not_eq_pp_cons m x f1 ?.apply sym_eq. assumption.
204 intros.simplify.intro.apply not_eq_nn_cons m x f1 ?.apply sym_eq. assumption.
206 change in match (eqfb (cons x f1) (cons y g1)) with
207 (andb (eqZb x y) (eqfb f1 g1)).
209 intro.generalize in match H.
211 simplify.apply eq_f2.assumption.apply H2.
212 simplify.intro.apply H2.apply eq_cons_to_eq2 x y.assumption.
214 intro.apply H1.apply eq_cons_to_eq1 f1 g1.assumption.
219 [ (pp n) \Rightarrow (nn n)
220 | (nn n) \Rightarrow (pp n)
221 | (cons x g) \Rightarrow (cons (Zopp x) (finv g))].
223 definition Z_to_ratio :Z \to ratio \def
224 \lambda x:Z. match x with
226 | (pos n) \Rightarrow frac (pp n)
227 | (neg n) \Rightarrow frac (nn n)].
229 let rec ftimes f g \def
233 [(pp m) \Rightarrow Z_to_ratio (Zplus (pos n) (pos m))
234 | (nn m) \Rightarrow Z_to_ratio (Zplus (pos n) (neg m))
235 | (cons y g1) \Rightarrow frac (cons (Zplus (pos n) y) g1)]
238 [(pp m) \Rightarrow Z_to_ratio (Zplus (neg n) (pos m))
239 | (nn m) \Rightarrow Z_to_ratio (Zplus (neg n) (neg m))
240 | (cons y g1) \Rightarrow frac (cons (Zplus (neg n) y) g1)]
241 | (cons x f1) \Rightarrow
243 [ (pp m) \Rightarrow frac (cons (Zplus x (pos m)) f1)
244 | (nn m) \Rightarrow frac (cons (Zplus x (neg m)) f1)
245 | (cons y g1) \Rightarrow
246 match ftimes f1 g1 with
247 [ one \Rightarrow Z_to_ratio (Zplus x y)
248 | (frac h) \Rightarrow frac (cons (Zplus x y) h)]]].
250 theorem symmetric2_ftimes: symmetric2 fraction ratio ftimes.
252 apply fraction_elim2 (\lambda f,g.ftimes f g = ftimes g f).
254 change with Z_to_ratio (Zplus (pos n) (pos n1)) = Z_to_ratio (Zplus (pos n1) (pos n)).
255 apply eq_f.apply sym_Zplus.
256 change with frac (cons (Zplus (pos n) z) f) = frac (cons (Zplus z (pos n)) f).
257 rewrite < sym_Zplus.reflexivity.
258 change with Z_to_ratio (Zplus (pos n) (neg n1)) = Z_to_ratio (Zplus (neg n1) (pos n)).
259 apply eq_f.apply sym_Zplus.
261 change with Z_to_ratio (Zplus (neg n) (pos n1)) = Z_to_ratio (Zplus (pos n1) (neg n)).
262 apply eq_f.apply sym_Zplus.
263 change with frac (cons (Zplus (neg n) z) f) = frac (cons (Zplus z (neg n)) f).
264 rewrite < sym_Zplus.reflexivity.
265 change with Z_to_ratio (Zplus (neg n) (neg n1)) = Z_to_ratio (Zplus (neg n1) (neg n)).
266 apply eq_f.apply sym_Zplus.
268 change with frac (cons (Zplus x1 (pos m)) f) = frac (cons (Zplus (pos m) x1) f).
269 rewrite < sym_Zplus.reflexivity.
271 change with frac (cons (Zplus x1 (neg m)) f) = frac (cons (Zplus (neg m) x1) f).
272 rewrite < sym_Zplus.reflexivity.
275 match ftimes f g with
276 [ one \Rightarrow Z_to_ratio (Zplus x1 y1)
277 | (frac h) \Rightarrow frac (cons (Zplus x1 y1) h)] =
278 match ftimes g f with
279 [ one \Rightarrow Z_to_ratio (Zplus y1 x1)
280 | (frac h) \Rightarrow frac (cons (Zplus y1 x1) h)].
281 rewrite < H.rewrite < sym_Zplus.
285 theorem ftimes_finv : \forall f:fraction. ftimes f (finv f) = one.
288 change with Z_to_ratio (Zplus (pos n) (Zopp (pos n))) = one.
289 rewrite > Zplus_Zopp.reflexivity.
290 (* again: we would need something to help finding the right change *)
292 match ftimes f1 (finv f1) with
293 [ one \Rightarrow Z_to_ratio (Zplus z (Zopp z))
294 | (frac h) \Rightarrow frac (cons (Zplus z (Zopp z)) h)] = one.
296 rewrite > Zplus_Zopp.reflexivity.
297 change with Z_to_ratio (Zplus (neg n) (Zopp (neg n))) = one.
298 rewrite > Zplus_Zopp.reflexivity.
301 definition rtimes : ratio \to ratio \to ratio \def
305 | (frac f) \Rightarrow
307 [one \Rightarrow frac f
308 | (frac g) \Rightarrow ftimes f g]].
310 theorem symmetric_rtimes : symmetric ratio rtimes.
311 change with \forall r,s:ratio. rtimes r s = rtimes s r.
318 simplify.apply symmetric2_ftimes.
321 definition rinv : ratio \to ratio \def
325 | (frac f) \Rightarrow frac (finv f)].
327 theorem rtimes_rinv: \forall r:ratio. rtimes r (rinv r) = one.
330 simplify.apply ftimes_finv.