Section DistMult_impl_RepeatCore.
Variable A B : Set.
Hypothesis B_EqDec : EqDec B.
Variable P : B -> bool.
Variable c1 c2 : A -> Comp B.
Variable A_State : Set.
Variable A1 : Comp (A * A_State).
Variable A2 : A_State -> B -> Comp bool.
Hypothesis A1_wf : well_formed_comp A1.
Hypothesis A2_wf : forall s_A b, well_formed_comp (A2 s_A b).
Hypothesis c1_wf : forall a s_A, In (a, s_A) (getSupport A1) -> well_formed_comp (c1 a).
Hypothesis c1_repeat_wf : forall a s_A, In (a, s_A) (getSupport A1) -> exists b, In b (filter P (getSupport (c1 a))).
Hypothesis c2_wf : forall a s_A, In (a, s_A) (getSupport A1) -> well_formed_comp (c2 a).
Hypothesis c2_repeat_wf : forall a s_A, In (a, s_A) (getSupport A1) -> exists b, In b (filter P (getSupport (c2 a))).
Variable n : nat.
(* Assume A1 can effectively distinguish the cores as in RepeatCore_G, construct an adversary B that can distinguish the entire distributions given multiple samples as in DistMult_G. *)
Definition DM_RC_G1(c : A -> Comp B) :=
[a, s_A] <-$2 A1;
p <-$ Repeat (b <-$ (c a); g <-$ (A2 s_A b); ret (b, g)) (fun p => P (fst p));
ret (snd p).
intuition.
unfold RepeatCore_G, DM_RC_G1.
comp_skip.
comp_simp.
assert (evalDist (b0 <-$ Repeat (c a) P; A2 a0 b0) x
==
evalDist (p <-$ (b0 <-$ Repeat (c a) P; b <-$ A2 a0 b0; ret (b0, b)); ret (snd p)) x).
inline_first.
comp_skip.
inline_first.
rewrite <- evalDist_right_ident.
comp_skip.
comp_simp.
simpl.
reflexivity.
rewrite H2.
comp_skip.
eapply repeat_fission; intuition.
eauto.
eauto.
Qed.
Definition DM_RC_G2(c : A -> Comp B) :=
[a, s_A] <-$2 A1;
p <-$ Repeat (b <-$ (c a); g <-$ (A2 s_A b); ret (b, g)) (fun p => P (fst p));
ret (Some (snd p)).
intuition.
unfold DM_RC_G1, DM_RC_G2.
comp_skip.
comp_simp.
comp_skip.
dist_compute.
Qed.
Definition DM_RC_G3(c : A -> Comp B) :=
[a, s_A] <-$2 A1;
ls <-$ compMap _ (fun _ => b <-$ c a; g <-$ (A2 s_A b); ret (b, g)) (forNats n);
ret hd_error (map (fun x => snd x) (filter (fun p => P (fst p)) ls)).

Theorem DM_RC_G2_G3_close :
forall (c : A -> Comp B) k x,
(forall a s_A, In (a, s_A) (getSupport A1) -> well_formed_comp (c a)) ->
(forall a s_A, In (a, s_A) (getSupport A1) -> exists b, In b (filter P (getSupport (c a)))) ->
(forall a s_A, In (a, s_A) (getSupport A1) -> Pr[b <-$ c a; ret (negb (P b))] <= k) ->
| evalDist (DM_RC_G2 c) x - evalDist (DM_RC_G3 c) x | <= expRat k n.