(* 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.
intuition.
destruct H; intuition.
assert (forall p, In p (getSupport x) -> comp_spec P (f1 (fst p)) (f2 (snd p))).
intuition.
eapply H0.
eapply in_support_marginal_l; eauto.
eapply in_support_marginal_r; eauto.
eauto.
clear H0.
pose proof H2.
apply (list_choice) in H2.
destruct H2.
exists (Bind x x0).
intuition.
rewrite evalDist_seq_step.
eapply eqRat_trans.
eapply sumList_body_eq.
intros.
eapply ratMult_eqRat_compat.
eauto.
eapply eqRat_refl.
unfold marginal_l.
eapply eqRat_trans.
eapply sumList_body_eq.
intros.
eapply ratMult_eqRat_compat.
eapply evalDist_seq_step.
eapply eqRat_refl.
eapply eqRat_trans.
eapply sumList_body_eq.
intros.
symmetry.
eapply sumList_factor_constant_r.
rewrite sumList_comm.
dist_inline_first.
rewrite evalDist_seq_step.
eapply sumList_body_eq.
intros.
rewrite (@sumList_exactly_one _ (fst a)).
assert (Pr  [ret eqb (fst a) (fst a) ]  == 1).
dist_compute.
rewrite H5.
rewrite ratMult_1_r.
eapply ratMult_eqRat_compat; intuition.
specialize (H2 a).
intuition.
rewrite H2.
unfold marginal_l.
intuition.
eapply getSupport_NoDup.
eapply in_support_marginal_l; eauto.
intuition.
dist_compute.
rewrite evalDist_seq_step.
eapply eqRat_trans.
eapply sumList_body_eq.
intros.
eapply ratMult_eqRat_compat.
eauto.
eapply eqRat_refl.
unfold marginal_r.
eapply eqRat_trans.
eapply sumList_body_eq.
intros.
eapply ratMult_eqRat_compat.
eapply evalDist_seq_step.
eapply eqRat_refl.
eapply eqRat_trans.
eapply sumList_body_eq.
intros.
symmetry.
eapply sumList_factor_constant_r.
rewrite sumList_comm.
dist_inline_first.
rewrite evalDist_seq_step.
eapply sumList_body_eq.
intros.
rewrite (@sumList_exactly_one _ (snd a)).
assert (Pr  [ret eqb (snd a) (snd a) ] == 1).
dist_compute.
rewrite H5.
rewrite ratMult_1_r.
eapply ratMult_eqRat_compat; intuition.
specialize (H2 a); intuition.
rewrite H6.
unfold marginal_r; intuition.
eapply getSupport_NoDup.
eapply in_support_marginal_r; eauto.
intuition.
dist_compute.
simp_in_support.
specialize (H2 x1); intuition.
unfold eq_dec; intuition.
eapply (EqDec_dec _).
eapply (ret (c, d)).
Qed.
Ltac despec :=
match goal with
| [H : comp_spec _ _ _  |- _] => destruct H
end.
intuition.
despec; intuition.
exists x; intuition.
rewrite H1.
unfold marginal_l.
dist_skip.
eapply evalDist_ret_eq.
case_eq (eqb (fst x0) r1); intuition.
rewrite eqb_leibniz in H4.
subst.
eapply eqb_refl.
case_eq ( (@eqb A eqda1 (@fst A B x0) r1)); intuition.
rewrite eqb_leibniz in H5.
subst.
rewrite eqb_refl in H4.
intuition.
rewrite H.
unfold marginal_r.
dist_skip.
eapply evalDist_ret_eq.
case_eq (eqb (snd x0) r2); intuition.
rewrite eqb_leibniz in H4.
subst.
eapply eqb_refl.
case_eq ( (@eqb B eqdb1 (@snd A B x0) r2)); intuition.
rewrite eqb_leibniz in H5.
subst.
rewrite eqb_refl in H4.
intuition.
Qed.
intuition.
despec; intuition.
exists (p <-$ x; ret (snd p, fst p)).
intuition.
eapply eqRat_trans; eauto.
unfold marginal_r, marginal_l.
dist_inline_first.
dist_skip.
dist_compute.
eapply eqRat_trans; eauto.
unfold marginal_r, marginal_l.
dist_inline_first.
dist_skip.
dist_compute.
repeat simp_in_support.
simpl.
eauto.
Qed.
intuition.
(* special case: the events have probability 0 *)
destruct (eq_Rat_dec (evalDist c1 x) 0).
exists (a <-$ c1;
b <-$ c2;
ret (a, b)).
intuition.
unfold marginal_l.
dist_inline_first.
rewrite <- evalDist_right_ident.
dist_skip.
dist_inline_first.
dist_irr_r.
dist_compute.
unfold marginal_r.
dist_inline_first.
rewrite <- evalDist_right_ident.
dist_irr_r.
dist_inline_first.
dist_skip.
dist_compute.
repeat simp_in_support.
simpl in *.
exfalso.
eapply getSupport_not_In_evalDist.
eapply e.
trivial.
repeat simp_in_support.
simpl in *.
exfalso.
eapply getSupport_not_In_evalDist.
rewrite <- H1.
trivial.
trivial.
(* special case: the events have probability 1 *)
destruct (eq_Rat_dec (evalDist c1 x) 1).
exists (a <-$ c1;
b <-$ c2;
ret (a, b)).
split.
intuition.
unfold marginal_l.
dist_inline_first.
rewrite <- evalDist_right_ident.
dist_skip.
dist_inline_first.
dist_irr_r.
dist_compute.
split.
intuition.
unfold marginal_r.
dist_inline_first.
rewrite <- evalDist_right_ident.
dist_irr_r.
dist_inline_first.
dist_skip.
dist_compute.
intros.
repeat simp_in_support; simpl in *.
erewrite (evalDist_1) in H5; intuition.
2:{
rewrite <- H1.
trivial.
}
simpl in *; intuition.
erewrite (evalDist_1) in H3; eauto.
simpl in *; intuition.
(* general case *)
exists (a <-$ c1;
b <-$ if (eqb a x) then (ret y) else (Repeat c2 (fun b => negb (eqb b y)));
ret (a, b)).
split.
intuition.
unfold marginal_l.
dist_inline_first.
rewrite <- evalDist_right_ident.
dist_skip.
dist_inline_first.
dist_irr_r.
case_eq (eqb x0 x); intuition; wftac.
remember (getSupport c2) as s.
destruct s.
specialize (getSupport_length_nz H0); intuition.
rewrite <- Heqs in H4.
simpl in *.
omega.
destruct (EqDec_dec _ b y); subst.
destruct s.
exfalso.
eapply n0.
rewrite H1.
rewrite <- (evalDist_lossless H0).
rewrite <- Heqs.
rewrite sumList_cons.
unfold sumList; simpl.
eapply ratAdd_0_r.
eapply (@well_formed_Repeat _ _ _ _ b).
trivial.
eapply filter_In; intuition.
rewrite <- Heqs.
simpl.
intuition.
case_eq (eqb b y); intuition.
rewrite eqb_leibniz in H4.
subst.
specialize (getSupport_NoDup c2); intuition.
rewrite <- Heqs in H4.
inversion H4; clear H4; subst.
simpl in *.
intuition.
eapply (@well_formed_Repeat _ _ _ _ b).
trivial.
eapply filter_In; intuition.
rewrite <- Heqs; simpl; intuition.
case_eq (eqb b y); intuition.
rewrite eqb_leibniz in H4; subst.
intuition.
dist_compute.
split.
intuition.
unfold marginal_r.
dist_inline_first.
rewrite evalDist_seq_step.
rewrite (sumList_filter_partition (eqb x)).
symmetry.
eapply eqRat_trans.
eapply ratAdd_eqRat_compat.
eapply sumList_body_eq.
intros.
eapply ratMult_eqRat_compat.
eapply eqRat_refl.
apply filter_In in H2.
intuition.
rewrite eqb_leibniz in H4.
subst.
rewrite eqb_refl.
dist_inline_l.
simpl snd.
eapply eqRat_refl.
eapply sumList_body_eq; intuition.
apply filter_In in H2.
intuition.
case_eq (eqb x a); intuition.
rewrite eqb_leibniz in H2.
subst.
rewrite eqb_refl in H4.
simpl in *.
discriminate.
rewrite evalDist_assoc at 1.
case_eq (eqb a x); intuition.
rewrite eqb_leibniz in H5.
subst.
rewrite eqb_refl in H2.
discriminate.
eapply ratMult_eqRat_compat.
eapply eqRat_refl.
eapply evalDist_seq_eq.
intuition.
eapply eqRat_refl.
intuition.
dist_simp.
unfold snd.
eapply eqRat_refl.
intuition.
rewrite (@sumList_exactly_one _ x).
case_eq (eqb y r2); intuition.
rewrite eqb_leibniz in H2.
subst.
assert (Pr[ret true] == 1).
dist_compute.
rewrite H2.
rewrite ratMult_1_r.
clear H2.
rewrite H1.
symmetry.
rewrite ratAdd_0_r at 1.
eapply ratAdd_eqRat_compat; intuition.
symmetry.
eapply sumList_0.
intuition.
apply filter_In in H2.
intuition.
assert (Pr  [x0 <-$ Repeat c2 (fun b : B => negb (eqb b r2)); ret eqb x0 r2 ] ==
Pr  [x0 <-$ Repeat c2 (fun b : B => negb (eqb b r2)); ret eqb r2 x0 ] ).
dist_skip; dist_compute.
rewrite H2.
clear H2.
rewrite <- evalDist_event_equiv.
eapply ratMult_0.
right.
eapply evalDist_Repeat_0; intuition.
subst.
rewrite eqb_refl in H5.
simpl in *; discriminate.
assert (Pr [ret false] == 0).
dist_compute.
rewrite H3.
clear H3.
rewrite ratMult_0_r.
rewrite <- ratAdd_0_l.
rewrite sumList_factor_constant_r.
rewrite sumList_filter_complement.
rewrite H1.
rewrite evalDist_seq_step.
destruct (in_dec (EqDec_dec _) r2 (getSupport c2)).
rewrite (@sumList_exactly_one _ r2).
assert (Pr  [ret eqb r2 r2 ] == 1).
dist_compute.
rewrite H3.
clear H3.
rewrite ratMult_1_r.
simpl.
unfold indicator.
case_eq (eqb r2 y); intuition.
rewrite eqb_leibniz in H3; subst.
rewrite eqb_refl in H2.
discriminate.
simpl.
rewrite ratMult_1_l.
symmetry.
rewrite <- ratMult_1_l at 1.
rewrite <- ratMult_assoc.
eapply ratMult_eqRat_compat; intuition.
symmetry.
eapply eqRat_trans.
eapply ratMult_eqRat_compat.
eapply eqRat_refl.
eapply ratInverse_eqRat_compat.
intuition.
assert ( (filter (fun b : B => negb (eqb b y)) (getSupport c2)) =
(filter (fun b : B => negb (eqb y b)) (getSupport c2))).
eapply filter_ext; intuition.
rewrite eqb_symm.
trivial.
rewrite H5 in H4.
clear H5.
eapply n0.
eapply leRat_impl_eqRat.
eapply evalDist_le_1.
eapply ratSubtract_0_inv.
rewrite sumList_filter_complement in H4.
rewrite H1.
intuition.
trivial.
assert ((filter (fun b : B => negb (eqb b y)) (getSupport c2)) =
(filter (fun b : B => negb (eqb y b)) (getSupport c2))).
eapply filter_ext.
intuition.
rewrite eqb_symm.
intuition.
rewrite H4.
clear H4.
eapply sumList_filter_complement.
trivial.
rewrite ratMult_comm.
eapply ratInverse_prod_1.
intuition.
eapply n0.
eapply leRat_impl_eqRat.
eapply evalDist_le_1.
eapply ratSubtract_0_inv.
rewrite H1.
intuition.
eapply getSupport_NoDup.
simpl.
eapply filter_In; intuition.
rewrite eqb_symm.
rewrite H2.
trivial.
intuition.
dist_compute.
eapply eqRat_trans.
eapply ratMult_eqRat_compat.
eapply eqRat_refl.
eapply sumList_0.
intuition.
simpl in H3.
apply filter_In in H3.
intuition.
case_eq (eqb a r2); intuition.
rewrite eqb_leibniz in H3; subst.
intuition.
dist_compute.
rewrite ratMult_0_r.
symmetry.
eapply getSupport_not_In_evalDist.
trivial.
trivial.
eapply filter_NoDup.
eapply getSupport_NoDup.
eapply filter_In; intuition.
eapply getSupport_In_evalDist; intuition.
eapply eqb_refl.
intuition.
apply filter_In in H2.
intuition.
rewrite eqb_leibniz in H5.
intuition.
intros.
repeat simp_in_support;
simpl in *; intuition.
rewrite eqb_refl in Heqx2.
discriminate.
symmetry in Heqx2.
rewrite eqb_leibniz in Heqx2.
trivial.
simpl in *.
eapply filter_In in H5.
intuition.
rewrite eqb_refl in H4.
simpl in *.
discriminate.
Grab Existential Variables.
unfold eq_dec; intuition.
eapply (EqDec_dec _).
unfold eq_dec; intuition.
eapply (EqDec_dec _).
intuition.
intuition.
intuition.
intuition.
intuition.
Qed.

Theorem le_impl_comp_spec :
forall (A B : Set){eqda : EqDec A}{eqdb : EqDec B}(c1 : Comp A)(c2 : Comp B),
well_formed_comp c1 ->
well_formed_comp c2 ->
forall x y,
evalDist c1 x <= evalDist c2 y ->
comp_spec (fun a b => a = x -> b = y) c1 c2.