]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/dama/dama/models/q_bars.ma
Preparing for 0.5.9 release.
[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 "dama/nat_ordered_set.ma".
16 include "models/q_support.ma".
17 include "models/list_support.ma". 
18 include "logic/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,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 definition q2_lt := mk_rel bar (λx,y:bar.\fst x < \fst y).
33
34 interpretation "bar lt" 'lt x y = (rel_op ? q2_lt x y).
35
36 lemma q2_trans : ∀a,b,c:bar. a < b → b < c → a < c.
37 intros 3; cases a; cases b; cases c; unfold q2_lt; simplify; intros;
38 apply (q_lt_trans ??? H H1);
39 qed. 
40
41 definition q2_trel := mk_trans_rel bar q2_lt q2_trans.
42
43 interpretation "bar lt" 'lt x y = (FunClass_2_OF_trans_rel q2_trel x y).
44
45 definition canonical_q_lt : rel bar → trans_rel ≝ λx:rel bar.q2_trel.
46
47 coercion canonical_q_lt with nocomposites.
48
49 interpretation "bar lt" 'lt x y = (FunClass_2_OF_trans_rel (canonical_q_lt ?) x y).
50
51 definition nth_base ≝ λf,n. \fst (\nth f ▭ n).
52 definition nth_height ≝ λf,n. \snd (\nth f ▭ n).
53
54 record q_f : Type ≝ {
55  bars: list bar; 
56  bars_sorted : sorted q2_lt bars;
57  bars_begin_OQ : nth_base bars O = OQ;
58  bars_end_OQ : nth_height bars (pred (\len bars)) = 〈OQ,OQ〉
59 }.
60
61 lemma len_bases_gt_O: ∀f.O < \len (bars f).
62 intros; generalize in match (bars_begin_OQ f); cases (bars f); intros;
63 [2: simplify; apply le_S_S; apply le_O_n;
64 |1: normalize in H; destruct H;]
65 qed. 
66
67 lemma all_bases_positive : ∀f:q_f.∀i. OQ < nth_base (bars f) (S i).
68 intro f; generalize in match (bars_begin_OQ f); generalize in match (bars_sorted f);
69 cases (len_gt_non_empty ?? (len_bases_gt_O f)); intros;
70 cases (cmp_nat (\len l) i);
71 [2: lapply (sorted_tail_bigger q2_lt ?? ▭ H ? H2) as K;  
72     simplify in H1; rewrite < H1; apply K;
73 |1: simplify; elim l in i H2;[simplify; rewrite > nth_nil; apply (q_pos_OQ one)]
74     cases n in H3; intros; [simplify in H3; cases (not_le_Sn_O ? H3)] 
75     apply (H2 n1); simplify in H3; apply (le_S_S_to_le ?? H3);]
76 qed.
77
78 alias symbol "lt" (instance 9) = "Q less than".
79 alias symbol "lt" (instance 7) = "natural 'less than'".
80 alias symbol "lt" (instance 6) = "natural 'less than'".
81 alias symbol "lt" (instance 5) = "Q less than".
82 alias symbol "lt" (instance 4) = "natural 'less than'".
83 alias symbol "lt" (instance 2) = "natural 'less than'".
84 alias symbol "leq" = "Q less or equal than".
85 coinductive value_spec (f : list bar) (i : ℚ) : ℚ × ℚ → CProp ≝
86 | value_of : ∀j,q. 
87     nth_height f j = q →  nth_base f j < i → j < \len f →
88     (∀n.n<j → nth_base f n < i) →
89     (∀n.j < n → n < \len f → i ≤ nth_base f n) → value_spec f i q. 
90
91 definition match_pred ≝
92  λi.λx:bar.match q_cmp (Qpos i) (\fst x) with[ q_leq _ ⇒ true| q_gt _ ⇒ false].
93
94 definition match_domain ≝
95   λf: list bar.λi:ratio. pred (find ? (match_pred i) f ▭).
96    
97 definition value_simple ≝
98   λf: list bar.λi:ratio. nth_height f (match_domain f i).
99          
100 alias symbol "lt" (instance 5) = "Q less than".
101 alias symbol "lt" (instance 6) = "natural 'less than'".
102 definition value_lemma : 
103   ∀f:list bar.sorted q2_lt f → O < length bar f → 
104   ∀i:ratio.nth_base f O  < Qpos i → ∃p:ℚ×ℚ.value_spec f (Qpos i) p.
105 intros (f bars_sorted_f len_bases_gt_O_f i bars_begin_OQ_f);
106 exists [apply (value_simple f i);]
107 apply (value_of ?? (match_domain f i));
108 [1: reflexivity
109 |2: unfold match_domain; cases (cases_find bar (match_pred i) f ▭);
110     [1: cases i1 in H H1 H2 H3; simplify; intros;
111         [1: generalize in match (bars_begin_OQ_f); 
112             cases (len_gt_non_empty ?? (len_bases_gt_O_f)); simplify; intros;
113             assumption;
114         |2: cases (len_gt_non_empty ?? (len_bases_gt_O_f)) in H3;
115             intros; lapply (H3 n (le_n ?)) as K; unfold match_pred in K;
116             cases (q_cmp (Qpos i) (\fst (\nth (x::l) ▭ n))) in K;
117             simplify; intros; [destruct H5] assumption] 
118     |2: destruct H; cases (len_gt_non_empty ?? (len_bases_gt_O_f)) in H2;
119         simplify; intros; lapply (H (\len l) (le_n ?)) as K; clear H;
120         unfold match_pred in K; cases (q_cmp (Qpos i) (\fst (\nth (x::l) ▭ (\len l)))) in K;
121         simplify; intros; [destruct H2] assumption;]     
122 |5: intro; unfold match_domain; cases (cases_find bar (match_pred i) f ▭); intros;
123     [1: generalize in match (bars_sorted_f); 
124         cases (list_break ??? H) in H1; rewrite > H6;
125         rewrite < H1; simplify; rewrite > nth_len; unfold match_pred; 
126         cases (q_cmp (Qpos i) (\fst x)); simplify; 
127         intros (X Hs); [2: destruct X] clear X;
128         cases (sorted_pivot q2_lt ??? ▭ Hs);
129         cut (\len l1 ≤ n) as Hn; [2:
130           rewrite > H1;  cases i1 in H4; simplify; intro X; [2: assumption]
131           apply lt_to_le; assumption;]
132         unfold nth_base; rewrite > (nth_append_ge_len ????? Hn);
133         cut (n - \len l1 < \len (x::l2)) as K; [2:
134           simplify; rewrite > H1; rewrite > (?:\len l2 = \len f - \len (l1 @ [x]));[2:
135             rewrite > H6; repeat rewrite > len_append; simplify;
136             repeat rewrite < plus_n_Sm; rewrite < plus_n_O; simplify;
137             rewrite > sym_plus; rewrite < minus_plus_m_m; reflexivity;]
138           rewrite > len_append; rewrite > H1; simplify; rewrite < plus_n_SO;
139           apply le_S_S; clear H1 H6 H7 Hs H8 H9 Hn x l2 l1 H4 H3 H2 H;
140           elim (\len f) in i1 n H5; [cases (not_le_Sn_O ? H);]
141           simplify; cases n2; [ repeat rewrite < minus_n_O; apply le_S_S_to_le; assumption]
142           cases n1 in H1; [intros; rewrite > eq_minus_n_m_O; apply le_O_n]
143           intros; simplify; apply H; apply le_S_S_to_le; assumption;]
144         cases (n - \len l1) in K; simplify; intros; [ assumption]
145         lapply (H9 ? (le_S_S_to_le ?? H10)) as W; apply (q_le_trans ??? H7);
146         apply q_lt_to_le; apply W;
147     |2: cases (not_le_Sn_n i1); rewrite > H in ⊢ (??%);
148         apply (trans_le ??? ? H4); cases i1 in H3; intros; apply le_S_S; 
149         [ apply le_O_n; | assumption]]
150 |3: unfold match_domain; cases (cases_find bar (match_pred i) f ▭); [
151       cases i1 in H; intros; simplify; [assumption]
152       apply lt_S_to_lt; assumption;]
153     rewrite > H; cases (\len f) in len_bases_gt_O_f; intros; [cases (not_le_Sn_O ? H3)]
154     simplify; apply le_n;
155 |4: intros; unfold match_domain in H; cases (cases_find bar (match_pred i) f ▭) in H; simplify; intros; 
156     [1: lapply (H3 n); [2: cases i1 in H4; intros [assumption] apply le_S; assumption;]
157         unfold match_pred in Hletin; cases (q_cmp (Qpos i) (\fst (\nth f ▭ n))) in Hletin;
158         simplify; intros; [destruct H6] assumption;
159     |2: destruct H; cases f in len_bases_gt_O_f H2 H3; clear H1; simplify; intros;
160         [cases (not_le_Sn_O ? H)] lapply (H1 n); [2: apply le_S; assumption]
161         unfold match_pred in Hletin; cases (q_cmp (Qpos i) (\fst (\nth (b::l) ▭ n))) in Hletin;
162         simplify; intros; [destruct H4] assumption;]]
163 qed.    
164
165 lemma bars_begin_lt_Qpos : ∀q,r. nth_base (bars q) O<Qpos r.
166 intros; rewrite > bars_begin_OQ; apply q_pos_OQ;
167 qed.
168
169 lemma value : q_f → ratio → ℚ × ℚ.
170 intros; cases (value_lemma (bars q) ?? r); 
171 [ apply bars_sorted.
172 | apply len_bases_gt_O;
173 | apply w; 
174 | apply bars_begin_lt_Qpos;]
175 qed.
176
177 lemma cases_value : ∀f,i. value_spec (bars f) (Qpos i) (value f i).
178 intros; unfold value; 
179 cases (value_lemma (bars f) (bars_sorted f) (len_bases_gt_O f) i (bars_begin_lt_Qpos f i)); 
180 assumption;
181 qed.
182
183 definition same_values ≝ λl1,l2:q_f.∀input. value l1 input = value l2 input. 
184
185 definition same_bases ≝ λl1,l2:list bar. ∀i.\fst (\nth l1 ▭ i) = \fst (\nth l2 ▭ i).
186
187 lemma same_bases_cons: ∀a,b,l1,l2.
188   same_bases l1 l2 → \fst a = \fst b → same_bases (a::l1) (b::l2).
189 intros; intro; cases i; simplify; [assumption;] apply (H n);
190 qed.
191
192 alias symbol "lt" = "Q less than".
193 lemma unpos: ∀x:ℚ.OQ < x → ∃r:ratio.Qpos r = x.
194 intro; cases x; intros; [2:exists [apply r] reflexivity]
195 cases (?:False);
196 [ apply (q_lt_corefl ? H)| cases (q_lt_le_incompat ?? (q_neg_gt ?) (q_lt_to_le ?? H))]
197 qed.
198
199 notation < "x \blacksquare" non associative with precedence 50 for @{'unpos $x}.
200 interpretation "hide unpos proof" 'unpos x = (unpos x ?).
201