]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/dama/dama/models/q_function.ma
...
[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 definition rebase_spec ≝ 
19  λl1,l2:q_f.λp:q_f × q_f. 
20    And3
21     (same_bases (bars (\fst p)) (bars (\snd p)))
22     (same_values l1 (\fst p)) 
23     (same_values l2 (\snd p)).
24
25 definition same_values_simpl ≝
26  λl1,l2.∀p1,p2,p3,p4,p5,p6.same_values (mk_q_f l1 p1 p2 p3) (mk_q_f l2 p4 p5 p6).
27
28 alias symbol "pi2" = "pair pi2".
29 alias symbol "pi1" = "pair pi1".
30 definition rebase_spec_aux ≝ 
31  λl1,l2:list bar.λp:(list bar) × (list bar).
32    sorted q2_lt l1 → sorted q2_lt l2 →
33    (l1 ≠ [] → \snd (\nth l1 ▭ (pred (\len l1))) = 〈OQ,OQ〉) →
34    (l2 ≠ [] → \snd (\nth l2 ▭ (pred (\len l2))) = 〈OQ,OQ〉) →
35  And4
36    (nth_base l1 O = nth_base (\fst p) O ∨
37     nth_base l2 O = nth_base (\fst p) O) 
38    (sorted q2_lt (\fst p) ∧ sorted q2_lt (\snd p))
39    ((l1 ≠ [] → \snd (\nth (\fst p) ▭ (pred (\len (\fst p)))) = 〈OQ,OQ〉) ∧ 
40     (l2 ≠ [] → \snd (\nth (\snd p) ▭ (pred (\len (\snd p)))) = 〈OQ,OQ〉))  
41    (And3
42       (same_bases (\fst p) (\snd p))
43       (same_values_simpl l1 (\fst p)) 
44       (same_values_simpl l2 (\snd p))).   
45
46 definition eject ≝
47   λP.λp:∃x:(list bar) × (list bar).P x.match p with [ex_introT p _ ⇒ p].
48 coercion eject.
49 definition inject ≝ λP.λp:(list bar) × (list bar).λh:P p. ex_introT ? P p h.
50 coercion inject with 0 1 nocomposites.
51
52 axiom devil : False.
53         
54 definition copy ≝
55  λl:list bar.make_list ? (λn.〈nth_base l (\len l - S n),〈OQ,OQ〉〉) (\len l).
56
57 lemma list_elim_with_len:
58   ∀T:Type.∀P: nat → list T → CProp.
59     P O [] → (∀l,a,n.P (\len l) l → P (S n) (a::l)) →
60      ∀l.P (\len l) l.
61 intros;elim l; [assumption] simplify; apply H1; apply H2;
62 qed.
63  
64 lemma sorted_near:
65  ∀r,l. sorted r l → ∀i,d. S i < \len l → r (\nth l d i) (\nth l d (S i)).
66  intros 3; elim H; 
67  [1: cases (not_le_Sn_O ? H1);
68  |2: simplify in H1; cases (not_le_Sn_O ? (le_S_S_to_le ?? H1));
69  |3: simplify; cases i in H4; intros; [apply H1]
70      apply H3; apply le_S_S_to_le; apply H4]
71  qed.  
72  
73 lemma sorted_copy:
74   ∀l:list bar.sorted q2_lt l → sorted q2_lt (copy l).
75 intros 2; unfold copy; generalize in match (le_n (\len l)); 
76 elim (\len l) in ⊢ (?%?→? ? (? ? ? %));
77 simplify; [apply (sorted_nil q2_lt);] cases n in H1 H2;
78 simplify; intros; [apply (sorted_one q2_lt);]
79 apply (sorted_cons q2_lt);
80 [2: apply H1; apply lt_to_le; apply H2;
81 |1: elim l in H2 H; simplify; [simplify in H2; cases (not_le_Sn_O ? H2)]    
82     simplify in H3; unfold nth_base;
83     unfold canonical_q_lt; unfold q2_trel; unfold q2_lt; simplify;
84     change with (q2_lt (\nth (a::l1) ▭ (\len l1-S n1)) (\nth (a::l1) ▭ (\len l1-n1)));
85     cut (∃w.w = \len l1 - S n1); [2: exists[apply (\len l1 - S n1)] reflexivity]
86     cases Hcut; rewrite < H4; rewrite < (?:S w = \len l1 - n1);
87     [1: apply (sorted_near q2_lt (a::l1) H2); rewrite > H4;
88         simplify; apply le_S_S; elim (\len l1) in H3; simplify;
89         [ cases (not_le_Sn_O ? (le_S_S_to_le ?? H3));
90         | lapply le_S_S_to_le to H5 as H6;
91           lapply le_S_S_to_le to H6 as H7; clear H5 H6;
92           cases H7 in H3; intros; [rewrite < minus_n_n; apply le_S_S; apply le_O_n]
93           simplify in H5; apply le_S_S; apply (trans_le ???? (H5 ?));
94           [2: apply le_S_S; apply le_S_S; assumption;
95           |1: apply (lt_minus_S_n_to_le_minus_n n1 (S m) (S (minus m n1)) ?).
96               apply (not_le_to_lt (S (minus m n1)) (minus (S m) (S n1)) ?).
97               apply (not_le_Sn_n (minus (S m) (S n1))).]]
98     |2: rewrite > H4; lapply le_S_S_to_le to H3 as K;
99         clear H4 Hcut H3 H H1 H2; generalize in match K; clear K;
100         apply (nat_elim2 ???? n1 (\len l1)); simplify; intros;
101         [1: rewrite < minus_n_O; cases n2 in H; [intro; cases (not_le_Sn_O ? H)]
102             intros; cases n3; simplify; reflexivity;
103         |2: cases (not_le_Sn_O ? H);
104         |3: apply H; apply le_S_S_to_le; apply H1;]]]
105 qed.
106
107 lemma make_list_ext: ∀T,f1,f2,n. (∀x.x<n → f1 x = f2 x) → make_list T f1 n = make_list T f2 n.
108 intros 4;elim n; [reflexivity] simplify; rewrite > H1; [2: apply le_n]
109 apply eq_f; apply H; intros; apply H1; apply (trans_le ??? H2); apply le_S; apply le_n;
110 qed.
111     
112 lemma len_copy: ∀l. \len l = \len (copy l).
113 intro; elim l; [reflexivity] simplify; rewrite > H; clear H;
114 apply eq_f; elim (\len (copy l1)) in ⊢ (??%(??(???%))); [reflexivity] simplify;
115 rewrite > H in ⊢ (??%?); reflexivity;
116 qed. 
117
118 lemma same_bases_cons: ∀a,b,l1,l2.
119   same_bases l1 l2 → \fst a = \fst b → same_bases (a::l1) (b::l2).
120 intros; intro; cases i; simplify; [assumption;] apply (H n);
121 qed.
122
123 lemma minus_lt : ∀i,j. i < j → j - i = S (j - S i). 
124 intros 2;
125 apply (nat_elim2 ???? i j); simplify; intros;
126 [1: cases n in H; intros; rewrite < minus_n_O; [cases (not_le_Sn_O ? H);]
127     simplify; rewrite < minus_n_O; reflexivity;
128 |2: cases (not_le_Sn_O ? H);
129 |3: apply H; apply le_S_S_to_le; assumption;]
130 qed.
131     
132 lemma copy_same_bases: ∀l. same_bases l (copy l).
133 intro; unfold copy; elim l using list_elim_with_len; [1: intro;reflexivity]
134 simplify; rewrite < minus_n_n;
135 simplify in ⊢ (? ? (? ? (? ? ? % ?) ?));
136 apply same_bases_cons; [2: reflexivity]
137 cases l1 in H; [intros 2; reflexivity]
138 simplify in ⊢ (? ? (? ? (λ_:?.? ? ? (? ? %) ?) ?)→?);
139 simplify in ⊢ (?→? ? (? ? (λ_:?.? ? ? (? ? (? % ?)) ?) ?));
140 intro; rewrite > (make_list_ext ?? (λn:nat.〈nth_base (b::l2) (\len l2-n),〈OQ,OQ〉〉));[assumption]
141 intro; elim x; [simplify; rewrite < minus_n_O; reflexivity]
142 simplify in ⊢ (? ? (? ? ? (? ? %) ?) ?);
143 simplify in H2:(? ? %); rewrite > minus_lt; [reflexivity] apply le_S_S_to_le;
144 assumption;
145 qed.
146
147 lemma copy_rebases: 
148   ∀l1.rebase_spec_aux l1 [] 〈l1, copy l1〉.
149 intros; elim l1; intros 4;
150 [1: split; [left; reflexivity]; split; try assumption; unfold; intros;
151     unfold same_values; intros; reflexivity;
152 |2: rewrite > H3; [2: intro X; destruct X]
153     split; [left; reflexivity] split; 
154     unfold same_values_simpl; unfold same_values; intros; try reflexivity;
155     try assumption; [4: normalize in p2; destruct p2|2: cases H5; reflexivity;]
156     [1: apply (sorted_copy ? H1);
157     |2: apply (copy_same_bases (a::l));]]
158 qed.
159                
160 lemma copy_rebases_r: 
161   ∀l1.rebase_spec_aux [] l1 〈copy l1, l1〉.
162 intros; elim l1; intros 4;
163 [1: split; [left; reflexivity]; split; try assumption; unfold; intros;
164     unfold same_values; intros; reflexivity;
165 |2: rewrite > H4; [2: intro X; destruct X]
166     split; [right; simplify; rewrite < minus_n_n; reflexivity] split; 
167     unfold same_values_simpl; unfold same_values; intros; try reflexivity;
168     try assumption; [4: normalize in p2; destruct p2|2: cases H5; reflexivity;]
169     [1: apply (sorted_copy ? H2);
170     |2: intro; symmetry; apply (copy_same_bases (a::l));]]
171 qed.
172                
173 definition rebase: ∀l1,l2:q_f.∃p:q_f × q_f.rebase_spec l1 l2 p.
174 intros 2 (f1 f2); cases f1 (b1 Hs1 Hb1 He1); cases f2 (b2 Hs2 Hb2 He2); clear f1 f2;
175 alias symbol "plus" = "natural plus".
176 alias symbol "pi2" = "pair pi2".
177 alias symbol "pi1" = "pair pi1".
178 alias symbol "minus" = "Q minus".
179 letin aux ≝ ( 
180 let rec aux (l1,l2:list bar) (n : nat) on n : (list bar) × (list bar) ≝
181 match n with
182 [ O ⇒ 〈[], []〉
183 | S m ⇒
184   match l1 with
185   [ nil ⇒ 〈copy l2, l2〉
186   | cons he1 tl1 ⇒
187      match l2 with
188      [ nil ⇒ 〈l1, copy l1〉
189      | cons he2 tl2 ⇒  
190          let base1 ≝ \fst he1 in
191          let base2 ≝ \fst he2 in
192          let height1 ≝ \snd he1 in
193          let height2 ≝ \snd he2 in
194          match q_cmp base1 base2 with
195          [ q_leq Hp1 ⇒ 
196              match q_cmp base2 base1 with
197              [ q_leq Hp2 ⇒
198                  let rc ≝ aux tl1 tl2 m in 
199                  〈he1 :: \fst rc,he2 :: \snd rc〉
200              | q_gt Hp ⇒ 
201                  let rest ≝ base2 - base1 in
202                  let rc ≝ aux tl1 (〈rest,height2〉 :: tl2) m in
203                  〈〈base1,height1〉 :: \fst rc,〈base1,height2〉 :: \snd rc〉] 
204          | q_gt Hp ⇒ 
205              let rest ≝ base1 - base2 in
206              let rc ≝ aux (〈rest,height1〉 :: tl1) tl2 m in
207              〈〈base2,height1〉 :: \fst rc,〈base2,height2〉 :: \snd rc〉]]]]
208 in aux : ∀l1,l2,m.∃z.\len l1 + \len l2 ≤ m → rebase_spec_aux l1 l2 z);
209 [7: clearbody aux; cases (aux b1 b2 (\len b1 + \len b2)) (w Hw); clear aux;
210     cases (Hw (le_n ?) Hs1 Hs2 (λ_.He1) (λ_.He2)); clear Hw; cases H1; cases H2; cases H3; clear H3 H1 H2;
211     exists [constructor 1;constructor 1;[apply (\fst w)|5:apply (\snd w)]] try assumption;
212     [1,3: apply hide; cases H (X X); try rewrite < (H8 O); try rewrite < X; assumption
213     |2,4: apply hide;[apply H6|apply H7]intro X;[rewrite > X in Hb1|rewrite > X in Hb2]
214          normalize in Hb1 Hb2; [destruct Hb1|destruct Hb2]]
215     unfold; unfold same_values; simplify in ⊢ (? (? % %) ? ?); 
216     simplify in match (\snd 〈?,?〉); simplify in match (\fst 〈?,?〉);
217     split; [assumption; |apply H9;|apply H10]
218 |6: intro ABS; unfold; intros 4; clear H1 H2;
219     cases l in ABS H3; intros 1; [2: simplify in H1; cases (not_le_Sn_O ? H1)]
220     cases l1 in H4 H1; intros; [2: simplify in H2; cases (not_le_Sn_O ? H2)]
221     split; [ left; reflexivity|split; apply (sorted_nil q2_lt);|split; assumption;]
222     split; unfold; intros; unfold same_values; intros; reflexivity;
223 |5: intros; apply copy_rebases_r;
224 |4: intros; rewrite < H1; apply copy_rebases;
225 |3: cut (\fst b = \fst b3) as K; [2: apply q_le_to_le_to_eq; assumption] clear H6 H5 H4 H3;
226     intros; cases (aux l2 l3 n1); intros 4; simplify in match (\fst ≪w,H≫);
227     simplify in match (\fst 〈?,?〉); simplify in match (\snd 〈?,?〉);
228     cases H4; 
229       [2: apply le_S_S_to_le; apply (trans_le ???? H3); simplify;
230           rewrite < plus_n_Sm; apply le_S; apply le_n;
231       |3,4: apply (sorted_tail q2_lt); [2: apply H5|4:apply H6]
232       |5: intro; cases l2 in H7 H9; intros; [cases H9; reflexivity]
233           simplify in H7 ⊢ %; apply H7; intro; destruct H10;
234       |6: intro; cases l3 in H8 H9; intros; [cases H9; reflexivity]
235           simplify in H8 ⊢ %; apply H8; intro; destruct H10;]
236     clear aux; split; 
237     [1: left; reflexivity;
238     |2: cases H10;  
239
240     
241      
242  unfold rebase_spec_aux; intros; cases l1 in H2 H4 H6; intros; [ simplify in H2; destruct H2;]
243     lapply H6 as H7; [2: intro X; destruct X] clear H6 H5;
244     rewrite > H7; split; [right; simplify;
245     
246      split; [left;reflexivity]
247     split; 
248
249 ,2: unfold rest; apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ;
250       assumption;        
251 |8: intros; cases (?:False); apply (not_le_Sn_O ? H1);
252 |3: intros; generalize in match (unpos ??); intro X; cases X; clear X;
253     simplify in ⊢ (???? (??? (??? (??? (?? (? (?? (??? % ?) ?) ??)))) ?));
254     simplify in ⊢ (???? (???? (??? (??? (?? (? (?? (??? % ?) ?) ??)))))); 
255     clear H4; cases (aux (〈w,\snd b〉::l4) l5 n1); clear aux;
256     cut (len (〈w,\snd b〉::l4) + len l5 < n1) as K;[2:
257       simplify in H5; simplify; rewrite > sym_plus in H5; simplify in H5;
258       rewrite > sym_plus in H5; apply le_S_S_to_le; apply H5;] 
259     split;
260     [1: simplify in ⊢ (? % ?); simplify in ⊢ (? ? %); 
261         cases (H4 s K); clear K H4; intro input; cases input; [reflexivity]
262         simplify; apply H7; 
263     |2: simplify in ⊢ (? ? %); cases (H4 s K); clear H4 K H5 spec;
264         intro;
265         (* input < s + b1 || input >= s + b1 *)
266     |3: simplify in ⊢ (? ? %);]   
267 |4: intros; generalize in match (unpos ??); intro X; cases X; clear X;
268     (* duale del 3 *)
269 |5: intros; (* triviale, caso in cui non fa nulla *)
270 |6,7: (* casi base in cui allunga la lista più corta *) 
271 ]
272 elim devil;
273 qed.
274
275 include "Q/q/qtimes.ma".
276
277 let rec area (l:list bar) on l ≝
278   match l with 
279   [ nil ⇒ OQ
280   | cons he tl ⇒ area tl + Qpos (\fst he) * ⅆ[OQ,\snd he]].
281
282 alias symbol "pi1" = "exT \fst".
283 alias symbol "minus" = "Q minus".
284 alias symbol "exists" = "CProp exists".
285 definition minus_spec_bar ≝
286  λf,g,h:list bar.
287    same_bases f g → len f = len g →
288      ∀s,i:ℚ. \snd (\fst (value (mk_q_f s h) i)) = 
289        \snd (\fst (value (mk_q_f s f) i)) - \snd (\fst (value (mk_q_f s g) i)). 
290
291 definition minus_spec ≝
292  λf,g:q_f.
293    ∃h:q_f. 
294      ∀i:ℚ. \snd (\fst (value h i)) = 
295        \snd (\fst (value f i)) - \snd (\fst (value g i)). 
296
297 definition eject_bar : ∀P:list bar → CProp.(∃l:list bar.P l) → list bar ≝
298  λP.λp.match p with [ex_introT x _ ⇒ x].
299 definition inject_bar ≝ ex_introT (list bar).
300
301 coercion inject_bar with 0 1 nocomposites.
302 coercion eject_bar with 0 0 nocomposites.
303
304 lemma minus_q_f : ∀f,g. minus_spec f g.
305 intros;
306 letin aux ≝ (
307   let rec aux (l1, l2 : list bar) on l1 ≝
308     match l1 with
309     [ nil ⇒ []
310     | cons he1 tl1 ⇒
311         match l2 with
312         [ nil ⇒ []
313         | cons he2 tl2 ⇒ 〈\fst he1, \snd he1 - \snd he2〉 :: aux tl1 tl2]]
314   in aux : ∀l1,l2 : list bar.∃h.minus_spec_bar l1 l2 h);
315 [2: intros 4; simplify in H3; destruct H3;
316 |3: intros 4; simplify in H3; cases l1 in H2; [2: intro X; simplify in X; destruct X]    
317     intros; rewrite > (value_OQ_e (mk_q_f s []) i); [2: reflexivity]
318     rewrite > q_elim_minus; rewrite > q_plus_OQ; reflexivity;
319 |1: cases (aux l2 l3); unfold in H2; intros 4;
320     simplify in ⊢ (? ? (? ? ? (? ? ? (? % ?))) ?);
321     cases (q_cmp i (s + Qpos (\fst b)));
322     
323
324
325 definition excess ≝ 
326   λf,g.∃i.\snd (\fst (value f i)) < \snd (\fst (value g i)).
327