(* *)
(**************************************************************************)
-include "nat_ordered_set.ma".
+include "dama/nat_ordered_set.ma".
include "models/q_support.ma".
-include "models/list_support.ma".
-include "cprop_connectives.ma".
+include "models/list_support.ma".
+include "logic/cprop_connectives.ma".
-definition bar ≝ ratio × ℚ. (* base (Qpos) , height *)
-record q_f : Type ≝ { start : ℚ; bars: list bar }.
+definition bar ≝ ℚ × (ℚ × ℚ).
notation < "\rationals \sup 2" non associative with precedence 90 for @{'q2}.
interpretation "Q x Q" 'q2 = (Prod Q Q).
-definition empty_bar : bar ≝ 〈one,OQ〉.
+definition empty_bar : bar ≝ 〈Qpos one,〈OQ,OQ〉〉.
notation "\rect" with precedence 90 for @{'empty_bar}.
interpretation "q0" 'empty_bar = empty_bar.
notation < "\ldots\rect\square\EmptySmallSquare\ldots" with precedence 90 for @{'lq2}.
-interpretation "lq2" 'lq2 = (list bar).
-
-let rec sum_bases (l:list bar) (i:nat) on i ≝
- match i with
- [ O ⇒ OQ
- | S m ⇒
- match l with
- [ nil ⇒ sum_bases [] m + Qpos one
- | cons x tl ⇒ sum_bases tl m + Qpos (\fst x)]].
-
-axiom sum_bases_empty_nat_of_q_ge_OQ:
- ∀q:ℚ.OQ ≤ sum_bases [] (nat_of_q q).
-axiom sum_bases_empty_nat_of_q_le_q:
- ∀q:ℚ.sum_bases [] (nat_of_q q) ≤ q.
-axiom sum_bases_empty_nat_of_q_le_q_one:
- ∀q:ℚ.q < sum_bases [] (nat_of_q q) + Qpos one.
-
-lemma sum_bases_ge_OQ:
- ∀l,n. OQ ≤ sum_bases l n.
-intro; elim l; simplify; intros;
-[1: elim n; [apply q_eq_to_le;reflexivity] simplify;
- apply q_le_plus_trans; try assumption; apply q_lt_to_le; apply q_pos_lt_OQ;
-|2: cases n; [apply q_eq_to_le;reflexivity] simplify;
- apply q_le_plus_trans; [apply H| apply q_lt_to_le; apply q_pos_lt_OQ;]]
-qed.
+interpretation "lq2" 'lq2 = (list bar).
-alias symbol "leq" = "Q less or equal than".
-lemma sum_bases_O:
- ∀l.∀x.sum_bases l x ≤ OQ → x = O.
-intros; cases x in H; [intros; reflexivity] intro; cases (?:False);
-cases (q_le_cases ?? H);
-[1: apply (q_lt_corefl OQ); rewrite < H1 in ⊢ (?? %);
-|2: apply (q_lt_antisym ??? H1);] clear H H1; cases l;
-simplify; apply q_lt_plus_trans;
-try apply q_pos_lt_OQ;
-try apply (sum_bases_ge_OQ []);
-apply (sum_bases_ge_OQ l1);
-qed.
+definition q2_lt := mk_rel bar (λx,y:bar.\fst x < \fst y).
+interpretation "bar lt" 'lt x y = (rel_op ? q2_lt x y).
-lemma sum_bases_increasing:
- ∀l.∀n1,n2:nat.n1<n2→sum_bases l n1 < sum_bases l n2.
-intro; elim l 0;
-[1: intros 2; apply (cic:/matita/dama/nat_ordered_set/nat_elim2.con ???? n1 n2);
- [1: intro; cases n;
- [1: intro X; cases (not_le_Sn_O ? X);
- |2: simplify; intros; apply q_lt_plus_trans;
- [1: apply sum_bases_ge_OQ;|2: apply (q_pos_lt_OQ one)]]
- |2: simplify; intros; cases (not_le_Sn_O ? H);
- |3: simplify; intros; apply q_lt_inj_plus_r;
- apply H; apply le_S_S_to_le; apply H1;]
-|2: intros 5; apply (cic:/matita/dama/nat_ordered_set/nat_elim2.con ???? n1 n2);
- [1: simplify; intros; cases n in H1; intros;
- [1: cases (not_le_Sn_O ? H1);
- |2: simplify; apply q_lt_plus_trans;
- [1: apply sum_bases_ge_OQ;|2: apply q_pos_lt_OQ]]
- |2: simplify; intros; cases (not_le_Sn_O ? H1);
- |3: simplify; intros; apply q_lt_inj_plus_r; apply H;
- apply le_S_S_to_le; apply H2;]]
-qed.
+lemma q2_trans : ∀a,b,c:bar. a < b → b < c → a < c.
+intros 3; cases a; cases b; cases c; unfold q2_lt; simplify; intros;
+apply (q_lt_trans ??? H H1);
+qed.
+definition q2_trel := mk_trans_rel bar q2_lt q2_trans.
-definition eject1 ≝
- λP.λp:∃x:nat × ℚ.P x.match p with [ex_introT p _ ⇒ p].
-coercion eject1.
-definition inject1 ≝ λP.λp:nat × ℚ.λh:P p. ex_introT ? P p h.
-coercion inject1 with 0 1 nocomposites.
-
-definition value :
- ∀f:q_f.∀i:ℚ.∃p:nat × ℚ.
- Or4
- (And3 (i < start f) (\fst p = O) (\snd p = OQ))
- (And3
- (start f + sum_bases (bars f) (len (bars f)) ≤ i)
- (\fst p = O) (\snd p = OQ))
- (And3 (bars f = []) (\fst p = O) (\snd p = OQ))
- (And4
- (And3 (bars f ≠ []) (start f ≤ i) (i < start f + sum_bases (bars f) (len (bars f))))
- (\fst p ≤ (len (bars f)))
- (\snd p = \snd (nth (bars f) ▭ (\fst p)))
- (sum_bases (bars f) (\fst p) ≤ ⅆ[i,start f] ∧
- (ⅆ[i, start f] < sum_bases (bars f) (S (\fst p))))).
-intros;
-letin value ≝ (
- let rec value (p: ℚ) (l : list bar) on l ≝
- match l with
- [ nil ⇒ 〈nat_of_q p,OQ〉
- | cons x tl ⇒
- match q_cmp p (Qpos (\fst x)) with
- [ q_lt _ ⇒ 〈O, \snd x〉
- | _ ⇒
- let rc ≝ value (p - Qpos (\fst x)) tl in
- 〈S (\fst rc),\snd rc〉]]
- in value :
- ∀acc,l.∃p:nat × ℚ.OQ ≤ acc →
- Or
- (And3 (l = []) (\fst p = nat_of_q acc) (\snd p = OQ))
- (And3
- (sum_bases l (\fst p) ≤ acc)
- (acc < sum_bases l (S (\fst p)))
- (\snd p = \snd (nth l ▭ (\fst p)))));
-[5: clearbody value;
- cases (q_cmp i (start f));
- [2: exists [apply 〈O,OQ〉] simplify; constructor 1; split; try assumption;
- try reflexivity; apply q_lt_to_le; assumption;
- |1: cases (bars f); [exists [apply 〈O,OQ〉] simplify; constructor 3; split;try assumption;reflexivity;]
- cases (value ⅆ[i,start f] (b::l)) (p Hp);
- cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value; [cases H1; destruct H2]
- cases H1; clear H1; lapply (sum_bases_O (b::l) (\fst p)) as H1;
- [2: apply (q_le_trans ??? H2); rewrite > H; apply q_eq_to_le;
- rewrite > q_d_x_x; reflexivity;
- |1: exists [apply p] simplify; constructor 4; rewrite > H1; split;
- try split; try rewrite > q_d_x_x; try autobatch depth=2;
- [1: rewrite > H; rewrite > q_plus_sym; apply q_lt_plus;
- rewrite > q_plus_minus; apply q_lt_plus_trans; [apply sum_bases_ge_OQ]
- apply q_pos_lt_OQ;
- |2: rewrite > H; rewrite > q_d_x_x; apply q_eq_to_le; reflexivity;
- |3: rewrite > H; rewrite > q_d_x_x; apply q_lt_plus_trans;
- try apply sum_bases_ge_OQ; apply q_pos_lt_OQ;]]
- |3: cases (q_cmp i (start f+sum_bases (bars f) (len (bars f))));
- [1: exists [apply 〈O,OQ〉] simplify; constructor 2; split; try assumption;
- try reflexivity; rewrite > H1; apply q_eq_to_le; reflexivity;
- |3: exists [apply 〈O,OQ〉] simplify; constructor 2; split; try assumption;
- try reflexivity; apply q_lt_to_le; assumption;
- |2: generalize in match (refl_eq ? (bars f): bars f = bars f);
- generalize in match (bars f) in ⊢ (??? % → %); intro X; cases X; clear X;
- intros;
- [1: exists [apply 〈O,OQ〉] simplify; constructor 3; split; reflexivity;
- |2: cases (value ⅆ[i,start f] (b::l)) (p Hp);
- cases (Hp (q_dist_ge_OQ ? ?)); clear Hp value; [cases H3;destruct H4]
- cases H3; clear H3;
- exists [apply p]; constructor 4; split; try split; try assumption;
- [1: intro X; destruct X;
- |2: apply q_lt_to_le; assumption;
- |3: rewrite < H2; assumption;
- |4: cases (cmp_nat (\fst p) (len (bars f)));
- [1:apply lt_to_le;rewrite <H2; assumption|rewrite > H3;rewrite < H2;apply le_n]
- cases (?:False); cases (\fst p) in H3 H4 H6; clear H5;
- [1: intros; apply (not_le_Sn_O ? H5);
- |2: rewrite > q_d_sym; rewrite > q_d_noabs; [2: apply q_lt_to_le; assumption]
- intros; lapply (q_lt_inj_plus_r ?? (Qopp (start f)) H1); clear H1;
- generalize in match Hletin;
- rewrite > (q_plus_sym (start f)); rewrite < q_plus_assoc;
- do 2 rewrite < q_elim_minus; rewrite > q_plus_minus;
- rewrite > q_plus_OQ; intro K; apply (q_lt_corefl (i-start f));
- apply (q_lt_le_trans ???? H3); rewrite < H2;
- apply (q_lt_trans ??? K); apply sum_bases_increasing;
- assumption;]]]]]
-|1,3: intros; right; split;
- [1,4: clear H2; cases (value (q-Qpos (\fst b)) l1);
- cases (H2 (q_le_to_diff_ge_OQ ?? (? H1)));
- [1: intro; apply q_lt_to_le;assumption;
- |3: simplify; cases H4; apply q_le_minus; assumption;
- |2,5: simplify; cases H4; rewrite > H5; rewrite > H6;
- apply q_le_minus; apply sum_bases_empty_nat_of_q_le_q;
- |4: intro X; rewrite > X; apply q_eq_to_le; reflexivity;
- |*: simplify; apply q_le_minus; cases H4; assumption;]
- |2,5: cases (value (q-Qpos (\fst b)) l1);
- cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
- [1,4: intros; [apply q_lt_to_le|apply q_eq_to_le;symmetry] assumption;
- |3,6: cases H5; simplify; change with (q < sum_bases l1 (S (\fst w)) + Qpos (\fst b));
- apply q_lt_plus; assumption;
- |2,5: simplify; cases H5; rewrite > H6; simplify; rewrite > H7;
- apply q_lt_plus; apply sum_bases_empty_nat_of_q_le_q_one;]
- |*: cases (value (q-Qpos (\fst b)) l1); simplify;
- cases (H4 (q_le_to_diff_ge_OQ ?? (? H1)));
- [1,4: intros; [apply q_lt_to_le|apply q_eq_to_le;symmetry] assumption;
- |3,6: cases H5; assumption;
- |*: cases H5; rewrite > H6; rewrite > H8;
- elim (\fst w); [1,3:reflexivity;] simplify; assumption;]]
-|2: clear value H2; simplify; intros; right; split; [assumption|3:reflexivity]
- rewrite > q_plus_sym; rewrite > q_plus_OQ; assumption;
-|4: intros; left; split; reflexivity;]
-qed.
+interpretation "bar lt" 'lt x y = (FunClass_2_OF_trans_rel q2_trel x y).
+
+definition canonical_q_lt : rel bar → trans_rel ≝ λx:rel bar.q2_trel.
+
+coercion canonical_q_lt with nocomposites.
+
+interpretation "bar lt" 'lt x y = (FunClass_2_OF_trans_rel (canonical_q_lt ?) x y).
+
+definition nth_base ≝ λf,n. \fst (\nth f ▭ n).
+definition nth_height ≝ λf,n. \snd (\nth f ▭ n).
+
+record q_f : Type ≝ {
+ bars: list bar;
+ bars_sorted : sorted q2_lt bars;
+ bars_begin_OQ : nth_base bars O = OQ;
+ bars_end_OQ : nth_height bars (pred (\len bars)) = 〈OQ,OQ〉
+}.
-lemma value_OQ_l:
- ∀l,i.i < start l → \snd (\fst (value l i)) = OQ.
-intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
-try assumption; cases H2; cases (?:False); apply (q_lt_le_incompat ?? H H6);
+lemma len_bases_gt_O: ∀f.O < \len (bars f).
+intros; generalize in match (bars_begin_OQ f); cases (bars f); intros;
+[2: simplify; apply le_S_S; apply le_O_n;
+|1: normalize in H; destruct H;]
+qed.
+
+lemma all_bases_positive : ∀f:q_f.∀i. OQ < nth_base (bars f) (S i).
+intro f; generalize in match (bars_begin_OQ f); generalize in match (bars_sorted f);
+cases (len_gt_non_empty ?? (len_bases_gt_O f)); intros;
+cases (cmp_nat (\len l) i);
+[2: lapply (sorted_tail_bigger q2_lt ?? ▭ H ? H2) as K;
+ simplify in H1; rewrite < H1; apply K;
+|1: simplify; elim l in i H2;[simplify; rewrite > nth_nil; apply (q_pos_OQ one)]
+ cases n in H3; intros; [simplify in H3; cases (not_le_Sn_O ? H3)]
+ apply (H2 n1); simplify in H3; apply (le_S_S_to_le ?? H3);]
qed.
-
-lemma value_OQ_r:
- ∀l,i.start l + sum_bases (bars l) (len (bars l)) ≤ i → \snd (\fst (value l i)) = OQ.
-intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
-try assumption; cases H2; cases (?:False); apply (q_lt_le_incompat ?? H7 H);
+
+alias symbol "lt" (instance 9) = "Q less than".
+alias symbol "lt" (instance 7) = "natural 'less than'".
+alias symbol "lt" (instance 6) = "natural 'less than'".
+alias symbol "lt" (instance 5) = "Q less than".
+alias symbol "lt" (instance 4) = "natural 'less than'".
+alias symbol "lt" (instance 2) = "natural 'less than'".
+alias symbol "leq" = "Q less or equal than".
+coinductive value_spec (f : list bar) (i : ℚ) : ℚ × ℚ → CProp ≝
+| value_of : ∀j,q.
+ nth_height f j = q → nth_base f j < i → j < \len f →
+ (∀n.n<j → nth_base f n < i) →
+ (∀n.j < n → n < \len f → i ≤ nth_base f n) → value_spec f i q.
+
+definition match_pred ≝
+ λi.λx:bar.match q_cmp (Qpos i) (\fst x) with[ q_leq _ ⇒ true| q_gt _ ⇒ false].
+
+definition match_domain ≝
+ λf: list bar.λi:ratio. pred (find ? (match_pred i) f ▭).
+
+definition value_simple ≝
+ λf: list bar.λi:ratio. nth_height f (match_domain f i).
+
+alias symbol "lt" (instance 5) = "Q less than".
+alias symbol "lt" (instance 6) = "natural 'less than'".
+definition value_lemma :
+ ∀f:list bar.sorted q2_lt f → O < length bar f →
+ ∀i:ratio.nth_base f O < Qpos i → ∃p:ℚ×ℚ.value_spec f (Qpos i) p.
+intros (f bars_sorted_f len_bases_gt_O_f i bars_begin_OQ_f);
+exists [apply (value_simple f i);]
+apply (value_of ?? (match_domain f i));
+[1: reflexivity
+|2: unfold match_domain; cases (cases_find bar (match_pred i) f ▭);
+ [1: cases i1 in H H1 H2 H3; simplify; intros;
+ [1: generalize in match (bars_begin_OQ_f);
+ cases (len_gt_non_empty ?? (len_bases_gt_O_f)); simplify; intros;
+ assumption;
+ |2: cases (len_gt_non_empty ?? (len_bases_gt_O_f)) in H3;
+ intros; lapply (H3 n (le_n ?)) as K; unfold match_pred in K;
+ cases (q_cmp (Qpos i) (\fst (\nth (x::l) ▭ n))) in K;
+ simplify; intros; [destruct H5] assumption]
+ |2: destruct H; cases (len_gt_non_empty ?? (len_bases_gt_O_f)) in H2;
+ simplify; intros; lapply (H (\len l) (le_n ?)) as K; clear H;
+ unfold match_pred in K; cases (q_cmp (Qpos i) (\fst (\nth (x::l) ▭ (\len l)))) in K;
+ simplify; intros; [destruct H2] assumption;]
+|5: intro; unfold match_domain; cases (cases_find bar (match_pred i) f ▭); intros;
+ [1: generalize in match (bars_sorted_f);
+ cases (list_break ??? H) in H1; rewrite > H6;
+ rewrite < H1; simplify; rewrite > nth_len; unfold match_pred;
+ cases (q_cmp (Qpos i) (\fst x)); simplify;
+ intros (X Hs); [2: destruct X] clear X;
+ cases (sorted_pivot q2_lt ??? ▭ Hs);
+ cut (\len l1 ≤ n) as Hn; [2:
+ rewrite > H1; cases i1 in H4; simplify; intro X; [2: assumption]
+ apply lt_to_le; assumption;]
+ unfold nth_base; rewrite > (nth_append_ge_len ????? Hn);
+ cut (n - \len l1 < \len (x::l2)) as K; [2:
+ simplify; rewrite > H1; rewrite > (?:\len l2 = \len f - \len (l1 @ [x]));[2:
+ rewrite > H6; repeat rewrite > len_append; simplify;
+ repeat rewrite < plus_n_Sm; rewrite < plus_n_O; simplify;
+ rewrite > sym_plus; rewrite < minus_plus_m_m; reflexivity;]
+ rewrite > len_append; rewrite > H1; simplify; rewrite < plus_n_SO;
+ apply le_S_S; clear H1 H6 H7 Hs H8 H9 Hn x l2 l1 H4 H3 H2 H;
+ elim (\len f) in i1 n H5; [cases (not_le_Sn_O ? H);]
+ simplify; cases n2; [ repeat rewrite < minus_n_O; apply le_S_S_to_le; assumption]
+ cases n1 in H1; [intros; rewrite > eq_minus_n_m_O; apply le_O_n]
+ intros; simplify; apply H; apply le_S_S_to_le; assumption;]
+ cases (n - \len l1) in K; simplify; intros; [ assumption]
+ lapply (H9 ? (le_S_S_to_le ?? H10)) as W; apply (q_le_trans ??? H7);
+ apply q_lt_to_le; apply W;
+ |2: cases (not_le_Sn_n i1); rewrite > H in ⊢ (??%);
+ apply (trans_le ??? ? H4); cases i1 in H3; intros; apply le_S_S;
+ [ apply le_O_n; | assumption]]
+|3: unfold match_domain; cases (cases_find bar (match_pred i) f ▭); [
+ cases i1 in H; intros; simplify; [assumption]
+ apply lt_S_to_lt; assumption;]
+ rewrite > H; cases (\len f) in len_bases_gt_O_f; intros; [cases (not_le_Sn_O ? H3)]
+ simplify; apply le_n;
+|4: intros; unfold match_domain in H; cases (cases_find bar (match_pred i) f ▭) in H; simplify; intros;
+ [1: lapply (H3 n); [2: cases i1 in H4; intros [assumption] apply le_S; assumption;]
+ unfold match_pred in Hletin; cases (q_cmp (Qpos i) (\fst (\nth f ▭ n))) in Hletin;
+ simplify; intros; [destruct H6] assumption;
+ |2: destruct H; cases f in len_bases_gt_O_f H2 H3; clear H1; simplify; intros;
+ [cases (not_le_Sn_O ? H)] lapply (H1 n); [2: apply le_S; assumption]
+ unfold match_pred in Hletin; cases (q_cmp (Qpos i) (\fst (\nth (b::l) ▭ n))) in Hletin;
+ simplify; intros; [destruct H4] assumption;]]
+qed.
+
+lemma bars_begin_lt_Qpos : ∀q,r. nth_base (bars q) O<Qpos r.
+intros; rewrite > bars_begin_OQ; apply q_pos_OQ;
qed.
-
-lemma value_OQ_e:
- ∀l,i.bars l = [] → \snd (\fst (value l i)) = OQ.
-intros; cases (value l i) (q Hq); cases Hq; clear Hq; simplify; cases H1; clear H1;
-try assumption; cases H2; cases (?:False); apply (H1 H);
+
+lemma value : q_f → ratio → ℚ × ℚ.
+intros; cases (value_lemma (bars q) ?? r);
+[ apply bars_sorted.
+| apply len_bases_gt_O;
+| apply w;
+| apply bars_begin_lt_Qpos;]
qed.
-
-lemma value_ok:
- ∀f,i. bars f ≠ [] → start f ≤ i → i < start f + sum_bases (bars f) (len (bars f)) →
- And4
- (\fst (\fst (value f i)) ≤ (len (bars f)))
- (\snd (\fst (value f i)) = \snd (nth (bars f) ▭ (\fst (\fst (value f i)))))
- (sum_bases (bars f) (\fst (\fst (value f i))) ≤ ⅆ[i,start f])
- (ⅆ[i, start f] < sum_bases (bars f) (S (\fst (\fst (value f i))))).
-intros; cases (value f i); cases H3; simplify; clear H3; cases H4;
-[1,2,3: cases (?:False);
- [1: apply (q_lt_le_incompat ?? H3 H1);
- |2: apply (q_lt_le_incompat ?? H2 H3);
- |3: apply (H H3);]
-|4: split; cases H7; try assumption;]
+
+lemma cases_value : ∀f,i. value_spec (bars f) (Qpos i) (value f i).
+intros; unfold value;
+cases (value_lemma (bars f) (bars_sorted f) (len_bases_gt_O f) i (bars_begin_lt_Qpos f i));
+assumption;
qed.
-
-definition same_values ≝
- λl1,l2:q_f.
- ∀input.\snd (\fst (value l1 input)) = \snd (\fst (value l2 input)).
-definition same_bases ≝
- λl1,l2:q_f.
- (∀i.\fst (nth (bars l1) ▭ i) = \fst (nth (bars l2) ▭ i)).
+definition same_values ≝ λl1,l2:q_f.∀input. value l1 input = value l2 input.
+
+definition same_bases ≝ λl1,l2:list bar. ∀i.\fst (\nth l1 ▭ i) = \fst (\nth l2 ▭ i).
+
+lemma same_bases_cons: ∀a,b,l1,l2.
+ same_bases l1 l2 → \fst a = \fst b → same_bases (a::l1) (b::l2).
+intros; intro; cases i; simplify; [assumption;] apply (H n);
+qed.
alias symbol "lt" = "Q less than".
lemma unpos: ∀x:ℚ.OQ < x → ∃r:ratio.Qpos r = x.
intro; cases x; intros; [2:exists [apply r] reflexivity]
cases (?:False);
-[ apply (q_lt_corefl ? H)|apply (q_neg_gt ? H)]
+[ apply (q_lt_corefl ? H)| cases (q_lt_le_incompat ?? (q_neg_gt ?) (q_lt_to_le ?? H))]
qed.
+
+notation < "x \blacksquare" non associative with precedence 50 for @{'unpos $x}.
+interpretation "hide unpos proof" 'unpos x = (unpos x ?).
+