(** * Decidability and Enumerability *)

Require Export Shared.Prelim.


(* ** Definitions *)

Definition compl X p := fun x : X => ~ p x.
Definition decidable {X} (p : X -> Prop) := exists f, forall x, p x <-> f x = true.
Definition enumerable {X} (p : X -> Prop) := exists f, forall x, p x <-> exists n : nat, f n = Some x.

Definition discrete X := decidable (fun '(x,y) => x = y :> X). 
Definition enumerable__T X := exists f : nat -> option X, forall x, exists n, f n = Some x.

(* *** more practical type-theoretic characterisations *)

Lemma dec_decidable' X p :
  (forall x : X, dec (p x)) -> { f : _ | forall x, p x <-> f x = true}.
Proof.
(* Goal: forall _ : forall x : X, dec (p x), @sig (forall _ : X, bool) (fun f : forall _ : X, bool => forall x : X, iff (p x) (@eq bool (f x) true)) *)
  intros d. exists (fun x => if d x then true else false). intros x. destruct (d x); firstorder congruence.
Qed.

Lemma decidable_iff X p :
  decidable p <-> inhabited (forall x : X, dec (p x)).
Proof.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  split.
(* Goal: forall x : X, iff (p x) (@ex nat (fun m : nat => @In X x ((fix L' (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (L' n0) (L n0) end) m))) *)
  - intros [f]. econstructor. intros x. specialize (H x). destruct (f x); firstorder congruence.
  - intros [d]. eapply dec_decidable' in d as [f]. now exists f.
Qed.

Lemma discrete_iff X :
  discrete X <-> inhabited (eq_dec X).
Proof.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  split.
  - intros [] % decidable_iff. econstructor. intros x y; destruct (X0 (x,y)); firstorder.
  - intros [d]. eapply decidable_iff. econstructor. intros (x,y). eapply d.
Qed.

(* ** Facts *)

Lemma dec_compl X p :
  decidable p -> decidable (fun x : X => ~ p x).
Proof.
(* Goal: forall _ : @enumerable (prod X Y) p, @enumerable Y (fun y : Y => @ex X (fun x : X => p (@pair X Y x y))) *)
  intros [f]. exists (fun x => negb (f x)).
(* Goal: forall x : X, iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x0 : X => x0) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  intros. rewrite H. destruct (f x); split; cbn; congruence.
Qed.

Lemma dec_conj X p q :
  decidable p -> decidable q -> decidable (fun x : X => p x /\ q x).
Proof.
(* Goal: forall (_ : @decidable X p) (_ : @decidable X q), @decidable X (fun x : X => or (p x) (q x)) *)
  intros [f] [g]. exists (fun x => f x && g x).
(* Goal: forall x : X, iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x0 : X => x0) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  intros. rewrite H, H0. destruct (f x), (g x); cbn; firstorder congruence.
Qed.

Lemma dec_disj X p q :
  decidable p -> decidable q -> decidable (fun x : X => p x \/ q x).
Proof.
(* Goal: forall (_ : @decidable X p) (_ : @decidable X q), @decidable X (fun x : X => or (p x) (q x)) *)
  intros [f] [g]. exists (fun x => f x || g x).
(* Goal: forall x : X, iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x0 : X => x0) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  intros. rewrite H, H0. destruct (f x), (g x); cbn; firstorder congruence.
Qed.

Theorem dec_count_enum X (p : X -> Prop) :
  decidable p -> enumerable__T X -> enumerable p.
Proof.
(* Goal: forall (_ : @decidable X p) (_ : enumerable__T X), @enumerable X p *)
  intros [d Hd] [c Hc].
(* Goal: @enumerable X p *)
  exists (fun n => match c n with Some x => if d x then Some x else None | None => None end).
(* Goal: forall x : X, iff (p x) (@ex nat (fun m : nat => @In X x ((fix L' (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (L' n0) (L n0) end) m))) *)
  setoid_rewrite Hd. intros x. split; intros.
  - destruct (Hc x) as [n Hn]. exists n. now rewrite Hn, H.
  - destruct H as [n H]. destruct (c n) eqn:E.
    + destruct (d x0) eqn:E2; now inv H.
    + inv H.
Qed.

Theorem dec_count_enum' X (p : X -> Prop) :
  decidable p -> enumerable__T X -> enumerable (fun x => ~ p x).
Proof.
(* Goal: forall (_ : @decidable X p) (_ : enumerable__T X), @enumerable X (fun x : X => not (p x)) *)
  intros ? % dec_compl ?. eapply dec_count_enum; eauto.
Qed.

(** ** Closure of enumerable types *)

Lemma enumerable_enumerable_T X :
  enumerable (fun _ : X => True) <-> enumerable__T X.
Proof.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  split.
(* Goal: forall x : X, iff (p x) (@ex nat (fun m : nat => @In X x ((fix L' (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (L' n0) (L n0) end) m))) *)
  - intros [e He]. exists e. intros x. now eapply He.
(* Goal: forall x : X, iff (p x) (@ex nat (fun m : nat => @In X x ((fix L' (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (L' n0) (L n0) end) m))) *)
  - intros [c Hc]. exists c. intros x. split; eauto.
Qed.

Definition cumulative {X} (L: nat -> list X) :=
  forall n, exists A, L (S n) = L n ++ A.
Hint Extern 0 (cumulative _) => intros ?; cbn; eauto.

Lemma cum_ge {X} (L: nat -> list X) n m :
  cumulative L -> m >= n -> exists A, L m = L n ++ A.
Proof.
(* Goal: forall (_ : @cumulative X L) (_ : ge m n), @ex (list X) (fun A : list X => @eq (list X) (L m) (@app X (L n) A)) *)
  induction 2 as [|m _ IH].
  - exists nil. now rewrite app_nil_r.
  - destruct (H m) as (A&->), IH as [B ->].
    exists (B ++ A). now rewrite app_assoc.
Qed.

Lemma cum_ge' {X} (L: nat -> list X) x n m :
  cumulative L -> x el L n -> m >= n -> x el L m.
Proof.
  intros ? H [A ->] % (cum_ge (L := L)). apply in_app_iff. eauto. eauto.
Qed.

Definition enum {X} p (L: nat -> list X) :=
  cumulative L /\ forall x, p x <-> exists m, x el L m.

Section enumerable_enum.

  Variable X : Type.
  Variable p : X -> Prop.
  Variables (e : nat -> option X).
  
  Definition T_ (n : nat) : list X :=  match e n with Some x => [x] | None => [] end.

  Lemma count_enum' : exists L : nat -> list X, forall x, (exists n, e n = Some x) <-> (exists n, x el L n).
  Proof.
(* Goal: @ex (forall _ : nat, list X) (fun L : forall _ : nat, list X => forall x : X, iff (@ex nat (fun n : nat => @eq (option X) (e n) (@Some X x))) (@ex nat (fun n : nat => @In X x (L n)))) *)
    exists T_. split; intros [n H].
    - exists n. unfold T_. rewrite H. eauto.
    - unfold T_ in *. destruct (e n) eqn:E. inv H. eauto. inv H0. inv H.
  Qed.
      
End enumerable_enum.

Lemma enum_to_cumulative X (p : X -> Prop) L :
  (forall x, p x <-> exists m : nat, x el L m) -> exists L, enum p L.
Proof.
  intros H. exists (fix L' n := match n with 0 => [] | S n => L' n ++ L n end).
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  split.
  - eauto.
(* Goal: forall x : X, iff (p x) (@ex nat (fun m : nat => @In X x ((fix L' (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (L' n0) (L n0) end) m))) *)
  - intros x. rewrite H. split; intros [m Hm].
    + exists (S m). eauto.
    + induction m; try now inv Hm.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      eapply in_app_iff in Hm as []; eauto.
Qed.    

(** ** Enumerability of pairs of natural numbers *)

Class enumT X :=
  {
    L_T :> nat -> list X;
    cum_T : cumulative L_T ;
    el_T : forall x, exists m, x el L_T m
  }.

Arguments L_T {_ _} _, _ {_} _.
Hint Immediate cum_T.

Lemma discrete_bool : discrete bool.
Proof.
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  eapply discrete_iff. econstructor. exact _.
Qed.

Lemma discrete_nat : discrete nat.
Proof.
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  eapply discrete_iff. econstructor. exact _.
Qed.

Lemma discrete_nat_nat : discrete (nat * nat).
Proof.
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  eapply discrete_iff. econstructor. exact _.
Qed.

Instance enum_bool : enumT bool.
Proof.
(* Goal: enumT bool *)
  exists (fun n => [true; false]).
  - eauto.
  - intros b; exists 1; destruct b; cbn; eauto.
Qed.

Lemma count_bool :
  enumerable__T bool.
Proof.
(* Goal: enumerable__T bool *)
  exists (fun n => match n with 0 => Some true | _ => Some false end).
(* Goal: forall _ : or (p x) (q x), @ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m)) *)
  intros []. now exists 0. now exists 1.
Qed.

Instance enumT_nat : enumT nat.
Proof.
  exists (fix f n := match n with 0 => [0] | S n => f n ++ [S n] end).
  - intros ?; cbn; eauto.
  - intros n. exists n. destruct n; eauto.
Defined.

Lemma count_nat :
  enumerable__T nat.
Proof.
(* Goal: @ex nat (fun n : nat => @eq (option Y) match f n with | Some (pair x y as p0) => @Some Y y | None => @None Y end (@Some Y y)) *)
  exists Some. intros n. now exists n.
Qed.

Lemma T_nat_in n m : m <= n -> m el L_T nat n.
Proof.
(* Goal: forall _ : le m n, @In nat m (@L_T nat enumT_nat n) *)
  induction 1.
  - destruct m; cbn. tauto. apply in_app_iff. cbn. tauto. 
  - cbn. apply in_app_iff. now left.
Qed.

Lemma T_nat_length n : length (L_T nat n) = S n.
Proof.
(* Goal: @eq nat (@length nat (@L_T nat enumT_nat n)) (S n) *)
  induction n; cbn; try rewrite app_length. omega. cbn in *. omega. 
Qed.

Section enumerable_prod.

  Variable X Y : Type.

  Section fixLs.
    
    Variables (L_X : enumT X).
    Variables (L_Y : enumT Y).
    
    Fixpoint T_prod (n : nat) : list (X * Y) :=
      match n
      with
      | 0 => [ (x,y) | (x,y) ∈ (L_T X 0 × L_T Y 0) ]
      | S n => T_prod n ++ [ (x,y) | (x,y) ∈ (L_T X n  × L_T Y n) ]
      end.

    Lemma T_prod_cum : cumulative T_prod.
    Proof.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      intros ?; cbn; eauto.
    Qed.

  End fixLs.
  
  Lemma T_prod_el LX LY (a : X * Y)  :
    exists n, a el T_prod LX LY n.
  Proof.
(* Goal: @ex nat (fun n : nat => @In (prod X Y) a (T_prod LX LY n)) *)
    destruct a. destruct (el_T x) as [m1], (el_T y) as [m2].
    exists (1 + m1 + m2). cbn. in_app 2.
(* Goal: gt (Init.Nat.add (@length (prod nat nat) (@T_prod nat nat enumT_nat enumT_nat n)) (S k)) (S n) *)
    in_collect (x,y); eapply cum_ge'; eauto; omega.
  Qed.

  Global Instance prod_enumerable (LX : enumT X) (LY : enumT Y) : enumT (X * Y). 
  Proof.
(* Goal: enumT (prod X Y) *)
    exists (T_prod LX LY).
    - apply T_prod_cum.
    - apply T_prod_el.
  Defined.

End enumerable_prod.

Lemma C_exhaustive n m : (n,m) el L_T (1 + n + m).
Proof.
(* Goal: @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) (Init.Nat.add (S O) m)) *)
  cbn. in_app 2. in_collect (n, m); apply T_nat_in; omega.  
Qed.

Lemma C_longenough n : length (L_T (nat * nat) n) > n.
Proof.
(* Goal: @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) (Init.Nat.add (S O) m)) *)
  induction n; cbn.
  - omega.
  - rewrite app_length, map_length, prod_length, T_nat_length. cbn in *. remember (n + n * S n) as k. omega.
Qed.

Definition R_nat_nat n : option (nat * nat) := nthe n (L_T n).

Lemma pairs_retract :  forall p, exists n, R_nat_nat n = Some p. 
Proof.
  intros [n m].  
  unfold R_nat_nat. destruct(pos (fun x y => Dec (x = y)) (n,m) (L_T (1 + n + m))) as [ k | ] eqn:A.
(* Goal: @ex nat (fun n : nat => @eq (option X) (ofNat n) (@Some X x)) *)
  exists k. destruct (nthe k (L_T k)) eqn:B.
  - eapply pos_nthe in A.
    destruct (le_lt_dec k (1 + n + m)) as [D | ?].
    + destruct (cum_ge (@cum_T (nat * nat) _) D) as [B' HB]. rewrite HB in A.
      rewrite (nthe_app_l _ B) in A. now injection A.
    + assert (1 + n + m <= k) as D by omega.
      destruct (cum_ge (@cum_T (nat * nat) _) D) as [B' HB]. rewrite HB in B.
      rewrite (nthe_app_l  _ A) in B. now injection B.
  - exfalso. edestruct nthe_length. 2:{ rewrite e in B. inv B. } eapply C_longenough.
  - exfalso. destruct (el_pos (fun x y => Dec (x = y)) (C_exhaustive n m)) as [k H]. congruence.
Qed.

Lemma enumerable_nat_nat : enumerable__T (nat * nat).
Proof.
(* Goal: enumerable__T (prod nat nat) *)
  exists R_nat_nat. eapply pairs_retract.
Qed.
  
Section enum_enumerable.
  
  Context X L p { enum_X : @enum X p L }.

  Definition ofNat n := match R_nat_nat n with Some (n, m) => nthe n ((L m)) | None => None end.

  Lemma enum_count : enumerable p.
  Proof.
(* Goal: forall x : X, iff (p x) (@ex nat (fun n : nat => @eq (option X) (ofNat n) (@Some X x))) *)
    exists ofNat. unfold R_nat_nat. destruct enum_X as [CX HX].
(* Goal: forall x : X, iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x0 : X => x0) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
    intros. rewrite HX.
    - split; intros [n].
      + eapply In_nth_error in H as [m].
(* Goal: @ex nat (fun n : nat => @eq (option X) (ofNat n) (@Some X x)) *)
        destruct (pairs_retract (m, n)) as [k]. exists k. unfold ofNat. now rewrite H0.
      + unfold ofNat in *. destruct R_nat_nat as [ [] | ].
(* Goal: @ex nat (fun m : nat => @In X x (L m)) *)
        eapply nth_error_In in H. eauto. inv H.
  Qed.
  
End enum_enumerable.

(** ** Discrete types are closed under ... *)

Lemma discrete_prod X Y : discrete X -> discrete Y -> discrete (X * Y).
Proof.
(* Goal: forall (_ : discrete X) (_ : discrete Y), discrete (sum X Y) *)
  intros [d1] % discrete_iff [d2] % discrete_iff.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
  eapply discrete_iff. eauto.
Qed.

Lemma discrete_sum X Y : discrete X -> discrete Y -> discrete (X + Y).
Proof.
(* Goal: forall (_ : discrete X) (_ : discrete Y), discrete (sum X Y) *)
  intros [d1] % discrete_iff [d2] % discrete_iff.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
  eapply discrete_iff. eauto.
Qed.


Lemma discrete_option X : discrete X -> discrete (option X).
Proof.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
  intros [d1] % discrete_iff. eapply discrete_iff. eauto.
Qed.

Lemma discrete_list X : discrete X -> discrete (list X).
Proof.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
  intros [d1] % discrete_iff. eapply discrete_iff. eauto.
Qed.

(** ** Enumerable types are closed under ... *)

Lemma enumerable_enum X p :
  (exists L, enum p L) <-> @enumerable X p.
Proof.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  split.
  - intros [L]. eapply enum_count; eauto.
  - intros [f]. destruct count_enum' with (e := f) as (L & ?).
    eapply enum_to_cumulative. intros. now rewrite <- H0, H.
Qed.

Lemma enum_enumT X :
  enumerable__T X <-> inhabited (enumT X).
Proof.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  rewrite <- enumerable_enumerable_T, <-  enumerable_enum. split. 
  - intros [L [] ]. econstructor. unshelve econstructor. exact L. all: firstorder.
  - intros [ [] ]. exists L_T0. firstorder.
Qed.

Lemma enumerable__T_prod X Y : enumerable__T X -> enumerable__T Y -> enumerable__T (X * Y).
Proof.
(* Goal: forall (_ : enumerable__T X) (_ : enumerable__T Y), enumerable__T (sum X Y) *)
  intros [LX] % enum_enumT [LY] % enum_enumT.
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  eapply enum_enumT. econstructor.
(* Goal: enumT (list X) *)
  exact _.  
Qed.

Lemma enumerable__T_sum X Y : enumerable__T X -> enumerable__T Y -> enumerable__T (X + Y).
Proof.
(* Goal: forall (_ : enumerable__T X) (_ : enumerable__T Y), enumerable__T (sum X Y) *)
  intros [LX] % enum_enumT [LY] % enum_enumT.
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  eapply enum_enumT. econstructor.
  exists (fix f n := match n with 0 => [] | S n => f n ++ [ inl x | x ∈ L_T X n ] ++ [inr y | y ∈ L_T Y n] end).
  - eauto.
  - intros [].
    + destruct (el_T x) as [m]. exists (1 + m).
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      cbn. in_app 2. in_collect x. eauto.
    + destruct (el_T y) as [m]. exists (1 + m).
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      cbn. in_app 3. in_collect y. eauto.
Qed.

Lemma enumerable__T_option X : enumerable__T X -> enumerable__T (option X).
Proof.
(* Goal: forall _ : @enumerable (prod X Y) p, @enumerable Y (fun y : Y => @ex X (fun x : X => p (@pair X Y x y))) *)
  intros [f]. exists (fun n => match n with 0 => Some None | S n => Some (f n) end).
(* Goal: forall _ : or (p x) (q x), @ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m)) *)
  intros [].
  - destruct (H x) as [n]. exists (S n). congruence.
  - now exists 0.
Qed.

Section enumerable_list.

  Variable X : Type.

  Section fixL.
    
    Variables (LX : enumT X).

    Fixpoint T_list (n : nat) : list (list X) :=
      match n
      with
      | 0 => [ [] ]
      | S n => T_list n ++ [ x :: L | (x,L) ∈ (L_T X n × T_list n) ]
      end.

    Lemma T_list_cum : cumulative T_list. 
    Proof.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      intros ?; cbn; eauto. 
    Qed.

  End fixL.

  Lemma T_list_el LX l :
    exists n, l el T_list LX n.
  Proof.
    induction l.
    - exists 0. cbn. eauto.
    - destruct IHl as [n IH].
      destruct (el_T a) as [m ?].
(* Goal: forall x : X, iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x0 : X => x0) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
      exists (1 + n + m). cbn. intros. in_app 2.
      in_collect (a,l).
(* Goal: gt (Init.Nat.add (@length (prod nat nat) (@T_prod nat nat enumT_nat enumT_nat n)) (S k)) (S n) *)
      all: eapply cum_ge'; eauto using T_list_cum; omega. 
  Qed.
  
  Global Instance enumerable_list (LX : enumT X) : enumT (list X).
  Proof.
(* Goal: enumT (list X) *)
    exists (T_list LX). apply T_list_cum. apply T_list_el.
  Qed.

End enumerable_list.

Lemma enumerable__T_list X : enumerable__T X -> enumerable__T (list X).
Proof.
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  intros [LX] % enum_enumT. eapply enum_enumT. econstructor.
(* Goal: enumT (list X) *)
  exact _.
Qed.

(** ** Enumerable predicates are closed under ... *)

Lemma enumerable_disj X (p q : X -> Prop) :
  enumerable p -> enumerable q -> enumerable (fun x => p x \/ q x).
Proof.
(* Goal: forall (_ : @enumerable X p) (_ : @enumerable X q), @enumerable X (fun x : X => or (p x) (q x)) *)
  intros [Lp] % enumerable_enum [Lq] % enumerable_enum.
(* Goal: @enumerable X (fun x : X => or (p x) (q x)) *)
  eapply enumerable_enum.
  exists (fix f n := match n with 0 => [] | S n => f n ++ [ x | x ∈ Lp n] ++ [ y | y ∈ Lq n] end).
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  econstructor.
  - eauto.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  - intros. split.
    + intros [].
      * eapply H in H1 as [m]. exists (1 + m). cbn. in_app 2. in_collect x. eauto.
      * eapply H0 in H1 as [m]. exists (1 + m). cbn. in_app 3. in_collect x. eauto.
    + intros [m]. induction m.
      * inv H1.
      * inv_collect; firstorder.
Qed.  

Lemma enumerable_conj X (p q : X -> Prop) :
  discrete X -> enumerable p -> enumerable q -> enumerable (fun x => p x /\ q x).
Proof.
  intros [] % discrete_iff [Lp] % enumerable_enum [Lq] % enumerable_enum.
(* Goal: @enumerable X (fun x : X => or (p x) (q x)) *)
  eapply enumerable_enum.
  exists (fix f n := match n with 0 => [] | S n => f n ++ [ x | x ∈ Lp n, x el Lq n] end).
(* Goal: @enum X (fun x : X => or (p x) (q x)) (fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) *)
  econstructor.
  - eauto.
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  - intros. split.
    + intros []. eapply H in H1 as [m1]. eapply H0 in H2 as [m2].
      exists (1 + m1 + m2). cbn. in_app 2. in_collect x.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      eapply cum_ge'; eauto. firstorder. omega.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
      eapply cum_ge'; eauto. firstorder. omega.
    + intros [m]. induction m.
      * inv H1.
      * inv_collect; firstorder.
Qed.

Lemma projection X Y (p : X * Y -> Prop) :
  enumerable p -> enumerable (fun x => exists y, p (x,y)).
Proof.
(* Goal: forall _ : @enumerable (prod X Y) p, @enumerable Y (fun y : Y => @ex X (fun x : X => p (@pair X Y x y))) *)
  intros [f].
(* Goal: @enumerable X (fun x : X => @ex Y (fun y : Y => p (@pair X Y x y))) *)
  exists (fun n => match f n with Some (x, y) => Some x | None => None end).
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  intros; split.
  - intros [y ?]. eapply H in H0 as [n]. exists n. now rewrite H0.
  - intros [n ?]. destruct (f n) as [ [] | ] eqn:E; inv H0.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
    exists y. eapply H. eauto.
Qed.

Lemma projection' X Y (p : X * Y -> Prop) :
  enumerable p -> enumerable (fun y => exists x, p (x,y)).
Proof.
(* Goal: forall _ : @enumerable (prod X Y) p, @enumerable Y (fun y : Y => @ex X (fun x : X => p (@pair X Y x y))) *)
  intros [f].
(* Goal: @enumerable Y (fun y : Y => @ex X (fun x : X => p (@pair X Y x y))) *)
  exists (fun n => match f n with Some (x, y) => Some y | None => None end).
(* Goal: iff (or (p x) (q x)) (@ex nat (fun m : nat => @In X x ((fix f (n : nat) : list X := match n with | O => @nil X | S n0 => @app X (f n0) (@app X (@map X X (fun x : X => x) (Lp n0)) (@map X X (fun y : X => y) (Lq n0))) end) m))) *)
  intros y; split.
  - intros [x ?]. eapply H in H0 as [n]. exists n. now rewrite H0.
  - intros [n ?]. destruct (f n) as [ [] | ] eqn:E; inv H0.
(* Goal: @ex nat (fun n : nat => @eq (option (prod X Y)) (f n) (@Some (prod X Y) (@pair X Y x y))) *)
    exists x. eapply H. eauto.
Qed.
