]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/dama/dama/models/q_function.ma
47dccaf5d486402bc91eab72c6b528d82affbbac
[helm.git] / helm / software / matita / contribs / dama / dama / models / q_function.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "russell_support.ma".
16 include "models/q_bars.ma".
17
18 (* move in nat/minus *)
19 lemma minus_lt : ∀i,j. i < j → j - i = S (j - S i). 
20 intros 2;
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;]
26 qed.
27
28 definition copy ≝
29  λl:list bar.make_list ? (λn.〈nth_base l (\len l - S n),〈OQ,OQ〉〉) (\len l).
30
31 lemma sorted_copy:
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;]]]
63 qed.
64
65 lemma len_copy: ∀l. \len (copy l) = \len l. 
66 intro; unfold copy; apply len_mk_list;
67 qed.
68
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);
72 qed.
73
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;
86 assumption;
87 qed.
88
89 lemma prepend_sorted_with_same_head:
90  ∀r,x,l1,l2,d1,d2.
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) →
93    sorted r (x::l2).
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;]
100 qed.
101
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;
108 |2: cases (H3 H4);]
109 qed.
110        
111 definition rebase_spec ≝ 
112  λl1,l2:q_f.λp:q_f × q_f. 
113    And3
114     (same_bases (bars (\fst p)) (bars (\snd p)))
115     (same_values l1 (\fst p)) 
116     (same_values l2 (\snd p)).
117     
118
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).
121
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〉) →
129  And4
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〉))  
135    (And3
136       (same_bases (\fst p) (\snd p))
137       (same_values_simpl l1 (\fst p)) 
138       (same_values_simpl l2 (\snd p))).   
139
140 lemma copy_rebases: 
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));]]
151 qed.
152
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));]]
164 qed.
165
166 definition eject ≝
167   λP.λp:∃x:(list bar) × (list bar).P x.match p with [ex_introT p _ ⇒ p].
168 coercion eject.
169 definition inject ≝ λP.λp:(list bar) × (list bar).λh:P p. ex_introT ? P p h.
170 coercion inject with 0 1 nocomposites.
171
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".
176 letin aux ≝ ( 
177 let rec aux (l1,l2:list bar) (n : nat) on n : (list bar) × (list bar) ≝
178 match n with
179 [ O ⇒ 〈[], []〉
180 | S m ⇒
181   match l1 with
182   [ nil ⇒ 〈copy l2, l2〉
183   | cons he1 tl1 ⇒
184      match l2 with
185      [ nil ⇒ 〈l1, copy l1〉
186      | cons he2 tl2 ⇒  
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
192          [ q_leq Hp1 ⇒ 
193              match q_cmp base2 base1 with
194              [ q_leq Hp2 ⇒
195                  let rc ≝ aux tl1 tl2 m in 
196                  〈he1 :: \fst rc,he2 :: \snd rc〉
197              | q_gt Hp ⇒ 
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〉] 
201          | q_gt Hp ⇒ 
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; 
224     intros 5; 
225     simplify in match (\fst 〈?,?〉); simplify in match (\snd 〈?,?〉);
226     cases H5; 
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;]
234     clear aux H5; 
235     simplify in match (\fst 〈?,?〉) in H9 H10 H11 H12; 
236     simplify in match (\snd 〈?,?〉) in H9 H10 H11 H12;
237     split; 
238     [1: left; reflexivity;
239     |2: cases H10; cases H12; clear H15 H16 H12 H7 H8 H11 H10;
240         cases H9; clear H9;
241         [1: lapply (H14 O) as K1; clear H14; change in K1 with (nth_base w1 O = nth_base w2 O);
242             split; 
243             [1: apply (move_head_sorted ??? H4 H5 H7); STOP
244
245     
246      
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;
250     
251      split; [left;reflexivity]
252     split; 
253
254 ,2: unfold rest; apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ;
255       assumption;        
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;] 
264     split;
265     [1: simplify in ⊢ (? % ?); simplify in ⊢ (? ? %); 
266         cases (H4 s K); clear K H4; intro input; cases input; [reflexivity]
267         simplify; apply H7; 
268     |2: simplify in ⊢ (? ? %); cases (H4 s K); clear H4 K H5 spec;
269         intro;
270         (* input < s + b1 || input >= s + b1 *)
271     |3: simplify in ⊢ (? ? %);]   
272 |4: intros; generalize in match (unpos ??); intro X; cases X; clear X;
273     (* duale del 3 *)
274 |5: intros; (* triviale, caso in cui non fa nulla *)
275 |6,7: (* casi base in cui allunga la lista più corta *) 
276 ]
277 elim devil;
278 qed.
279
280 include "Q/q/qtimes.ma".
281
282 let rec area (l:list bar) on l ≝
283   match l with 
284   [ nil ⇒ OQ
285   | cons he tl ⇒ area tl + Qpos (\fst he) * ⅆ[OQ,\snd he]].
286
287 alias symbol "pi1" = "exT \fst".
288 alias symbol "minus" = "Q minus".
289 alias symbol "exists" = "CProp exists".
290 definition minus_spec_bar ≝
291  λf,g,h:list 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)). 
295
296 definition minus_spec ≝
297  λf,g:q_f.
298    ∃h:q_f. 
299      ∀i:ℚ. \snd (\fst (value h i)) = 
300        \snd (\fst (value f i)) - \snd (\fst (value g i)). 
301
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).
305
306 coercion inject_bar with 0 1 nocomposites.
307 coercion eject_bar with 0 0 nocomposites.
308
309 lemma minus_q_f : ∀f,g. minus_spec f g.
310 intros;
311 letin aux ≝ (
312   let rec aux (l1, l2 : list bar) on l1 ≝
313     match l1 with
314     [ nil ⇒ []
315     | cons he1 tl1 ⇒
316         match l2 with
317         [ nil ⇒ []
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)));
327     
328
329
330 definition excess ≝ 
331   λf,g.∃i.\snd (\fst (value f i)) < \snd (\fst (value g i)).
332