(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *   INRIA, CNRS and contributors - Copyright 1999-2018       *)
(* <O___,, *       (see CREDITS file for the list of authors)           *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
Definition enumeration (A : Type) (u : nat -> A) (v : A -> nat) : Prop :=
(forall x : A, u (v x) = x) /\ (forall n : nat, v (u n) = n).
Definition in_holed_interval (a b hole : R) (u : nat -> R) (n : nat) : Prop :=
Rlt a (u n) /\ Rlt (u n) b /\ u n <> hole.
Definition point_in_holed_interval (a b h : R) : R :=
if Req_EM_T h (Rdiv (Rplus a b) (INR 2)) then (Rdiv (Rplus a h) (INR 2))
else (Rdiv (Rplus a b) (INR 2)).
(* An enumeration of R reaches any open interval of R, extract the first two real numbers in it. *)
Definition first_in_holed_interval (u : nat -> R) (v : R -> nat) (a b h : R)
: enumeration R u v -> Rlt a b
-> { n : nat | in_holed_interval a b h u n
/\ forall k : nat, k < n -> ~in_holed_interval a b h u k }.
Definition first_two_in_interval (u : nat -> R) (v : R -> nat) (a b : R)
(pen : enumeration R u v) (plow : Rlt a b)
: prod R R :=
let (first_index, pr) := first_in_holed_interval u v a b b pen plow in
let (second_index, pr2) := first_in_holed_interval u v a b (u first_index) pen plow in
if Rle_dec (u first_index) (u second_index) then (u first_index, u second_index)
else (u second_index, u first_index).
(* If u,v is an enumeration of R, this sequence of open intervals tears the segment [0,1]. The recursive definition needs the proof that the previous interval is ordered, hence the type.  The first sequence is increasing, the second decreasing. The first is below the second. Therefore the first sequence has a limit, a least upper bound b, that u cannot reach, which contradicts u (v b) = b. *)
Definition tearing_sequences (u : nat -> R) (v : R -> nat)
: (enumeration R u v) -> nat -> { ab : prod R R | Rlt (fst ab) (snd ab) }.
Definition tearing_elem_fst (u : nat -> R) (v : R -> nat) (pen : enumeration R u v) (x : R)
:= exists n : nat, x = fst (proj1_sig (tearing_sequences u v pen n)).
(* The limit of the first tearing sequence cannot be reached by u *)
Definition torn_number (u : nat -> R) (v : R -> nat) (pen : enumeration R u v) :
{m : R | is_lub (tearing_elem_fst u v pen) m}.

(* The torn number is between both tearing sequences, so it could have been chosen at each step. *)
Lemma torn_number_below_second_sequence (u : nat -> R) (v : R -> nat)
(en : enumeration R u v) :
forall n : nat, Rlt (proj1_sig (torn_number u v en))
(snd (proj1_sig (tearing_sequences u v en n))).