Set Warnings "-extraction-opaque-accessed,-extraction".
Set Warnings "-notation-overridden,-parsing".

Require Import String. 
Require Import List.

Require Import ZArith.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import ssrfun ssrbool ssrnat eqtype seq.

From QuickChick Require Import
     GenLowInterface GenHighInterface RandomQC Tactics Sets Show.


Set Bullet Behavior "Strict Subproofs".

Lemma nthE T (def : T) s i : List.nth i s def = nth def s i.
Proof.
(* Goal: None *)
(* Goal: None *)
elim: s i => [|x s IHs i]; first by case.
(* Goal: None *)
by case: i.
Qed.

Module Impl (GenLow : GenLowInterface.Sig) :
  GenHighInterface.Sig GenLow.
Import GenLow.

Definition liftGen {A B} (f: A -> B) (a : G A)
: G B := nosimpl
             (bindGen a (fun x =>
              returnGen (f x))).

(* Goal: None *)
(* Goal: None *)
Definition liftGen2 {A1 A2 B}
           (C : A1 -> A2 -> B) (m1 : G A1) (m2  : G A2)
: G B:=
  nosimpl (bindGen m1 (fun x1 => bindGen m2 (fun x2 => returnGen (C x1 x2)))).

Definition liftGen3 {A1 A2 A3 R} (F : A1 -> A2 -> A3 -> R)
         (m1 : G A1) (m2 : G A2) (m3 : G A3)

: G R := nosimpl(
  bindGen m1 (fun x1 =>
  bindGen m2 (fun x2 =>
  bindGen m3 (fun x3 =>
  returnGen (F x1 x2 x3))))).

(* Goal: None *)
(* Goal: None *)
Definition liftGen4 {A1 A2 A3 A4 R}
           (F : A1 -> A2 -> A3 -> A4 -> R)
           (m1 : G A1) (m2 : G A2) (m3 : G A3) (m4: G A4)
: G R := nosimpl(
  bindGen m1 (fun x1 =>
  bindGen m2 (fun x2 =>
  bindGen m3 (fun x3 =>
  bindGen m4 (fun x4 =>
  returnGen (F x1 x2 x3 x4)))))).

(* Goal: None *)
(* Goal: None *)
Definition liftGen5 {A1 A2 A3 A4 A5 R}
           (F : A1 -> A2 -> A3 -> A4 -> A5 -> R)
           (m1 : G A1) (m2 : G A2) (m3 : G A3) (m4: G A4) (m5 : G A5)
: G R := nosimpl(
  bindGen m1 (fun x1 =>
  bindGen m2 (fun x2 =>
  bindGen m3 (fun x3 =>
  bindGen m4 (fun x4 =>
  bindGen m5 (fun x5 =>
  returnGen (F x1 x2 x3 x4 x5))))))).


(* Goal: None *)
(* Goal: None *)
Definition sequenceGen {A : Type} (ms : list (G A)) : G (list A) :=
  foldr (fun m m' => bindGen m  (fun x =>
                          bindGen m' (fun xs =>
                          returnGen (x :: xs)))) (returnGen [::]) ms.

Fixpoint foldGen {A B : Type} (f : A -> B -> G A) (l : list B) (a : A)
: G A := nosimpl(
  match l with
    | [::] => returnGen a
    | (x :: xs) => bindGen (f a x) (foldGen f xs)
  end).

(* Goal: None *)
(* Goal: None *)
Definition oneOf_ {A : Type} (def: G A) (gs : list (G A)) : G A :=
  bindGen (choose (0, length gs - 1)) (nth def gs).

(* Goal: None *)
(* Goal: None *)
Definition oneof {A} :=
  @deprecate (G A -> list (G A) -> G A) "oneof" "oneOf_" oneOf_.

(* Goal: None *)
(* Goal: None *)
Fixpoint pick {A : Type} (def : G A) (xs : list (nat * G A)) n : nat * G A :=
  match xs with
    | nil => (0, def)
    | (k, x) :: xs =>
      if (n < k) then (k, x)
      else pick def xs (n - k)
  end.

(* This should use urns! *)
(* Goal: None *)
(* Goal: None *)
Fixpoint pickDrop {A : Type} (xs : list (nat * G (option A))) n : nat * G (option A) * list (nat * G (option A)) :=
  match xs with
    | nil => (0, returnGen None, nil)
    | (k, x) :: xs =>
      if (n < k) then  (k, x, xs)
      else let '(k', x', xs') := pickDrop xs (n - k)
           in (k', x', (k,x)::xs')
  end. 

(* Goal: None *)
(* Goal: None *)
Definition sum_fst {A : Type} (gs : list (nat * A)) : nat :=
  foldl (fun t p => t + (fst p)) 0 gs.

(* Goal: None *)
(* Goal: None *)
Definition freq_ {A : Type} (def : G A) (gs : list (nat * G A))
: G A :=
  let tot := sum_fst gs in
  bindGen (choose (0, tot-1)) (fun n =>
  @snd _ _ (pick def gs n)).

(* Goal: None *)
(* Goal: None *)
Definition frequency {A}:= 
  @deprecate (G A -> list (nat * G A) -> G A) "frequency" "freq_" freq_.

(* Goal: None *)
(* Goal: None *)
Fixpoint backtrackFuel {A : Type} (fuel : nat) (tot : nat) (gs : list (nat * G (option A))) : G (option A) :=
  match fuel with 
    | O => returnGen None
    | S fuel' => bindGen (choose (0, tot-1)) (fun n => 
                 let '(k, g, gs') := pickDrop gs n in
                 bindGen g (fun ma =>
                 match ma with 
                   | Some a => returnGen (Some a)
                   | None => backtrackFuel fuel' (tot - k) gs'
                 end ))
  end.

(* Goal: None *)
(* Goal: None *)
Definition backtrack {A : Type} (gs : list (nat * G (option A))) : G (option A) :=
  backtrackFuel (length gs) (sum_fst gs) gs.

(* Goal: None *)
(* Goal: None *)
Definition vectorOf {A : Type} (k : nat) (g : G A)
: G (list A) :=
  foldr (fun m m' =>
                bindGen m (fun x =>
                bindGen m' (fun xs => returnGen (cons x xs)))
             ) (returnGen nil) (nseq k g).

(* Goal: None *)
(* Goal: None *)
Definition listOf {A : Type} (g : G A) : G (list A) :=
  sized (fun n => bindGen (choose (0, n)) (fun k => vectorOf k g)).

(* Goal: None *)
(* Goal: None *)
Definition elems_ {A : Type} (def : A) (l : list A) :=
  let n := length l in
  bindGen (choose (0, n - 1)) (fun n' =>
  returnGen (List.nth n' l def)).

(* Goal: None *)
(* Goal: None *)
Definition elements {A} :=
  @deprecate (A -> list A -> G A) "elements" "elems_" elems_.  

(* Goal: None *)
(* Goal: None *)
Definition bindGenOpt {A B}
           (g : G (option A)) (f : A -> G (option B)) : G (option B) :=
  bindGen g (fun ma =>
    match ma with
    | None => returnGen None
    | Some a => f a
    end).

(* Goal: None *)
(* Goal: None *)
Definition retryBody {A : Type}
           (retry : nat -> G (option A) -> G (option A))
           (n : nat) (g : G (option A)) : G (option A) :=
  bindGen g (fun x =>
               match x, n with
               | Some a, _ => returnGen (Some a)
               | None, O => returnGen None
               | None, S n' => retry n' g
               end).

(* Rerun a generator [g] until it returns a [Some], or stop after
     [n+1] tries. *)
(* Goal: None *)
(* Goal: None *)
Fixpoint retry {A : Type} (n : nat) (g : G (option A)) :
  G (option A) :=
  retryBody retry n g.

(* Filter the output of a generator [g], returning [None] when the
     predicate [p] is [false]. The generator is run once. *)
Definition suchThatMaybe1 {A : Type} (g : G A) (p : A -> bool) :
  G (option A) :=
  fmap (fun x => if p x then Some x else None) g.

(* Retry a generator [g : G A] until it returns a value satisfying
     the predicate, or stop after [size+1] times, where [size] is the
     current size value. *)
Definition suchThatMaybe {A : Type} (g : G A) (p : A -> bool) :
  G (option A) :=
  sized (fun n => retry n (suchThatMaybe1 g p)).

(* Retry a generator [g : G (option A)] until it returns a value
     satisfying the predicate, or stop after [size+1] times, where
     [size] is the current size value. *)
(* Goal: None *)
(* Goal: None *)
Definition suchThatMaybeOpt {A : Type} (g : G (option A))
           (p : A -> bool) : G (option A) :=
  sized (fun n => retry n (fmap (fun x =>
                                   match x with
                                   | None => None
                                   | Some a => if p a then Some a else None
                                   end) g)).

(* Retry a generator until it returns a value, or stop after
     [size+1] times. *)
(* Goal: None *)
(* Goal: None *)
Definition retrySized {A : Type} (g : G (option A)) : G (option A) :=
  sized (fun n => retry n g).

(* * Semantics *)

Lemma semLiftGen {A B} (f: A -> B) (g: G A) :
  semGen (liftGen f g) <--> f @: semGen g.
Proof.
(* Goal: None *)
  rewrite imset_bigcup. apply: eq_bigcupr => size.
(* Goal: None *)
    by rewrite semBindSize (eq_bigcupr _ (fun a => semReturnSize (f a) size)).
Qed.

Ltac solveLiftGenX :=
intros; split; intros;
repeat
  match goal with
    | [ H : exists _, _ |- _ ] => destruct H as [? [? ?]]
    | [ H : semGenSize _ _ _ |- _ ] =>
      try (apply semBindSize in H; destruct H as [? [? ?]]);
      try (apply semReturnSize in H; subst)
  end;
  [ by repeat (eexists; split; [eassumption |])
  | repeat (apply semBindSize; eexists; split; try eassumption);
      by apply semReturnSize ].

Lemma semLiftGenSize {A B} (f: A -> B) (g: G A) size :
  semGenSize (liftGen f g) size <--> f @: (semGenSize g size).
Proof. 
(* Goal: None *)
    by rewrite semBindSize (eq_bigcupr _ (fun a => semReturnSize (f a) size)).
 Qed.

Program Instance liftGenUnsized {A B} (f : A -> B) (g : G A) 
(* Goal: None *)
(* Goal: None *)
        `{Unsized _ g} : Unsized (liftGen f g).
Next Obligation.
(* Goal: None *)
  by rewrite ! semLiftGenSize (unsized s1 s2).
Qed.

Program Instance liftGenMonotonic {A B} (f : A -> B) (g : G A) 
(* Goal: None *)
(* Goal: None *)
        `{SizeMonotonic _ g} : SizeMonotonic (liftGen f g).
Next Obligation.
(* Goal: None *)
  rewrite ! semLiftGenSize. apply imset_incl. by apply monotonic.
Qed.

Lemma semLiftGen2Size {A1 A2 B} (f: A1 -> A2 -> B) (g1 : G A1) (g2 : G A2) s :
  semGenSize (liftGen2 f g1 g2) s <-->
  f @2: (semGenSize g1 s, semGenSize g2 s).
Proof. 
(* Goal: None *)
  rewrite semBindSize curry_imset2l; apply: eq_bigcupr => x.
(* Goal: None *)
    by rewrite semBindSize; apply: eq_bigcupr => y; rewrite semReturnSize.
Qed.

     
Lemma semLiftGen2SizeMonotonic {A1 A2 B} (f: A1 -> A2 -> B)
                               (g1 : G A1) (g2 : G A2) 
(* Goal: None *)
(* Goal: None *)
                               `{SizeMonotonic _ g1} `{SizeMonotonic _ g2} :
  semGen (liftGen2 f g1 g2) <--> f @2: (semGen g1, semGen g2).
Proof.
(* Goal: None *)
  rewrite /semGen. setoid_rewrite semLiftGen2Size.
(* Goal: None *)
  move => b. split. 
  - move => [sb [_ Hb]]. (* point-free reasoning would be nice here *)
(* Goal: None *)
    destruct Hb as [a [[Hb11 Hb12] Hb2]]. exists a. split; [| by apply Hb2].
(* Goal: None *)
    split; eexists; by split; [| eassumption].
  - move => [[a1 a2] [[[s1 [_ G1]] [s2 [_ G2]]] Hf]]. compute in Hf.
(* Goal: None *)
    exists (max s1 s2). split; first by [].
(* Goal: None *)
    exists (a1,a2). split; last by []. split => /=;
    (eapply monotonic; last eassumption); 
    apply/leP; solve [ apply Max.le_max_l | apply Max.le_max_r ].
Qed.

Lemma semLiftGen2Unsized1 {A1 A2 B} (f: A1 -> A2 -> B)
(* Goal: None *)
(* Goal: None *)
      (g1 : G A1) (g2 : G A2) `{Unsized _ g1}:
  semGen (liftGen2 f g1 g2) <--> f @2: (semGen g1, semGen g2).
Proof.
(* Goal: None *)
  rewrite /semGen. setoid_rewrite semLiftGen2Size.
(* Goal: None *)
  move=> b. split.
  - move => [n [_ [[a1 a2] [[/= H2 H3] H4]]]]. exists (a1, a2).
    split; auto; split; eexists; split; eauto; reflexivity.
  - move => [[a1 a2] [[[s1 /= [H2 H2']] [s2 [H3 H3']]] H4]].
    eexists. split; first by eauto. 
    exists (a1, a2); split; eauto.
(* Goal: None *)
(* Goal: None *)
    split; last by eauto. simpl. 
    eapply unsized; eauto; apply (unsized2 H); eauto.
Qed.
  
Lemma semLiftGen2Unsized2 {A1 A2 B} (f: A1 -> A2 -> B)
(* Goal: None *)
(* Goal: None *)
      (g1 : G A1) (g2 : G A2) `{Unsized _ g2}:
  semGen (liftGen2 f g1 g2) <--> f @2: (semGen g1, semGen g2).
Proof.
(* Goal: None *)
  rewrite /semGen. setoid_rewrite semLiftGen2Size.
(* Goal: None *)
  move=> b. split. 
  - move => [n [_ [[a1 a2] [[/= H2 H3] H4]]]]. exists (a1, a2).
    split; auto; split; eexists; split; eauto; reflexivity.
  - move => [[a1 a2] [[[s1 /= [H2 H2']] [s2 [H3 H3']]] H4]].
    eexists. split; first by auto.
(* Goal: None *)
    exists (a1, a2). split; eauto.
(* Goal: None *)
(* Goal: None *)
    split; first by eauto. simpl. 
    eapply unsized; eauto.
Qed.

Lemma semLiftGen3Size :
forall {A1 A2 A3 B} (f: A1 -> A2 -> A3 -> B)
       (g1: G A1) (g2: G A2) (g3: G A3) size,
  semGenSize (liftGen3 f g1 g2 g3) size <-->
  fun b =>
    exists a1, semGenSize g1 size a1 /\
               (exists a2, semGenSize g2 size a2 /\
                           (exists a3, semGenSize g3 size a3 /\
                                       (f a1 a2 a3) = b)).
Proof. solveLiftGenX. Qed.

Program Instance liftGen2Unsized {A1 A2 B} (f : A1 -> A2 -> B) (g1 : G A1)
(* Goal: None *)
(* Goal: None *)
        `{Unsized _ g1} (g2 : G A2) `{Unsized _ g2} : Unsized (liftGen2 f g1 g2).
Next Obligation.
(* Goal: None *)
  rewrite ! semLiftGen2Size. 
(* Goal: None *)
  rewrite ! curry_imset2l. by setoid_rewrite (unsized s1 s2).
Qed.

Program Instance liftGen2Monotonic {A1 A2 B} (f : A1 -> A2 -> B) (g1 : G A1)
(* Goal: None *)
(* Goal: None *)
        `{SizeMonotonic _ g1} (g2 : G A2) `{SizeMonotonic _ g2} : 
  SizeMonotonic (liftGen2 f g1 g2).
Next Obligation.
(* Goal: None *)
  rewrite ! semLiftGen2Size. rewrite ! curry_imset2l. 
  move => b [a1 [Ha1 [a2 [Ha2 <-]]]].
(* Goal: None *)
  do 2 (eexists; split; first by eapply (monotonic H1); eauto).
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
  reflexivity.
Qed.


(* CH: Made this more beautiful than the rest *)
(* CH: Should anyway use dependent types for a generic liftGenN *)
Lemma semLiftGen4Size A1 A2 A3 A4 B (f : A1 -> A2 -> A3 -> A4 -> B)
                     (g1 : G A1) (g2 : G A2) (g3 : G A3) (g4 : G A4) s :
  semGenSize (liftGen4 f g1 g2 g3 g4) s <-->
  [set b : B | exists a1 a2 a3 a4, semGenSize g1 s a1 /\ semGenSize g2 s a2 /\
                 semGenSize g3 s a3 /\ semGenSize g4 s a4 /\ f a1 a2 a3 a4 = b].
Proof.
  split; unfold liftGen4; intros.
  - repeat match goal with
    | [ H : semGenSize _ _ _ |- _ ] =>
      try (apply semBindSize in H; destruct H as [? [? ?]]);
      try (apply semReturnSize in H; subst)
    end.
    do 4 eexists. repeat (split; [eassumption|]). assumption.
  - repeat match goal with
    | [ H : exists _, _ |- _ ] => destruct H as [? [? ?]]
    | [ H : and _ _ |- _ ] => destruct H as [? ?]
    end.
    repeat (apply semBindSize; eexists; split; [eassumption|]).
    apply semReturnSize. assumption.
Qed.

(* begin semLiftGen4SizeMonotonic *)
Lemma semLiftGen4SizeMonotonic A1 A2 A3 A4 B (f : A1 -> A2 -> A3 -> A4 -> B)
                               (g1 : G A1) (g2 : G A2) (g3 : G A3) (g4 : G A4)
(* Goal: None *)
(* Goal: None *)
                               `{SizeMonotonic _ g1} `{SizeMonotonic _ g2}
(* Goal: None *)
(* Goal: None *)
                               `{SizeMonotonic _ g3} `{SizeMonotonic _ g4} :
  semGen (liftGen4 f g1 g2 g3 g4) <-->
  [set b : B | exists a1 a2 a3 a4, semGen g1 a1 /\ semGen g2 a2 /\
                 semGen g3 a3 /\ semGen g4 a4 /\ f a1 a2 a3 a4 = b].
(* end semLiftGen4SizeMonotonic *)
Proof.
(* Goal: None *)
  rewrite /semGen. setoid_rewrite semLiftGen4Size.
(* Goal: None *)
  move => b. split. 
  - move => [s [_ [a1 [a2 [a3 [a4 [Ha1 [Ha2 [Ha3 [Ha4 Hb]]]]]]]]]]; subst.
    exists a1. exists a2. exists a3. exists a4. 
    repeat split; exists s; (split; [reflexivity | eassumption ]). 
  -  move => [a1 [a2 [a3 [a4 [[s1 [_ Ha1]] 
                                [[s2 [_ Ha2]] 
                                   [[s3 [_ Ha3]] 
                                      [[s4 [_ Ha4]] Hb]]]]]]]]; subst.
    exists (max s1 (max s2 (max s3 s4))). 
(* Goal: None *)
(* Goal: None *)
    split; first by [].
    exists a1. exists a2. exists a3. exists a4. 
    repeat split; (eapply monotonic; [ apply/leP | ]; last eassumption).
(* Goal: None *)
    by eapply Max.le_max_l.
(* Goal: None *)
    eapply Nat.max_le_iff. right. by eapply Max.le_max_l.
    eapply Nat.max_le_iff. right.
(* Goal: None *)
    eapply Nat.max_le_iff. right. by eapply Max.le_max_l.
    eapply Nat.max_le_iff. right.
    eapply Nat.max_le_iff. right.
    eapply Nat.max_le_iff. by right. 
Qed.

(* Goal: None *)
(* Goal: None *)
Program Instance liftGen4Monotonic {A B C D E} 
        (f : A -> B -> C -> D -> E)
        (g1 : G A) (g2 : G B) (g3 : G C) (g4 : G D) 
(* Goal: None *)
(* Goal: None *)
        `{ SizeMonotonic _ g1} `{ SizeMonotonic _ g2}
(* Goal: None *)
(* Goal: None *)
        `{ SizeMonotonic _ g3} `{ SizeMonotonic _ g4} 
: SizeMonotonic (liftGen4 f g1 g2 g3 g4). 
Next Obligation.
(* Goal: None *)
  rewrite ! semLiftGen4Size.
(* Goal: None *)
  move => t /= [a1 [a2 [a3 [a4 [Ha1 [Ha2 [Ha3 [Ha4 H5]]]]]]]]; subst.
  eexists. eexists. eexists. eexists. 
  repeat (split; try reflexivity); by eapply monotonic; eauto. 
Qed.

Lemma semLiftGen5Size :
forall {A1 A2 A3 A4 A5 B} (f: A1 -> A2 -> A3 -> A4 -> A5 -> B)
       (g1: G A1) (g2: G A2) (g3: G A3) (g4: G A4) (g5: G A5) size,
  semGenSize (liftGen5 f g1 g2 g3 g4 g5) size <-->
  fun b =>
    exists a1, semGenSize g1 size a1 /\
               (exists a2, semGenSize g2 size a2 /\
                           (exists a3, semGenSize g3 size a3 /\
                                       (exists a4, semGenSize g4 size a4 /\
                                                   (exists a5, semGenSize g5 size a5 /\
                                                               (f a1 a2 a3 a4 a5) = b)))).
Proof. solveLiftGenX. Qed.

Lemma Forall2_cons T U (P : T -> U -> Prop) x1 s1 x2 s2 :
  List.Forall2 P (x1 :: s1) (x2 :: s2) <-> P x1 x2 /\ List.Forall2 P s1 s2.
Proof.
(* Goal: None *)
(* Goal: None *)
split=> [H|[? ?]]; last by constructor.
(* Goal: None *)
by inversion H.
Qed.

Lemma semSequenceGenSize A (gs : list (G A)) n :
  semGenSize (sequenceGen gs) n <-->
  [set l | length l = length gs /\
    List.Forall2 (fun y => semGenSize y n) gs l].
Proof.
(* Goal: None *)
elim: gs => [|g gs IHgs].
(* Goal: None *)
  by rewrite semReturnSize /set1; case=> // a l; split=> // [[]].
(* Goal: None *)
rewrite /= semBindSize; setoid_rewrite semBindSize; setoid_rewrite semReturnSize.
(* Goal: None *)
setoid_rewrite IHgs; case=> [| x l].
(* Goal: None *)
(* Goal: None *)
  split; first by case=> ? [? [? [?]]].
(* Goal: None *)
  by move=> H; inversion H.
rewrite Forall2_cons; split; first by case=> y [gen_y [s [[<- ?]]]] [<- <-].
(* Goal: None *)
by case=> [[<-] [? ?]]; exists x; split => //; exists l; split.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma Forall2_SizeMonotonic {A} x n (gs : list (G A)) l :
  x <= n -> gs \subset SizeMonotonic -> 
  List.Forall2 (semGenSize^~ x) gs l ->
  List.Forall2 (semGenSize^~ n) gs l.
Proof. 
(* Goal: None *)
  intros. induction H1; auto.
(* Goal: None *)
  apply subconsset in H0. destruct H0; auto. 
(* Goal: None *)
  constructor; auto. eapply H0; eauto.
Qed.

Lemma semSequenceGenSizeMonotonic A (gs : list (G A)) :
  (gs \subset SizeMonotonic) ->
  semGen (sequenceGen gs) <-->
  [set l | length l = length gs /\
    List.Forall2 semGen gs l].
Proof.
(* Goal: None *)
  intros. rewrite /semGen. setoid_rewrite semSequenceGenSize.
(* Goal: None *)
  move => l. split.
  - move => [n [ _ [H1 H2]]]. split; auto.
(* Goal: None *)
    induction H2; subst; simpl; constructor.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    + exists n. split; auto. reflexivity. 
    + apply IHForall2; eauto. 
(* Goal: None *)
      apply subconsset in H. destruct H; auto. 
  - move => [H1 H2]. revert gs H H1 H2. induction l; intros gs H H1 H2.
    + destruct gs; try discriminate. exists 0. 
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
      split; auto. reflexivity.
    + destruct gs; try discriminate.
(* Goal: None *)
      apply subconsset in H. move : H => [H3 H4].  
(* Goal: None *)
      inversion H2; subst. destruct H6 as [n [ _ H5]].
(* Goal: None *)
      eapply IHl in H8; auto. destruct H8 as [x [_ [H7 H8]]].
(* Goal: None *)
      destruct (x <= n) eqn:Hle. 
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
      { exists n. split; eauto; first by reflexivity. split; auto. 
(* Goal: None *)
        constructor; auto. eapply Forall2_SizeMonotonic; eauto. }
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
      { exists x.  split; first by reflexivity. split; auto.
        constructor; auto. eapply H3; last by eassumption. 
        rewrite -> leq_eqVlt, -> Bool.orb_false_iff in Hle. 
        destruct Hle; auto. rewrite leqNgt H0 //. }
Qed.
 
(* Goal: None *)
(* Goal: None *)
Lemma semVectorOfSize {A : Type} (k : nat) (g : G A) n :
  semGenSize (vectorOf k g) n <-->
  [set l | length l = k /\ l \subset (semGenSize g n)].
Proof.
(* Goal: None *)
elim: k => [|k IHk].
(* Goal: None *)
  rewrite /vectorOf /= semReturnSize.
(* Goal: None *)
  by move=> s; split=> [<-|[] /size0nil ->] //; split.
(* Goal: None *)
rewrite /vectorOf /= semBindSize; setoid_rewrite semBindSize.
(* Goal: None *)
setoid_rewrite semReturnSize; setoid_rewrite IHk.
(* Goal: None *)
(* Goal: None *)
case=> [|x l]; first by split=> [[? [? [? [?]]]] | []].
split=> [[y [gen_y [l' [[length_l' ?] [<- <-]]]]]|] /=.
(* Goal: None *)
(* Goal: None *)
  split; first by rewrite length_l'.
(* Goal: None *)
(* Goal: None *)
  exact/subconsset.
(* Goal: None *)
by case=> [[?]] /subconsset [? ?]; exists x; split => //; exists l.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semVectorOfUnsized {A} (g : G A) (k : nat) `{Unsized _ g}: 
  semGen (vectorOf k g) <--> [set l | length l = k /\ l \subset semGen g ]. 
Proof.
(* Goal: None *)
  rewrite /semGen.
(* Goal: None *)
  setoid_rewrite semVectorOfSize.
(* Goal: None *)
  move => l; split.
  - move => [k' [_ [H1 H2]]]. split; auto. exists k'. split; auto.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    reflexivity.
  - move => [H1 H2]. 
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    exists k. split; first by reflexivity.
(* Goal: None *)
    split; auto. move => a /H2 [x [_ Hx]]. 
    by eapply unsized; eauto.
Qed.

(* Goal: None *)
(* Goal: None *)
Program Instance vectorOfUnsized {A} (k : nat) (g : G A) 
(* Goal: None *)
(* Goal: None *)
        `{Unsized _ g } : Unsized (vectorOf k g).
Next Obligation.
(* Goal: None *)
  rewrite ! semVectorOfSize. 
(* Goal: None *)
  split; move => [H1 H2]; split => //; by rewrite unsized; eauto.
Qed.

(* Goal: None *)
(* Goal: None *)
Program Instance vectorOfMonotonic {A} (k : nat) (g : G A) 
(* Goal: None *)
(* Goal: None *)
        `{SizeMonotonic _ g } : SizeMonotonic (vectorOf k g).
Next Obligation.
(* Goal: None *)
  rewrite ! semVectorOfSize. 
(* Goal: None *)
  move => l [H1 H2]; split => // a Ha. by eapply (monotonic H0); eauto.
Qed.


(* Goal: None *)
(* Goal: None *)
Lemma semListOfSize {A : Type} (g : G A) size :
  semGenSize (listOf g) size <-->
  [set l | length l <= size /\ l \subset (semGenSize g size)].
Proof.
(* Goal: None *)
rewrite /listOf semSizedSize semBindSize; setoid_rewrite semVectorOfSize.
rewrite semChooseSize // => l; split=> [[n [/andP [_ ?] [-> ?]]]| [? ?]] //.
(* Goal: None *)
by exists (length l).
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semListOfUnsized {A} (g : G A) (k : nat) `{Unsized _ g} : 
  semGen (listOf g) <--> [set l | l \subset semGen g ]. 
Proof.
(* Goal: None *)
  rewrite /semGen.
(* Goal: None *)
  setoid_rewrite semListOfSize. 
(* Goal: None *)
  move => l; split.
  - move => [k' [_ [H1 H2]]]. exists k'. split; auto.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    reflexivity.
  - move => Hl. exists (length l). repeat split => //.
    move => a /Hl [s [_ Ha]]. by eapply unsized; eauto.
Qed.

(* Goal: None *)
(* Goal: None *)
Program Instance listOfMonotonic {A} (g : G A) 
(* Goal: None *)
(* Goal: None *)
        `{SizeMonotonic _ g } : SizeMonotonic (listOf g).
Next Obligation.
(* Goal: None *)
  rewrite ! semListOfSize.
(* Goal: None *)
  move => l [H1 H2]; split => //. by eapply leq_trans; eauto.
(* Goal: None *)
  move => a /H2 Ha. by eapply monotonic; eauto.
Qed.


(* Goal: None *)
(* Goal: None *)
Lemma In_nth_exists {A} (l: list A) x def :
  List.In x l -> exists n, nth def l n = x /\ (n < length l)%coq_nat.
Proof.
(* Goal: None *)
elim : l => [| a l IHl] //=.
(* Goal: None *)
move => [H | /IHl [n [H1 H2]]]; subst.
(* Goal: @ex nat (fun n : nat => and (@eq A (@nth A def (@cons A x l) n) x) (lt n (S (@Datatypes.length A l)))) *)
(* Goal: @ex nat (fun n0 : nat => and (@eq A (@nth A def (@cons A a l) n0) (@nth A def l n)) (lt n0 (S (@Datatypes.length A l)))) *)
  exists 0; split => //; omega.
exists n.+1; split => //; omega.
Qed.

Lemma nth_imset T (def : T) l : nth def l @: [set n | n < length l] <--> l.
Proof.
(* Goal: None *)
(* Goal: None *)
case: l => [|x l] t; first by split=> //; case=> ?; rewrite ltn0; case.
split; first by case=> n [? <-]; rewrite -nthE; apply/List.nth_In/ltP.
(* Goal: None *)
by case/(In_nth_exists _ _ def) => n [? ?]; exists n; split=> //; apply/ltP.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semOneofSize {A} (l : list (G A)) (def : G A) s : semGenSize (oneof def l) s
  <--> if l is nil then semGenSize def s else \bigcup_(x in l) semGenSize x s.
Proof.
(* Goal: is_true b *)
case: l => [|g l].
(* Goal: None *)
  rewrite semBindSize semChooseSize //.
  rewrite (eq_bigcupl [set 0]) ?bigcup_set1 // => a; split=> [/andP [? ?]|<-] //.
(* Goal: None *)
  by apply/antisym/andP.
(* Goal: None *)
rewrite semBindSize semChooseSize //.
(* Goal: None *)
set X := (fun a : nat => is_true (_ && _)).
(* Goal: None *)
by rewrite (reindex_bigcup (nth def (g :: l)) X) // /X subn1 nth_imset.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semOneof {A} (l : list (G A)) (def : G A) :
  semGen (oneof def l) <-->
  if l is nil then semGen def else \bigcup_(x in l) semGen x.
Proof.
by case: l => [|g l]; rewrite 1?bigcupC; apply: eq_bigcupr => sz;
  apply: semOneofSize.
Qed.

(* Goal: None *)
(* Goal: None *)
Program Instance oneofMonotonic {A} (x : G A) (l : list (G A))
(* Goal: None *)
(* Goal: None *)
        `{ SizeMonotonic _ x} `(l \subset SizeMonotonic) 
: SizeMonotonic (oneof x l). 
Next Obligation.
(* Goal: None *)
  rewrite !semOneofSize. elim : l H0 => [_ | g gs IH /subconsset [H2 H3]] /=.
(* Goal: None *)
  - by apply monotonic.
  - specialize (IH H3). move => a [ga [[Hga | Hga] Hgen]]; subst.
(* Goal: None *)
    exists ga. split => //. left => //.
(* Goal: None *)
    eapply monotonic; eauto. exists ga.
(* Goal: None *)
    split. right => //.
(* Goal: None *)
    apply H3 in Hga. by apply (monotonic H1). 
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semElementsSize {A} (l: list A) (def : A) s :
  semGenSize (elements def l) s <--> if l is nil then [set def] else l.
Proof.
(* Goal: None *)
rewrite semBindSize.
(* Goal: None *)
setoid_rewrite semReturnSize.
(* Goal: None *)
rewrite semChooseSize //=.
(* Goal: @set_eq A (@bigcup nat A (fun a : nat => is_true (ssrnat.leq a (subn (@Datatypes.length A l) (S O)))) (fun a : nat => @set1 A (@List.nth A a l def))) match l with | nil => @set1 A def | cons a l0 => @seq_In A l end *)
setoid_rewrite nthE. (* SLOW *)
(* Goal: is_true b *)
case: l => [|x l] /=.
(* Goal: None *)
  rewrite (eq_bigcupl [set 0]) ?bigcup_set1 // => n.
  by rewrite leqn0; split=> [/eqP|->].
rewrite -(@reindex_bigcup _ _ _ (nth def (x :: l)) _ (x :: l)) ?coverE //.
(* Goal: None *)
by rewrite subn1 /= nth_imset.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semElements {A} (l: list A) (def : A) :
  (semGen (elements def l)) <--> if l is nil then [set def] else l.
Proof.
(* Goal: None *)
rewrite /semGen; setoid_rewrite semElementsSize; rewrite bigcup_const //.
(* Goal: None *)
by do 2! constructor.
Qed.

(* Goal: None *)
(* Goal: None *)
Program Instance elementsUnsized {A} {def : A} (l : list A) : 
  Unsized (elements def l).
Next Obligation.
(* Goal: None *)
  rewrite ! semElementsSize. by case: l.
Qed.

(* A rather long frequency proof, probably we can do better *)

Lemma not_lt : forall n m, (false = (n < m)) -> (m <= n).
Proof.
(* Goal: None *)
  move => n m. by elim: n m=> [| n IHn]; case.
Qed.

Lemma sum_fstE A x (a : A) l:
  sum_fst ((x, a) :: l) = x + sum_fst l.
Proof.
(* Goal: None *)
rewrite /sum_fst /=.
(* Goal: None *)
(* Goal: None *)
elim: l 0 x => [n x|[n1 x1] l IHl p q] /=; first by rewrite addnC.
by rewrite -IHl; congr foldl; rewrite addnAC.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma sum_fst_eq0P {A} (l : list (nat * A)) :
  sum_fst l = 0 <-> [seq x <- l | x.1 != 0] = [::].
Proof.
(* Goal: None *)
by elim: l => [|[[|n] x] l IHl] //=; split=> //; rewrite sum_fstE.
Qed.

Lemma pick_def :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G A)) n def,
    sum_fst l <= n ->
    pick def l n = (0, def).
Proof.
(* Goal: None *)
  move=> A l n def Hleq.
(* Goal: None *)
  elim : l n Hleq => //=. case=> //= i p l IHl n Hleq.
(* Goal: None *)
  rewrite sum_fstE in Hleq.
(* Goal: None *)
  remember (n < i). case: b Heqb => Heqb; symmetry in Heqb.
  - have : (i + sum_fst l) < i by eapply (leq_ltn_trans); eassumption.
    rewrite -ltn_subRL. by have -> : forall i, (i - i) = 0 by elim.
  - apply IHl. rewrite -(leq_add2r i) subnK.
(* Goal: None *)
      by rewrite addnC. by apply/not_lt.
Qed.

Lemma pick_exists :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G A)) n def,
    n <  sum_fst l <->
    exists x, List.In x l /\ pick def l n = x /\ fst x <> 0.
Proof.
(* Goal: None *)
  move => A l n def. split.
  - move => Hlt.
(* Goal: None *)
    elim : l n Hlt => //. case => i p xs IHxs n Hlt.
(* Goal: None *)
    rewrite sum_fstE in Hlt.
    move/(_ (n-i)) : IHxs => IHxs. simpl.
(* Goal: None *)
    remember (n < i). case: b Heqb => [Heqb | /not_lt Heqb].
    + exists (i, p). split => //=. by left.  split => //=.
(* Goal: None *)
      move => contra; subst. by rewrite ltn0 in Heqb.
    + rewrite -(ltn_add2r i) [X in _  < X]addnC subnK // in IHxs.
(* Goal: None *)
      move/(_ Hlt) : IHxs => [x [H1 [H2 H3]]].
(* Goal: None *)
      by exists x; split; [right | split].
  - move => [x [HIn [Hpick Hneq]]].
(* Goal: None *)
    remember (n < sum_fst l).
    case: b  Heqb => //= /not_lt/pick_def H.
    rewrite H in Hpick. rewrite -Hpick //= in Hneq.
Qed.

Lemma pick_In :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G A)) x def,
    List.In x l /\ fst x <> 0 ->
    exists n, pick def l n = x.
Proof.
(* Goal: None *)
  move => A l x def [HIn Hfst].
(* Goal: None *)
  elim : l HIn => //=. case => //= i g xs IHxs [H1 | H2]; subst.
  + exists 0. simpl in *.
    have H : 0 < i by  elim : i Hfst IHxs => //=.
(* Goal: None *)
    rewrite H.
(* Goal: None *)
      by split => //=.
  + move/(_ H2) : IHxs => [n Hpick].
    exists (n + i). rewrite -[X in _ < X]add0n ltn_add2r ltn0.
      by rewrite  -[X in _ - X]add0n subnDr subn0.
Qed.

Lemma pick_imset A (def : G A) l :
  pick def l @: [set m | m < sum_fst l] <--> [seq x <- l | x.1 != 0].
Proof.
(* Goal: None *)
elim: l => [|[n x] l IHl] /=.
(* Goal: None *)
  rewrite /sum_fst /=.
  have->: (fun m => m < 0) <--> set0 by [].
(* Goal: None *)
  by rewrite imset0.
(* Goal: is_true b *)
case: n => /= [|n].
  rewrite -IHl => t; split=> [[y []]|].
    by rewrite sum_fstE add0n subn0 => lt_y <-; exists y.
  by case=> y [lt_y <-]; exists y; split=> //; rewrite subn0.
(* Goal: None *)
move=> t; split=> /= [[p [lt_p]]|].
  case: ifP => [_ <-|lt_pn ?]; first by left.
    right; rewrite -(IHl t); exists (p - n.+1); split=> //.
(* Goal: None *)
  rewrite sum_fstE in lt_p.
  by rewrite -(ltn_add2r n.+1) subnK 1?addnC // leqNgt lt_pn.
case=> [<-|]; first by exists 0; split => //; rewrite sum_fstE.
(* Goal: None *)
rewrite -(IHl t); case=> p [lt_p <-]; exists (n.+1 + p); split.
(* Goal: None *)
  by rewrite sum_fstE ltn_add2l.
(* Goal: None *)
by rewrite ltnNge leq_addr addKn.
Qed.

Lemma pickDrop_def :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G (option A))) n,
    sum_fst l <= n ->
    pickDrop l n = (0, returnGen None, l).
Proof.
(* Goal: None *)
  move=> A l n Hleq.
(* Goal: None *)
  elim : l n Hleq => //=. case=> //= i p l IHl n Hleq.
(* Goal: None *)
  rewrite sum_fstE in Hleq.
(* Goal: None *)
  remember (n < i). case: b Heqb => Heqb; symmetry in Heqb.
  - have : (i + sum_fst l) < i by eapply (leq_ltn_trans); eassumption.
    rewrite -ltn_subRL. by have -> : forall i, (i - i) = 0 by elim.
  - rewrite IHl; auto. rewrite -(leq_add2r i) subnK.
(* Goal: None *)
      by rewrite addnC. by apply/not_lt.
Qed.

(* Probably needs something about l' and l. *)
(* ZP : added a few things *)
Lemma pickDrop_exists :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G (option A))) n,
    n <  sum_fst l <->
    exists k g l',
      List.In (k,g) l /\ pickDrop l n = (k,g,l') /\ k <> 0 /\
      l <--> [set (k, g)] :|: l' /\
      length l' + 1 = length l /\
      sum_fst l' + k = sum_fst l.
Proof.
(* Goal: None *)
  move => A l n. split.
  - move => Hlt.
(* Goal: None *)
    elim : l n Hlt => //. case => i p xs IHxs n Hlt.
(* Goal: None *)
    rewrite sum_fstE in Hlt.
    move/(_ (n-i)) : IHxs => IHxs. simpl.
(* Goal: None *)
    remember (n < i). case: b Heqb => [Heqb | /not_lt Heqb].
    + exists i. exists p. exists xs. split => //=. by left.  split => //=.
(* Goal: None *)
      split. move => contra; subst. by rewrite ltn0 in Heqb.
(* Goal: None *)
      split. by rewrite cons_set_eq.
(* Goal: None *)
      split. by ssromega.
(* Goal: None *)
      rewrite sum_fstE. by ssromega.
    + rewrite -(ltn_add2r i) [X in _  < X]addnC subnK // in IHxs.
(* Goal: None *)
      move/(_ Hlt) : IHxs => [k [g [gs [H1 [H2 [H3 [H4 [H5 H6]]]]]]]].
(* Goal: None *)
      exists k. exists g. exists ((i,p)::gs).
      split; [right | split; [| split; [| split; [| split]]]];
      try (simpl; eauto; by rewrite H2).
(* Goal: None *)
      rewrite !cons_set_eq H4.
      rewrite setU_assoc (setU_comm [set (i, p)]) -setU_assoc.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
      reflexivity.
(* Goal: None *)
      simpl. by ssromega.
(* Goal: None *)
      simpl. rewrite !sum_fstE. by ssromega.
  - move => [k [g [gs [HIn [Hpick [Hneq _]]]]]].
(* Goal: None *)
    remember (n < sum_fst l).
    case: b  Heqb => //= /not_lt/pickDrop_def H.
(* Goal: None *)
    rewrite H in Hpick. 
(* Goal: is_true false *)
    inversion Hpick; subst; eauto.
Qed.

Lemma pickDrop_In :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G (option A))) k x,
    List.In (k,x) l /\ k <> 0 ->
    exists n l', pickDrop l n = (k,x,l').
Proof.
(* Goal: None *)
  move => A l k x [HIn Hfst].
(* Goal: None *)
  elim : l HIn => //=. case => //= i g xs IHxs [H1 | H2]; subst.
  + exists 0.  exists xs. simpl in *.
(* Goal: None *)
    inversion H1; subst; clear H1.
    have H : 0 < k by  elim : k Hfst IHxs => //=.
(* Goal: None *)
    rewrite H.
(* Goal: None *)
      by split => //=.
  + move/(_ H2) : IHxs => [n [l' Hpick]].
    exists (n + i). exists ((i,g)::l'). 
    rewrite -[X in _ < X]add0n ltn_add2r ltn0.
    rewrite  -[X in _ - X]add0n subnDr subn0.
(* Goal: None *)
    by rewrite Hpick.
Qed.

Lemma pickDrop_In_strong :
(* Goal: None *)
(* Goal: None *)
  forall {A} (l: list (nat * G (option A))) k x,
    List.In (k,x) l /\ k <> 0 ->
    exists n l',
      pickDrop l n = (k,x,l') /\
      n < sum_fst l /\
      length l = length l' + 1.
Proof.
(* Goal: None *)
  move => A l k x [HIn Hfst].
(* Goal: None *)
  elim : l HIn => //=. case => //= i g xs IHxs [H1 | H2]; subst.
  + exists 0.  exists xs. simpl in *.
(* Goal: None *)
    inversion H1; subst; clear H1.
    have H : 0 < k by  elim : k Hfst IHxs => //=.
(* Goal: None *)
    rewrite H. split ; [| split ]; simpl; auto.
(* Goal: None *)
    rewrite sum_fstE. now ssromega.
(* Goal: None *)
    now ssromega.
  + move/(_ H2) : IHxs => [n [l' [Hpick [Hlt Hlen]]]].
    exists (n + i). exists ((i,g)::l'). 
    rewrite -[X in _ < X]add0n ltn_add2r ltn0.
    rewrite  -[X in _ - X]add0n subnDr subn0.
(* Goal: None *)
    rewrite Hpick. simpl.
(* Goal: None *)
    split ; [| split ]; simpl; auto.
(* Goal: None *)
    rewrite sum_fstE. now ssromega.
(* Goal: None *)
    now ssromega.
Qed.

(* begin semFrequencySize *)
(* Goal: None *)
(* Goal: None *)
Lemma semFrequencySize {A}
      (l : list (nat * G A)) (def : G A) (size: nat) :
  semGenSize (frequency def l) size <-->
    let l' := [seq x <- l | x.1 != 0] in
    if l' is nil then semGenSize def size else
      \bigcup_(x in l') semGenSize x.2 size.
(* end semFrequencySize *)
Proof.
(* Goal: None *)
rewrite semBindSize semChooseSize //=.
case lsupp: {1}[seq x <- l | x.1 != 0] => [|[n g] gs].
move/sum_fst_eq0P: lsupp => suml; rewrite suml.
(* Goal: None *)
  rewrite (@eq_bigcupl _ _ _ [set 0]) ?bigcup_set1 ?pick_def // ?leqn0 ?suml //.
  by move=> n; split; rewrite leqn0; [move/eqP|] => ->.
symmetry; apply: reindex_bigcup.
(* Goal: None *)
have pos_suml: 0 < sum_fst l.
(* Goal: None *)
  have [] := sum_fst_eq0P l.
(* Goal: None *)
  by rewrite lsupp; case: (sum_fst l) => // /(_ erefl).
have->: (fun a : nat => a <= sum_fst l - 1) <--> [set m | m < sum_fst l].
  by move=> m /=; rewrite -ltnS subn1 prednK.
(* Goal: None *)
(* Goal: None *)
exact: pick_imset.
Qed.

(* begin semFrequency *)
(* Goal: None *)
(* Goal: None *)
Lemma semFrequency {A} (l : list (nat * G A)) (def : G A) :
  semGen (frequency def l) <-->
    let l' := [seq x <- l | x.1 != 0] in
    if l' is nil then semGen def else
      \bigcup_(x in l') semGen x.2.
(* end semFrequency *)
Proof.
by case lsupp: {1}[seq x <- l | x.1 != 0] => [|[n g] gs] /=;
rewrite 1?bigcupC; apply: eq_bigcupr => sz;
have := (semFrequencySize l def sz); rewrite lsupp.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma frequencySizeMonotonic {A} (g0 : G A) lg :
  SizeMonotonic g0 ->
  List.Forall (fun p => SizeMonotonic (snd p)) lg ->
  SizeMonotonic (frequency g0 lg).
Proof.
  intros H1.  unfold frequency.
  intros Hall. eapply bindMonotonicStrong.
(* Goal: None *)
  eauto with typeclass_instances.
  intros x Heq. eapply semChoose in Heq; eauto.  
(* Goal: None *)
  move : Heq => /andP [Hep1 Heq2]. 
  destruct (sum_fst lg) eqn:Heq.
  - rewrite pick_def. eassumption.
    subst. ssromega.
  - edestruct (pick_exists lg x g0) as [[[n' g] [Hin [Hp Hg]]] H2].
(* Goal: None *)
    rewrite Heq. unfold leq, super, ChooseNat, OrdNat in Hep1, Heq2.
(* Goal: None *)
    ssromega.
    eapply List.Forall_forall in Hall; [ | ].
    eassumption.
    subst. rewrite Hp. eassumption.
Qed.

Instance frequencySizeMonotonic_alt :
(* Goal: None *)
(* Goal: None *)
  forall {A : Type} (g0 : G A) (lg : seq (nat * G A)),
    SizeMonotonic g0 ->
    lg \subset [set x | SizeMonotonic x.2 ] ->
    SizeMonotonic (frequency g0 lg).
Proof.
(* Goal: None *)
  intros A g ls Hm Hin.
  eapply frequencySizeMonotonic. eassumption.
(* Goal: None *)
  induction ls. now constructor.
(* Goal: None *)
(* Goal: None *)
  constructor. eapply Hin.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
  constructor. reflexivity.
(* Goal: None *)
  eapply IHls.  eapply subset_trans; eauto.
  constructor 2. eassumption.
Qed.

Lemma eq_lt_0 : (fun x => x <= 0) <--> [set 0].
Proof. 
(* Goal: @set_eq nat (fun x : nat => is_true (ssrnat.leq x O)) (@set1 nat O) *)
  move => x; split => H; auto.
  - destruct x; auto. 
    + unfold set1; auto.
    + inversion H.
  - inversion H; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semBacktrackFuelDef {A} fuel (l : list (nat * G (option A))) size :
  sum_fst l = 0 -> 
  semGenSize (backtrackFuel fuel 0 l) size <--> [set None].
Proof.
(* Goal: None *)
  move: l size. 
(* Goal: None *)
  induction fuel => l size HSum //=.
  - by rewrite semReturnSize.
  - rewrite semBindSize semChooseSize //=.
(* Goal: None *)
    rewrite (@eq_bigcupl _ _ _ [set 0]) ?bigcup_set1 ?pickDrop_def // ?sub0n ?leqn0 ?HSum //=.
    + rewrite semBindSize semReturnSize bigcup_set1; eauto.
    + by apply eq_lt_0.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma in_memP {A : eqType} x (l : seq A) :
  reflect (List.In x l) (x \in l)%bool.
Proof.
(* Goal: None *)
  induction l; simpl.
  - constructor; eauto.
  - rewrite in_cons.
(* Goal: None *)
    destruct (x == a) eqn:Heq; move : Heq => /eqP Heq; subst; simpl.
    + constructor; eauto.
    + eapply equivP; try eassumption.
(* Goal: iff (@In (Equality.sort A) x l) (or (@eq (Equality.sort A) a x) (@In (Equality.sort A) x l)) *)
      split; firstorder. congruence.
Qed.  

(* Goal: None *)
(* Goal: None *)
Lemma forall_leq_sum_fst {A} (l : list (nat * A)) :
  forall a n, seq_In l (n, a) -> n <= sum_fst l.
Proof.
(* Goal: forall (a : A) (n : nat) (_ : @seq_In (prod nat A) l (@pair nat A n a)), is_true (ssrnat.leq n (@sum_fst A l)) *)
  elim : l => [| [n a] l IH]; auto.
(* Goal: None *)
  rewrite sum_fstE.
  move => n' a' /= [[H1 H2] | H2]; subst.
(* Goal: None *)
  by ssromega.
(* Goal: None *)
  apply IH in H2. by ssromega.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma pickDrop_leq_top {A} (l : seq (nat * G (option A))) (n : nat) k g l' size s :
  pickDrop l n = (k, g, l') ->
  semGenSize g size (Some s) ->
  n < sum_fst l.
Proof.
(* Goal: None *)
  revert n l'.
(* Goal: None *)
  elim : l => [|[m a] l IH] n l' /= Hpd Hgen.
  - move : Hpd => [H1 H2 H3]; subst.
(* Goal: None *)
    apply semReturnSize in Hgen. discriminate.
  - rewrite sum_fstE.
(* Goal: None *)
    destruct (n < m) eqn:H. by ssromega.
    destruct (pickDrop l (n - m)) as [[k' x'] xs'] eqn:Heq. 
    move : Hpd => [H1 H2 H3]; subst.
(* Goal: None *)
    eapply IH in Heq. by ssromega.
    eassumption.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma backtrackFuelSizeMonotonic {A : Type} tot fuel (lg : seq (nat * G (option A))) :
    sum_fst lg = tot -> length lg = fuel -> 
    lg \subset [set x | SizeMonotonic x.2 ] ->
    SizeMonotonic (backtrackFuel fuel tot lg).
Proof.
(* Goal: None *)
  move: tot lg.
(* Goal: None *)
  induction fuel => tot lg.
  - move => HSum /List.length_zero_iff_nil HLen; subst; simpl.
(* Goal: None *)
    eauto with typeclass_instances.
  - move => HSum HLen Hsub.
(* Goal: None *)
    simpl. 
(* Goal: None *)
    refine (@bindMonotonicStrong _ _ _ _ _ _).
(* Goal: None *)
    move => x /semChoose Hin.
(* Goal: None *)
    unfold leq, super, ChooseNat, OrdNat in Hin.
    specialize (Hin (leq0n (tot-1))).
    destruct (sum_fst lg) eqn:Hsum; subst.
    + rewrite pickDrop_def.
(* Goal: None *)
      refine (@bindMonotonicStrong _ _ _ _ _ _).
      * intros [ y | ].
(* Goal: None *)
        now eauto with typeclass_instances.
(* Goal: None *)
        move => _.
(* Goal: None *)
        intros s1 s2 Hs. rewrite !semBacktrackFuelDef; eauto.
(* Goal: None *)
        eapply subset_refl.
(* Goal: None *)
      * rewrite Hsum. ssromega.
    + edestruct (pickDrop_exists lg x) as [[k [g' [lg' [Hin' [Hdrop [Hneq [Heq [Heq' Hlen]]]]]]]] _].
(* Goal: None *)
      ssromega. rewrite Hdrop.
(* Goal: None *)
      refine (@bindMonotonicStrong _ _ _ _ _ _).
      eapply Hsub in Hin'. eassumption.
(* Goal: None *)
      intros [ a | ].
(* Goal: None *)
      now eauto with typeclass_instances.
(* Goal: None *)
      intros _. eapply IHfuel.
      * rewrite Hsum in Hlen. rewrite <- Hlen. ssromega.
(* Goal: None *)
      * rewrite HLen in Heq'. ssromega.
      * eapply subset_trans; [| eassumption ].
(* Goal: None *)
        rewrite Heq. eapply setU_subset_r.
(* Goal: None *)
        eapply subset_refl.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma pickDrop_subset {A} (l1 l2 : seq (nat * G (option A))) (n m : nat) g :
  pickDrop l1 n = (m, g, l2) ->
  l2 \subset l1.
Proof.
  revert n l2. induction l1; simpl in *; intros n l2 H1.
  - inv H1; eapply subset_refl. 
  - destruct a as [k x].
(* Goal: None *)
    destruct (n < k) eqn:heq.
    + inv H1. intros x Hin. right; eauto.
    + destruct (pickDrop l1 (n - k)) as [[k' x'] xs'] eqn:Heq. inv H1.
(* Goal: None *)
      rewrite !cons_set_eq. eapply setU_set_subset_compat.
(* Goal: None *)
(* Goal: None *)
      now apply subset_refl.
(* Goal: None *)
      eauto.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma length_sum_lst {A} (l : list (nat * A)) :
  length l = 0 ->
  sum_fst l = 0.
Proof.
(* Goal: forall _ : @eq nat (@Datatypes.length (prod nat A) l) O, @eq nat (@sum_fst A l) O *)
  destruct l; eauto; intros H; inv H.
Qed.

(* Goal: None *)
(* Goal: None *)
(* Lemma pickDrop_length_strong {A} (l1 l2 : seq (nat * G (option A))) (n m : nat) g : *)
(*   pickDrop l1 n = (m, g, l2) -> *)
(*   length l2 < length l1 \/ *)
(*   (length l2 = 0 /\ length l1 = 0) \/ *)
(*   (length l2 = 0 /\ (sum_fst l1 = 0 \/ sum_fst l1 <= n)). *)
(* Proof. *)
(*   revert l2 n m g. induction l1; simpl; intros l2 n m g H. *)
(*   - inv H; eauto. *)
(*   - destruct a as [k x]. *)
(*     destruct (n < k) eqn:heq. *)
(*     + inv H. left. now ssromega. *)
(*     +  *)
(*       destruct (pickDrop l1 (n - k)) as [[k' x'] xs'] eqn:Heq. *)
(*       inv H. eapply IHl1 in Heq. *)
(*       destruct Heq as [Hlt' | [[Heq1 Heq2] | [Heq1 [Heq2 | Heq2]]]]. *)
(* Goal: None *)
(*       * left. ssromega. *)
(* Goal: None *)
(*       * simpl. rewrite sum_fstE Heq1 Heq2. *)
(* Goal: None *)
(*         destruct l1; try discriminate. right. right. rewrite sum_fst_eq0P simpl. Hlt.  *)
(* Goal: None *)
(*         rewrite length_sum_lst in Hlt; eauto. ssromega. *)
(* Goal: None *)
(*       * rewrite sum_fstE in Hlt. *)
(* Goal: None *)
(*         rewrite Heq2 in Hlt. ssromega. *)
(* Qed. *)

(* Goal: None *)
(* Goal: None *)
(* Corollary pickDrop_length {A} (l1 l2 : seq (nat * G (option A))) (n m : nat) g : *)
(*   pickDrop l1 n = (m, g, l2) -> *)
(*   n < sum_fst l1 -> *)
(*   length l2 < length l1 \/ length l2 = 0. *)
(* Proof. *)
(*   intros H1 H2.  *)
(*   edestruct (@pickDrop_length_strong A); eauto. *)
(*   inv H; inv H0; eauto. *)
(* Qed. *)


(* Goal: None *)
(* Goal: None *)
Lemma pickDrop_sum_fst {A} (lg  : seq (nat * G (option A))) n :
  sum_fst lg = 0 -> exists l, pickDrop lg n = (0, returnGen None, l) /\ sum_fst l = 0.
Proof.
(* Goal: None *)
  induction lg; eauto.
  intros H.
  - eexists [::]. split; reflexivity.
  - destruct a. intros H. rewrite sum_fstE in H.
(* Goal: None *)
    assert (Heq : n0 = 0) by ssromega.
(* Goal: None *)
    assert (Heq' : sum_fst lg = 0) by ssromega. subst.
(* Goal: None *)
    simpl.  edestruct IHlg as [l [H1 H2]]. eassumption.
    replace (n - 0) with n by ssromega. rewrite H1.
(* Goal: None *)
    eexists. split. reflexivity. rewrite sum_fstE. ssromega.
Qed.   

(* Goal: None *)
(* Goal: None *)
Lemma backtrackFuel_sum_fst {A} fuel tot (lg  : seq (nat * G (option A))) s :
  sum_fst lg = 0 ->
  semGenSize (backtrackFuel fuel tot lg) s <--> [set None].
Proof.
  revert lg tot; induction fuel; simpl; intros lg tot Heq.
  - now rewrite semReturnSize.
  - rewrite semBindSize.
(* Goal: None *)
    intros x; split.
    + move => [n [H1 H2]].
(* Goal: None *)
      eapply semChooseSize in H1; eauto.
      edestruct (@pickDrop_sum_fst A) with (n := n) as [l' [H3 H4]]. eassumption.
(* Goal: None *)
      rewrite H3 in H2. eapply semBindSize in H2.
(* Goal: None *)
      move : H2 => [a [/semReturnSize Heq1 Hb]]. inv Heq1.
      eapply IHfuel; eassumption.
    + move => H; inv H. eexists 0.
(* Goal: None *)
      split.
(* Goal: None *)
      now eapply semChooseSize; eauto.
      edestruct (@pickDrop_sum_fst A) with (n := 0) as [l' [H3 H4]].
      eassumption.
(* Goal: None *)
      rewrite H3.
(* Goal: None *)
      eapply semBindSize.
(* Goal: None *)
      eexists None. split. eapply semReturnSize; reflexivity.
(* Goal: None *)
      eapply IHfuel; eauto.
Qed.      

(* Goal: None *)
(* Goal: None *)
Lemma backtrackFuel_list_mon {A : Type} tot1 tot2 fuel1 fuel2 (lg1 lg2 : seq (nat * G (option A))) s :
  sum_fst lg1 = tot1 -> length lg1 = fuel1 ->
  sum_fst lg2 = tot2 -> length lg2 = fuel2 ->
  lg1 \subset lg2 ->
  isSome :&: semGenSize (backtrackFuel fuel1 tot1 lg1) s \subset
  isSome :&: semGenSize (backtrackFuel fuel2 tot2 lg2) s.
Proof.
(* Goal: None *)
  move : tot1 tot2 fuel2 lg1 lg2 s.
  induction fuel1; intros tot1 tot2 fuel2 lg1 lg2 s Htot1 Hf1 Htot2 Hf2 Hsub x [Hs Hin];
(* Goal: None *)
  destruct x; try discriminate; split; auto.
  - simpl in Hin. eapply semReturnSize in Hin; inv Hin.
  - assert (Ha : tot1 > 0). 
(* Goal: None *)
(* Goal: None *)
    { destruct tot1; auto.
      apply backtrackFuel_sum_fst in Hin; auto; inv Hin. }
    simpl in Hin. apply semBindSize in Hin.
    destruct Hin as [n [Hgen Hgen']].
    apply semChooseSize in Hgen; auto.
    move : Hgen => /andP [Hleq1  Hleq2].
    destruct (pickDrop lg1 n) as [[k g] gs'] eqn:Heq.
    apply semBindSize in Hgen'.
    destruct Hgen' as [b [Hg1 Hg2]].
    assert (Hlt : n < sum_fst lg1).
(* Goal: None *)
(* Goal: None *)
    { unfold leq, super, ChooseNat, OrdNat in *. now ssromega. }
    edestruct (pickDrop_exists lg1 n) as [[m [g' [lg' [Hin' [Hdrop [Hneq [Heq1 [Heq2 Hlen]]]]]]]] _];
(* Goal: None *)
      subst; eauto.
(* Goal: None *)
    rewrite Heq in Hdrop. inv Hdrop.
    destruct b. 
    + eapply semReturnSize in Hg2. inv Hg2.
      edestruct (@pickDrop_In_strong A) as [b' [l' [Hpick [Hfst Hlen']]]].
      split; [| eassumption ].
      eapply Hsub.  eassumption.
(* Goal: None *)
      rewrite Hlen'. simpl.
      replace (length l' + 1) with ((length l').+1); [| now ssromega ].
(* Goal: None *)
      simpl.
(* Goal: None *)
      eapply semBindSize. eexists b'. split.
(* Goal: None *)
      eapply semChooseSize. now eauto.
      apply/andP. unfold leq, super, ChooseNat, OrdNat in *.
(* Goal: None *)
      split; now ssromega.
(* Goal: None *)
      rewrite Hpick.
(* Goal: None *)
      eapply semBindSize. eexists. split.
(* Goal: None *)
      eassumption. simpl. eapply semReturnSize. reflexivity.
    + assert (Hin : ((fun u : option A => u) :&: semGenSize (backtrackFuel (length lg2) (sum_fst lg2) lg2) s) (Some a)).
(* Goal: None *)
(* Goal: None *)
      { eapply IHfuel1; [| | | | | now split; eauto ]; try eassumption; eauto.
(* Goal: None *)
        * ssromega.
(* Goal: None *)
        * ssromega.
        * eapply subset_trans.
          eapply pickDrop_subset. eassumption. eassumption. }
      now inv Hin.
Qed.
  
(* Goal: None *)
(* Goal: None *)
Lemma backtrackFuelSizeMonotonicOpt {A : Type} tot fuel (lg : seq (nat * G (option A))) :
    sum_fst lg = tot -> length lg = fuel -> 
    lg \subset [set x | SizeMonotonicOpt x.2 ] ->
    SizeMonotonicOpt (backtrackFuel fuel tot lg).
Proof.
(* Goal: None *)
  move: tot lg.
(* Goal: None *)
  induction fuel => tot lg.
  - move => HSum /List.length_zero_iff_nil HLen; subst; simpl.
    auto with typeclass_instances.
  - move => HSum HLen Hsub.
    intros s1 s2 Hleq x H.
    assert (Ha : tot > 0). 
(* Goal: None *)
(* Goal: None *)
    { destruct tot; auto;
      apply backtrackFuel_sum_fst in H; auto. inv H. }
    eapply semBindSize in H.
    destruct H as [n [Hn H]].
(* Goal: None *)
    eapply semChooseSize in Hn; eauto.
    destruct (pickDrop lg n) as [[k g] gs'] eqn:Heqd.
    eapply semBindSize in H.
    destruct H as [b [Hgb Hf]].
    assert (Hlt : n < sum_fst lg).
(* Goal: None *)
(* Goal: None *)
    { unfold leq, super, ChooseNat, OrdNat in *. now ssromega. }
    edestruct (pickDrop_exists lg n) as [[m [g' [lg' [Hin' [Hdrop [Hneq [Heq1 [Heq2 Hlen]]]]]]]] _];
(* Goal: None *)
      subst; eauto.
    rewrite Hdrop in Heqd; inv Heqd. subst.
    destruct b as [b |].
    + eapply semBindSize.
(* Goal: None *)
      eexists n. split.
(* Goal: None *)
      eapply semChooseSize; now eauto.
(* Goal: None *)
      rewrite Hdrop. eapply semBindSize.
(* Goal: None *)
      exists (Some b). split. eapply Hsub in Hin'.
(* Goal: None *)
      eapply monotonicOpt; eauto.
      apply semReturnSize; apply semReturnSize in Hf; auto.
    + Admitted. (*
      (* have Hin :(isSome :&: semGenSize (backtrackFuel fuel (sum_fst lg - k) gs') s1) (Some a).
(* Goal: None *)
(* Goal: None *)
      { split ; eauto. } *)
      eapply IHfuel in Hin; try eassumption. destruct Hin as [_ Hin].
      * eapply backtrackFuel_list_mon; [| | | | | split; [ auto | eassumption ] ];
(* Goal: None *)
        try auto; try ssromega.
(* Goal: @set_incl A (fun x : A => is_true (f x)) (fun x : A => is_true (f x)) *)
        rewrite Heq1. apply setU_set_incl_r. apply subset_refl.
(* Goal: None *)
      * ssromega.
(* Goal: None *)
      * ssromega.
      * eapply subset_trans; [| eassumption ].
(* Goal: None *)
        eapply pickDrop_subset; eauto.
Qed.
*)

(* Goal: None *)
(* Goal: None *)
Corollary backtrackSizeMonotonic {A : Type} (lg : seq (nat * G (option A))) :
  lg \subset [set x | SizeMonotonic x.2 ] ->
  SizeMonotonic (backtrack lg).
Proof.
(* Goal: None *)
  intros Hin. unfold backtrack.
(* Goal: None *)
  apply backtrackFuelSizeMonotonic; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Corollary backtrackSizeMonotonicOpt {A : Type} (lg : seq (nat * G (option A))) :
  lg \subset [set x | SizeMonotonicOpt x.2 ] ->
  SizeMonotonicOpt (backtrack lg).
Proof.
(* Goal: None *)
  intros Hin. unfold backtrack.
(* Goal: None *)
  eapply backtrackFuelSizeMonotonicOpt; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semBacktrackFuel {A} tot fuel (l : list (nat * G (option A))) size :
  sum_fst l = tot -> length l = fuel -> 
  semGenSize (backtrackFuel fuel tot l) size <--> 
  (\bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: (semGenSize x.2 size))) :|:
  ([set None] :&: (\bigcap_(x in l :&: (fun x => x.1 <> 0)) (semGenSize x.2 size))).
Proof.
(* Goal: None *)
  move: tot l size.
(* Goal: None *)
  induction fuel => tot l size.
  - move => HSum /List.length_zero_iff_nil HLen; subst; simpl.
    by rewrite setI_comm !nil_set_eq setI_set0_abs bigcup_set0 bigcap_set0
               setU_comm setU_set0_neut setI_setT_neut semReturnSize.
  - move => HSum HLen.
(* Goal: None *)
    rewrite semBindSize semChooseSize //=. 
(* Goal: None *)
    split.
(* Goal: None *)
(* Goal: None *)
    { destruct (sum_fst l) eqn:Hsum; subst.
      - move => [n [Hleq H]].
(* Goal: None *)
        rewrite pickDrop_def in H; eauto.
        move : H =>  /semBindSize [[b |] [H1 H2]].
        + eapply semReturnSize in H1. inversion H1.
        + eapply semBacktrackFuelDef in H2; auto.
(* Goal: None *)
          inversion H2; subst.
          right. split; auto.
(* Goal: None *)
          move => [n' a] [H3 H4]. eapply forall_leq_sum_fst in H3.
(* Goal: None *)
          subst; simpl in *. ssromega.
      - move => [m [Hleq H]].
(* Goal: None *)
        move: (pickDrop_exists l m) => [H1 H2].
        edestruct H1 as [k [g [l' [HIn [Hpd [Hkneq [Hsub [Hlen Hfst]]]]]]]].
(* Goal: None *)
        rewrite Hsum; auto. ssromega.
        rewrite Hpd in H. eapply semBindSize in H.
(* Goal: None *)
        move : H => [a' [Hg Hb]]. 
        destruct a'. 
        + left. exists (k, g).
          apply semReturnSize in Hb. inversion Hb; subst.
(* Goal: None *)
            by firstorder.
        + eapply IHfuel in Hb; auto.
          * move : Hb => [Hsome | [Hnone Hcap]].
            left. eapply incl_bigcupl; [| eassumption ].
            apply setI_subset_compat.
(* Goal: None *)
            rewrite Hsub. apply setU_subset_r. by apply subset_refl.
(* Goal: None *)
            by apply subset_refl.   
(* Goal: None *)
            right. split; eauto.
(* Goal: None *)
            eapply eq_bigcapl. rewrite Hsub.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
            rewrite setI_setU_distr. reflexivity.
(* Goal: None *)
            apply bigcap_setI_l. split; eauto.
            apply bigcap_setU_l. apply bigcap_set1.
            inversion Hnone; subst. eassumption.
          * rewrite Hsum in Hfst. rewrite <- Hfst. ssromega.
          * ssromega. }
(* Goal: None *)
(* Goal: None *)
    { move => [[[k g] [[Hg1 Hg2] [Ha1 Ha2]]] | [Hnone Hcap]]; simpl in *.
      - edestruct (pickDrop_In l) as [n [gs' Heq]]; eauto.
(* Goal: None *)
        destruct a; try discriminate.
(* Goal: None *)
        exists n. split. rewrite <- HSum.
        eapply pickDrop_leq_top in Heq; auto; [by ssromega | eauto].
(* Goal: None *)
        rewrite Heq.
(* Goal: None *)
        eapply semBindSize. exists (Some a). split; eauto.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
        apply semReturnSize. reflexivity.
      - destruct a; try discriminate.
        destruct (sum_fst l) eqn:Hsum.
(* Goal: None *)
        + eexists 0. split; auto.
          rewrite pickDrop_def; auto; [| rewrite Hsum; auto].
(* Goal: None *)
          eapply semBindSize. exists None. split.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
          apply semReturnSize. reflexivity.
          subst. apply semBacktrackFuelDef; eauto.
        + subst.
(* Goal: None *)
          move: (pickDrop_exists l 0) => [Hex _].
          edestruct Hex as [k [g [gs' [Hin [Hpd [Hneq [Hsub [Hlen Hfst]]]]]]]]; auto; [rewrite Hsum; auto|].
(* Goal: None *)
          exists 0. split; eauto. rewrite Hpd.
(* Goal: None *)
          eapply semBindSize. exists None. split.
          specialize (Hcap (k, g)). eapply Hcap.
(* Goal: None *)
          split; auto.
(* Goal: None *)
          eapply IHfuel.
          rewrite Hsum in Hfst. rewrite <- Hfst. by ssromega.
(* Goal: None *)
          by ssromega.
(* Goal: None *)
          right. split. reflexivity.
          eapply incl_bigcapl; [| eassumption ].
          rewrite Hsub. apply setI_subset_compat.
          apply setU_subset_r. by apply subset_refl.
          by apply subset_refl. }
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semBacktrackSize {A} (l : list (nat * G (option A))) size :
  semGenSize (backtrack l) size <--> 
  (\bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: (semGenSize x.2 size))) :|:
  ([set None] :&: (\bigcap_(x in l :&: (fun x => x.1 <> 0)) (semGenSize x.2 size))).
Proof.
(* Goal: None *)
  eauto using semBacktrackFuel.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma bigcup_cons_setI_subset_compat_backtrack {A}
      (n : nat) (g g' : G (option A)) (l l' : seq (nat * G (option A))) s :
  isSome :&: semGenSize g s  \subset isSome :&: semGenSize g' s ->
  \bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s) \subset
  \bigcup_(x in (l' :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s) ->
  \bigcup_(x in (((n, g) :: l) :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s) \subset
  \bigcup_(x in (((n, g') :: l') :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s).
Proof.
  intros H1 H2 x [y [[Hin1 Hin2] [Hin3 Hin4]]].
(* Goal: None *)
  inv Hin1; simpl in *.
(* Goal: None *)
  - eexists. split. split.
(* Goal: None *)
    now left; eauto.
    now eassumption.
(* Goal: None *)
    split; auto.
(* Goal: None *)
    simpl. eapply H1. split; auto.
  - edestruct H2 as [y' [[Hin1' Hin2'] [Hin3' Hin4']]].
    { eexists. split; split; eassumption. }
(* Goal: None *)
    eexists. split. split.
(* Goal: None *)
    now right; eauto.
    now eassumption.
(* Goal: None *)
    split; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma bigcup_cons_setI_subset_pres_backtrack {A}
      (n : nat) (g : G (option A)) (l l' : seq (nat * G (option A))) s :
  \bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s) \subset
  \bigcup_(x in (l' :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s) ->
  \bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: semGenSize x.2 s) \subset
  \bigcup_(x in ((n, g) :: l') :&: (fun x => x.1 <> 0)) (isSome :&: semGenSize x.2 s).
Proof.
  intros H1 x [y [[Hin1 Hin2] [Hin3 Hin4]]].
  edestruct H1 as [y' [[Hin1' Hin2'] [Hin3' Hin4']]].
  { eexists. split; split; eassumption. }
(* Goal: None *)
  eexists. split. split.
(* Goal: None *)
  now right; eauto.
  now eassumption.
(* Goal: None *)
  split; eauto.
Qed.

Lemma semBacktrack_sound :
  forall (A : Type) (l : seq (nat * G (option A))),
    semGen (backtrack l) \subset
    (\bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: (semGen x.2))) :|:
    ([set None] :&: (\bigcap_(x in l :&: (fun x => x.1 <> 0)) (semGen x.2))).
Proof.
  intros A l x [s [_ H]].
  eapply semBacktrackSize in H.
  inv H.
  + left. destruct H0 as [y [[Hin1 Hin2] [Hin3 Hin4]]].
    eexists. split; split; eauto.
    eexists.
(* Goal: None *)
    now split; eauto.
  + destruct H0 as [Hnone Hcap].
(* Goal: None *)
    right. split; eauto.
    intros y Hin.
    eapply Hcap in Hin.
    eexists.
(* Goal: None *)
    now split; eauto.
Qed.

Lemma semBacktrack_complete :
  forall (A : Type) (l : seq (nat * G (option A))),
    \bigcup_(x in (l :&: (fun x => x.1 <> 0))) (isSome :&: (semGen x.2)) \subset
    semGen (backtrack l).
Proof.
  intros A l x [y [[Hin1 Hin2] [Hin3 Hin4]]].
  destruct Hin4 as [s [_ Hin]].
  eexists s. split; [ now constructor | ].
  eapply semBacktrackSize.
  left. eexists.
(* Goal: None *)
  split; split; eauto.
Qed.

Lemma semFoldGen_right :
  forall {A B : Type} (f : A -> B -> G A) (bs : list B) (a0 : A) (s : nat),
    semGenSize (foldGen f bs a0) s <-->
    [ set an |
      foldr (fun b p => [set a_prev | exists a, a \in (semGenSize (f a_prev b) s :&: p)]) 
            [set an] bs a0].
Proof.
(* Goal: None *)
  move => A B f bs a0 s. rewrite /foldGen. 
(* Goal: None *)
   elim : bs a0 => [| b bs IHbs] a0 an. 
(* Goal: None *)
  - split. 
    + move/semReturnSize => ->. reflexivity. 
     + move => ->. now apply semReturnSize.
(* Goal: None *)
  - split. 
    + move/semBindSize => [a [H1 H2]]. 
(* Goal: None *)
       exists a. split => //. now apply IHbs.
(* Goal: None *)
    + move => [a [H1 H2]]. apply semBindSize. exists a. split => //.
(* Goal: None *)
       now apply IHbs. 
Qed.

(* Goal: None *)
(* Goal: None *)
Definition genPair {A B : Type} (ga : G A) (gb : G B) : G (A * B) :=
  liftGen2 pair ga gb.

Definition curry {A B C : Type} (f : A * B -> C) (a : A) (b : B) := f (a,b).

Definition uncurry {A B C : Type} (f : A -> B -> C) (ab : A * B) :=
  match ab with
  | (a,b) => f a b
  end.

Lemma mergeBinds :
  forall A B C (ga : G A) (gb : G B) (f : A -> B -> G C),
    semGen (bindGen ga (fun x => bindGen gb (f x))) <-->
    semGen (bindGen (genPair ga gb) (uncurry f)).
Proof.
(* Goal: None *)
  intros. unfold semGen. repeat setoid_rewrite semBindSize.
(* Goal: None *)
                                setoid_rewrite semReturnSize.
(* Goal: None *)
  intro c. split; intros [s [_ H]]; exists s; split; try by [].
  - destruct H as [a [Ha [b [Hb Hc]]]].
(* Goal: None *)
    exists (a,b). split. exists a. split; first by [].
(* Goal: None *)
(* Goal: None *)
                         exists b. split; first by [].
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    reflexivity. by [].
  - destruct H as [[a b] [[a' [Ha [b' [Hb H]]]] Hc]].
(* Goal: None *)
    inversion H; subst; clear H.
(* Goal: None *)
    exists a. split. by []. exists b. split; by [].
Qed.    

Module QcDefaultNotation.

Notation " 'elems' [ x ] " := (elements x (cons x nil)) : qc_scope.
Notation " 'elems' [ x ; y ] " := (elements x (cons x (cons y nil))) : qc_scope.
Notation " 'elems' [ x ; y ; .. ; z ] " :=
  (elements x (cons x (cons y .. (cons z nil) ..))) : qc_scope.
Notation " 'elems' ( x ;; l ) " :=
  (elements x (cons x l)) (at level 1, no associativity) : qc_scope.

Notation " 'oneOf' [ x ] " := (oneof x (cons x nil)) : qc_scope.
Notation " 'oneOf' [ x ; y ] " := (oneof x (cons x (cons y nil))) : qc_scope.
Notation " 'oneOf' [ x ; y ; .. ; z ] " :=
  (oneof x (cons x (cons y .. (cons z nil) ..))) : qc_scope.
Notation " 'oneOf' ( x ;; l ) " :=
  (oneof x (cons x l))  (at level 1, no associativity) : qc_scope.

Notation " 'freq' [ x ] " := (frequency x nil) : qc_scope.
Notation " 'freq' [ ( n , x ) ; y ] " :=
  (frequency x (cons (n, x) (cons y nil))) : qc_scope.
Notation " 'freq' [ ( n , x ) ; y ; .. ; z ] " :=
  (frequency x (cons (n, x) (cons y .. (cons z nil) ..))) : qc_scope.
Notation " 'freq' ( ( n , x ) ;; l ) " :=
  (frequency x (cons (n, x) l)) (at level 1, no associativity) : qc_scope.

End QcDefaultNotation.

Module QcDoNotation.

Notation "'do!' X <- A ; B" :=
  (bindGen A (fun X => B))
  (at level 200, X ident, A at level 100, B at level 200).
Notation "'doM!' X <- A ; B" :=
  (bindGenOpt A (fun X => B))
  (at level 200, X ident, A at level 100, B at level 200).

End QcDoNotation.

Import QcDefaultNotation. Open Scope qc_scope.

(* CH: Reusing :: instead of ;; would have been nice, but I didn't manage *)

Lemma semElemsSize A (x : A) xs s : semGenSize (elems (x ;; xs)) s <--> x :: xs.
Proof. rewrite semElementsSize. reflexivity. Qed.

Lemma semOneOfSize A (g0 : G A) (gs : list (G A)) s :
  semGenSize (oneOf (g0 ;; gs)) s  <--> \bigcup_(g in (g0 :: gs)) semGenSize g s.
Proof. rewrite semOneofSize. reflexivity. Qed.

(* begin semElems *)
Lemma semElems A (x : A) xs : semGen (elems (x ;; xs)) <--> x :: xs.
(* end semElems *)
Proof. by rewrite semElements. Qed.

(* begin semOneOf *)
Lemma semOneOf A (g0 : G A) (gs : list (G A)) :
  semGen (oneOf (g0 ;; gs))  <--> \bigcup_(g in (g0 :: gs)) semGen g.
(* end semOneOf *)
Proof. by rewrite semOneof. Qed.

(* Operators like betterSized (better name pending) are guaranteed to
   produce size-monotonic generators (provided the body has this
   property). Note: this doesn't hold for sized! *)

Definition betterSized {A} (f : nat -> G A) :=
  sized (fun x => bindGen (choose (0, x)) f).

Program Instance betterSizedIndeedBetter {A} (f : nat -> G A) 
        (H: forall s, SizeMonotonic (f s)) :
  SizeMonotonic (betterSized f).
Next Obligation.
(* Goal: None *)
  rewrite /betterSized . 
(* Goal: None *)
  rewrite !semSizedSize !semBindSize !semChooseSize; last by []; last by [].
(* Goal: None *)
  move => a [s1' [/andP [_ H11] H12]].
  eexists. split; last by eapply monotonic; eauto. 
(* Goal: None *)
  apply/andP; split => //. by eapply leq_trans; eauto. 
Qed.


Instance bindOptMonotonicOpt
         {A B} (g : G (option A)) (f : A -> G (option B))
(* Goal: None *)
(* Goal: None *)
         `{SizeMonotonicOpt _ g} `{forall x, SizeMonotonicOpt (f x)} :
  SizeMonotonicOpt (bindGenOpt g f).
Proof.
(* Goal: None *)
  intros s1 s2 Hleq.
(* Goal: None *)
  unfold semGenSizeOpt.
(* Goal: None *)
  rewrite !semBindSize. move => b.
(* Goal: None *)
  move => [a [Hsome Hb]].
(* Goal: None *)
  exists a.
(* Goal: None *)
  destruct a.
(* Goal: None *)
  - split.
(* Goal: None *)
    eapply monotonicOpt; eauto; eexists; eauto.
(* Goal: None *)
    eapply monotonicOpt; eauto; eexists; eauto.
  - apply semReturnSize in Hb; discriminate Hb.
Qed.

Instance bindOptMonotonic
         {A B} (g : G (option A)) (f : A -> G (option B))
(* Goal: None *)
(* Goal: None *)
         `{SizeMonotonic _ g} `{forall x, SizeMonotonic (f x)} :
  SizeMonotonic (bindGenOpt g f).
Proof.
(* Goal: None *)
  intros s1 s2 Hleq.
(* Goal: None *)
  intros x Hx. eapply semBindSize in Hx.
(* Goal: None *)
  destruct Hx as [a [Hg Hf]].
(* Goal: None *)
  destruct a as [a | ].
  - eapply H in Hg; try eassumption.
    eapply H0 in Hf; try eassumption.
(* Goal: None *)
    eapply semBindSize.
(* Goal: None *)
    eexists; split; eauto.
  - eapply H in Hg; try eassumption.
(* Goal: None *)
    eapply semReturnSize in Hf. inv Hf.
(* Goal: None *)
    eapply semBindSize.
(* Goal: None *)
    eexists; split; eauto. simpl.
    eapply semReturnSize.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    reflexivity.
Qed.

Lemma semBindOptSizeMonotonicIncl_r {A B} (g : G (option A)) (f : A -> G (option B)) (s1 : set A) (s2 : A -> set B) :
  semGen g \subset (Some @: s1) :|: [set None] ->
  (forall x, semGen (f x) \subset Some @: (s2 x) :|: [set None]) ->
  semGen (bindGenOpt g f) \subset Some @: (\bigcup_(a in s1) s2 a) :|: [set None].
Proof.
Admitted.

Lemma semBindOptSizeMonotonicIncl_l {A B} (g : G (option A)) (f : A -> G (option B)) (s1 : set A)
      (fs : A -> set B)
(* Goal: None *)
(* Goal: None *)
      `{Hg : SizeMonotonicOpt _ g}
(* Goal: None *)
(* Goal: None *)
      `{Hf : forall a, SizeMonotonicOpt (f a)} :
  Some @: s1 \subset semGen g ->
  (forall x, Some @: (fs x) \subset semGen (f x)) ->
  (Some @: \bigcup_(a in s1) (fs a)) \subset semGen (bindGenOpt g f).
Proof.
Admitted.

Lemma  semBindOptSizeOpt_subset_compat {A B : Type} (g g' : G (option A)) (f f' : A -> G (option B)) :
  (forall s, isSome :&: semGenSize g s \subset isSome :&: semGenSize g' s) ->
  (forall x s, isSome :&: semGenSize (f x) s \subset isSome :&: semGenSize (f' x) s) ->
  (forall s, isSome :&: semGenSize (bindGenOpt g f) s \subset isSome :&: semGenSize (bindGenOpt g' f') s).
Proof.
(* Goal: None *)
  intros Hg Hf s x [Hin1 Hin2].
(* Goal: None *)
  split; [ eassumption |].
  unfold bindGenOpt in *.
(* Goal: None *)
  eapply semBindSize in Hin2. destruct Hin2 as [a [Hg' Hf']].
(* Goal: None *)
  destruct a as [a |].
  - assert (Hg'' : ((fun u : option A => u) :&: semGenSize g s) (Some a)).
(* Goal: None *)
    { split; eauto. }
(* Goal: None *)
    eapply Hg in Hg''.  destruct Hg'' as [_ Hg''].
(* Goal: None *)
    eapply semBindSize. eexists; split; [ eassumption |].
(* Goal: None *)
    simpl. eapply Hf. split; eauto.
  - eapply semReturnSize in Hf'.  inv Hf'. discriminate.
Qed.

Definition GOpt A := G (option A).

(* Goal: None *)
(* Goal: None *)
Global Instance Monad_GOpt : Monad GOpt := {
  ret A x := returnGen (Some x);
  bind A B := bindGenOpt;
}.

(* Goal: None *)
(* Goal: None *)
Lemma semSize_retryBody {A} (n : nat) (g : G (option A)) (s : nat) :
  match n with
  | S n' => semGenSize (retry n' g) s <--> semGenSize g s
  | O => True
  end ->
  semGenSize (retryBody retry n g) s <--> semGenSize g s.
Proof.
(* Goal: None *)
  intro Hn.
(* Goal: None *)
  unfold retryBody.
(* Goal: None *)
  rewrite semBindSize.
(* Goal: None *)
  intros a; split; intro Ha.
  - destruct Ha as [a1 [Hg Ha]].
(* Goal: None *)
    destruct a1.
    + apply semReturnSize in Ha.
(* Goal: None *)
      inversion Ha; subst; auto.
    + destruct n.
      * apply semReturnSize in Ha; inversion Ha; auto.
      * apply Hn; auto.
  - exists a. split; auto.
(* Goal: None *)
    destruct a.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    + apply semReturnSize; reflexivity.
    + destruct n.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
      * apply semReturnSize; reflexivity.
      * apply Hn; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semSize_retry {A} (n : nat) (g : G (option A)) (s : nat) :
  semGenSize (retry n g) s <--> semGenSize g s.
Proof.
(* Goal: None *)
  induction n; apply semSize_retryBody; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semSizeOpt_retry {A} (n : nat) (g : G (option A)) (s : nat) :
  semGenSizeOpt (retry n g) s <--> semGenSizeOpt g s.
Proof.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
  unfold semGenSizeOpt. rewrite semSize_retry. reflexivity.
Qed.

Lemma semSizeOpt_suchThatMaybe1 {A : Type} (g : G A) (p : A -> bool)
      (s : nat) :
  semGenSizeOpt (suchThatMaybe1 g p) s <--> semGenSize g s :&: p.
Proof.
(* Goal: None *)
  unfold semGenSizeOpt, suchThatMaybe1.
(* Goal: None *)
  rewrite semFmapSize.
(* Goal: None *)
  intros a; split; intros Ha.
  - destruct Ha as [a' [Ha' Hp]].
    destruct (p a') eqn:Hp'; inversion Hp; subst.
(* Goal: None *)
    split; auto.
  - destruct Ha as [Hg Hp].
(* Goal: None *)
    exists a.
(* Goal: None *)
    rewrite Hp.
(* Goal: None *)
    split; auto.
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    reflexivity.
Qed.

Lemma semSizeOpt_suchThatMaybe {A : Type} (g : G A) (p : A -> bool)
      (s : nat) :
  semGenSizeOpt (suchThatMaybe g p) s <--> semGenSize g s :&: p.
Proof.
(* Goal: None *)
  unfold suchThatMaybe.
(* Goal: None *)
  unfold semGenSizeOpt.
(* Goal: None *)
  rewrite semSizedSize.
(* Goal: None *)
  fold (semGenSizeOpt (retry s (suchThatMaybe1 g p)) s).
(* Goal: None *)
  rewrite semSizeOpt_retry.
(* Goal: None *)
  apply semSizeOpt_suchThatMaybe1.
Qed.

(* Goal: None *)
(* Goal: None *)
Lemma semSizeOpt_suchThatMaybeOpt {A : Type} (g : G (option A))
      (p : A -> bool) (s : nat) :
  semGenSizeOpt (suchThatMaybeOpt g p) s <--> semGenSizeOpt g s :&: p.
Proof.
(* Goal: None *)
  unfold suchThatMaybeOpt.
(* Goal: None *)
  unfold semGenSizeOpt.
(* Goal: None *)
  rewrite semSizedSize semSize_retry semFmapSize.
(* Goal: None *)
  intros a; split; intros Ha.
  - destruct Ha as [[ a'| ] [Hg Ha]]; inversion Ha.
    destruct (p a') eqn:Hp; inversion H0; subst.
(* Goal: None *)
    split; auto.
  - destruct Ha as [Hg Hp].
(* Goal: None *)
    exists (Some a).
(* Goal: @set1 (option A) (@Some A a) (@Some A a) *)
    rewrite Hp; split; auto; reflexivity.
Qed.

(* Goal: None *)
(* Goal: None *)
Instance Monotonic_retry {A} (n : nat) (g : G (option A)) :
  SizeMonotonic g ->
  SizeMonotonic (retry n g).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  do 2 rewrite semSize_retry; auto.
Qed.

(* Goal: None *)
(* Goal: None *)
Instance MonotonicOpt_retry {A} (n : nat) (g : G (option A)) :
  SizeMonotonicOpt g ->
  SizeMonotonicOpt (retry n g).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  do 2 rewrite semSizeOpt_retry; auto.
Qed.

Instance Monotonic_suchThatMaybe1
         {A : Type} (g : G A) (f : A -> bool) :
  SizeMonotonic g ->
  SizeMonotonic (suchThatMaybe1 g f).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  unfold suchThatMaybe1.
(* Goal: None *)
  do 2 rewrite semFmapSize.
(* Goal: None *)
  apply imset_incl; auto.
Qed.

Instance MonotonicOpt_suchThatMaybe1
         {A : Type} (g : G A) (f : A -> bool) :
  SizeMonotonic g ->
  SizeMonotonicOpt (suchThatMaybe1 g f).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  unfold semGenSizeOpt.
(* Goal: None *)
  apply somes_subset.
(* Goal: None *)
  apply Monotonic_suchThatMaybe1; auto.
Qed.

Instance Monotonic_suchThatMaybe
         {A : Type} (g : G A) (f : A -> bool) :
  SizeMonotonic g ->
  SizeMonotonic (suchThatMaybe g f).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  unfold suchThatMaybe.
(* Goal: None *)
  do 2 rewrite semSizedSize semSize_retry.
(* Goal: None *)
  apply Monotonic_suchThatMaybe1; auto.
Qed.

Instance MonotonicOpt_suchThatMaybe
         {A : Type} (g : G A) (f : A -> bool) :
  SizeMonotonic g ->
  SizeMonotonicOpt (suchThatMaybe g f).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  unfold semGenSizeOpt.
(* Goal: None *)
  apply somes_subset.
(* Goal: None *)
  apply Monotonic_suchThatMaybe; auto.
Qed.

Instance MonotonicOpt_suchThatMaybeOpt
         {A : Type} (g : G (option A)) (f : A -> bool) :
  SizeMonotonicOpt g ->
  SizeMonotonicOpt (suchThatMaybeOpt g f).
Proof.
(* Goal: None *)
  intros Hg s1 s2 Hs.
(* Goal: None *)
  do 2 rewrite semSizeOpt_suchThatMaybeOpt.
(* Goal: None *)
  apply setI_subset_compat; auto.
(* Goal: @set_incl A (fun x : A => is_true (f x)) (fun x : A => is_true (f x)) *)
  apply subset_refl.
Qed.

End Impl.
