1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
16 include "list/list.ma".
17 include "cprop_connectives.ma".
20 notation "\rationals" non associative with precedence 99 for @{'q}.
21 interpretation "Q" 'q = Q.
23 definition bar ≝ ratio × ℚ. (* base (Qpos) , height *)
24 record q_f : Type ≝ { start : ℚ; bars: list bar }.
28 axiom qlt : ℚ → ℚ → CProp.
30 interpretation "Q plus" 'plus x y = (qp x y).
31 interpretation "Q minus" 'minus x y = (qm x y).
32 interpretation "Q less than" 'lt x y = (qlt x y).
34 inductive q_comparison (a,b:ℚ) : CProp ≝
35 | q_eq : a = b → q_comparison a b
36 | q_lt : a < b → q_comparison a b
37 | q_gt : b < a → q_comparison a b.
39 axiom q_cmp:∀a,b:ℚ.q_comparison a b.
41 definition qle ≝ λa,b:ℚ.a = b ∨ a < b.
43 interpretation "Q less or equal than" 'leq x y = (qle x y).
45 axiom q_le_minus: ∀a,b,c:ℚ. a ≤ c - b → a + b ≤ c.
46 axiom q_le_minus_r: ∀a,b,c:ℚ. a - b ≤ c → a ≤ c + b.
47 axiom q_lt_plus: ∀a,b,c:ℚ. a - b < c → a < c + b.
48 axiom q_lt_minus: ∀a,b,c:ℚ. a + b < c → a < c - b.
50 axiom q_dist : ℚ → ℚ → ℚ.
52 notation "hbox(\dd [term 19 x, break term 19 y])" with precedence 90
53 for @{'distance $x $y}.
54 interpretation "ℚ distance" 'distance x y = (q_dist x y).
56 axiom q_dist_ge_OQ : ∀x,y:ℚ. OQ ≤ ⅆ[x,y].
58 axiom q_lt_to_le: ∀a,b:ℚ.a < b → a ≤ b.
59 axiom q_le_to_diff_ge_OQ : ∀a,b.a ≤ b → OQ ≤ b-a.
60 axiom q_plus_OQ: ∀x:ℚ.x + OQ = x.
61 axiom q_plus_sym: ∀x,y:ℚ.x + y = y + x.
62 axiom nat_of_q: ℚ → nat.
64 interpretation "list nth" 'nth = (nth _).
65 interpretation "list nth" 'nth_appl l d i = (nth _ l d i).
66 notation "'nth'" with precedence 90 for @{'nth}.
67 notation < "'nth' \nbsp term 90 l \nbsp term 90 d \nbsp term 90 i"
68 with precedence 69 for @{'nth_appl $l $d $i}.
70 notation < "\rationals \sup 2" non associative with precedence 90 for @{'q2}.
71 interpretation "Q x Q" 'q2 = (Prod Q Q).
73 definition make_list ≝
75 let rec make_list (n:nat) on n ≝
76 match n with [ O ⇒ nil ? | S m ⇒ def m :: make_list m]
79 interpretation "'mk_list' appl" 'mk_list_appl f n = (make_list _ f n).
80 interpretation "'mk_list'" 'mk_list = (make_list _).
81 notation "'mk_list'" with precedence 90 for @{'mk_list}.
82 notation < "'mk_list' \nbsp term 90 f \nbsp term 90 n"
83 with precedence 69 for @{'mk_list_appl $f $n}.
86 definition empty_bar : bar ≝ 〈one,OQ〉.
87 notation "\rect" with precedence 90 for @{'empty_bar}.
88 interpretation "q0" 'empty_bar = empty_bar.
90 notation < "\ldots\rect\square\EmptySmallSquare\ldots" with precedence 90 for @{'lq2}.
91 interpretation "lq2" 'lq2 = (list bar).
93 notation "'len'" with precedence 90 for @{'len}.
94 interpretation "len" 'len = (length _).
95 notation < "'len' \nbsp term 90 l" with precedence 69 for @{'len_appl $l}.
96 interpretation "len appl" 'len_appl l = (length _ l).
98 lemma len_mk_list : ∀T:Type.∀f:nat→T.∀n.len (mk_list f n) = n.
99 intros; elim n; [reflexivity] simplify; rewrite > H; reflexivity;
102 let rec sum_bases (l:list bar) (i:nat)on i ≝
107 [ nil ⇒ sum_bases l m + Qpos one
108 | cons x tl ⇒ sum_bases tl m + Qpos (\fst x)]].
110 axiom sum_bases_empty_nat_of_q_ge_OQ:
111 ∀q:ℚ.OQ ≤ sum_bases [] (nat_of_q q).
112 axiom sum_bases_empty_nat_of_q_le_q:
113 ∀q:ℚ.sum_bases [] (nat_of_q q) ≤ q.
114 axiom sum_bases_empty_nat_of_q_le_q_one:
115 ∀q:ℚ.q < sum_bases [] (nat_of_q q) + Qpos one.
118 λP.λp:∃x:nat × ℚ.P x.match p with [ex_introT p _ ⇒ p].
120 definition inject1 ≝ λP.λp:nat × ℚ.λh:P p. ex_introT ? P p h.
121 coercion inject1 with 0 1 nocomposites.
124 ∀f:q_f.∀i:ℚ.∃p:nat × ℚ.
125 match q_cmp i (start f) with
126 [ q_lt _ ⇒ \snd p = OQ
129 (sum_bases (bars f) (\fst p) ≤ ⅆ[i,start f])
130 (ⅆ[i, start f] < sum_bases (bars f) (S (\fst p)))
131 (\snd p = \snd (nth (bars f) ▭ (\fst p)))].
133 alias symbol "pi2" = "pair pi2".
134 alias symbol "pi1" = "pair pi1".
136 let rec value (p: ℚ) (l : list bar) on l ≝
138 [ nil ⇒ 〈nat_of_q p,OQ〉
140 match q_cmp p (Qpos (\fst x)) with
141 [ q_lt _ ⇒ 〈O, \snd x〉
143 let rc ≝ value (p - Qpos (\fst x)) tl in
144 〈S (\fst rc),\snd rc〉]]
146 ∀acc,l.∃p:nat × ℚ. OQ ≤ acc →
148 (sum_bases l (\fst p) ≤ acc)
149 (acc < sum_bases l (S (\fst p)))
150 (\snd p = \snd (nth l ▭ (\fst p))));
152 cases (q_cmp i (start f));
153 [2: exists [apply 〈O,OQ〉] simplify; reflexivity;
154 |*: cases (value ⅆ[i,start f] (bars f)) (p Hp);
155 cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value;
156 exists[1,3:apply p]; simplify; split; assumption;]
158 [1,4: clear H2; cases (value (q-Qpos (\fst b)) l1);
159 cases (H2 (q_le_to_diff_ge_OQ ?? (? H1)));
160 [1,3: intros; [right|left;symmetry] assumption]
161 simplify; apply q_le_minus; assumption;
162 |2,5: cases (value (q-Qpos (\fst b)) l1);
163 cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
164 [1,3: intros; [right|left;symmetry] assumption]
166 change with (q < sum_bases l1 (S (\fst w)) + Qpos (\fst b));
167 apply q_lt_plus; assumption;
168 |*: cases (value (q-Qpos (\fst b)) l1); simplify;
169 cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
170 [1,3: intros; [right|left;symmetry] assumption]
172 |2: clear value H2; simplify; intros; split; [assumption|3:reflexivity]
173 rewrite > q_plus_sym; rewrite > q_plus_OQ; assumption;
174 |4: simplify; intros; split;
175 [1: apply sum_bases_empty_nat_of_q_le_q;
176 |2: apply sum_bases_empty_nat_of_q_le_q_one;
177 |3: elim (nat_of_q q); [reflexivity] simplify; assumption]]
181 definition same_values ≝
183 ∀input.\snd (\fst (value l1 input)) = \snd (\fst (value l2 input)).
185 definition same_bases ≝
187 (∀i.\fst (nth (bars l1) ▭ i) = \fst (nth (bars l2) ▭ i)).
189 axiom q_lt_corefl: ∀x:Q.x < x → False.
190 axiom q_lt_antisym: ∀x,y:Q.x < y → y < x → False.
191 axiom q_neg_gt: ∀r:ratio.OQ < Qneg r → False.
192 axiom q_d_x_x: ∀x:Q.ⅆ[x,x] = OQ.
193 axiom q_pos_OQ: ∀x.Qpos x ≤ OQ → False.
194 axiom q_lt_plus_trans:
195 ∀x,y:Q.OQ ≤ x → OQ < y → OQ < x + y.
196 axiom q_pos_lt_OQ: ∀x.OQ < Qpos x.
197 axiom q_le_plus_trans:
198 ∀x,y:Q. OQ ≤ x → OQ ≤ y → OQ ≤ x + y.
199 axiom q_lt_trans: ∀x,y,z:Q. x < y → y < z → x < z.
200 axiom q_le_trans: ∀x,y,z:Q. x ≤ y → y ≤ z → x ≤ z.
201 axiom q_d_noabs: ∀x,y. x ≤ y → ⅆ[x,y] = y - x.
202 axiom q_d_sym: ∀x,y. ⅆ[x,y] = ⅆ[y,x].
203 axiom q_le_S: ∀x,y,z.OQ ≤ x → x + y ≤ z → y ≤ z.
204 axiom q_plus_minus: ∀x.Qpos x + Qneg x = OQ.
205 axiom q_minus: ∀x,y. y - Qpos x = y + Qneg x.
206 axiom q_minus_r: ∀x,y. y + Qpos x = y - Qneg x.
207 axiom q_plus_assoc: ∀x,y,z.x + (y + z) = x + y + z.
209 lemma unpos: ∀x:ℚ.OQ < x → ∃r:ratio.Qpos r = x.
210 intro; cases x; intros; [2:exists [apply r] reflexivity]
212 [ apply (q_lt_corefl ? H)|apply (q_neg_gt ? H)]
215 notation < "\blacksquare" non associative with precedence 90 for @{'hide}.
216 definition hide ≝ λT:Type.λx:T.x.
217 interpretation "hide" 'hide = (hide _ _).
219 lemma sum_bases_ge_OQ:
220 ∀l,n. OQ ≤ sum_bases (bars l) n.
221 intro; elim (bars l); simplify; intros;
222 [1: elim n; [left;reflexivity] simplify;
223 apply q_le_plus_trans; try assumption; apply q_lt_to_le; apply q_pos_lt_OQ;
224 |2: cases n; [left;reflexivity] simplify;
225 apply q_le_plus_trans; [apply H| apply q_lt_to_le; apply q_pos_lt_OQ;]]
229 ∀l:q_f.∀x.sum_bases (bars l) x ≤ OQ → x = O.
230 intros; cases x in H; [intros; reflexivity] intro; cases (?:False);
232 [1: apply (q_lt_corefl OQ); rewrite < H1 in ⊢ (?? %);
233 |2: apply (q_lt_antisym ??? H1);] clear H H1; cases (bars l);
234 simplify; apply q_lt_plus_trans;
235 try apply q_pos_lt_OQ;
236 try apply (sum_bases_ge_OQ (mk_q_f OQ []));
237 apply (sum_bases_ge_OQ (mk_q_f OQ l1));
240 lemma initial_shift_same_values:
241 ∀l1:q_f.∀init.init < start l1 →
243 (mk_q_f init (〈\fst (unpos (start l1 - init) ?),OQ〉:: bars l1)).
244 [apply hide; apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ; assumption]
245 intros; generalize in ⊢ (? ? (? ? (? ? (? ? ? (? ? ? (? ? %)) ?) ?))); intro;
246 cases (unpos (start l1-init) H1); intro input;
247 simplify in ⊢ (? ? ? (? ? ? (? ? ? (? (? ? (? ? (? ? ? % ?) ?)) ?))));
248 cases (value (mk_q_f init (〈w,OQ〉::bars l1)) input);
249 simplify in ⊢ (? ? ? (? ? ? %));
250 cases (q_cmp input (start (mk_q_f init (〈w,OQ〉::bars l1)))) in H3;
251 whd in ⊢ (% → ?); simplify in H3;
252 [1: intro; cases H4; clear H4; rewrite > H3;
253 cases (value l1 init); simplify; cases (q_cmp init (start l1)) in H4;
254 [1: cases (?:False); apply (q_lt_corefl init); rewrite > H4 in ⊢ (?? %); apply H;
255 |3: cases (?:False); apply (q_lt_antisym init (start l1)); assumption;
256 |2: whd in ⊢ (% → ?); intro; rewrite > H8; clear H8 H4;
257 rewrite > H7; clear H7; rewrite > (?:\fst w1 = O); [reflexivity]
258 symmetry; apply le_n_O_to_eq;
259 rewrite > (sum_bases_O (mk_q_f init (〈w,OQ〉::bars l1)) (\fst w1)); [apply le_n]
260 clear H6 w2; simplify in H5:(? ? (? ? %));
261 destruct H3; rewrite > q_d_x_x in H5; assumption;]
262 |2: intros; cases (value l1 input); simplify in ⊢ (? ? (? ? ? %) ?);
263 cases (q_cmp input (start l1)) in H5; whd in ⊢ (% → ?);
264 [1: cases (?:False); clear w2 H4 w1 H2 w H1;
265 apply (q_lt_antisym init (start l1)); [assumption] rewrite < H5; assumption
266 |2: intros; rewrite > H6; clear H6; rewrite > H4; reflexivity;
267 |3: cases (?:False); apply (q_lt_antisym input (start l1)); [2: assumption]
268 apply (q_lt_trans ??? H3 H);]
269 |3: intro; cases H4; clear H4;
270 cases (value l1 input); simplify; cases (q_cmp input (start l1)) in H4; whd in ⊢ (% → ?);
271 [1: intro; cases H8; clear H8; rewrite > H11; rewrite > H7; clear H11 H7;
272 simplify in ⊢ (? ? ? (? ? ? (? ? % ? ?)));
273 cut (\fst w1 = S (\fst w2)) as Key; [rewrite > Key; reflexivity;]
274 cut (\fst w2 = O); [2: clear H10;
275 symmetry; apply le_n_O_to_eq; rewrite > (sum_bases_O l1 (\fst w2)); [apply le_n]
276 apply (q_le_trans ??? H9); rewrite < H4; rewrite > q_d_x_x;
278 rewrite > Hcut; clear Hcut H10 H9; simplify in H5 H6;
279 cut (ⅆ[input,init] = Qpos w) as E; [2:
280 rewrite > H2; rewrite < H4; rewrite > q_d_sym;
281 rewrite > q_d_noabs; [reflexivity] right; assumption;]
282 cases (\fst w1) in H5 H6; intros;
283 [1: cases (?:False); clear H5; simplify in H6;
284 apply (q_lt_corefl ⅆ[input,init]);
285 rewrite > E in ⊢ (??%); rewrite < q_plus_OQ in ⊢ (??%);
286 rewrite > q_plus_sym; assumption;
287 |2: cases n in H5 H6; [intros; reflexivity] intros;
288 cases (?:False); clear H6; cases (bars l1) in H5; simplify; intros;
289 [apply (q_pos_OQ one);|apply (q_pos_OQ (\fst b));]
290 apply (q_le_S ??? (sum_bases_ge_OQ (mk_q_f init ?) n1));[apply [];|3:apply l]
291 simplify in ⊢ (? (? (? % ?) ?) ?); rewrite < (q_plus_minus w);
292 apply q_le_minus_r; rewrite < q_minus_r;
293 rewrite < E in ⊢ (??%); assumption;]
294 |2: intros; rewrite > H8; rewrite > H7; clear H8 H7;
295 simplify in H5 H6 ⊢ %;
296 simplify in H5:(? ? (? ? %));
300 alias symbol "pi2" = "pair pi2".
301 alias symbol "pi1" = "pair pi1".
302 definition rebase_spec ≝
303 ∀l1,l2:q_f.∃p:q_f × q_f.
305 (*len (bars (\fst p)) = len (bars (\snd p))*)
306 (start (\fst p) = start (\snd p))
307 (same_bases (\fst p) (\snd p))
308 (same_values l1 (\fst p))
309 (same_values l2 (\snd p)).
311 definition rebase_spec_simpl ≝
312 λstart.λl1,l2:list bar.λp:(list bar) × (list bar).
314 (same_bases (mk_q_f start (\fst p)) (mk_q_f start (\snd p)))
315 (same_values (mk_q_f start l1) (mk_q_f start (\fst p)))
316 (same_values (mk_q_f start l2) (mk_q_f start (\snd p))).
318 (* a local letin makes russell fail *)
319 definition cb0h : list bar → list bar ≝
320 λl.mk_list (λi.〈\fst (nth l ▭ i),OQ〉) (len l).
323 λP.λp:∃x:(list bar) × (list bar).P x.match p with [ex_introT p _ ⇒ p].
325 definition inject ≝ λP.λp:(list bar) × (list bar).λh:P p. ex_introT ? P p h.
326 coercion inject with 0 1 nocomposites.
328 definition rebase: rebase_spec.
329 intros 2 (f1 f2); cases f1 (s1 l1); cases f2 (s2 l2); clear f1 f2;
331 λs.λl1,l2.λm.λz.len l1 + len l2 < m → rebase_spec_simpl s l1 l2 z);
332 alias symbol "pi1" (instance 34) = "exT \fst".
333 alias symbol "pi1" (instance 21) = "exT \fst".
335 let rec aux (l1,l2:list bar) (n:nat) on n : (list bar) × (list bar) ≝
337 [ O ⇒ 〈 nil ? , nil ? 〉
340 [ nil ⇒ 〈cb0h l2, l2〉
343 [ nil ⇒ 〈l1, cb0h l1〉
345 let base1 ≝ Qpos (\fst he1) in
346 let base2 ≝ Qpos (\fst he2) in
347 let height1 ≝ (\snd he1) in
348 let height2 ≝ (\snd he2) in
349 match q_cmp base1 base2 with
351 let rc ≝ aux tl1 tl2 m in
352 〈he1 :: \fst rc,he2 :: \snd rc〉
354 let rest ≝ base2 - base1 in
355 let rc ≝ aux tl1 (〈\fst (unpos rest ?),height2〉 :: tl2) m in
356 〈〈\fst he1,height1〉 :: \fst rc,〈\fst he1,height2〉 :: \snd rc〉
358 let rest ≝ base1 - base2 in
359 let rc ≝ aux (〈\fst (unpos rest ?),height1〉 :: tl1) tl2 m in
360 〈〈\fst he2,height1〉 :: \fst rc,〈\fst he2,height2〉 :: \snd rc〉
362 in aux : ∀l1,l2,m.∃z.∀s.spec s l1 l2 m z); unfold spec;
363 [9: clearbody aux; unfold spec in aux; clear spec;
365 [1: cases (aux l1 l2 (S (len l1 + len l2)));
366 cases (H1 s1 (le_n ?)); clear H1;
367 exists [apply 〈mk_q_f s1 (\fst w), mk_q_f s2 (\snd w)〉] split;
369 |3: intro; apply (H3 input);
370 |4: intro; rewrite > H in H4;
371 rewrite > (H4 input); reflexivity;]
372 |2: letin l2' ≝ (〈\fst (unpos (s2-s1) ?),OQ〉::l2);[
373 apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ;
375 cases (aux l1 l2' (S (len l1 + len l2')));
376 cases (H1 s1 (le_n ?)); clear H1 aux;
377 exists [apply 〈mk_q_f s1 (\fst w), mk_q_f s1 (\snd w)〉] split;
381 |4: intro; rewrite < (H4 input); clear H3 H4 H2 w;
382 cases (value (mk_q_f s1 l2') input);
383 cases (q_cmp input (start (mk_q_f s1 l2'))) in H1;
385 [1: intros; cases H2; clear H2; whd in ⊢ (??? %);
386 cases (value (mk_q_f s2 l2) input);
387 cases (q_cmp input (start (mk_q_f s2 l2))) in H2;
389 [1: intros; cases H6; clear H6; change with (w1 = w);
392 |1,2: unfold rest; apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ;
399 |8: intros; cases (?:False); apply (not_le_Sn_O ? H1);]