Section SumList.
Variable A : Set.
Definition sumList(ls : list A)(f : A -> Rat) := fold_left (fun a b => a + (f b)) ls 0.
(* Facts about sumList *)
Abort.
Abort.
induction ls; intuition; simpl in *.
eapply eqRat_refl.
eapply eqRat_trans.
eapply IHls.
eapply eqRat_trans.
eapply ratAdd_assoc.
eapply ratAdd_eqRat_compat.
eapply eqRat_refl.
eapply eqRat_symm.
eapply IHls.
Qed.
induction ls; intuition; simpl in *.
eapply IHls; eauto.
eapply ratAdd_eqRat_compat; eauto.
Qed.
induction 1; intuition; simpl in *.
eapply IHPermutation; eauto.
eapply ratAdd_eqRat_compat; eauto.
eapply fold_add_body_eq.
eapply eqRat_trans.
eapply ratAdd_assoc.
eapply eqRat_trans.
2:{
eapply eqRat_symm.
eapply ratAdd_assoc.
}
eapply ratAdd_eqRat_compat; eauto.
eapply eqRat_trans.
eapply ratAdd_comm.
eapply ratAdd_eqRat_compat; eauto.
intuition.
eapply eqRat_trans; eauto.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply eqRat_symm; eauto.
intuition.
eapply eqRat_symm.
eapply H2.
eapply Permutation_in.
eapply Permutation_sym.
eauto.
eauto.
simpl.
eapply IHPermutation2.
trivial.
intuition.
eapply H2.
eapply Permutation_in.
eapply Permutation_sym.
eauto.
eauto.
Qed.
induction ls; intuition; simpl in *.
eapply IHls; eauto.
rewrite H0.
eapply ratAdd_eqRat_compat; eauto.
eapply eqRat_refl.
intuition.
Qed.
induction ls; intuition; simpl in *.
intuition.
unfold sumList.
simpl.
eapply eqRat_refl.
intuition; subst.
unfold sumList in *; simpl in *.
eapply eqRat_trans.
eapply ratAdd_0.
eapply eqRat_trans.
eapply ratAdd_comm.
eapply eqRat_trans.
eapply eqRat_symm.
eapply fold_add_init.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply ratAdd_comm.
intuition.
eapply eqRat_refl.
eapply H.
eapply eqRat_refl.
eapply IHls.
unfold sumList in *; simpl in *.
eapply ratAdd_0.
eapply eqRat_trans.
eapply eqRat_symm.
eapply fold_add_init.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply ratAdd_comm.
intuition.
eapply eqRat_refl.
eapply H.
trivial.
unfold sumList.
simpl.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply ratAdd_comm.
intuition.
eapply eqRat_refl.
eapply eqRat_trans.
eapply fold_add_init.
eapply ratAdd_0.
intuition.
unfold sumList in *.
eapply IHls.
intuition.
Qed.
induction ls; intuition; unfold sumList in *; simpl in *.
exfalso.
apply H.
eapply eqRat_refl.
destruct H.
intuition.
assert (f a + fold_left (fun r a => r + f a) ls 0 == 0 -> False).
intuition.
eapply H.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply ratAdd_comm.
intuition.
eapply eqRat_refl.
eapply eqRat_trans.
eapply fold_add_init.
trivial.
eapply ratAdd_nz in H0.
destruct H0.
econstructor; intuition.
apply IHls in H0.
destruct H0; intuition.
exists x.
intuition.
assert (f a + fold_left (fun r a => r + f a) ls 0 == 0).
eapply eqRat_trans.
eapply eqRat_symm.
eapply fold_add_init.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply ratAdd_comm.
intuition.
eapply eqRat_refl.
trivial.
apply ratAdd_0 in H1.
intuition.
destruct H.
intuition.
subst.
intuition.
apply IHls in H3; intuition.
econstructor; eauto.
Qed.
End SumList.
intuition.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
rewrite ratAdd_0_r.
apply eqRat_refl.
apply eqRat_refl.
apply fold_add_init.
Qed.
induction ls; intuition; simpl in *; intuition.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_r.
apply eqRat_refl.
apply eqRat_refl.
trivial.
Qed.
intuition.
eapply eqRat_trans.
eapply fold_add_body_eq.
apply eqRat_refl.
eapply H.
simpl.
eapply fold_add_eq_init.
Qed.
induction ls; intuition; simpl in *.
eapply eqRat_refl.
rewrite IHls.
eapply fold_add_body_eq.
repeat rewrite <- ratAdd_assoc.
apply ratAdd_eqRat_compat; intuition.
rewrite ratAdd_assoc.
rewrite ratAdd_assoc.
apply ratAdd_eqRat_compat; intuition.
eapply ratAdd_comm.
intuition.
Qed.
induction lsa; destruct lsb; intuition; simpl in *; intuition.
apply eqRat_symm.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_r.
apply eqRat_refl.
apply eqRat_refl.
apply fold_add_eq_init.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_r.
apply eqRat_refl.
apply eqRat_refl.
apply fold_add_eq_init.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_l.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_l.
apply eqRat_refl.
apply eqRat_refl.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_l.
apply eqRat_refl.
apply eqRat_refl.
simpl.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
eapply eqRat_refl.
eapply fold_add_init_0.
simpl.
eapply eqRat_trans.
eapply fold_add_init_0.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
apply eqRat_refl.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
apply ratAdd_0_r.
apply eqRat_refl.
apply eqRat_symm.
eapply fold_add_eq.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
apply eqRat_refl.
apply ratAdd_eqRat_compat.
apply eqRat_refl.
eapply IHlsa.
eapply eqRat_symm.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_l.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_l.
apply eqRat_refl.
apply eqRat_refl.
eapply fold_add_body_eq; intuition.
rewrite <- ratAdd_0_l.
apply eqRat_refl.
apply eqRat_refl.
simpl.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
eapply eqRat_refl.
eapply fold_add_init_0.
simpl.
eapply eqRat_trans.
eapply fold_add_init_0.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
apply eqRat_refl.
eapply eqRat_trans.
eapply fold_add_body_eq; intuition.
apply ratAdd_0_r.
apply eqRat_refl.
apply eqRat_symm.
eapply fold_add_eq.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
eapply fold_add_init_0.
eapply eqRat_refl.
eapply eqRat_symm.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
eapply fold_add_init_0.
eapply eqRat_refl.
repeat rewrite <- ratAdd_assoc.
eapply ratAdd_eqRat_compat.
repeat rewrite (ratAdd_assoc (f a b)).
eapply ratAdd_eqRat_compat; intuition.
eapply ratAdd_comm.
eapply eqRat_refl.
Qed.
intuition.
eapply fold_add_comm; intuition.
Qed.
intuition.
eapply fold_add_body_eq; intuition.
Qed.
induction ls; intuition; simpl in *.
intuition.
eapply eqRat_trans.
eapply fold_add_body_eq.
eapply eqRat_symm.
eapply ratMult_distrib_r.
intuition.
eapply eqRat_refl.
eapply IHls.
Qed.
intuition.
unfold sumList.
rewrite <- fold_add_factor_constant_r.
eapply fold_add_body_eq.
eapply eqRat_symm.
eapply ratMult_0_l.
intuition.
Qed.
intuition.
eapply eqRat_trans.
eapply sumList_body_eq; intuition.
apply ratMult_comm.
eapply eqRat_trans.
eapply sumList_factor_constant_r.
apply ratMult_comm.
Qed.
induction ls; intuition; simpl in *.
eapply eqRat_trans.
eapply ratAdd_0_l.
eapply ratAdd_eqRat_compat.
eapply eqRat_symm.
apply ratMult_0_r.
intuition.
rewrite IHls.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
apply eqRat_refl.
apply ratAdd_comm.
rewrite <- (ratAdd_assoc).
apply ratAdd_eqRat_compat; intuition.
eapply eqRat_symm.
eapply eqRat_trans.
eapply ratMult_eqRat_compat.
apply eqRat_refl.
apply ratS_num.
rewrite ratMult_distrib.
rewrite ratAdd_comm.
apply ratAdd_eqRat_compat; intuition.
apply ratMult_1_r.
Qed.
intuition.
eapply eqRat_trans.
apply fold_add_body_const.
rewrite <- ratAdd_0_r.
eapply eqRat_refl.
Qed.
induction ls; intuition; simpl in *;
intuition; subst.
assert (init + f a <= r).
eapply IHls.
apply H.
eapply ratAdd_any_le; eauto.
assert (init + f a0 <= r).
eapply IHls.
eauto.
eapply ratAdd_any_le.
eapply leRat_trans.
apply eqRat_impl_leRat.
apply ratAdd_comm.
eauto.
edestruct IHls; eauto.
Qed.
intuition.
eapply fold_add_iter_le; eauto.
Qed.
Fixpoint removeFirst(A : Set)(eqd : eq_dec A)(ls : list A) a :=
match ls with
| nil => nil
| a' :: ls' =>
if (eqd a a') then ls' else a' :: (removeFirst eqd ls' a)
end.
induction ls; intuition; simpl in *;
intuition; subst.
destruct (eqd a0 a0); try congruence.
eapply Permutation_refl.
destruct (eqd a0 a); subst.
eapply Permutation_refl.
eapply perm_trans; [idtac | eapply perm_swap].
eapply perm_skip.
eauto.
Qed.
induction ls; intuition; simpl in *.
destruct (eqd a2 a); subst.
intuition.
simpl in *.
intuition.
eapply IHls; eauto.
Qed.
induction ls; intuition; simpl in *.
inversion H; clear H; subst.
destruct(eqd a0 a); subst.
intuition.
simpl in *.
intuition; subst.
eapply IHls; eauto.
Qed.
induction ls; intuition; simpl in *.
inversion H; clear H; subst.
destruct (eqd a0 a); subst.
trivial.
econstructor.
eapply removeFirst_not_in.
intuition.
eapply IHls; eauto.
Qed.
induction ls; intuition; simpl in *.
destruct (eqd a2 a); subst.
intuition.
congruence.
simpl.
intuition.
Qed.
induction ls; intuition; simpl in *.
destruct (eqd a2 a); subst.
intuition.
simpl in *.
intuition.
right.
eapply IHls; eauto.
Qed.
(* ls1 is the smaller set.  This function returns a permutation of ls2 where the first elements of ls2 match the order of the elements in ls1.  This function only works if ls1 is a subset of ls2. *)
Fixpoint matchOrder (A : Set)(eqd : eq_dec A)(ls1 ls2 : list A) :=
match ls1 with
| nil => ls2
| a :: ls1' =>
a :: (matchOrder eqd ls1' (removeFirst eqd ls2 a))
end.
induction ls1; destruct ls2; intuition; simpl in *; eauto; intuition; subst.
inversion H; clear H; subst.
inversion H0; clear H0; subst.
destruct (eqd a a1); subst.
intuition.
right.
eapply (IHls1 (a1 :: removeFirst _ ls2 a)); intuition.
econstructor.
eapply removeFirst_not_in; intuition.
eapply removeFirst_NoDup; intuition.
simpl.
destruct (H1 a0); auto.
right.
eapply removeFirst_in; eauto.
intuition. subst.
intuition.
inversion H; clear H; subst.
inversion H0; clear H0; subst.
destruct (H1 a); intuition; subst.
destruct (eqd a a); try congruence.
right.
eapply (IHls1 ls2); intuition.
destruct (H1 a0); intuition; subst.
intuition.
destruct (eqd a a0); try congruence.
destruct (eqd a a1); subst; auto.
right.
eapply (IHls1 (a0 :: removeFirst _ ls2 a)); intuition.
econstructor.
eapply removeFirst_not_in; trivial.
eapply removeFirst_NoDup; trivial.
simpl.
destruct (H1 a2); eauto.
destruct (eqd a0 a2); intuition.
right.
eapply removeFirst_in; trivial.
intuition.
subst.
intuition.
simpl.
destruct (eqd a0 a1); intuition.
right.
eapply removeFirst_in; trivial.
intuition.
inversion H; clear H; subst.
inversion H0; clear H0; subst.
destruct (eqd a0 a1); subst; intuition.
right.
assert (In a1 (if eqd a a0 then ls2 else a0 :: removeFirst eqd ls2 a)).
eapply IHls1.
trivial.
destruct (eqd a a0).
trivial.
econstructor.
eapply removeFirst_not_in; eauto.
eapply removeFirst_NoDup; intuition.
intuition.
destruct (eqd a a0).
destruct (H1 a2).
intuition.
subst.
intuition.
trivial.
simpl.
destruct (eqd a0 a2); intuition.
right.
destruct (H1 a2).
destruct (eqd a a2); intuition.
subst.
intuition.
eapply removeFirst_in.
trivial.
intuition. subst.
intuition.
trivial.
destruct (eqd a a0); intuition.
simpl in *.
intuition.
eapply removeFirst_in_iff.
eauto.
Qed.
induction ls1; intuition; simpl in *.
intuition; subst.
eapply (IHls1 (removeFirst eqd ls2 a)).
eapply H3.
intuition.
eapply removeFirst_not_in.
intuition.
eapply H0.
eauto.
eapply H.
eauto.
Qed.
induction ls1; intuition; simpl in *.
destruct (eqd a a0); subst;
intuition.
eapply IHls1.
intuition.
eapply H0.
assert (~ In a0 ls1).
intuition.
specialize (@matchOrder_not_in_h _ eqd ls1 (removeFirst eqd ls2 a) a0); intuition.
exfalso.
eapply H4; eauto.
intuition.
eapply removeFirst_not_in.
intuition.
eapply H0.
eapply H5.
eapply H3.
Qed.
induction ls1; intuition; simpl in *.
inversion H1; clear H1; subst.
econstructor.
eapply matchOrder_not_in.
intuition.
destruct (eqd a a0); subst; intuition.
eapply removeFirst_in.
eapply H.
intuition.
intuition.
eapply removeFirst_NoDup_not_in; eauto.
eapply IHls1; intuition.
destruct (eqd a a0); subst; intuition.
eapply removeFirst_in; eauto.
eapply removeFirst_NoDup; eauto.
Qed.
intuition.
eapply NoDup_Permutation; intuition.
eapply matchOrder_NoDup; eauto.
eapply (@matchOrder_In _ eqd ls1 ls2); eauto.
eapply (@matchOrder_In _ eqd ls1 ls2); eauto.
Qed.
induction ls1; intuition; simpl in *.
inversion H; clear H; subst.
f_equal.
eapply IHls1; eauto.
eapply removeFirst_NoDup; eauto.
intuition.
destruct (eqd a0 a); subst; intuition.
eapply removeFirst_in.
eapply H1.
intuition.
intuition.
Qed.
induction ls; intuition; simpl in *.
destruct n; simpl in *;
trivial.
inversion H0; clear H0; subst.
destruct n; simpl in *.
eapply eqRat_symm.
eapply eqRat_trans.
eapply fold_add_eq_init_f; intuition.
rewrite (ratAdd_0_r init1).
eapply ratAdd_eqRat_compat; intuition.
eapply IHls.
eapply ratAdd_eqRat_compat; intuition.
trivial.
intuition.
eapply H1.
intuition.
intuition.
subst.
intuition.
Qed.
induction 1; intuition.
inversion H0; clear H0; subst.
econstructor.
intuition.
eapply H3.
eapply Permutation_in.
eapply Permutation_sym.
eapply H.
eauto.
eauto.
inversion H; clear H; subst.
inversion H3; clear H3; subst.
simpl in *.
econstructor.
simpl.
intuition.
econstructor;
intuition.
Qed.
intuition.
erewrite <- matchOrder_firstn at 1.
eapply eqRat_symm.
eapply eqRat_trans.
eapply fold_add_rat_perm.
eapply matchOrder_permutation.
eapply H0.
eauto.
intuition.
eapply eqRat_refl.
intuition.
eapply eqRat_refl.
eapply eqRat_symm.
eapply fold_add_matchOrder; intuition.
eapply permutation_NoDup.
eapply matchOrder_permutation; eauto.
eauto.
eapply H3.
intuition.
eapply H5.
rewrite matchOrder_firstn; eauto.
eauto.
eauto.
eauto.
Grab Existential Variables.
trivial.
Qed.
intuition.
eapply fold_add_subset; intuition.
Qed.
Fixpoint flatten(A : Type)(ls : list (list A)) :=
match ls with
| nil => nil
| a :: ls' => a ++ (flatten ls')
end.
induction ls; simpl in *; intuition.
destruct H; intuition.
apply in_app_or in H.
intuition.
exists a; intuition.
apply IHls in H0.
destruct H0.
intuition.
exists x; intuition.
apply in_or_app.
destruct H; intuition.
subst.
intuition.
right.
eapply IHls.
econstructor.
eauto.
Qed.
induction ls; destruct ls'; intuition; simpl in *.
intuition.
destruct H; simpl in *; subst; intuition.
destruct H; simpl in *; subst; intuition.
rewrite app_length.
simpl.
omega.
rewrite app_length.
specialize (IHls (a0 :: ls') H).
simpl in *.
intuition.
Qed.
Fixpoint getUnique(A : Set)(ls : list A)(pf : eq_dec A) : list A :=
match ls with
| nil => nil
| a :: ls' =>
let ls'' := (getUnique ls' pf) in
if (in_dec pf a ls'')
then ls''
else a :: ls''
end.
induction ls; simpl in *; intuition.
destruct (in_dec _ a (getUnique ls _)).
right.
eapply IHls.
eapply H.
simpl in *.
intuition.
right.
eapply IHls.
eapply H0.
Qed.
induction ls; simpl in *; intuition.
subst.
destruct (in_dec _ a0 (getUnique ls _)); simpl; auto.
destruct (in_dec _ a (getUnique ls _)); simpl.
eapply IHls. trivial.
right.
eapply IHls.
trivial.
Qed.
induction ls; intuition; simpl in *.
econstructor.
destruct (in_dec _ a (getUnique ls _)); eauto.
econstructor; eauto.
Qed.
induction ls; intuition; simpl in *.
destruct (in_dec eqd a (getUnique ls eqd)).
destruct ls; simpl in *; intuition.
simpl.
omega.
Qed.
Definition maxList(ls : list nat) : nat :=
fold_left max ls O.
induction ls; intuition; simpl in *.
eapply le_trans.
eapply Max.max_lub_l.
eapply le_refl.
eapply IHls.
Qed.
induction ls; intuition.
inversion H.
simpl in *.
intuition; subst.
eapply le_trans.
eapply Max.max_lub_r.
eapply le_refl.
eapply fold_left_max_ge_init.
Qed.
intuition.
eapply maxList_correct_h.
trivial.
Qed.
(* relational folds and other llist operations *)
Inductive pred_count(A : Type)(p : A -> Prop) : list A -> nat -> Prop :=
| pc_nil :
pred_count p nil 0
| pc_yes :
forall ls n a,
pred_count p ls n ->
p a ->
pred_count p (a :: ls) (S n)
| pc_no : forall ls n a,
pred_count p ls n ->
~p a ->
pred_count p (a :: ls) n.
induction ls; intuition; simpl in *.
inversion H; subst.
trivial.
inversion H; clear H; subst.
eapply le_n_S.
eapply IHls.
trivial.
econstructor.
eapply IHls.
trivial.
Qed.
(* listRepeat is used to show some interesting properties of rel_map and pred_count. *)
Fixpoint listRepeat(A : Type)(a : A) n :=
match n with
| 0 => nil
| S n' => a :: (listRepeat a n')
end.
induction n; intuition; simpl in *.
f_equal.
eauto.
Qed.
induction n; intuition; simpl in *.
inversion H. trivial.
inversion H; clear H; subst.
f_equal.
eapply IHn; eauto.
congruence.
Qed.
induction n; intuition; simpl in *.
inversion H0. trivial.
inversion H0; clear H0; subst.
intuition.
eapply IHn; eauto.
Qed.
induction ls; intuition.
inversion H. inversion H0. trivial.
inversion H; clear H; subst.
inversion H0; clear H0; subst.
f_equal.
eapply IHls; eauto.
intuition.
inversion H0; clear H0; subst.
intuition.
eapply IHls; eauto.
Qed.
induction 1; intuition; simpl in *.
f_equal.
eauto.
specialize (H1 a).
intuition.
Qed.
induction 1; intuition; simpl in *.
exists O. exists O.
intuition;
destruct n; simpl; econstructor.
destruct n0; simpl.
destruct (IHpred_count O).
destruct H1.
intuition. subst.
exists x.
exists (S x0).
intuition.
econstructor; trivial.
destruct (IHpred_count n0).
destruct H1.
destruct H1.
intuition. subst.
exists (S x).
exists x0.
intuition.
econstructor; trivial.
destruct n0; simpl.
destruct (IHpred_count O).
destruct H1.
intuition. subst.
exists x.
exists x0.
intuition.
econstructor; trivial.
destruct (IHpred_count n0).
destruct H1.
destruct H1.
intuition. subst.
exists x.
exists x0.
intuition.
econstructor; trivial.
Qed.
induction ls; intuition; simpl in *; subst.
econstructor.
econstructor; eauto.
Qed.
induction ls; intuition; simpl in *.
econstructor.
econstructor; eauto.
Qed.
induction n; intuition; simpl in *.
intuition.
intuition.
Qed.
induction ls1; intuition; simpl in *.
inversion H; subst; clear H.
simpl.
trivial.
inversion H; clear H; subst.
simpl.
econstructor; eauto.
econstructor; eauto.
Qed.
induction 1; intuition.
inversion H0; clear H0; subst.
econstructor; eauto.
econstructor; eauto.
inversion H; clear H; subst.
inversion H2; clear H2; subst.
econstructor; eauto.
econstructor; eauto.
eapply pc_no; eauto.
econstructor; eauto.
inversion H2; clear H2; subst.
econstructor; eauto.
econstructor; eauto.
econstructor; eauto.
econstructor; eauto.
Qed.
induction 1; intuition; simpl in *.
exfalso.
eapply H1.
eauto.
trivial.
eapply IHpred_count.
intuition.
eapply H1; eauto.
Qed.
intuition.
assert (pred_count P (a :: (removeFirst eqd ls a)) c).
eapply pred_count_permutation.
eapply removeFirst_permutation.
trivial.
trivial.
inversion H4; clear H4; subst.
assert (n = 0)%nat.
eapply pred_count_eq_none_inv.
eauto.
intuition.
eapply H3.
eapply removeFirst_in_iff.
eauto.
assert (~In a (removeFirst eqd ls a)).
eapply removeFirst_NoDup_not_in.
trivial.
intuition; subst; intuition.
trivial.
omega.
intuition.
Qed.
induction ls; intuition.
exists O.
econstructor.
destruct H0.
destruct (H a).
exists (S x).
econstructor; eauto.
exists x.
econstructor; eauto.
Qed.
Inductive rel_map(A B : Type)(r : A -> B -> Prop) : list A -> list B -> Prop :=
| rm_nil :
rel_map r nil nil
| rm_step :
forall lsa lsb a b,
rel_map r lsa lsb ->
r a b ->
rel_map r (a :: lsa) (b :: lsb).
induction ls_c; intuition.
inversion H; subst.
simpl.
econstructor.
inversion H; clear H; subst.
simpl.
econstructor; eauto.
Qed.
induction 1; intuition; simpl in *.
f_equal.
eauto.
Qed.
induction 1; intuition; simpl in *.
intuition.
intuition; subst.
eauto.
Qed.
induction ls1; intuition; subst.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
trivial.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
f_equal.
eapply H0; eauto.
eapply IHls1; eauto.
Qed.
induction 1; intuition; subst.
econstructor.
econstructor.
eapply IHrel_map; eauto.
intuition.
eapply H2.
simpl.
intuition.
simpl.
intuition.
trivial.
Qed.
induction ls; intuition.
inversion H0; subst; clear H0.
inversion H1. trivial.
inversion H0; clear H0; subst.
inversion H1; clear H1; subst.
exfalso.
eapply H; eauto. simpl. intuition.
eapply IHls; eauto.
intuition.
eapply H; eauto. simpl. intuition.
Qed.
induction lsa1; intuition; simpl in *.
econstructor.
inversion H; clear H; subst.
econstructor.
eapply IHlsa1; eauto.
trivial.
inversion H; clear H; subst.
eapply IHlsa1; eauto.
Qed.
induction lsa; intuition; simpl in *.
inversion H; subst; clear H.
econstructor.
inversion H; clear H; subst.
econstructor.
eapply IHlsa; eauto.
trivial.
Qed.
induction lsa; intuition; simpl in *.
econstructor.
econstructor; eauto.
Qed.
induction lsa1; intuition; simpl in *.
inversion H; clear H; subst.
simpl.
trivial.
inversion H; clear H; subst.
rewrite <- app_comm_cons.
econstructor; eauto.
Qed.
induction lsa; intuition; simpl in *.
inversion H.
econstructor.
inversion H; clear H; subst.
econstructor; eauto.
Qed.
induction 1; intuition; simpl in *.
intuition.
intuition;
subst.
econstructor; eauto.
destruct (IHrel_map b0).
trivial.
intuition.
econstructor; eauto.
Qed.
induction ls; intuition; simpl in *.
intuition; subst.
eapply H; eauto.
eapply IHls; eauto.
Qed.
induction 1; intuition; simpl in *.
econstructor.
inversion H1; clear H1; subst.
econstructor.
eapply ne_all_not_in.
intuition.
subst.
destruct (rel_map_inverse H a').
trivial.
intuition.
eapply H2.
left; eauto.
right; eauto.
intuition; subst; intuition.
eauto.
eauto.
trivial.
eapply IHrel_map; intuition.
subst.
eapply H2.
right.
eapply H3.
right.
eapply H4.
trivial.
eauto.
eauto.
trivial.
Qed.
induction 1; intuition; simpl in *.
intuition; subst.
left; eauto.
right; eauto.
Qed.
induction lsa; intuition.
exists nil.
econstructor.
destruct H0.
destruct (H a).
exists (x0 :: x).
econstructor; eauto.
Qed.
induction 1; intuition; simpl in *.
inversion H; clear H; subst.
trivial.
inversion H1; clear H1; subst.
f_equal.
eauto.
eauto.
Qed.
induction 1; intuition; simpl in *.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
econstructor.
inversion H2; clear H2; subst.
inversion H3; clear H3; subst.
assert (b = b0).
eapply H0; eauto.
subst.
eapply perm_skip.
eauto.
inversion H1; clear H1; subst.
inversion H5; clear H5; subst.
inversion H2; clear H2; subst.
inversion H5; clear H5; subst.
assert (b0 = b1); eauto; subst.
assert (b = b2); eauto; subst.
eapply perm_trans.
eapply perm_swap.
eapply perm_skip.
eapply perm_skip.
eauto.
assert (lsb0 = lsb1).
eapply rel_map_func; eauto.
subst.
eapply Permutation_refl.
edestruct rel_map_left_total; eauto.
eapply perm_trans.
eapply IHPermutation1; eauto.
eapply IHPermutation2; eauto.
Qed.
induction lsa; intuition.
inversion H; clear H; subst.
econstructor.
inversion H; clear H; subst.
econstructor.
eapply IHlsa; eauto.
intuition.
eapply H0.
simpl.
intuition.
trivial.
Qed.
induction 1; intuition; simpl in *.
intuition.
intuition; subst.
econstructor; eauto.
edestruct IHrel_map; eauto.
intuition.
econstructor; eauto.
Qed.
(* a relational version of sumList *)
Inductive sumList_rel(A : Type)(rel : A -> Rat -> Prop) : list A -> Rat -> Prop :=
| slr_nil :
forall r,
r == rat0 ->
sumList_rel rel nil r
| slr_cons :
forall (ls : list A)(a : A) r1 r2 r3,
sumList_rel rel ls r1 ->
rel a r2 ->
r3 == r2 + r1 ->
sumList_rel rel (a :: ls) r3.
induction ls; intuition; simpl in *.
inversion H0. inversion H1. subst.
rewrite H2.
rewrite H3.
eapply leRat_trans.
eapply eqRat_impl_leRat.
eapply (ratIdentityIndiscernables 0).
intuition.
eapply rat0_le_all.
inversion H0; clear H0; subst.
inversion H1; clear H1; subst.
rewrite ratS_num.
eapply leRat_trans.
2:{
eapply eqRat_impl_leRat.
eapply eqRat_symm.
eapply ratMult_distrib.
}
rewrite H7.
rewrite H9.
rewrite rat_distance_of_sum.
eapply ratAdd_leRat_compat.
rewrite <- ratMult_1_r.
eapply ratMult_leRat_compat.
eapply H; eauto.
intuition.
eapply IHls; eauto.
Qed.
induction ls; intuition; simpl in *.
inversion H.
intuition.
inversion H; clear H; subst.
rewrite H6.
rewrite (H0 a r2); eauto.
rewrite <- ratAdd_0_l.
eapply IHls; eauto.
Qed.
induction ls; intuition; simpl in *.
intuition.
inversion H1; clear H1; subst.
inversion H; clear H; subst.
intuition; subst.
rewrite H10.
rewrite (H3 r2); eauto.
rewrite (sumList_rel_all_0_inv H5).
rewrite <- ratAdd_0_r.
intuition.
intuition.
eapply H2.
right.
eauto.
intuition; subst.
eauto.
trivial.
eapply eqRat_trans.
rewrite H10.
eapply ratAdd_eqRat_compat.
eapply H2.
left.
eauto.
intuition; subst; intuition.
trivial.
eauto.
rewrite <- ratAdd_0_l.
intuition.
Qed.
induction 1; intuition; subst.
econstructor.
rewrite <- H1.
trivial.
econstructor.
eapply IHsumList_rel.
intuition.
eapply eqRat_refl.
trivial.
eauto.
rewrite <- H3.
trivial.
Qed.
induction ls; intuition; simpl in *.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
inversion H; clear H; subst.
rewrite H3.
rewrite H1.
rewrite H2.
rewrite <- ratAdd_0_l.
intuition.
inversion H1; clear H1; subst.
inversion H2; clear H2; subst.
inversion H; clear H; subst.
assert (r7 == r3 + r5).
eauto.
assert (r6 == r0 + r4).
eauto.
rewrite H12.
rewrite H8.
rewrite H10.
rewrite H.
rewrite H1.
repeat rewrite <- ratAdd_assoc.
eapply ratAdd_eqRat_compat; intuition.
rewrite ratAdd_assoc.
rewrite (ratAdd_comm r5).
rewrite <- ratAdd_assoc.
intuition.
Qed.
induction ls; intuition; simpl in *.
exists 0.
econstructor.
intuition.
edestruct H.
left.
eauto.
edestruct IHls.
intuition.
econstructor.
econstructor.
eauto.
eauto.
eapply eqRat_refl.
Qed.
induction ls; intuition.
inversion H; subst; clear H.
apply ratMult_0 in H0.
destruct H0.
econstructor; trivial.
exfalso.
eapply rat_num_nz; [idtac | eapply H].
eapply posnat_pos.
inversion H; clear H; subst.
symmetry in H5.
apply ratMult_inverse in H5.
rewrite ratMult_distrib_r in H5.
symmetry in H5.
econstructor; eauto.
eapply IHls.
eapply sumList_rel_body_eq.
eapply H2.
intuition.
rewrite ratMult_assoc.
rewrite <- ratMult_1_r at 1.
eapply ratMult_eqRat_compat; intuition.
rewrite <- ratMult_num_den.
symmetry.
eapply num_dem_same_rat1.
unfold posnatMult, natToPosnat, posnatToNat.
destruct p1.
destruct p2.
apply mult_comm.
trivial.
Qed.
induction 1; intuition; simpl in *.
inversion H0; clear H0; subst.
econstructor; eauto.
inversion H; clear H; subst.
inversion H2; clear H2; subst.
econstructor; eauto.
econstructor; eauto.
eapply eqRat_refl.
rewrite H5.
rewrite H7.
repeat rewrite <- ratAdd_assoc.
eapply ratAdd_eqRat_compat; intuition.
eapply ratAdd_comm.
Qed.
induction ls; intuition; simpl in *.
econstructor.
intuition.
econstructor.
eapply IHls.
intuition.
eauto.
rewrite <- ratAdd_0_l.
intuition.
Qed.
induction 1; intuition; simpl in *.
eapply sumList_rel_body_eq.
eapply sumList_rel_all_0.
intuition.
eapply H5.
eauto.
simpl.
intuition.
intuition.
intuition.
trivial.
inversion H2; clear H2; subst.
symmetry in H1.
eapply sumList_rel_body_eq; intuition; eauto.
clear H1.
clear r3.
destruct (in_dec H4 a ls2).
eapply sumList_rel_permutation.
eapply Permutation_sym.
eapply removeFirst_permutation.
eauto.
econstructor.
eapply IHsumList_rel; intuition.
eapply removeFirst_NoDup.
trivial.
eapply H5.
right.
eapply H1.
trivial.
trivial.
eapply H6.
intuition.
intuition.
eapply H2.
eapply removeFirst_in.
trivial.
intuition.
subst.
intuition.
destruct (H4 a a0); subst.
eapply H6.
intuition.
intuition.
eapply removeFirst_NoDup_not_in.
eapply H3.
eauto.
eapply H7.
eapply removeFirst_in_iff.
eauto.
simpl. intuition.
eauto.
intuition.
assert (rel a 0).
eapply H6.
intuition.
intuition.
assert (r2 == 0).
eapply H5.
eauto.
trivial.
trivial.
eapply sumList_rel_body_eq.
3:{
rewrite H2.
rewrite <- ratAdd_0_l.
eapply eqRat_refl.
}
2:{
intuition.
eauto.
}
2:{
eauto.
}
eapply IHsumList_rel; intuition.
eapply H5.
right.
eauto.
trivial.
trivial.
eapply H7.
trivial.
intuition.
subst.
intuition.
Grab Existential Variables.
trivial.
Qed.
induction ls; intuition.
unfold sumList.
simpl.
econstructor.
intuition.
unfold sumList in *. simpl in *.
econstructor.
eapply IHls.
eauto.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
rewrite fold_add_init.
eapply eqRat_refl.
intuition.
Qed.
intuition.
unfold sumList. simpl.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
rewrite fold_add_init.
eapply eqRat_refl.
intuition.
Qed.
induction ls; intuition; unfold sumList in *; simpl in *.
rewrite <- ratAdd_0_l.
intuition.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
rewrite fold_add_init.
rewrite (IHls f1 f2).
eapply eqRat_trans.
2:{
symmetry.
eapply ratAdd_eqRat_compat.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
eapply fold_add_init.
intuition.
eapply eqRat_refl.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
eapply fold_add_init.
intuition.
eapply eqRat_refl.
}
repeat rewrite <- ratAdd_assoc.
eapply ratAdd_eqRat_compat.
repeat rewrite ratAdd_assoc.
eapply ratAdd_eqRat_compat; intuition.
rewrite ratAdd_comm.
eapply ratAdd_eqRat_compat; intuition.
eapply fold_add_body_eq; intuition.
intuition.
Qed.
induction lsa; destruct lsb; intuition.
unfold sumList in *; simpl in *; intuition.
assert (sumList (b :: lsb) (fun b : B => sumList nil (fun a : A => f a b)) == 0).
eapply sumList_0.
intuition.
unfold sumList. simpl.
intuition.
rewrite H.
unfold sumList in *; simpl in *; intuition.
assert (sumList (a :: lsa) (fun a0 : A => sumList nil (fun b : B => f a0 b)) == 0).
eapply sumList_0.
intuition.
unfold sumList in *; simpl in *; intuition.
rewrite H.
unfold sumList in *; simpl in *; intuition.
rewrite sumList_body_eq.
2:{
intuition.
eapply sumList_cons.
}
rewrite sumList_sum.
rewrite (sumList_cons lsa a (fun a0 : A => sumList lsb (f a0))).
rewrite IHlsa.
symmetry.
rewrite sumList_body_eq.
2:{
intuition.
eapply sumList_cons.
}
rewrite sumList_sum.
repeat rewrite sumList_cons.
repeat rewrite <- ratAdd_assoc.
eapply ratAdd_eqRat_compat; intuition.
repeat rewrite ratAdd_assoc.
eapply ratAdd_eqRat_compat; intuition.
eapply ratAdd_comm.
Qed.
intuition.
erewrite <- matchOrder_firstn at 1.
eapply eqRat_symm.
eapply eqRat_trans.
eapply fold_add_rat_perm.
eapply matchOrder_permutation.
eapply H0.
eauto.
intuition.
eapply eqRat_refl.
intuition.
eapply eqRat_refl.
eapply eqRat_symm.
eapply fold_add_matchOrder; intuition.
eapply permutation_NoDup.
eapply matchOrder_permutation; eauto.
eauto.
eapply H3.
eapply matchOrder_In.
eapply H0.
trivial.
intuition.
eauto.
intuition.
eapply H5.
rewrite matchOrder_firstn; trivial.
trivial.
trivial.
trivial.
Grab Existential Variables.
trivial.
Qed.
intuition.
eapply fold_add_subset'; eauto.
intuition.
Qed.
induction ls; intuition.
inversion H0.
simpl in *.
inversion H; clear H; subst.
intuition; subst.
assert (sumList ls f == 0).
eapply sumList_0.
intuition.
eapply H1.
intuition.
intuition; subst; intuition.
unfold sumList in *; simpl.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
rewrite fold_add_init.
rewrite H.
rewrite <- ratAdd_0_r.
intuition.
intuition.
assert (sumList ls f == f a).
eapply IHls; intuition.
unfold sumList in *.
simpl.
rewrite fold_add_body_eq.
2:{
eapply ratAdd_comm.
}
rewrite fold_add_init.
rewrite H0.
rewrite H1.
rewrite <- ratAdd_0_l.
intuition.
intuition.
intuition; subst; intuition.
intuition.
Qed.
induction 1; intuition; simpl in *.
eapply IHPermutation.
eapply ratAdd_eqRat_compat; intuition.
rewrite fold_add_body_eq.
eapply eqRat_refl.
rewrite ratAdd_assoc.
rewrite (ratAdd_comm (f y)).
rewrite <- ratAdd_assoc.
intuition.
eapply ratAdd_eqRat_compat; intuition.
eapply ratAdd_eqRat_compat; intuition.
intuition.
eapply eqRat_trans.
eapply IHPermutation1.
eapply H1.
eapply IHPermutation2.
intuition.
Qed.
intuition.
eapply fold_add_permutation; intuition.
Qed.
induction 1; intuition; subst.
econstructor.
rewrite <- H1.
trivial.
econstructor.
eapply IHsumList_rel.
intuition.
eapply eqRat_refl.
trivial.
eapply H2.
simpl.
intuition.
eauto.
rewrite <- H3.
trivial.
Qed.
induction lsa; intuition.
exists nil.
econstructor.
edestruct (IHlsa _ _ H).
intuition.
destruct (H a).
eapply H0.
simpl. intuition.
exists (x0 :: x).
econstructor; eauto.
Qed.
induction ls; intuition; subst; simpl in *.
trivial.
f_equal.
eauto.
Qed.
Fixpoint getNats s n :=
match n with
| O => nil
| S n' => (s + n')%nat :: (getNats s n')
end.
induction ls; intuition.
inversion H; clear H; subst.
econstructor.
rewrite H3.
apply ratMult_0_r.
inversion H; clear H; subst.
econstructor.
eauto.
unfold ratMult_rel.
intuition.
eapply ratMult_eqRat_compat; eauto.
rewrite H8.
eapply ratMult_distrib.
Qed.

Lemma series_le : forall n (f1 f2 : nat -> Rat -> Prop) r1 r2,
(forall i v1 v2, f1 i v1 -> f2 i v2 -> v2 <= v1) ->
sumList_rel f1 (getNats O n) r1 ->
sumList_rel f2 (getNats O n) r2 ->
r2 <= r1.