]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/dama/dama/models/q_rebase.ma
...
[helm.git] / helm / software / matita / contribs / dama / dama / models / q_rebase.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 "models/q_function.ma".
16
17 alias symbol "lt" = "Q less than".
18 alias symbol "Q" = "Rationals".
19 axiom q_unlimited: ∀x:ℚ.∃y:ratio.x<Qpos y.                
20 axiom q_halving: ∀x,y:ℚ.∃z:ℚ.x<z ∧ z<y.
21 alias symbol "not" = "logical not".
22 axiom q_not_OQ_lt_Qneg: ∀r. ¬ (OQ < Qneg r).
23 lemma same_values_unit_OQ: 
24   ∀b1,b2,h1,l. OQ < b2 → b2 < b1 → sorted q2_lt (〈b1,h1〉::l) →
25     sorted q2_lt  [〈b2,〈OQ,OQ〉〉] → 
26     same_values_simpl (〈b1,h1〉::l) [〈b2,〈OQ,OQ〉〉]  → h1 = 〈OQ,OQ〉.
27 intros 5 (b1 b2 h1 l POS); cases l;
28 [1: intros; cases (q_unlimited b1); cut (b2 < Qpos w); [2:apply (q_lt_trans ??? H H4);] 
29     lapply (H3 H1 ? H2 ? w H4 Hcut) as K; simplify; [1,2: autobatch]
30     rewrite > (value_unit 〈b1,h1〉) in K;
31     rewrite > (value_unit 〈b2,〈OQ,OQ〉〉) in K; assumption;
32 |2: intros; unfold in H3; lapply depth=0 (H3 H1 ? H2 ?) as K; [1,2:simplify; autobatch]
33     clear H3; cases (q_halving b1 (b1 + \fst p)) (w Pw); cases w in Pw; intros;
34     [cases (q_lt_le_incompat ?? POS); apply q_lt_to_le; cases H3;
35      apply (q_lt_trans ???? H4); assumption;
36     |3: elim H3; lapply (q_lt_trans ??? H H4); lapply (q_lt_trans ??? POS Hletin);
37         cases (q_not_OQ_lt_Qneg ? Hletin1);
38     | cases H3; lapply (K r);
39       [2: simplify; assumption
40       |3: simplify; apply (q_lt_trans ???? H4); assumption;
41       |rewrite > (value_head b1,h1 .. q) in Hletin;
42         
43
44
45  (* MANCA che le basi sono positive, 
46                poi con halving prendi tra b1 e \fst p e hai h1=OQ,OQ*)
47
48
49 definition eject ≝
50   λP.λp:∃x:(list bar) × (list bar).P x.match p with [ex_introT p _ ⇒ p].
51 coercion eject.
52 definition inject ≝ λP.λp:(list bar) × (list bar).λh:P p. ex_introT ? P p h.
53 coercion inject with 0 1 nocomposites.
54
55 definition rebase: ∀l1,l2:q_f.∃p:q_f × q_f.rebase_spec l1 l2 p.
56 intros 2 (f1 f2); cases f1 (b1 Hs1 Hb1 He1); cases f2 (b2 Hs2 Hb2 He2); clear f1 f2;
57 alias symbol "leq" = "natural 'less or equal to'".
58 alias symbol "minus" = "Q minus".
59 letin aux ≝ ( 
60 let rec aux (l1,l2:list bar) (n : nat) on n : (list bar) × (list bar) ≝
61 match n with
62 [ O ⇒ 〈[], []〉
63 | S m ⇒
64   match l1 with
65   [ nil ⇒ 〈copy l2, l2〉
66   | cons he1 tl1 ⇒
67      match l2 with
68      [ nil ⇒ 〈l1, copy l1〉
69      | cons he2 tl2 ⇒  
70          let base1 ≝ \fst he1 in
71          let base2 ≝ \fst he2 in
72          let height1 ≝ \snd he1 in
73          let height2 ≝ \snd he2 in
74          match q_cmp base1 base2 with
75          [ q_leq Hp1 ⇒ 
76              match q_cmp base2 base1 with
77              [ q_leq Hp2 ⇒
78                  let rc ≝ aux tl1 tl2 m in 
79                  〈he1 :: \fst rc,he2 :: \snd rc〉
80              | q_gt Hp ⇒ 
81                  let rest ≝ base2 - base1 in
82                  let rc ≝ aux tl1 (〈rest,height2〉 :: tl2) m in
83                  〈〈base1,height1〉 :: \fst rc,〈base1,height2〉 :: \snd rc〉] 
84          | q_gt Hp ⇒ 
85              let rest ≝ base1 - base2 in
86              let rc ≝ aux (〈rest,height1〉 :: tl1) tl2 m in
87              〈〈base2,height1〉 :: \fst rc,〈base2,height2〉 :: \snd rc〉]]]]
88 in aux : ∀l1,l2,m.∃z.\len l1 + \len l2 ≤ m → rebase_spec_aux l1 l2 z);
89 [7: clearbody aux; cases (aux b1 b2 (\len b1 + \len b2)) (res Hres);
90     exists; [split; constructor 1; [apply (\fst res)|5:apply (\snd res)]]
91     [1,4: apply hide; cases (Hres (le_n ?) Hs1 He1 Hs2 He2); assumption;
92     |2,5: apply hide; cases (Hres (le_n ?) Hs1 He1 Hs2 He2); clear Hres aux;
93           lapply (H3 O) as K; clear H1 H2 H3 H4 H5; unfold nth_base; 
94           cases H in K He1 He2 Hb1 Hb2; simplify; intros; assumption;
95     |3,6: apply hide; cases (Hres (le_n ?) Hs1 He1 Hs2 He2); clear Hres aux;
96           cases H in He1 He2; simplify; intros;
97           [1,6,8,12: assumption;
98           |2,7: rewrite > len_copy; generalize in match (\len ?); intro X;
99                 cases X; [1,3: reflexivity] simplify;
100                 [apply (copy_OQ ys n);|apply (copy_OQ xs n);]
101           |3,4: rewrite < H6; assumption;
102           |5: cases r1 in H6; simplify; intros; [reflexivity] rewrite < H6; assumption;
103           |9,11: rewrite < H7; assumption;
104           |10: cases r2 in H7; simplify; intros; [reflexivity] rewrite < H7; assumption]]
105     split; cases (Hres (le_n ?) Hs1 He1 Hs2 He2); clear Hres; unfold same_values; intros; 
106     [1: assumption
107     |2,3: simplify in match (\snd 〈?,?〉); simplify in match (\fst 〈?,?〉);
108         apply same_values_simpl_to_same_values; assumption]
109 |3: cut (\fst b3 = \fst b) as E; [2: apply q_le_to_le_to_eq; assumption]
110     clear H6 H5 H4 H3 He2 Hb2 Hs2 b2 He1 Hb1 Hs1 b1; cases (aux l2 l3 n1) (rc Hrc); 
111     clear aux; intro K; simplify in K; rewrite <plus_n_Sm in K; 
112     lapply le_S_S_to_le to K as W; lapply lt_to_le to W as R; 
113     simplify in match (? ≪rc,Hrc≫); intros (Hsbl2 Hendbl2 Hsb3l3 Hendb3l3);
114     change in Hendbl2 with (\snd (\last ▭ (b::l2)) = 〈OQ,OQ〉);
115     change in Hendb3l3 with (\snd (\last ▭ (b3::l3)) = 〈OQ,OQ〉);
116     cases (Hrc R) (RC S1 S2 SB SV1 SV2); clear Hrc R W K; 
117       [2,4: apply (sorted_tail q2_lt);[apply b|3:apply b3]assumption;
118       |3: cases l2 in Hendbl2; simplify; intros; [reflexivity] assumption; 
119       |5: cases l3 in Hendb3l3; simplify; intros; [reflexivity] assumption;]
120     constructor 1; simplify in match (\fst 〈?,?〉); simplify in match (\snd 〈?,?〉);
121     [1: cases b in E Hsbl2 Hendbl2; cases b3 in Hsb3l3 Hendb3l3; intros (Hsbl3 Hendbl2 E Hsb3l2 Hendb3l3); 
122         simplify in E; destruct E; constructor 3;
123         [1: clear Hendbl2 Hsbl3 SV2 SB S2;
124             cases RC in S1 SV1 Hsb3l2 Hendb3l3; intros; 
125             [1,6: reflexivity;
126             |3,4: assumption;
127             |5: simplify in H6:(??%) ⊢ %; rewrite > H3; cases r1 in H6; intros [2:reflexivity]
128                 use same_values_unit_OQ; 
129
130             |2: simplify in H3:(??%) ⊢ %; rewrite > H3; rewrite > len_copy; elim (\len ys); [reflexivity]
131           symmetry; apply (copy_OQ ys n2);
132         | cases H8 in H5 H7; simplify; intros; [2,6:reflexivity|3,4,5: assumption] 
133           simplify; rewrite > H5; rewrite > len_copy; elim (\len xs); [reflexivity]
134           symmetry; apply (copy_OQ xs n2);]
135     |2: apply (aux_preserves_sorting ? b3 ??? H8); assumption;
136     |3: apply (aux_preserves_sorting2 ? b3 ??? H8); try assumption;
137         try reflexivity; cases (inversion_sorted ?? H4);[2:rewrite >H3; apply (sorted_one q2_lt);]
138         cases l2 in H3 H4; intros; [apply (sorted_one q2_lt)]
139         apply (sorted_cons q2_lt);[2:apply (sorted_tail q2_lt ?? H3);] whd;
140         rewrite > E; assumption;
141     |4: apply I 
142     |5: apply I
143     |6: intro; elim i; intros; simplify; solve [symmetry;assumption|apply H13]
144     |7: unfold; intros; clear H9 H10 H11 H12 H13; simplify in Hi1 Hi2 H16 H18;
145        cases H8 in H14 H15 H17 H3 H16 H18 H5 H6; 
146        simplify in match (\fst 〈?,?〉); simplify in match (\snd 〈?,?〉); intros;
147        [1: reflexivity;
148        |2: simplify in H3; rewrite > (value_unit b); rewrite > H3; symmetry;
149            cases b in H3 H12 Hi2; intros 2; simplify in H12; rewrite > H12;
150            intros; change in ⊢ (? ? (? % ? ? ? ?) ?) with (copy (〈q,〈OQ,OQ〉〉::〈b1,〈OQ,OQ〉〉::ys));
151            apply (value_copy (〈q,〈OQ,OQ〉〉::〈b1,〈OQ,OQ〉〉::ys));
152        |3: apply (same_value_tail b b1 h1 h3 xs r1 input); assumption;
153        |4: apply (same_value_tail b b1 h1 h1 xs r1 input); assumption;
154        |5: simplify in H9; STOP
155              
156        |6: reflexivity;]
157                           
158             ]
159     |8: 
160     
161
162 include "Q/q/qtimes.ma".
163
164 let rec area (l:list bar) on l ≝
165   match l with 
166   [ nil ⇒ OQ
167   | cons he tl ⇒ area tl + Qpos (\fst he) * ⅆ[OQ,\snd he]].
168
169 alias symbol "pi1" = "exT \fst".
170 alias symbol "minus" = "Q minus".
171 alias symbol "exists" = "CProp exists".
172 definition minus_spec_bar ≝
173  λf,g,h:list bar.
174    same_bases f g → len f = len g →
175      ∀s,i:ℚ. \snd (\fst (value (mk_q_f s h) i)) = 
176        \snd (\fst (value (mk_q_f s f) i)) - \snd (\fst (value (mk_q_f s g) i)). 
177
178 definition minus_spec ≝
179  λf,g:q_f.
180    ∃h:q_f. 
181      ∀i:ℚ. \snd (\fst (value h i)) = 
182        \snd (\fst (value f i)) - \snd (\fst (value g i)). 
183
184 definition eject_bar : ∀P:list bar → CProp.(∃l:list bar.P l) → list bar ≝
185  λP.λp.match p with [ex_introT x _ ⇒ x].
186 definition inject_bar ≝ ex_introT (list bar).
187
188 coercion inject_bar with 0 1 nocomposites.
189 coercion eject_bar with 0 0 nocomposites.
190
191 lemma minus_q_f : ∀f,g. minus_spec f g.
192 intros;
193 letin aux ≝ (
194   let rec aux (l1, l2 : list bar) on l1 ≝
195     match l1 with
196     [ nil ⇒ []
197     | cons he1 tl1 ⇒
198         match l2 with
199         [ nil ⇒ []
200         | cons he2 tl2 ⇒ 〈\fst he1, \snd he1 - \snd he2〉 :: aux tl1 tl2]]
201   in aux : ∀l1,l2 : list bar.∃h.minus_spec_bar l1 l2 h);
202 [2: intros 4; simplify in H3; destruct H3;
203 |3: intros 4; simplify in H3; cases l1 in H2; [2: intro X; simplify in X; destruct X]    
204     intros; rewrite > (value_OQ_e (mk_q_f s []) i); [2: reflexivity]
205     rewrite > q_elim_minus; rewrite > q_plus_OQ; reflexivity;
206 |1: cases (aux l2 l3); unfold in H2; intros 4;
207     simplify in ⊢ (? ? (? ? ? (? ? ? (? % ?))) ?);
208     cases (q_cmp i (s + Qpos (\fst b)));
209     
210
211
212 definition excess ≝ 
213   λf,g.∃i.\snd (\fst (value f i)) < \snd (\fst (value g i)).
214