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 include "Q/q/qinv.ma".
18 let rec nat_fact_to_fraction_inv l \def
20 [nf_last a \Rightarrow nn a
21 |nf_cons a p \Rightarrow
22 cons (neg_Z_of_nat a) (nat_fact_to_fraction_inv p)
26 definition nat_fact_all_to_Q_inv \def
29 [nfa_zero \Rightarrow OQ
30 |nfa_one \Rightarrow Qpos one
31 |nfa_proper l \Rightarrow Qpos (frac (nat_fact_to_fraction_inv l))
35 definition nat_to_Q_inv \def
36 \lambda n. nat_fact_all_to_Q_inv (factorize n).
38 definition frac:nat \to nat \to Q \def
39 \lambda p,q. Qtimes (nat_to_Q p) (Qinv (nat_to_Q q)).
41 theorem Qtimes_frac_frac: \forall p,q,r,s.
42 Qtimes (frac p q) (frac r s) = (frac (p*r) (q*s)).
45 rewrite > associative_Qtimes.
46 rewrite < associative_Qtimes in ⊢ (? ? (? ? %) ?).
47 rewrite > symmetric_Qtimes in ⊢ (? ? (? ? (? % ?)) ?).
48 rewrite > associative_Qtimes in ⊢ (? ? (? ? %) ?).
49 rewrite < associative_Qtimes.
50 rewrite < times_Qtimes.
51 rewrite < Qinv_Qtimes'.
52 rewrite < times_Qtimes.
58 definition numQ:Q \to Z \def
62 |Qpos r \Rightarrow Z_of_nat (defactorize (numeratorQ (Qpos r)))
63 |Qneg r \Rightarrow neg_Z_of_nat (defactorize (numeratorQ (Qpos r)))
67 definition numQ:Q \to nat \def
68 \lambda q. defactorize (numeratorQ q).
70 definition denomQ:Q \to nat \def
71 \lambda q. defactorize (numeratorQ (Qinv q)).
74 theorem frac_numQ_denomQ1: \forall r:ratio.
75 frac (numQ (Qpos r)) (denomQ (Qpos r)) = (Qpos r).
77 unfold frac.unfold denomQ.unfold numQ.
79 rewrite > factorize_defactorize.
80 rewrite > factorize_defactorize.
86 |apply Qtimes_numerator_denominator.
92 theorem frac_numQ_denomQ2: \forall r:ratio.
93 frac (numQ (Qneg r)) (denomQ (Qneg r)) = (Qpos r).
95 unfold frac.unfold denomQ.unfold numQ.
97 rewrite > factorize_defactorize.
98 rewrite > factorize_defactorize.
104 |apply Qtimes_numerator_denominator.
109 definition Qabs:Q \to Q \def \lambda q.
112 |Qpos q \Rightarrow Qpos q
113 |Qneg q \Rightarrow Qpos q
116 theorem frac_numQ_denomQ: \forall q.
117 frac (numQ q) (denomQ q) = (Qabs q).
121 |simplify in ⊢ (? ? ? %).apply frac_numQ_denomQ1
122 |simplify in ⊢ (? ? ? %).apply frac_numQ_denomQ2
126 definition Qfrac: Z \to nat \to Q \def
127 \lambda z,n.match z with
129 |Zpos m \Rightarrow (frac (S m) n)
130 |Zneg m \Rightarrow Qopp (frac (S m) n)
133 definition QnumZ \def \lambda q.
136 |Qpos r \Rightarrow Z_of_nat (numQ (Qpos r))
137 |Qneg r \Rightarrow neg_Z_of_nat (numQ (Qpos r))
140 theorem Qfrac_Z_of_nat: \forall n,m.
141 Qfrac (Z_of_nat n) m = frac n m.
142 intros.cases n;reflexivity.
145 theorem Qfrac_neg_Z_of_nat: \forall n,m.
146 Qfrac (neg_Z_of_nat n) m = Qopp (frac n m).
147 intros.cases n;reflexivity.
150 theorem Qfrac_QnumZ_denomQ: \forall q.
151 Qfrac (QnumZ q) (denomQ q) = q.
156 (Qfrac (Z_of_nat (numQ (Qpos r))) (denomQ (Qpos r))=Qpos r).
157 rewrite > Qfrac_Z_of_nat.
158 apply frac_numQ_denomQ1.
159 |simplify in ⊢ (? ? ? %).apply frac_numQ_denomQ2