(* Use of this source code is governed by the license described	* * in the LICENSE file at the root of the source tree.		*)
Set Implicit Arguments.
(* stuff that needs to go somewhere else *)
intuition.
apply (ratAdd_leRat_compat_l r3) in H1.
assert (ratSubtract r1 r3 + r3 == r1).
rewrite ratAdd_comm.
eapply ratSubtract_ratAdd_inverse_2; intuition.
rewrite <- H2.
rewrite H1.
rewrite ratAdd_comm.
eapply eqRat_impl_leRat.
apply ratSubtract_ratAdd_inverse_2; intuition.
Qed.
intuition.
eapply (eqRat_ratAdd_same_r (evalDist c a)).
symmetry.
rewrite ratAdd_comm.
rewrite ratSubtract_ratAdd_inverse_2.
rewrite <- (evalDist_lossless H).
rewrite (sumList_filter_partition (eqb a)).
rewrite ratAdd_comm.
eapply ratAdd_eqRat_compat; intuition.
destruct (eq_Rat_dec (evalDist c a) 0).
rewrite e.
eapply sumList_0; intuition.
apply filter_In in H0.
intuition.
rewrite eqb_leibniz in H2; subst.
trivial.
rewrite (@sumList_exactly_one _ a); intuition.
eapply filter_NoDup.
eapply getSupport_NoDup.
eapply filter_In; intuition.
eapply getSupport_In_evalDist; intuition.
eapply eqb_refl.
apply filter_In in H0; intuition.
rewrite eqb_leibniz in H3; subst.
intuition.
eapply evalDist_le_1.
Qed.
intuition.
simpl.
unfold indicator.
case_eq (P a); intuition.
rewrite ratMult_1_l.
eapply ratMult_0.
right.
eapply getSupport_not_In_evalDist.
intuition.
eapply H;
eauto.
rewrite ratMult_0_l.
eapply ratMult_0_l.
Qed.
intuition.
destruct (in_dec (EqDec_dec _) a (getSupport c)).
rewrite evalDist_seq_step.
rewrite (@sumList_exactly_one _ a).
dist_compute.
eapply getSupport_NoDup.
trivial.
intuition.
dist_compute.
rewrite evalDist_seq_step.
assert (evalDist c a == 0).
eapply getSupport_not_In_evalDist; trivial.
rewrite H.
symmetry.
eapply sumList_0.
intuition.
dist_compute.
Qed.
induction ls; intuition; simpl in *.
rewrite H.
destruct (f2 a); eauto.
f_equal;
eauto.
Qed.
(* definitions related to the program logic *)
Definition marginal_l(A B : Set){eqd : EqDec A}(c : Comp (A * B))(a : A) :=
Pr[x <-$ c; ret eqb (fst x) a].
Definition marginal_r(A B : Set){eqd : EqDec B}(c : Comp (A * B))(b : B) :=
Pr[x <-$ c; ret eqb (snd x) b].
intuition.
eapply getSupport_In_evalDist.
intuition.
rewrite H in H1.
clear H.
unfold marginal_l in *.
simpl in *.
eapply sumList_0 in H1; eauto.
eapply ratMult_0 in H1.
intuition.
eapply getSupport_In_evalDist;
eauto.
dist_compute.
Qed.
intuition.
eapply getSupport_In_evalDist.
intuition.
rewrite H in H1.
clear H.
unfold marginal_r in *.
simpl in *.
eapply sumList_0 in H1; eauto.
eapply ratMult_0 in H1.
intuition.
eapply getSupport_In_evalDist;
eauto.
dist_compute.
Qed.
Definition comp_spec (R1 R2 : Set)
{eqd1 : EqDec R1}{eqd2 : EqDec R2}
(post : R1 -> R2 -> Prop)(c1 : Comp R1)(c2 : Comp R2) :=
exists c : Comp (R1 * R2),
(forall r1, evalDist c1 r1 == marginal_l c r1) /\
(forall r2, evalDist c2 r2 == marginal_r c r2) /\
(forall p, In p (getSupport c) -> post (fst p) (snd p)).
induction ls; intuition.
exists (fun a => b).
intuition.
simpl in *.
intuition.
edestruct (H a); intuition.
edestruct (H0); intuition.
exists (fun a0 => if (eqd a a0) then x else (x0 a0)).
intuition.
inversion H3; clear H3; subst.
destruct (eqd a0 a0); intuition.
destruct (eqd a a0); intuition.
subst.
trivial.
Qed.

Theorem comp_spec_seq :
forall {A B : Set} P' {C D : Set} P{eqda : EqDec A}{eqdb : EqDec B}{eqdc : EqDec C}{eqdd : EqDec D}(c1 : Comp A)(c2 : Comp B) (c : C) (d : D)
(f1 : A -> Comp C)(f2 : B -> Comp D),
comp_spec P' c1 c2 ->
(forall a b, In a (getSupport c1) -> In b (getSupport c2) -> P' a b -> comp_spec P (f1 a) (f2 b)) ->
comp_spec P (Bind c1 f1) (Bind c2 f2).