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 (**************************************************************************)
15 include "russell_support.ma".
16 include "models/q_bars.ma".
18 (* move in nat/minus *)
19 lemma minus_lt : ∀i,j. i < j → j - i = S (j - S i).
21 apply (nat_elim2 ???? i j); simplify; intros;
22 [1: cases n in H; intros; rewrite < minus_n_O; [cases (not_le_Sn_O ? H);]
23 simplify; rewrite < minus_n_O; reflexivity;
24 |2: cases (not_le_Sn_O ? H);
25 |3: apply H; apply le_S_S_to_le; assumption;]
29 λl:list bar.make_list ? (λn.〈nth_base l (\len l - S n),〈OQ,OQ〉〉) (\len l).
32 ∀l:list bar.sorted q2_lt l → sorted q2_lt (copy l).
33 intros 2; unfold copy; generalize in match (le_n (\len l));
34 elim (\len l) in ⊢ (?%?→? ? (? ? ? %));
35 simplify; [apply (sorted_nil q2_lt);] cases n in H1 H2;
36 simplify; intros; [apply (sorted_one q2_lt);]
37 apply (sorted_cons q2_lt);
38 [2: apply H1; apply lt_to_le; apply H2;
39 |1: elim l in H2 H; simplify; [simplify in H2; cases (not_le_Sn_O ? H2)]
40 simplify in H3; unfold nth_base;
41 unfold canonical_q_lt; unfold q2_trel; unfold q2_lt; simplify;
42 change with (q2_lt (\nth (a::l1) ▭ (\len l1-S n1)) (\nth (a::l1) ▭ (\len l1-n1)));
43 cut (∃w.w = \len l1 - S n1); [2: exists[apply (\len l1 - S n1)] reflexivity]
44 cases Hcut; rewrite < H4; rewrite < (?:S w = \len l1 - n1);
45 [1: apply (sorted_near q2_lt (a::l1) H2); rewrite > H4;
46 simplify; apply le_S_S; elim (\len l1) in H3; simplify;
47 [ cases (not_le_Sn_O ? (le_S_S_to_le ?? H3));
48 | lapply le_S_S_to_le to H5 as H6;
49 lapply le_S_S_to_le to H6 as H7; clear H5 H6;
50 cases H7 in H3; intros; [rewrite < minus_n_n; apply le_S_S; apply le_O_n]
51 simplify in H5; apply le_S_S; apply (trans_le ???? (H5 ?));
52 [2: apply le_S_S; apply le_S_S; assumption;
53 |1: apply (lt_minus_S_n_to_le_minus_n n1 (S m) (S (minus m n1)) ?).
54 apply (not_le_to_lt (S (minus m n1)) (minus (S m) (S n1)) ?).
55 apply (not_le_Sn_n (minus (S m) (S n1))).]]
56 |2: rewrite > H4; lapply le_S_S_to_le to H3 as K;
57 clear H4 Hcut H3 H H1 H2; generalize in match K; clear K;
58 apply (nat_elim2 ???? n1 (\len l1)); simplify; intros;
59 [1: rewrite < minus_n_O; cases n2 in H; [intro; cases (not_le_Sn_O ? H)]
60 intros; cases n3; simplify; reflexivity;
61 |2: cases (not_le_Sn_O ? H);
62 |3: apply H; apply le_S_S_to_le; apply H1;]]]
65 lemma len_copy: ∀l. \len (copy l) = \len l.
66 intro; unfold copy; apply len_mk_list;
69 lemma same_bases_cons: ∀a,b,l1,l2.
70 same_bases l1 l2 → \fst a = \fst b → same_bases (a::l1) (b::l2).
71 intros; intro; cases i; simplify; [assumption;] apply (H n);
74 lemma copy_same_bases: ∀l. same_bases l (copy l).
75 intro; unfold copy; elim l using list_elim_with_len; [1: intro;reflexivity]
76 simplify; rewrite < minus_n_n;
77 simplify in ⊢ (? ? (? ? (? ? ? % ?) ?));
78 apply same_bases_cons; [2: reflexivity]
79 cases l1 in H; [intros 2; reflexivity]
80 simplify in ⊢ (? ? (? ? (λ_:?.? ? ? (? ? %) ?) ?)→?);
81 simplify in ⊢ (?→? ? (? ? (λ_:?.? ? ? (? ? (? % ?)) ?) ?));
82 intro; rewrite > (mk_list_ext ?? (λn:nat.〈nth_base (b::l2) (\len l2-n),〈OQ,OQ〉〉));[assumption]
83 intro; elim x; [simplify; rewrite < minus_n_O; reflexivity]
84 simplify in ⊢ (? ? (? ? ? (? ? %) ?) ?);
85 simplify in H2:(? ? %); rewrite > minus_lt; [reflexivity] apply le_S_S_to_le;
89 lemma prepend_sorted_with_same_head:
91 sorted r (x::l1) → sorted r l2 →
92 (r x (\nth l1 d1 O) → r x (\nth l2 d2 O)) → (l1 = [] → r x d1) →
94 intros 8 (R x l1 l2 d1 d2 Sl1 Sl2); inversion Sl1; inversion Sl2;
95 intros; destruct; try assumption; [3: apply (sorted_one R);]
96 [1: apply sorted_cons;[2:assumption] apply H2; apply H3; reflexivity;
97 |2: apply sorted_cons;[2: assumption] apply H5; apply H6; reflexivity;
98 |3: apply sorted_cons;[2: assumption] apply H5; assumption;
99 |4: apply sorted_cons;[2: assumption] apply H8; apply H4;]
102 lemma move_head_sorted: ∀x,l1,l2.
103 sorted q2_lt (x::l1) → sorted q2_lt l2 → nth_base l2 O = nth_base l1 O →
104 l1 ≠ [] → sorted q2_lt (x::l2).
105 intros; apply (prepend_sorted_with_same_head q2_lt x l1 l2 ▭ ▭);
106 try assumption; intros; unfold nth_base in H2; whd in H4;
107 [1: rewrite < H2 in H4; assumption;
111 definition rebase_spec ≝
112 λl1,l2:q_f.λp:q_f × q_f.
114 (same_bases (bars (\fst p)) (bars (\snd p)))
115 (same_values l1 (\fst p))
116 (same_values l2 (\snd p)).
119 definition same_values_simpl ≝
120 λl1,l2.∀p1,p2,p3,p4,p5,p6.same_values (mk_q_f l1 p1 p2 p3) (mk_q_f l2 p4 p5 p6).
122 alias symbol "pi2" = "pair pi2".
123 alias symbol "pi1" = "pair pi1".
124 definition rebase_spec_aux ≝
125 λl1,l2:list bar.λp:(list bar) × (list bar).
126 sorted q2_lt l1 → sorted q2_lt l2 →
127 (l1 ≠ [] → \snd (\nth l1 ▭ (pred (\len l1))) = 〈OQ,OQ〉) →
128 (l2 ≠ [] → \snd (\nth l2 ▭ (pred (\len l2))) = 〈OQ,OQ〉) →
130 (nth_base l1 O = nth_base (\fst p) O ∨
131 nth_base l2 O = nth_base (\fst p) O)
132 (sorted q2_lt (\fst p) ∧ sorted q2_lt (\snd p))
133 ((l1 ≠ [] → \snd (\nth (\fst p) ▭ (pred (\len (\fst p)))) = 〈OQ,OQ〉) ∧
134 (l2 ≠ [] → \snd (\nth (\snd p) ▭ (pred (\len (\snd p)))) = 〈OQ,OQ〉))
136 (same_bases (\fst p) (\snd p))
137 (same_values_simpl l1 (\fst p))
138 (same_values_simpl l2 (\snd p))).
141 ∀l1.rebase_spec_aux l1 [] 〈l1, copy l1〉.
142 intros; elim l1; intros 4;
143 [1: split; [left; reflexivity]; split; try assumption; unfold; intros;
144 unfold same_values; intros; reflexivity;
145 |2: rewrite > H3; [2: intro X; destruct X]
146 split; [left; reflexivity] split;
147 unfold same_values_simpl; unfold same_values; intros; try reflexivity;
148 try assumption; [4: normalize in p2; destruct p2|2: cases H5; reflexivity;]
149 [1: apply (sorted_copy ? H1);
150 |2: apply (copy_same_bases (a::l));]]
153 lemma copy_rebases_r:
154 ∀l1.rebase_spec_aux [] l1 〈copy l1, l1〉.
155 intros; elim l1; intros 4;
156 [1: split; [left; reflexivity]; split; try assumption; unfold; intros;
157 unfold same_values; intros; reflexivity;
158 |2: rewrite > H4; [2: intro X; destruct X]
159 split; [right; simplify; rewrite < minus_n_n; reflexivity] split;
160 unfold same_values_simpl; unfold same_values; intros; try reflexivity;
161 try assumption; [4: normalize in p2; destruct p2|2: cases H5; reflexivity;]
162 [1: apply (sorted_copy ? H2);
163 |2: intro; symmetry; apply (copy_same_bases (a::l));]]
167 λP.λp:∃x:(list bar) × (list bar).P x.match p with [ex_introT p _ ⇒ p].
169 definition inject ≝ λP.λp:(list bar) × (list bar).λh:P p. ex_introT ? P p h.
170 coercion inject with 0 1 nocomposites.
172 definition rebase: ∀l1,l2:q_f.∃p:q_f × q_f.rebase_spec l1 l2 p.
173 intros 2 (f1 f2); cases f1 (b1 Hs1 Hb1 He1); cases f2 (b2 Hs2 Hb2 He2); clear f1 f2;
174 alias symbol "leq" = "natural 'less or equal to'".
175 alias symbol "minus" = "Q minus".
177 let rec aux (l1,l2:list bar) (n : nat) on n : (list bar) × (list bar) ≝
182 [ nil ⇒ 〈copy l2, l2〉
185 [ nil ⇒ 〈l1, copy l1〉
187 let base1 ≝ \fst he1 in
188 let base2 ≝ \fst he2 in
189 let height1 ≝ \snd he1 in
190 let height2 ≝ \snd he2 in
191 match q_cmp base1 base2 with
193 match q_cmp base2 base1 with
195 let rc ≝ aux tl1 tl2 m in
196 〈he1 :: \fst rc,he2 :: \snd rc〉
198 let rest ≝ base2 - base1 in
199 let rc ≝ aux tl1 (〈rest,height2〉 :: tl2) m in
200 〈〈base1,height1〉 :: \fst rc,〈base1,height2〉 :: \snd rc〉]
202 let rest ≝ base1 - base2 in
203 let rc ≝ aux (〈rest,height1〉 :: tl1) tl2 m in
204 〈〈base2,height1〉 :: \fst rc,〈base2,height2〉 :: \snd rc〉]]]]
205 in aux : ∀l1,l2,m.∃z.\len l1 + \len l2 ≤ m → rebase_spec_aux l1 l2 z);
206 [7: clearbody aux; cases (aux b1 b2 (\len b1 + \len b2)) (w Hw); clear aux;
207 cases (Hw (le_n ?) Hs1 Hs2 (λ_.He1) (λ_.He2)); clear Hw; cases H1; cases H2; cases H3; clear H3 H1 H2;
208 exists [constructor 1;constructor 1;[apply (\fst w)|5:apply (\snd w)]] try assumption;
209 [1,3: apply hide; cases H (X X); try rewrite < (H8 O); try rewrite < X; assumption
210 |2,4: apply hide;[apply H6|apply H7]intro X;[rewrite > X in Hb1|rewrite > X in Hb2]
211 normalize in Hb1 Hb2; [destruct Hb1|destruct Hb2]]
212 unfold; unfold same_values; simplify in ⊢ (? (? % %) ? ?);
213 simplify in match (\snd 〈?,?〉); simplify in match (\fst 〈?,?〉);
214 split; [assumption; |apply H9;|apply H10]
215 |6: intro ABS; unfold; intros 4; clear H1 H2;
216 cases l in ABS H3; intros 1; [2: simplify in H1; cases (not_le_Sn_O ? H1)]
217 cases l1 in H4 H1; intros; [2: simplify in H2; cases (not_le_Sn_O ? H2)]
218 split; [ left; reflexivity|split; apply (sorted_nil q2_lt);|split; assumption;]
219 split; unfold; intros; unfold same_values; intros; reflexivity;
220 |5: intros; apply copy_rebases_r;
221 |4: intros; rewrite < H1; apply copy_rebases;
222 |3: cut (\fst b = \fst b3) as K; [2: apply q_le_to_le_to_eq; assumption] clear H6 H5 H4 H3;
223 intros; cases (aux l2 l3 n1); cases w in H4 (w1 w2); clear w;
225 simplify in match (\fst 〈?,?〉); simplify in match (\snd 〈?,?〉);
227 [2: apply le_S_S_to_le; apply (trans_le ???? H3); simplify;
228 rewrite < plus_n_Sm; apply le_S; apply le_n;
229 |3,4: apply (sorted_tail q2_lt); [2: apply H4|4:apply H6]
230 |5: intro; cases l2 in H7 H9; intros; [cases H9; reflexivity]
231 simplify in H7 ⊢ %; apply H7; intro; destruct H10;
232 |6: intro; cases l3 in H8 H9; intros; [cases H9; reflexivity]
233 simplify in H8 ⊢ %; apply H8; intro; destruct H10;]
235 simplify in match (\fst 〈?,?〉) in H9 H10 H11 H12;
236 simplify in match (\snd 〈?,?〉) in H9 H10 H11 H12;
238 [1: left; reflexivity;
239 |2: cases H10; cases H12; clear H15 H16 H12 H7 H8 H11 H10;
241 [1: lapply (H14 O) as K1; clear H14; change in K1 with (nth_base w1 O = nth_base w2 O);
243 [1: apply (move_head_sorted ??? H4 H5 H7); STOP
247 unfold rebase_spec_aux; intros; cases l1 in H2 H4 H6; intros; [ simplify in H2; destruct H2;]
248 lapply H6 as H7; [2: intro X; destruct X] clear H6 H5;
249 rewrite > H7; split; [right; simplify;
251 split; [left;reflexivity]
254 ,2: unfold rest; apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ;
256 |8: intros; cases (?:False); apply (not_le_Sn_O ? H1);
257 |3: intros; generalize in match (unpos ??); intro X; cases X; clear X;
258 simplify in ⊢ (???? (??? (??? (??? (?? (? (?? (??? % ?) ?) ??)))) ?));
259 simplify in ⊢ (???? (???? (??? (??? (?? (? (?? (??? % ?) ?) ??))))));
260 clear H4; cases (aux (〈w,\snd b〉::l4) l5 n1); clear aux;
261 cut (len (〈w,\snd b〉::l4) + len l5 < n1) as K;[2:
262 simplify in H5; simplify; rewrite > sym_plus in H5; simplify in H5;
263 rewrite > sym_plus in H5; apply le_S_S_to_le; apply H5;]
265 [1: simplify in ⊢ (? % ?); simplify in ⊢ (? ? %);
266 cases (H4 s K); clear K H4; intro input; cases input; [reflexivity]
268 |2: simplify in ⊢ (? ? %); cases (H4 s K); clear H4 K H5 spec;
270 (* input < s + b1 || input >= s + b1 *)
271 |3: simplify in ⊢ (? ? %);]
272 |4: intros; generalize in match (unpos ??); intro X; cases X; clear X;
274 |5: intros; (* triviale, caso in cui non fa nulla *)
275 |6,7: (* casi base in cui allunga la lista più corta *)
280 include "Q/q/qtimes.ma".
282 let rec area (l:list bar) on l ≝
285 | cons he tl ⇒ area tl + Qpos (\fst he) * ⅆ[OQ,\snd he]].
287 alias symbol "pi1" = "exT \fst".
288 alias symbol "minus" = "Q minus".
289 alias symbol "exists" = "CProp exists".
290 definition minus_spec_bar ≝
292 same_bases f g → len f = len g →
293 ∀s,i:ℚ. \snd (\fst (value (mk_q_f s h) i)) =
294 \snd (\fst (value (mk_q_f s f) i)) - \snd (\fst (value (mk_q_f s g) i)).
296 definition minus_spec ≝
299 ∀i:ℚ. \snd (\fst (value h i)) =
300 \snd (\fst (value f i)) - \snd (\fst (value g i)).
302 definition eject_bar : ∀P:list bar → CProp.(∃l:list bar.P l) → list bar ≝
303 λP.λp.match p with [ex_introT x _ ⇒ x].
304 definition inject_bar ≝ ex_introT (list bar).
306 coercion inject_bar with 0 1 nocomposites.
307 coercion eject_bar with 0 0 nocomposites.
309 lemma minus_q_f : ∀f,g. minus_spec f g.
312 let rec aux (l1, l2 : list bar) on l1 ≝
318 | cons he2 tl2 ⇒ 〈\fst he1, \snd he1 - \snd he2〉 :: aux tl1 tl2]]
319 in aux : ∀l1,l2 : list bar.∃h.minus_spec_bar l1 l2 h);
320 [2: intros 4; simplify in H3; destruct H3;
321 |3: intros 4; simplify in H3; cases l1 in H2; [2: intro X; simplify in X; destruct X]
322 intros; rewrite > (value_OQ_e (mk_q_f s []) i); [2: reflexivity]
323 rewrite > q_elim_minus; rewrite > q_plus_OQ; reflexivity;
324 |1: cases (aux l2 l3); unfold in H2; intros 4;
325 simplify in ⊢ (? ? (? ? ? (? ? ? (? % ?))) ?);
326 cases (q_cmp i (s + Qpos (\fst b)));
331 λf,g.∃i.\snd (\fst (value f i)) < \snd (\fst (value g i)).