1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "nat_ordered_set.ma".
16 include "models/q_support.ma".
17 include "models/list_support.ma".
18 include "cprop_connectives.ma".
20 definition bar ≝ ℚ × ℚ.
22 notation < "\rationals \sup 2" non associative with precedence 90 for @{'q2}.
23 interpretation "Q x Q" 'q2 = (Prod Q Q).
25 definition empty_bar : bar ≝ 〈Qpos one,OQ〉.
26 notation "\rect" with precedence 90 for @{'empty_bar}.
27 interpretation "q0" 'empty_bar = empty_bar.
29 notation < "\ldots\rect\square\EmptySmallSquare\ldots" with precedence 90 for @{'lq2}.
30 interpretation "lq2" 'lq2 = (list bar).
32 definition q2_lt := mk_rel bar (λx,y:bar.\fst x < \fst y).
34 interpretation "bar lt" 'lt x y = (rel_op _ q2_lt x y).
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);
41 definition q2_trel := mk_trans_rel bar q2_lt q2_trans.
43 interpretation "bar lt" 'lt x y = (FunClass_2_OF_trans_rel q2_trel x y).
45 definition canonical_q_lt : rel bar → trans_rel ≝ λx:rel bar.q2_trel.
47 coercion canonical_q_lt with nocomposites.
49 interpretation "bar lt" 'lt x y = (FunClass_2_OF_trans_rel (canonical_q_lt _) x y).
51 definition nth_base ≝ λf,n. \fst (\nth f ▭ n).
52 definition nth_height ≝ λf,n. \snd (\nth f ▭ n).
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
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;]
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);]
79 lemma lt_n_plus_n_Sm : ∀n,m:nat.n < n + S m.
80 intros; rewrite > sym_plus; apply (le_S_S n (m+n)); apply (le_plus_n m n); qed.
84 lemma all_bigger_can_concat_bigger:
86 (∀i.i< len l1 → nth_base l1 i < \fst b) →
87 (∀i.i< len l2 → \fst b ≤ nth_base l2 i) →
88 (∀i.i< len l1 → start ≤ i → x ≤ nth_base l1 i) →
89 start ≤ n → n < len (l1@b::l2) → x ≤ \fst b → x ≤ nth_base (l1@b::l2) n.
90 intros; cases (cmp_nat n (len l1));
91 [1: unfold nth_base; rewrite > (nth_concat_lt_len ????? H6);
92 apply (H2 n); assumption;
93 |2: rewrite > H6; unfold nth_base; rewrite > nth_len; assumption;
94 |3: unfold nth_base; rewrite > nth_concat_ge_len; [2: apply lt_to_le; assumption]
95 rewrite > len_concat in H4; simplify in H4; rewrite < plus_n_Sm in H4;
96 lapply linear le_S_S_to_le to H4 as K; rewrite > sym_plus in K;
97 lapply linear le_plus_to_minus to K as X;
98 generalize in match X; generalize in match (n - len l1); intro W; cases W; clear W X;
99 [intros; assumption] intros;
100 apply (q_le_trans ??? H5); apply (H1 n1); assumption;]
105 inductive value_spec (f : q_f) (i : ℚ) : ℚ → nat → CProp ≝
107 nth_height (bars f) j = q →
108 nth_base (bars f) j < i →
109 (∀n.j < n → n < \len (bars f) → i ≤ nth_base (bars f) n) → value_spec f i q j.
112 definition value : ∀f:q_f.∀i:ratio.∃p:ℚ.∃j.value_spec f (Qpos i) p j.
113 intros; letin P ≝ (λx:bar.match q_cmp (Qpos i) (\fst x) with
116 exists [apply (nth_height (bars f) (pred (find ? P (bars f) ▭)));]
117 exists [apply (pred (find ? P (bars f) ▭))] apply value_of;
119 |2: cases (cases_find bar P (bars f) ▭);
120 [1: cases i1 in H H1 H2 H3; simplify; intros;
121 [1: generalize in match (bars_begin_OQ f);
122 cases (len_gt_non_empty ?? (len_bases_gt_O f)); simplify; intros;
123 rewrite > H4; apply q_pos_OQ;
124 |2: cases (len_gt_non_empty ?? (len_bases_gt_O f)) in H3;
125 intros; lapply (H3 n (le_n ?)) as K; unfold P in K;
126 cases (q_cmp (Qpos i) (\fst (\nth (x::l) ▭ n))) in K;
127 simplify; intros; [destruct H5] assumption]
128 |2: destruct H; cases (len_gt_non_empty ?? (len_bases_gt_O f)) in H2;
129 simplify; intros; lapply (H (\len l) (le_n ?)) as K; clear H;
130 unfold P in K; cases (q_cmp (Qpos i) (\fst (\nth (x::l) ▭ (\len l)))) in K;
131 simplify; intros; [destruct H2] assumption;]
132 |3: intro; cases (cases_find bar P (bars f) ▭); intros;
135 generalize in match (bars_begin_OQ f); generalize in match (bars_sorted f);
136 generalize in match (bars_end_OQ f);
137 cases (len_gt_non_empty ?? (len_bases_gt_O f)); simplify;
142 alias symbol "pi2" = "pair pi2".
143 alias symbol "pi1" = "pair pi1".
144 alias symbol "lt" (instance 7) = "Q less than".
145 alias symbol "leq" = "Q less or equal than".
146 letin value_spec_aux ≝ (
149 (\snd q = nth_height f (\fst q))
150 (nth_base f (\fst q) < i)
151 (∀n.(\fst q) < n → n < len f → i ≤ nth_base f n));
152 alias symbol "lt" (instance 5) = "Q less than".
154 let rec value (acc: nat × ℚ) (l : list bar) on l : nat × ℚ ≝
158 match q_cmp (\fst x) (Qpos i) with
159 [ q_leq _ ⇒ value 〈S (\fst acc), \snd x〉 tl
163 ∀story. story @ l = bars f → S (\fst acc) = len story →
164 value_spec_aux story (Qpos i) acc →
165 value_spec_aux (story @ l) (Qpos i) p);
166 [4: clearbody value; unfold value_spec;
167 generalize in match (bars_begin_OQ f);
168 generalize in match (bars_sorted f);
169 cases (bars_not_nil f) in value; intros (value S); generalize in match (sorted_tail_bigger ?? S);
170 clear S; cases (value 〈O,\snd x〉 l) (p Hp); intros;
171 exists[apply (\snd p)];exists [apply (\fst p)] simplify;
172 cases (Hp [x] (refl_eq ??) (refl_eq ??) ?) (Hg HV);
173 [unfold; split; [apply le_n|reflexivity|rewrite > H; apply q_pos_OQ;]
174 intros; cases n in H2 H3; [intro X; cases (not_le_Sn_O ? X)]
175 intros; cases (not_le_Sn_O ? (le_S_S_to_le (S n1) O H3))]
176 split;[rewrite > HV; reflexivity] split; [assumption;]
177 intros; cases n in H4 H5; intros [cases (not_le_Sn_O ? H4)]
178 apply (H3 (S n1)); assumption;
179 |1: unfold value_spec_aux; clear value value_spec_aux H2; intros;
180 cases H4; clear H4; split;
181 [1: apply (trans_lt ??? H5); rewrite > len_concat; simplify; apply lt_n_plus_n_Sm;
182 |2: unfold nth_height; rewrite > nth_concat_lt_len;[2:assumption]assumption;
183 |3: unfold nth_base; rewrite > nth_concat_lt_len;[2:assumption]
184 apply (q_le_lt_trans ???? H7); apply q_le_n;
185 |4: intros; (*clear H6 H5 H4 H l;*) lapply (bars_sorted f) as HS;
186 apply (all_bigger_can_concat_bigger story l1 (S (\fst p)));[6:apply q_lt_to_le]try assumption;
187 [1: rewrite < H2 in HS; cases (sorted_pivot ??? HS); assumption
188 |2: rewrite < H2 in HS; cases (sorted_pivot ??? HS);
189 intros; apply q_lt_to_le; apply H11; assumption;
190 |3: intros; apply H8; assumption;]]
191 |3: intro; rewrite > append_nil; intros; assumption;
192 |2: intros; cases (value 〈S (\fst p),\snd b〉 l1); unfold; simplify;
193 cases (H6 (story@[b]) ???);
194 [1: rewrite > associative_append; apply H3;
195 |2: simplify; rewrite > H4; rewrite > len_concat; rewrite > sym_plus; reflexivity;
196 |4: rewrite < (associative_append ? story [b] l1); split; assumption;
197 |3: cases H5; clear H5; split; simplify in match (\snd ?); simplify in match (\fst ?);
198 [1: rewrite > len_concat; simplify; rewrite < plus_n_SO; apply le_S_S; assumption;
213 cases (q_cmp i (start f));
214 [2: exists [apply 〈O,OQ〉] simplify; constructor 1; split; try assumption;
215 try reflexivity; apply q_lt_to_le; assumption;
216 |1: cases (bars f); [exists [apply 〈O,OQ〉] simplify; constructor 3; split;try assumption;reflexivity;]
217 cases (value ⅆ[i,start f] (b::l)) (p Hp);
218 cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value; [cases H1; destruct H2]
219 cases H1; clear H1; lapply (sum_bases_O (b::l) (\fst p)) as H1;
220 [2: apply (q_le_trans ??? H2); rewrite > H; apply q_eq_to_le;
221 rewrite > q_d_x_x; reflexivity;
222 |1: exists [apply p] simplify; constructor 4; rewrite > H1; split;
223 try split; try rewrite > q_d_x_x; try autobatch depth=2;
224 [1: rewrite > H; rewrite > q_plus_sym; apply q_lt_plus;
225 rewrite > q_plus_minus; apply q_lt_plus_trans; [apply sum_bases_ge_OQ]
227 |2: rewrite > H; rewrite > q_d_x_x; apply q_eq_to_le; reflexivity;
228 |3: rewrite > H; rewrite > q_d_x_x; apply q_lt_plus_trans;
229 try apply sum_bases_ge_OQ; apply q_pos_lt_OQ;]]
230 |3: cases (q_cmp i (start f+sum_bases (bars f) (len (bars f))));
231 [1: exists [apply 〈O,OQ〉] simplify; constructor 2; split; try assumption;
232 try reflexivity; rewrite > H1; apply q_eq_to_le; reflexivity;
233 |3: exists [apply 〈O,OQ〉] simplify; constructor 2; split; try assumption;
234 try reflexivity; apply q_lt_to_le; assumption;
235 |2: generalize in match (refl_eq ? (bars f): bars f = bars f);
236 generalize in match (bars f) in ⊢ (??? % → %); intro X; cases X; clear X;
238 [1: exists [apply 〈O,OQ〉] simplify; constructor 3; split; reflexivity;
239 |2: cases (value ⅆ[i,start f] (b::l)) (p Hp);
240 cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value; [cases H3;destruct H4]
242 exists [apply p]; constructor 4; split; try split; try assumption;
243 [1: intro X; destruct X;
244 |2: apply q_lt_to_le; assumption;
245 |3: rewrite < H2; assumption;
246 |4: cases (cmp_nat (\fst p) (len (bars f)));
247 [1:apply lt_to_le;rewrite <H2; assumption|rewrite > H3;rewrite < H2;apply le_n]
248 cases (?:False); cases (\fst p) in H3 H4 H6; clear H5;
249 [1: intros; apply (not_le_Sn_O ? H5);
250 |2: rewrite > q_d_sym; rewrite > q_d_noabs; [2: apply q_lt_to_le; assumption]
251 intros; lapply (q_lt_inj_plus_r ?? (Qopp (start f)) H1); clear H1;
252 generalize in match Hletin;
253 rewrite > (q_plus_sym (start f)); rewrite < q_plus_assoc;
254 do 2 rewrite < q_elim_minus; rewrite > q_plus_minus;
255 rewrite > q_plus_OQ; intro K; apply (q_lt_corefl (i-start f));
256 apply (q_lt_le_trans ???? H3); rewrite < H2;
257 apply (q_lt_trans ??? K); apply sum_bases_increasing;
259 |1,3: intros; right; split;
260 [1,4: clear H2; cases (value (q-Qpos (\fst b)) l1);
261 cases (H2 (q_le_to_diff_ge_OQ ?? (? H1)));
262 [1: intro; apply q_lt_to_le;assumption;
263 |3: simplify; cases H4; apply q_le_minus; assumption;
264 |2,5: simplify; cases H4; rewrite > H5; rewrite > H6;
265 apply q_le_minus; apply sum_bases_empty_nat_of_q_le_q;
266 |4: intro X; rewrite > X; apply q_eq_to_le; reflexivity;
267 |*: simplify; apply q_le_minus; cases H4; assumption;]
268 |2,5: cases (value (q-Qpos (\fst b)) l1);
269 cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
270 [1,4: intros; [apply q_lt_to_le|apply q_eq_to_le;symmetry] assumption;
271 |3,6: cases H5; simplify; change with (q < sum_bases l1 (S (\fst w)) + Qpos (\fst b));
272 apply q_lt_plus; assumption;
273 |2,5: simplify; cases H5; rewrite > H6; simplify; rewrite > H7;
274 apply q_lt_plus; apply sum_bases_empty_nat_of_q_le_q_one;]
275 |*: cases (value (q-Qpos (\fst b)) l1); simplify;
276 cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
277 [1,4: intros; [apply q_lt_to_le|apply q_eq_to_le;symmetry] assumption;
278 |3,6: cases H5; assumption;
279 |*: cases H5; rewrite > H6; rewrite > H8;
280 elim (\fst w); [1,3:reflexivity;] simplify; assumption;]]
281 |2: clear value H2; simplify; intros; right; split; [assumption|3:reflexivity]
282 rewrite > q_plus_sym; rewrite > q_plus_OQ; assumption;
283 |4: intros; left; split; reflexivity;]
287 ∀l,i.i < start l → \snd (\fst (value l i)) = OQ.
288 intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
289 try assumption; cases H2; cases (?:False); apply (q_lt_le_incompat ?? H H6);
293 ∀l,i.start l + sum_bases (bars l) (len (bars l)) ≤ i → \snd (\fst (value l i)) = OQ.
294 intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
295 try assumption; cases H2; cases (?:False); apply (q_lt_le_incompat ?? H7 H);
299 ∀l,i.bars l = [] → \snd (\fst (value l i)) = OQ.
300 intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
301 try assumption; cases H2; cases (?:False); apply (H1 H);
304 inductive value_ok_spec (f : q_f) (i : ℚ) : nat × ℚ → Type ≝
305 | value_ok : ∀n,q. n ≤ (len (bars f)) →
306 q = \snd (nth (bars f) ▭ n) →
307 sum_bases (bars f) n ≤ ⅆ[i,start f] →
308 ⅆ[i, start f] < sum_bases (bars f) (S n) → value_ok_spec f i 〈n,q〉.
311 ∀f,i.bars f ≠ [] → start f ≤ i → i < start f + sum_bases (bars f) (len (bars f)) →
312 value_ok_spec f i (\fst (value f i)).
313 intros; cases (value f i); simplify;
314 cases H3; simplify; clear H3; cases H4; clear H4;
315 [1,2,3: cases (?:False);
316 [1: apply (q_lt_le_incompat ?? H3 H1);
317 |2: apply (q_lt_le_incompat ?? H2 H3);
319 |4: cases H7; clear H7; cases w in H3 H4 H5 H6 H8; simplify; intros;
320 constructor 1; assumption;]
323 definition same_values ≝
325 ∀input.\snd (\fst (value l1 input)) = \snd (\fst (value l2 input)).
327 definition same_bases ≝
328 λl1,l2:list bar. (∀i.\fst (nth l1 ▭ i) = \fst (nth l2 ▭ i)).
330 alias symbol "lt" = "Q less than".
331 lemma unpos: ∀x:ℚ.OQ < x → ∃r:ratio.Qpos r = x.
332 intro; cases x; intros; [2:exists [apply r] reflexivity]
334 [ apply (q_lt_corefl ? H)|apply (q_neg_gt ? H)]
337 notation < "x \blacksquare" non associative with precedence 50 for @{'unpos $x}.
338 interpretation "hide unpos proof" 'unpos x = (unpos x _).