Set Implicit Arguments.
Plus List Permutation Arith Relations RelationClasses Morphisms
arith_lems util list_utils nat_seqs Omega.
(* sum *)
Fixpoint sum (l: list nat): nat :=
match l with
| nil => 0
| h :: t => h + sum t
end.
Open Scope R_scope.
(* Rsum *)
Definition Rsum: list R -> R := fold_right Rplus 0.
Proof with auto.
induction l...
simpl.
intros.
rewrite plus_INR.
rewrite H...
rewrite IHl...
Qed.
Instance Rsum_Permutation: Proper (Permutation ==> eq) Rsum.
Proof with auto.
intros x y p.
induction p...
simpl.
rewrite IHp...
simpl.
do 2 rewrite <- Rplus_assoc.
rewrite (Rplus_comm y)...
rewrite IHp1...
Qed.
Proof with auto with real.
induction x...
simpl.
intros.
rewrite IHx...
Qed.
Proof with auto.
induction l; intros.
simpl.
field.
simpl Rsum.
simpl @length.
rewrite IHl.
rewrite (H a).
rewrite S_INR.
field.
left...
intros.
apply H.
right...
Qed.
Proof with auto with real.
induction l...
simpl @length.
intros.
simpl Rsum.
apply Rle_trans with (a + INR (length l) * r).
apply Rplus_le_compat_l.
apply IHl.
intros.
apply H.
right...
apply Rle_trans with (r + INR (length l) * r).
apply Rplus_le_compat_r.
firstorder.
simpl @length.
rewrite S_INR.
rewrite Rmult_plus_distr_r.
rewrite Rmult_1_l...
Qed.
Proof with auto with real.
induction l; intros.
simpl.
rewrite Rmult_0_l...
simpl @length.
simpl Rsum.
rewrite S_INR.
rewrite Rmult_plus_distr_r.
rewrite Rmult_1_l.
rewrite Rplus_comm.
apply Rplus_le_compat.
apply H.
left...
apply IHl.
intros.
apply H.
right...
Qed.
Proof with try field.
induction l; simpl...
rewrite IHl...
Qed.
Proof with auto with real.
intros.
rewrite map_app.
rewrite Rsum_app.
rewrite Rsum_map_plus.
do 2 rewrite map_map.
reflexivity.
Qed.
Instance elemsR_le_Rsum: Proper (elemsR Rle ==> Rle) Rsum.
Proof with auto with real.
repeat intro.
induction H...
simpl...
Qed.
Proof with field.
unfold ext_eq, Basics.compose.
induction n; intros.
simpl...
simpl Rsum.
rewrite S_INR.
rewrite IHn...
Qed.
Proof with auto with real.
induction l...
simpl.
rewrite Rsum_app.
congruence.
Qed.
(* Things like the three lemmas above are more elegantly stated as extensional equalities on function compositions, but stated that way they cannot be easily rewritten with.. *)
Proof with auto with real.
induction l...
intros.
simpl.
apply Rplus_le_le_0_compat; intuition.
Qed.
Proof with auto.
unfold RsumOver.
intros.
rewrite <- (map_length f l).
apply Rsum_le...
intros.
destruct (In_map_inv H0).
destruct H1.
subst...
Qed.
Proof with auto.
induction b; simpl; intros.
induction (nats b' w)...
simpl.
rewrite <- IHl...
rewrite nats_Sb.
rewrite IHb.
rewrite map_map...
Qed.
Proof with auto.
intros.
replace b with ((b + 0)%nat)...
rewrite nats_plusb.
unfold RsumOver.
rewrite map_map...
f_equal.
unfold compose.
apply map_ext...
Qed.
Proof with auto with real.
unfold RsumOver.
intros.
induction l...
simpl.
apply Rplus_le_compat.
apply H.
left...
apply IHl.
intros.
apply H.
right...
Qed.
Proof with auto.
unfold compose.
unfold RsumOver.
induction l.
simpl.
apply Rmult_0_r.
simpl.
rewrite <- IHl.
apply Rmult_plus_distr_l.
Qed.
Proof with auto with arith.
unfold compose.
unfold RsumOver...
intros.
apply Rsum_Permutation.
rewrite <- (map_map (minus d) f).
apply Permutation_map.
apply NoDup_incl_Permutation.
rewrite map_length.
repeat rewrite nats_length...
apply NoDup_map.
intros.
apply minus_eq_inv_r with d...
destruct (In_nats_inv _ _ _ H0)...
apply le_trans with ((b + w)%nat)...
destruct (In_nats_inv _ _ _ H1).
apply le_trans with ((b + w)%nat)...
apply NoDup_nats.
intro.
intro.
destruct (In_map_inv H0).
destruct H1.
destruct (In_nats_inv _ _ _ H2).
subst.
apply In_nats; omega.
Qed.
(* Ravg *)
Definition Ravg (l: list R): R := Rsum l / INR (length l).
Proof with auto.
unfold Ravg.
simpl.
unfold Rdiv.
rewrite Rmult_0_l...
Qed.
Instance Ravg_Permutation: Proper (Permutation ==> eq) Ravg.
Proof with auto with real.
induction l.
simpl.
rewrite Ravg_nil...
intros.
rewrite Ravg_cons.
unfold Rdiv.
apply Rmult_le_pos...
apply Rplus_le_le_0_compat.
firstorder.
apply Rmult_le_pos...
firstorder.
Qed.
Proof with auto.
induction l.
rewrite Ravg_nil...
intros.
rewrite Ravg_cons.
rewrite IHl.
rewrite Rmult_0_l.
rewrite (H a).
rewrite Rplus_0_l.
unfold Rdiv.
rewrite Rmult_0_l...
left...
intros.
apply H.
right...
Qed.
(* Ravg_0 and Ravg_0_le cannot be generalized to arbitrary constant resp. lower bound, because Ravg nil = 0 *)
Proof with auto with real.
induction x; intros.
simpl.
rewrite Ravg_nil.
rewrite Rmult_0_l.
rewrite Rplus_0_l.
rewrite Rmult_comm.
rewrite Rmult_assoc.
destruct y.
rewrite Ravg_nil.
rewrite Rmult_0_l...
rewrite Rinv_r...
simpl @length...
simpl @length.
simpl app.
do 2 rewrite Ravg_cons.
rewrite IHx.
rewrite app_length.
unfold Rdiv.
rewrite (Rmult_assoc (a + Ravg x * INR (length x)) ).
rewrite (Rmult_comm (/ INR (S (length x)))).
rewrite Rinv_r...
rewrite Rmult_1_r.
simpl plus.
rewrite Rmult_comm.
apply Rmult_eq_compat_l.
rewrite Rplus_assoc.
apply Rplus_eq_compat_l.
case_eq ((length x + length y)%nat); intros.
destruct x.
destruct y.
simpl.
repeat rewrite Rmult_0_r...
elimtype False.
simpl in H.
discriminate.
elimtype False.
simpl in H.
discriminate.
rewrite Rmult_comm.
rewrite <- Rmult_assoc.
rewrite Rinv_r...
Qed.

Lemma Ravg_0_inv (l: list R): Ravg l = 0 -> (forall x, In x l -> 0 <= x) -> forall x, In x l -> x = 0.