unfold TrueRepeat_G, TrueRepeat_G1.
inline_first.
comp_skip.
assert (Pr  [b <-$ Repeat (c x) P; ret f b ] ==
Pr  [b <-$ (y <-$ compMap _ (fun _ => (c x)) (forNats n);
match (hd_error (filter P y)) with
| None => Repeat (c x) P
| Some z => ret z
end); ret f b ]).
comp_skip.
eapply Repeat_unroll_n.
eauto.
eauto.
rewrite H0.
clear H0.
inline_first.
comp_skip.
case_eq (hd_error (filter P x0)); intuition.
comp_irr_r.
edestruct Repeat_c_terminating; eauto.
eapply well_formed_Repeat.
unfold eq_dec.
intuition.
eapply (EqDec_dec _).
eauto.
eauto.
reflexivity.
Qed.
Definition TrueRepeat_G2 :=
a <-$ A1;
bs <-$ compMap _ (fun _ => c a) (forNats n);
b_opt <- hd_error (filter P bs);
b' <-$ Repeat (c a) P;
b <- match b_opt with
| None => b'
| Some x => x
end;
ret (f b, if b_opt then false else true).

(* Specifications: *)
Theorem TrueRepeat_G1_G2_equiv :
Pr [TrueRepeat_G1] <= Pr[x <-$ TrueRepeat_G2; ret (fst x || snd x)].