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 "Z/compare.ma".
16 include "nat/factorization.ma".
18 (* a fraction is a list of Z-coefficients for primes, in natural
19 order. The last coefficient must eventually be different from 0 *)
21 inductive fraction : Set \def
23 | nn: nat \to fraction
24 | cons : Z \to fraction \to fraction.
26 let rec nat_fact_to_fraction n ≝
29 | nf_cons m l ⇒ cons (Z_of_nat m) (nat_fact_to_fraction l)
32 (* returns the numerator of a fraction in the form of a nat_fact_all *)
33 let rec numerator f \def
35 [pp a \Rightarrow nfa_proper (nf_last a)
36 |nn a \Rightarrow nfa_one
38 let n \def numerator l in
40 [nfa_zero \Rightarrow (* this case is vacuous *) nfa_zero
43 [OZ \Rightarrow nfa_one
44 |pos x \Rightarrow nfa_proper (nf_last x)
45 |neg x \Rightarrow nfa_one
47 |nfa_proper g \Rightarrow
49 [OZ \Rightarrow nfa_proper (nf_cons O g)
50 |pos x \Rightarrow nfa_proper (nf_cons (S x) g)
51 |neg x \Rightarrow nfa_proper (nf_cons O g)
56 theorem not_eq_numerator_nfa_zero:
57 \forall f.numerator f \neq nfa_zero.
59 [simplify.intro.destruct H
60 |simplify.intro.destruct H
61 |simplify.generalize in match H.
63 [intro.elim H1.reflexivity
65 cases z;simplify;intro;destruct H2
67 cases z;simplify;intro;destruct H2
72 theorem numerator_nat_fact_to_fraction: \forall g:nat_fact.
73 numerator (nat_fact_to_fraction g) = nfa_proper g.
76 [simplify.reflexivity.
77 |simplify.rewrite > H.simplify.
82 (* double elimination principles *)
83 theorem fraction_elim2:
84 \forall R:fraction \to fraction \to Prop.
85 (\forall n:nat.\forall g:fraction.R (pp n) g) \to
86 (\forall n:nat.\forall g:fraction.R (nn n) g) \to
87 (\forall x:Z.\forall f:fraction.\forall m:nat.R (cons x f) (pp m)) \to
88 (\forall x:Z.\forall f:fraction.\forall m:nat.R (cons x f) (nn m)) \to
89 (\forall x,y:Z.\forall f,g:fraction.R f g \to R (cons x f) (cons y g)) \to
90 \forall f,g:fraction. R f g.
100 (* boolean equality *)
101 let rec eqfb f g \def
105 [ (pp m) \Rightarrow eqb n m
106 | (nn m) \Rightarrow false
107 | (cons y g1) \Rightarrow false]
110 [ (pp m) \Rightarrow false
111 | (nn m) \Rightarrow eqb n m
112 | (cons y g1) \Rightarrow false]
113 | (cons x f1) \Rightarrow
115 [ (pp m) \Rightarrow false
116 | (nn m) \Rightarrow false
117 | (cons y g1) \Rightarrow andb (eqZb x y) (eqfb f1 g1)]].
121 \lambda f. match f with
122 [ (pp n) \Rightarrow n
123 | (nn n) \Rightarrow n
124 | (cons x f) \Rightarrow O].
127 \lambda f. match f with
128 [ (pp n) \Rightarrow (pos n)
129 | (nn n) \Rightarrow (neg n)
130 | (cons x f) \Rightarrow x].
133 \lambda f. match f with
134 [ (pp n) \Rightarrow (pp n)
135 | (nn n) \Rightarrow (nn n)
136 | (cons x f) \Rightarrow f].
138 theorem injective_pp : injective nat fraction pp.
139 unfold injective.intros.
140 change with ((aux (pp x)) = (aux (pp y))).
141 apply eq_f.assumption.
144 theorem injective_nn : injective nat fraction nn.
145 unfold injective.intros.
146 change with ((aux (nn x)) = (aux (nn y))).
147 apply eq_f.assumption.
150 theorem eq_cons_to_eq1: \forall f,g:fraction.\forall x,y:Z.
151 (cons x f) = (cons y g) \to x = y.
153 change with ((fhd (cons x f)) = (fhd (cons y g))).
154 apply eq_f.assumption.
157 theorem eq_cons_to_eq2: \forall x,y:Z.\forall f,g:fraction.
158 (cons x f) = (cons y g) \to f = g.
160 change with ((ftl (cons x f)) = (ftl (cons y g))).
161 apply eq_f.assumption.
164 theorem not_eq_pp_nn: \forall n,m:nat. pp n \neq nn m.
165 intros.unfold Not. intro.
166 change with match (pp n) with
167 [ (pp n) \Rightarrow False
168 | (nn n) \Rightarrow True
169 | (cons x f) \Rightarrow True].
174 theorem not_eq_pp_cons:
175 \forall n:nat.\forall x:Z. \forall f:fraction.
177 intros.unfold Not. intro.
178 change with match (pp n) with
179 [ (pp n) \Rightarrow False
180 | (nn n) \Rightarrow True
181 | (cons x f) \Rightarrow True].
186 theorem not_eq_nn_cons:
187 \forall n:nat.\forall x:Z. \forall f:fraction.
189 intros.unfold Not. intro.
190 change with match (nn n) with
191 [ (pp n) \Rightarrow True
192 | (nn n) \Rightarrow False
193 | (cons x f) \Rightarrow True].
198 theorem decidable_eq_fraction: \forall f,g:fraction.
200 intros.unfold decidable.
201 apply (fraction_elim2 (\lambda f,g. f=g \lor (f=g \to False))).
203 elim ((decidable_eq_nat n n1) : n=n1 \lor (n=n1 \to False)).
204 left.apply eq_f. assumption.
205 right.intro.apply H.apply injective_pp.assumption.
206 right.apply not_eq_pp_nn.
207 right.apply not_eq_pp_cons.
209 right.intro.apply (not_eq_pp_nn n1 n).apply sym_eq. assumption.
210 elim ((decidable_eq_nat n n1) : n=n1 \lor (n=n1 \to False)).
211 left. apply eq_f. assumption.
212 right.intro.apply H.apply injective_nn.assumption.
213 right.apply not_eq_nn_cons.
214 intros.right.intro.apply (not_eq_pp_cons m x f1).apply sym_eq.assumption.
215 intros.right.intro.apply (not_eq_nn_cons m x f1).apply sym_eq.assumption.
217 elim ((decidable_eq_Z x y) : x=y \lor (x=y \to False)).
218 left.apply eq_f2.assumption.
220 right.intro.apply H2.apply (eq_cons_to_eq1 f1 g1).assumption.
221 right.intro.apply H1.apply (eq_cons_to_eq2 x y f1 g1).assumption.
224 theorem eqfb_to_Prop: \forall f,g:fraction.
225 match (eqfb f g) with
226 [true \Rightarrow f=g
227 |false \Rightarrow f \neq g].
228 intros.apply (fraction_elim2
229 (\lambda f,g.match (eqfb f g) with
230 [true \Rightarrow f=g
231 |false \Rightarrow f \neq g])).
233 simplify.apply eqb_elim.
234 intro.simplify.apply eq_f.assumption.
235 intro.simplify.unfold Not.intro.apply H.apply injective_pp.assumption.
236 simplify.apply not_eq_pp_nn.
237 simplify.apply not_eq_pp_cons.
239 simplify.unfold Not.intro.apply (not_eq_pp_nn n1 n).apply sym_eq. assumption.
240 simplify.apply eqb_elim.intro.simplify.apply eq_f.assumption.
241 intro.simplify.unfold Not.intro.apply H.apply injective_nn.assumption.
242 simplify.apply not_eq_nn_cons.
243 intros.simplify.unfold Not.intro.apply (not_eq_pp_cons m x f1).apply sym_eq. assumption.
244 intros.simplify.unfold Not.intro.apply (not_eq_nn_cons m x f1).apply sym_eq. assumption.
248 intro.generalize in match H.elim (eqfb f1 g1).
249 simplify.apply eq_f2.assumption.
251 simplify.unfold Not.intro.apply H2.apply (eq_cons_to_eq2 x y).assumption.
252 intro.simplify.unfold Not.intro.apply H1.apply (eq_cons_to_eq1 f1 g1).assumption.