]> 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 copy_rebases: 
108   ∀l1.rebase_spec_aux l1 [] 〈l1, copy l1〉.
109 intros; elim l1; intros 4;
110 [1: split; [left; reflexivity]; split; try assumption; unfold; intros;
111     unfold same_values; intros; reflexivity;
112 |2: rewrite > H3; [2: intro X; destruct X]
113     split; [left; reflexivity] split; 
114     unfold same_values_simpl; unfold same_values; intros; try reflexivity;
115     try assumption; [4: normalize in p2; destruct p2|2: cases H5; reflexivity;]
116     [1: apply (sorted_copy ? H1);
117     |2: cases H; try assumption;[2:apply (sorted_tail q2_lt ?? H1);
118         |3: intro; cases l in H5 H3; [intros (_ X); cases X; reflexivity]
119             intros; rewrite < H3; simplify; [2: intro X; destruct X]
120             reflexivity]
121         cases H8; clear H11 H10 H8 H7 H6 H5 H4 H3; simplify;
122         simplify in H9;sq elim l in H9; [rewrite < minus_n_n; intro; simplify;
123           elim i; [reflexivity] simplify; reflexivity;]    
124         simplify; apply H3; 
125         
126         
127         
128         
129     
130         
131 definition rebase: ∀l1,l2:q_f.∃p:q_f × q_f.rebase_spec l1 l2 p.
132 intros 2 (f1 f2); cases f1 (b1 Hs1 Hb1 He1); cases f2 (b2 Hs2 Hb2 He2); clear f1 f2;
133 alias symbol "plus" = "natural plus".
134 alias symbol "pi2" = "pair pi2".
135 alias symbol "pi1" = "pair pi1".
136 alias symbol "minus" = "Q minus".
137 letin aux ≝ ( 
138 let rec aux (l1,l2:list bar) (n : nat) on n : (list bar) × (list bar) ≝
139 match n with
140 [ O ⇒ 〈[], []〉
141 | S m ⇒
142   match l1 with
143   [ nil ⇒ 〈copy l2, l2〉
144   | cons he1 tl1 ⇒
145      match l2 with
146      [ nil ⇒ 〈l1, copy l1〉
147      | cons he2 tl2 ⇒  
148          let base1 ≝ \fst he1 in
149          let base2 ≝ \fst he2 in
150          let height1 ≝ \snd he1 in
151          let height2 ≝ \snd he2 in
152          match q_cmp base1 base2 with
153          [ q_leq Hp1 ⇒ 
154              match q_cmp base2 base1 with
155              [ q_leq Hp2 ⇒
156                  let rc ≝ aux tl1 tl2 m in 
157                  〈he1 :: \fst rc,he2 :: \snd rc〉
158              | q_gt Hp ⇒ 
159                  let rest ≝ base2 - base1 in
160                  let rc ≝ aux tl1 (〈rest,height2〉 :: tl2) m in
161                  〈〈base1,height1〉 :: \fst rc,〈base1,height2〉 :: \snd rc〉] 
162          | q_gt Hp ⇒ 
163              let rest ≝ base1 - base2 in
164              let rc ≝ aux (〈rest,height1〉 :: tl1) tl2 m in
165              〈〈base2,height1〉 :: \fst rc,〈base2,height2〉 :: \snd rc〉]]]]
166 in aux : ∀l1,l2,m.∃z.m = \len l1 + \len l2 → rebase_spec_aux l1 l2 z);
167 [7: clearbody aux; cases (aux b1 b2 (\len b1 + \len b2)) (w Hw); clear aux;
168     cases (Hw (refl_eq ??) Hs1 Hs2 (λ_.He1) (λ_.He2)); clear Hw; cases H1; cases H2; cases H3; clear H3 H1 H2;
169     exists [constructor 1;constructor 1;[apply (\fst w)|5:apply (\snd w)]] try assumption;
170     [1,3: apply hide; cases H (X X); try rewrite < (H8 O); try rewrite < X; assumption
171     |2,4: apply hide;[apply H6|apply H7]intro X;[rewrite > X in Hb1|rewrite > X in Hb2]
172          normalize in Hb1 Hb2; [destruct Hb1|destruct Hb2]]
173     unfold; unfold same_values; simplify in ⊢ (? (? % %) ? ?); 
174     simplify in match (\snd 〈?,?〉); simplify in match (\fst 〈?,?〉);
175     split; [assumption; |apply H9;|apply H10]
176 |6: intro ABS; unfold; intros 4; clear H1 H2;
177     cases l in ABS H3; intros 1; [2: simplify in H1; destruct H1]
178     cases l1 in H4 H1; intros; [2: simplify in H2; destruct H2]
179     split; [left;reflexivity|split; apply (sorted_nil q2_lt);|split; assumption;]
180     split; unfold; intros; unfold same_values; intros; reflexivity;
181 |5: unfold rebase_spec_aux; intros; cases l1 in H2 H4 H6; intros; [ simplify in H2; destruct H2;]
182     lapply H6 as H7; [2: intro X; destruct X] clear H6 H5;
183     rewrite > H7; split; [right; simplify;
184     
185      split; [left;reflexivity]
186     split; 
187
188 ,2: unfold rest; apply q_lt_minus; rewrite > q_plus_sym; rewrite > q_plus_OQ;
189       assumption;        
190 |8: intros; cases (?:False); apply (not_le_Sn_O ? H1);
191 |3: intros; generalize in match (unpos ??); intro X; cases X; clear X;
192     simplify in ⊢ (???? (??? (??? (??? (?? (? (?? (??? % ?) ?) ??)))) ?));
193     simplify in ⊢ (???? (???? (??? (??? (?? (? (?? (??? % ?) ?) ??)))))); 
194     clear H4; cases (aux (〈w,\snd b〉::l4) l5 n1); clear aux;
195     cut (len (〈w,\snd b〉::l4) + len l5 < n1) as K;[2:
196       simplify in H5; simplify; rewrite > sym_plus in H5; simplify in H5;
197       rewrite > sym_plus in H5; apply le_S_S_to_le; apply H5;] 
198     split;
199     [1: simplify in ⊢ (? % ?); simplify in ⊢ (? ? %); 
200         cases (H4 s K); clear K H4; intro input; cases input; [reflexivity]
201         simplify; apply H7; 
202     |2: simplify in ⊢ (? ? %); cases (H4 s K); clear H4 K H5 spec;
203         intro;
204         (* input < s + b1 || input >= s + b1 *)
205     |3: simplify in ⊢ (? ? %);]   
206 |4: intros; generalize in match (unpos ??); intro X; cases X; clear X;
207     (* duale del 3 *)
208 |5: intros; (* triviale, caso in cui non fa nulla *)
209 |6,7: (* casi base in cui allunga la lista più corta *) 
210 ]
211 elim devil;
212 qed.
213
214 include "Q/q/qtimes.ma".
215
216 let rec area (l:list bar) on l ≝
217   match l with 
218   [ nil ⇒ OQ
219   | cons he tl ⇒ area tl + Qpos (\fst he) * ⅆ[OQ,\snd he]].
220
221 alias symbol "pi1" = "exT \fst".
222 alias symbol "minus" = "Q minus".
223 alias symbol "exists" = "CProp exists".
224 definition minus_spec_bar ≝
225  λf,g,h:list bar.
226    same_bases f g → len f = len g →
227      ∀s,i:ℚ. \snd (\fst (value (mk_q_f s h) i)) = 
228        \snd (\fst (value (mk_q_f s f) i)) - \snd (\fst (value (mk_q_f s g) i)). 
229
230 definition minus_spec ≝
231  λf,g:q_f.
232    ∃h:q_f. 
233      ∀i:ℚ. \snd (\fst (value h i)) = 
234        \snd (\fst (value f i)) - \snd (\fst (value g i)). 
235
236 definition eject_bar : ∀P:list bar → CProp.(∃l:list bar.P l) → list bar ≝
237  λP.λp.match p with [ex_introT x _ ⇒ x].
238 definition inject_bar ≝ ex_introT (list bar).
239
240 coercion inject_bar with 0 1 nocomposites.
241 coercion eject_bar with 0 0 nocomposites.
242
243 lemma minus_q_f : ∀f,g. minus_spec f g.
244 intros;
245 letin aux ≝ (
246   let rec aux (l1, l2 : list bar) on l1 ≝
247     match l1 with
248     [ nil ⇒ []
249     | cons he1 tl1 ⇒
250         match l2 with
251         [ nil ⇒ []
252         | cons he2 tl2 ⇒ 〈\fst he1, \snd he1 - \snd he2〉 :: aux tl1 tl2]]
253   in aux : ∀l1,l2 : list bar.∃h.minus_spec_bar l1 l2 h);
254 [2: intros 4; simplify in H3; destruct H3;
255 |3: intros 4; simplify in H3; cases l1 in H2; [2: intro X; simplify in X; destruct X]    
256     intros; rewrite > (value_OQ_e (mk_q_f s []) i); [2: reflexivity]
257     rewrite > q_elim_minus; rewrite > q_plus_OQ; reflexivity;
258 |1: cases (aux l2 l3); unfold in H2; intros 4;
259     simplify in ⊢ (? ? (? ? ? (? ? ? (? % ?))) ?);
260     cases (q_cmp i (s + Qpos (\fst b)));
261     
262
263
264 definition excess ≝ 
265   λf,g.∃i.\snd (\fst (value f i)) < \snd (\fst (value g i)).
266