]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/dama/dama/models/q_bars.ma
d5a7806e799bdc99aaedfa33b0048937920487a2
[helm.git] / helm / software / matita / contribs / dama / dama / models / q_bars.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 "nat_ordered_set.ma".
16 include "models/q_support.ma".
17 include "models/list_support.ma".
18 include "cprop_connectives.ma". 
19
20 definition bar ≝ ℚ × ℚ.
21
22 notation < "\rationals \sup 2" non associative with precedence 90 for @{'q2}.
23 interpretation "Q x Q" 'q2 = (Prod Q Q).
24
25 definition empty_bar : bar ≝ 〈Qpos one,OQ〉.
26 notation "\rect" with precedence 90 for @{'empty_bar}.
27 interpretation "q0" 'empty_bar = empty_bar.
28
29 notation < "\ldots\rect\square\EmptySmallSquare\ldots" with precedence 90 for @{'lq2}.
30 interpretation "lq2" 'lq2 = (list bar). 
31
32 inductive sorted : list bar → Prop ≝ 
33 | sorted_nil : sorted []
34 | sorted_one : ∀x. sorted [x]
35 | sorted_cons : ∀x,y,tl. \fst x < \fst y → sorted (y::tl) → sorted (x::y::tl). 
36
37 definition nth_base ≝ λf,n. \fst (nth f ▭ n).
38 definition nth_height ≝ λf,n. \snd (nth f ▭ n).
39
40 record q_f : Type ≝ {
41  bars: list bar; 
42  bars_sorted : sorted bars;
43  bars_begin_OQ : nth_base bars O = OQ;
44  bars_tail_OQ : nth_height bars (pred (len bars)) = OQ
45 }.
46
47 lemma nth_nil: ∀T,i.∀def:T. nth [] def i = def.
48 intros; elim i; simplify; [reflexivity;] assumption; qed.
49
50 lemma len_concat: ∀T:Type.∀l1,l2:list T. len (l1@l2) = len l1 + len l2.
51 intros; elim l1; [reflexivity] simplify; rewrite < H; reflexivity;
52 qed.
53
54 inductive non_empty_list (A:Type) : list A → Type := 
55 | show_head: ∀x,l. non_empty_list A (x::l).
56
57 lemma bars_not_nil: ∀f:q_f.non_empty_list ? (bars f).
58 intro f; generalize in match (bars_begin_OQ f); cases (bars f); 
59 [1: intro X; normalize in X; destruct X;
60 |2: intros; constructor 1;]
61 qed.
62
63 lemma sorted_tail: ∀x,l.sorted (x::l) → sorted l.
64 intros; inversion H; intros; [destruct H1;|destruct H1;constructor 1;]
65 destruct H4; assumption;
66 qed.
67
68 lemma sorted_skip: ∀x,y,l. sorted (x::y::l) → sorted (x::l).
69 intros; inversion H; intros; [1,2: destruct H1]
70 destruct H4; inversion H2; intros; [destruct H4]
71 [1: destruct H4; constructor 2;
72 |2: destruct H7; constructor 3; [apply (q_lt_trans ??? H1 H4);]
73     apply (sorted_tail ?? H2);]
74 qed.
75
76 lemma sorted_tail_bigger : ∀x,l.sorted (x::l) → ∀i. i < len l → \fst x < nth_base l i.
77 intros 2; elim l; [ cases (not_le_Sn_O i H1);]
78 cases i in H2;
79 [2: intros; apply (H ? n);[apply (sorted_skip ??? H1)|apply le_S_S_to_le; apply H2]
80 |1: intros; inversion H1; intros; [1,2: destruct H3]
81     destruct H6; simplify; assumption;]
82 qed. 
83
84 lemma all_bases_positive : ∀f:q_f.∀i. OQ < nth_base (bars f) (S i).
85 intro f; generalize in match (bars_begin_OQ f); generalize in match (bars_sorted f);
86 cases (bars_not_nil f); intros;
87 cases (cmp_nat i (len l));
88 [1: lapply (sorted_tail_bigger ?? H ? H2) as K; simplify in H1;
89     rewrite > H1 in K; apply K;
90 |2: rewrite > H2; simplify; elim l; simplify; [apply (q_pos_OQ one)] 
91     assumption;
92 |3: simplify; elim l in i H2;[simplify; rewrite > nth_nil; apply (q_pos_OQ one)]
93     cases n in H3; intros; [cases (not_le_Sn_O ? H3)] apply (H2 n1);
94     apply (le_S_S_to_le ?? H3);]
95 qed.
96
97 lemma lt_n_plus_n_Sm : ∀n,m:nat.n < n + S m.
98 intros; rewrite > sym_plus; apply (le_S_S n (m+n)); apply (le_plus_n m n); qed.
99
100 lemma nth_concat_lt_len:
101   ∀T:Type.∀l1,l2:list T.∀def.∀i.i < len l1 → nth (l1@l2) def i = nth l1 def i.
102 intros 4; elim l1; [cases (not_le_Sn_O ? H)] cases i in H H1; simplify; intros;
103 [reflexivity| rewrite < H;[reflexivity] apply le_S_S_to_le; apply H1]
104 qed. 
105
106 lemma nth_concat_ge_len:
107   ∀T:Type.∀l1,l2:list T.∀def.∀i.
108     len l1 ≤ i → nth (l1@l2) def i = nth l2 def (i - len l1).
109 intros 4; elim l1; [ rewrite < minus_n_O; reflexivity]
110 cases i in H1; simplify; intros; [cases (not_le_Sn_O ? H1)]
111 apply H; apply le_S_S_to_le; apply H1;
112 qed. 
113
114 lemma nth_len:
115   ∀T:Type.∀l1,l2:list T.∀def,x.
116     nth (l1@x::l2) def (len l1) = x.
117 intros 2; elim l1;[reflexivity] simplify; apply H; qed.
118
119 lemma all_bigger_can_concat_bigger:
120    ∀l1,l2,start,b,x,n.
121     (∀i.i< len l1 → nth_base l1 i < \fst b) →
122     (∀i.i< len l2 → \fst b ≤ nth_base l2 i) →
123     (∀i.i< len l1 → start ≤ i → x ≤ nth_base l1 i) →
124     start ≤ n → n < len (l1@b::l2) → x ≤ \fst b → x ≤ nth_base (l1@b::l2) n.
125 intros; cases (cmp_nat n (len l1));
126 [1: unfold nth_base;  rewrite > (nth_concat_lt_len ????? H6);
127     apply (H2 n); assumption;
128 |2: rewrite > H6; unfold nth_base; rewrite > nth_len; assumption;
129 |3: unfold nth_base; rewrite > nth_concat_ge_len; [2: apply lt_to_le; assumption]
130     rewrite > len_concat in H4; simplify in H4; rewrite < plus_n_Sm in H4;
131     lapply linear le_S_S_to_le to H4 as K; rewrite > sym_plus in K;
132     lapply linear le_plus_to_minus to K as X; 
133     generalize in match X; generalize in match (n - len l1); intro W; cases W; clear W X;
134     [intros; assumption] intros;
135     apply (q_le_trans ??? H5); apply (H1 n1); assumption;]
136 qed. 
137
138 lemma sorted_head_smaller:
139   ∀l,p. sorted (p::l) → ∀i.i < len l → \fst p < nth_base l i.
140 intro l; elim l; intros; [cases (not_le_Sn_O ? H1)] cases i in H2; simplify; intros;
141 [1: inversion H1; [1,2: simplify; intros; destruct H3] intros; destruct H6; assumption; 
142 |2: apply (H p ? n ?); [apply (sorted_skip ??? H1)] apply le_S_S_to_le; apply H2]
143 qed.    
144
145
146 alias symbol "pi1" = "pair pi1".
147 alias symbol "lt" (instance 6) = "Q less than".
148 alias symbol "lt" (instance 2) = "Q less than".
149 alias symbol "and" = "logical and".
150 lemma sorted_pivot:
151   ∀l1,l2,p. sorted (l1@p::l2) → 
152     (∀i. i < len l1 → nth_base l1 i < \fst p) ∧
153     (∀i. i < len l2 → \fst p < nth_base l2 i).
154 intro l; elim l; 
155 [1: split; [intros; cases (not_le_Sn_O ? H1);] intros;
156     apply sorted_head_smaller; assumption;
157 |2: cases (H ?? (sorted_tail a (l1@p::l2) H1));
158     lapply depth = 0 (sorted_head_smaller (l1@p::l2) a H1) as Hs;
159     split; simplify; intros;
160     [1: cases i in H4; simplify; intros; 
161         [1: lapply depth = 0 (Hs (len l1)) as HS; 
162             unfold nth_base in HS; rewrite > nth_len in HS; apply HS;
163             rewrite > len_concat; simplify; apply lt_n_plus_n_Sm;
164         |2: apply (H2 n); apply le_S_S_to_le; apply H4]
165     |2: apply H3; assumption]]
166 qed.
167
168 definition eject_NxQ ≝
169   λP.λp:∃x:nat × ℚ.P x.match p with [ex_introT p _ ⇒ p].
170 coercion eject_NxQ.
171 definition inject_NxQ ≝ λP.λp:nat × ℚ.λh:P p. ex_introT ? P p h.
172 coercion inject_NxQ with 0 1 nocomposites.
173
174 definition value_spec : q_f → ℚ → nat × ℚ → Prop ≝
175   λf,i,q. nth_height (bars f) (\fst q) = \snd q ∧  
176         (nth_base (bars f) (\fst q)  < i ∧
177          ∀n.\fst q < n → n < len (bars f) → i ≤ nth_base (bars f) n). 
178
179 definition value :  ∀f:q_f.∀i:ratio.∃p:ℚ.∃j.value_spec f (Qpos i) 〈j,p〉.
180 intros;
181 alias symbol "pi2" = "pair pi2".
182 alias symbol "pi1" = "pair pi1".
183 alias symbol "lt" (instance 7) = "Q less than".
184 alias symbol "leq" = "Q less or equal than".
185 letin value_spec_aux ≝ (
186   λf,i,q. And4
187     (\fst q < len f)
188     (\snd q = nth_height f (\fst q))  
189     (nth_base f (\fst q) < i) 
190     (∀n.(\fst q) < n → n < len f → i ≤ nth_base f n));
191 alias symbol "lt" (instance 5) = "Q less than".
192 letin value ≝ (
193   let rec value (acc: nat × ℚ) (l : list bar) on l : nat × ℚ ≝
194     match l with
195     [ nil ⇒ acc
196     | cons x tl ⇒
197         match q_cmp (\fst x) (Qpos i) with
198         [ q_leq _ ⇒ value 〈S (\fst acc), \snd x〉 tl  
199         | q_gt _ ⇒ acc]]
200   in value :
201   ∀acc,l.∃p:nat × ℚ.
202     ∀story. story @ l = bars f → S (\fst acc) = len story →
203     value_spec_aux story (Qpos i) acc → 
204     value_spec_aux (story @ l) (Qpos i) p);
205 [4: clearbody value;  unfold value_spec;
206     generalize in match (bars_begin_OQ f);
207     generalize in match (bars_sorted f);
208     cases (bars_not_nil f) in value; intros (value S); generalize in match (sorted_tail_bigger ?? S);
209     clear S; cases (value 〈O,\snd x〉 l) (p Hp); intros; 
210     exists[apply (\snd p)];exists [apply (\fst p)] simplify; 
211     cases (Hp [x] (refl_eq ??) (refl_eq ??) ?) (Hg HV); 
212     [unfold; split; [apply le_n|reflexivity|rewrite > H; apply q_pos_OQ;]
213      intros; cases n in H2 H3; [intro X; cases (not_le_Sn_O ? X)]
214      intros; cases (not_le_Sn_O ? (le_S_S_to_le (S n1) O H3))]
215     split;[rewrite > HV; reflexivity] split; [assumption;]
216     intros; cases n in H4 H5; intros [cases (not_le_Sn_O ? H4)]
217     apply (H3 (S n1)); assumption;
218 |1: unfold value_spec_aux; clear value value_spec_aux H2; intros; 
219     cases H4; clear H4; split;
220     [1: apply (trans_lt ??? H5); rewrite > len_concat; simplify; apply lt_n_plus_n_Sm;
221     |2: unfold nth_height; rewrite > nth_concat_lt_len;[2:assumption]assumption; 
222     |3: unfold nth_base; rewrite > nth_concat_lt_len;[2:assumption]
223         apply (q_le_lt_trans ???? H7); apply q_le_n; 
224     |4: intros; (*clear H6 H5 H4 H l;*) lapply (bars_sorted f) as HS;
225         apply (all_bigger_can_concat_bigger story l1 (S (\fst p)));[6:apply q_lt_to_le]try assumption;
226         [1: rewrite < H2 in HS; cases (sorted_pivot ??? HS); assumption
227         |2: rewrite < H2 in HS; cases (sorted_pivot ??? HS); 
228             intros; apply q_lt_to_le; apply H11; assumption;
229         |3: intros; apply H8; assumption;]]
230 |3: intro; rewrite > append_nil; intros; assumption;
231 |2: intros; cases (value 〈S (\fst p),\snd b〉 l1); unfold; simplify;
232     cases (H6 (story@[b]) ???); 
233     [1: rewrite > associative_append; apply H3;
234     |2: simplify; rewrite > H4; rewrite > len_concat; rewrite > sym_plus; reflexivity;
235     |4: rewrite < (associative_append ? story [b] l1); split; assumption;
236     |3: cases H5; clear H5; split; simplify in match (\snd ?); simplify in match (\fst ?); 
237         [1: rewrite > len_concat; simplify; rewrite < plus_n_SO; apply le_S_S; assumption;
238         |2: 
239         |3: 
240         |4: ]]]
241
242
243
244
245
246
247
248
249
250
251 [5: clearbody value; 
252     cases (q_cmp i (start f));
253     [2: exists [apply 〈O,OQ〉] simplify; constructor 1; split; try assumption; 
254         try reflexivity; apply q_lt_to_le; assumption;
255     |1: cases (bars f); [exists [apply 〈O,OQ〉] simplify; constructor 3; split;try assumption;reflexivity;]
256         cases (value ⅆ[i,start f] (b::l)) (p Hp);
257         cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value; [cases H1; destruct H2]
258         cases H1; clear H1; lapply (sum_bases_O (b::l) (\fst p)) as H1;
259         [2: apply (q_le_trans ??? H2); rewrite > H; apply q_eq_to_le;
260             rewrite > q_d_x_x; reflexivity;
261         |1: exists [apply p] simplify; constructor 4; rewrite > H1; split;
262             try split; try rewrite > q_d_x_x; try autobatch depth=2;
263             [1: rewrite > H; rewrite > q_plus_sym; apply q_lt_plus;
264                 rewrite > q_plus_minus; apply q_lt_plus_trans; [apply sum_bases_ge_OQ]
265                 apply q_pos_lt_OQ;
266             |2: rewrite > H; rewrite > q_d_x_x; apply q_eq_to_le; reflexivity;
267             |3: rewrite > H; rewrite > q_d_x_x; apply q_lt_plus_trans;
268                 try apply sum_bases_ge_OQ; apply q_pos_lt_OQ;]]
269     |3: cases (q_cmp i (start f+sum_bases (bars f) (len (bars f))));
270         [1: exists [apply 〈O,OQ〉] simplify; constructor 2; split; try assumption; 
271             try reflexivity; rewrite > H1; apply q_eq_to_le; reflexivity;  
272         |3: exists [apply 〈O,OQ〉] simplify; constructor 2; split; try assumption; 
273             try reflexivity; apply q_lt_to_le; assumption;
274         |2: generalize in match (refl_eq ? (bars f): bars f = bars f);
275             generalize in match (bars f) in ⊢ (??? % → %); intro X; cases X; clear X;
276             intros;
277             [1: exists [apply 〈O,OQ〉] simplify; constructor 3; split; reflexivity;
278             |2: cases (value ⅆ[i,start f] (b::l)) (p Hp);
279                 cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value; [cases H3;destruct H4]
280                 cases H3; clear H3;
281                 exists [apply p]; constructor 4; split; try split; try assumption;
282                 [1: intro X; destruct X;  
283                 |2: apply q_lt_to_le; assumption;
284                 |3: rewrite < H2; assumption;
285                 |4: cases (cmp_nat (\fst p) (len (bars f)));
286                     [1:apply lt_to_le;rewrite <H2; assumption|rewrite > H3;rewrite < H2;apply le_n]   
287                     cases (?:False); cases (\fst p) in H3 H4 H6; clear H5;
288                     [1: intros; apply (not_le_Sn_O ? H5);
289                     |2: rewrite > q_d_sym; rewrite > q_d_noabs; [2: apply q_lt_to_le; assumption] 
290                         intros; lapply (q_lt_inj_plus_r ?? (Qopp (start f)) H1); clear H1;
291                         generalize in match Hletin;
292                         rewrite > (q_plus_sym (start f)); rewrite < q_plus_assoc;
293                         do 2 rewrite < q_elim_minus; rewrite > q_plus_minus;
294                         rewrite > q_plus_OQ; intro K; apply (q_lt_corefl (i-start f));
295                         apply (q_lt_le_trans ???? H3); rewrite < H2; 
296                         apply (q_lt_trans ??? K); apply sum_bases_increasing; 
297                         assumption;]]]]]                                 
298 |1,3: intros; right; split;
299      [1,4: clear H2; cases (value (q-Qpos (\fst b)) l1);
300            cases (H2 (q_le_to_diff_ge_OQ ?? (? H1)));
301            [1: intro; apply q_lt_to_le;assumption;
302            |3: simplify; cases H4; apply q_le_minus; assumption;
303            |2,5: simplify; cases H4; rewrite > H5; rewrite > H6;
304                  apply q_le_minus; apply sum_bases_empty_nat_of_q_le_q;
305            |4: intro X; rewrite > X; apply q_eq_to_le; reflexivity;
306            |*: simplify; apply q_le_minus; cases H4; assumption;]   
307     |2,5: cases (value (q-Qpos (\fst b)) l1); 
308           cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
309           [1,4: intros; [apply q_lt_to_le|apply q_eq_to_le;symmetry] assumption;
310           |3,6: cases H5; simplify; change with (q < sum_bases l1 (S (\fst w)) + Qpos (\fst b));
311                 apply q_lt_plus; assumption;
312           |2,5: simplify; cases H5; rewrite > H6; simplify; rewrite > H7;
313                 apply q_lt_plus; apply sum_bases_empty_nat_of_q_le_q_one;] 
314     |*: cases (value (q-Qpos (\fst b)) l1); simplify; 
315         cases (H4 (q_le_to_diff_ge_OQ ?? (? H1))); 
316         [1,4: intros; [apply q_lt_to_le|apply q_eq_to_le;symmetry] assumption;
317         |3,6: cases H5; assumption;
318         |*: cases H5; rewrite > H6; rewrite > H8;
319             elim (\fst w); [1,3:reflexivity;] simplify; assumption;]]
320 |2: clear value H2; simplify; intros; right; split; [assumption|3:reflexivity]
321     rewrite > q_plus_sym; rewrite > q_plus_OQ; assumption;
322 |4: intros; left; split; reflexivity;] 
323 qed.
324
325 lemma value_OQ_l:
326   ∀l,i.i < start l → \snd (\fst (value l i)) = OQ.
327 intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
328 try assumption; cases H2; cases (?:False); apply (q_lt_le_incompat ?? H H6);
329 qed.
330     
331 lemma value_OQ_r:
332   ∀l,i.start l + sum_bases (bars l) (len (bars l)) ≤ i → \snd (\fst (value l i)) = OQ.
333 intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
334 try assumption; cases H2; cases (?:False); apply (q_lt_le_incompat ?? H7 H);
335 qed.
336     
337 lemma value_OQ_e:
338   ∀l,i.bars l = [] → \snd (\fst (value l i)) = OQ.
339 intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
340 try assumption; cases H2; cases (?:False); apply (H1 H);
341 qed.
342
343 inductive value_ok_spec (f : q_f) (i : ℚ) : nat × ℚ → Type ≝
344  | value_ok : ∀n,q. n ≤ (len (bars f)) → 
345       q = \snd (nth (bars f) ▭ n) →
346       sum_bases (bars f) n ≤ ⅆ[i,start f] →
347            ⅆ[i, start f] < sum_bases (bars f) (S n) → value_ok_spec f i 〈n,q〉.
348   
349 lemma value_ok:
350   ∀f,i.bars f ≠ [] → start f ≤ i → i < start f + sum_bases (bars f) (len (bars f)) → 
351     value_ok_spec f i (\fst (value f i)). 
352 intros; cases (value f i); simplify;   
353 cases H3; simplify; clear H3; cases H4; clear H4;
354 [1,2,3: cases (?:False); 
355   [1: apply (q_lt_le_incompat ?? H3 H1);
356   |2: apply (q_lt_le_incompat ?? H2 H3);
357   |3: apply (H H3);]
358 |4: cases H7; clear H7; cases w in H3 H4 H5 H6 H8; simplify; intros;
359     constructor 1; assumption;]
360 qed.
361
362 definition same_values ≝
363   λl1,l2:q_f.
364    ∀input.\snd (\fst (value l1 input)) = \snd (\fst (value l2 input)). 
365
366 definition same_bases ≝ 
367   λl1,l2:list bar. (∀i.\fst (nth l1 ▭ i) = \fst (nth l2 ▭ i)).
368
369 alias symbol "lt" = "Q less than".
370 lemma unpos: ∀x:ℚ.OQ < x → ∃r:ratio.Qpos r = x.
371 intro; cases x; intros; [2:exists [apply r] reflexivity]
372 cases (?:False);
373 [ apply (q_lt_corefl ? H)|apply (q_neg_gt ? H)]
374 qed.
375
376 notation < "x \blacksquare" non associative with precedence 50 for @{'unpos $x}.
377 interpretation "hide unpos proof" 'unpos x = (unpos x _).
378