(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)
(** %\subsection*{ support :  has\_n\_elements.v }%*)
Set Implicit Arguments.
Unset Strict Implicit.
Require Export cast_between_subsets.
Require Export distinct_facts.
(** - There are n elements in a set whenever we can make a seq of n elements of that set, being all different, which form the full set  %\label{hasnelements}% *)
Definition has_n_elements (n : Nat) (A : Setoid) :=
exists v : seq n A, full A =' seq_set v in _ /\ distinct v.
intros.
generalize H0; generalize H1; clear H1 H0; intros H0 H1.
red in |- *.
rewrite H0 in H.
red in H.
inversion_clear H.
exists (Map_to_equal_subsets H1 x).
inversion_clear H2.
split.
simpl in |- *.
red in |- *.
split; intros.
simpl in |- *.
unfold subtype_image_equal in |- *.
simpl in H1.
red in H1.
destruct x0.
rename subtype_elt into a.
rename subtype_prf into sp.
generalize (H1 a).
intro.
inversion_clear H4.
generalize (H6 sp).
intro.
set (a' := Build_subtype H4) in *.
simpl in H; red in H.
generalize (H a').
intro.
inversion_clear H7.
generalize (H8 I); intro.
simpl in H7.
inversion_clear H7.
rename x0 into i.
exists i.
apply Trans with (subtype_elt a'); auto with algebra.
simpl in |- *.
red in H10.
apply Trans with (subtype_elt (x i)); auto with algebra.
simpl in |- *.
auto.
red in |- *.
intros.
red in H3; intro; (apply (H3 i j); auto with algebra).
simpl in |- *.
simpl in H4.
red in |- *.
red in H4.
apply Trans with (subtype_elt (Map_to_equal_subsets H1 x i));
auto with algebra.
apply Trans with (subtype_elt (Map_to_equal_subsets H1 x j));
auto with algebra.
Qed.
(** - These are trivial results but for the one level of subset-ness: *)
intros.
red in H.
inversion_clear H.
inversion_clear H0.
exists (Map_embed x).
simpl in H.
red in H.
simpl in H.
split; simpl in |- *.
red in |- *.
simpl in |- *.
intro a; split; intros.
generalize (H (Build_subtype H0)); clear H; intros (p, q); generalize (p I);
clear p q; intro.
inversion_clear H.
red in H2.
exists x0.
simpl in H2.
auto.
inversion_clear H0.
apply in_part_comp_l with (subtype_elt (x x0)); auto with algebra.
apply Map_embed_preserves_distinct; auto with algebra.
Qed.
intros.
red in |- *.
inversion_clear H.
inversion_clear H0.
assert (forall i : fin n, in_part (x i) B).
intro.
simpl in H.
red in H.
generalize (H (x i)).
intros (p, q).
apply q.
simpl in |- *.
exists i; apply Refl.
exists (cast_map_to_subset H0).
split.
simpl in |- *.
red in |- *.
split; intro.
simpl in |- *.
unfold subtype_image_equal in |- *.
simpl in H; red in H; simpl in H.
generalize (H (subtype_elt x0)).
intros (p, q).
assert (in_part (subtype_elt x0) B); auto with algebra.
generalize (p H3); intro.
inversion_clear H4.
exists x1.
apply Trans with (x x1); auto with algebra.
simpl in |- *.
auto with algebra.
red in |- *; intros; red in H1; red in H1.
intro.
apply (H1 _ _ H2).
simpl in H3.
red in H3; simpl in H3.
destruct x.
simpl in H3.
simpl in |- *.
auto.
Qed.
intros.
generalize (n_element_subset_sequence H).
intros (v, (E, D)).
apply Trans with (seq_set v); auto with algebra.
Qed.
intros.
apply seq_set_n_element_subset; auto with algebra.
exists (empty_seq A).
split.
apply Trans with (empty A); auto with algebra.
red in |- *.
intros.
apply False_ind; auto with algebra.
Qed.
Hint Resolve has_zero_elements_then_empty empty_then_has_zero_elements:
algebra.
intros.
inversion_clear H.
inversion_clear H0.
apply seq_set_n_element_subset; auto with algebra.
exists x.
tauto.
Qed.
induction n.
intros.
destruct m as [| n]; simpl in |- *.
auto.
inversion_clear H0.
set (c := head x) in *.
assert (B =' empty A in _).
generalize (n_element_subset_sequence H); intro p.
inversion_clear p.
inversion_clear H0.
apply Trans with (seq_set x0); auto with algebra.
simpl in H1; red in H1.
elim (H1 (C c)).
intros.
assert (in_part (subtype_elt c) C); auto with algebra.
generalize (H4 H5).
intros.
assert (in_part (C c) (empty A)).
apply in_part_comp_r with B; auto with algebra.
simpl in H7.
contradiction.
intros B HB.
induction m.
intros.
inversion_clear HB.
set (b := head x) in *.
assert (C =' empty A in _).
generalize (n_element_subset_sequence H); intro p.
inversion_clear p.
inversion_clear H2.
apply Trans with (seq_set x0); auto with algebra.
simpl in H0; red in H0.
elim (H0 (B b)).
intros.
assert (in_part (subtype_elt b) B); auto with algebra.
generalize (H3 H5).
intros.
assert (in_part (B b) (empty A)).
apply in_part_comp_r with C; auto with algebra.
simpl in H7.
contradiction.
generalize (n_element_subset_sequence HB); intro p.
inversion_clear p.
set (B' := seq_set (Seqtl x)) in *.
assert (distinct (Seqtl x)).
inversion_clear H.
apply Seqtl_preserves_distinct; auto with algebra.
assert (has_n_elements n B').
apply seq_set_n_element_subset.
exists (Seqtl x); auto with algebra.
intros.
simpl in H3.
red in H3.
elim (H3 (head x)).
intros.
assert (in_part (head x) B).
clear H3 H4 H5; inversion_clear H.
simpl in H3.
red in H3.
simpl in H3.
elim (H3 (head x)); intros.
unfold head in |- *.
apply H5; auto with algebra.
exists (Build_finiteT (lt_O_Sn n)).
unfold head in |- *; auto with algebra.
generalize (H4 H6).
intro.
clear H4 H5 H6.
rename H3 into HBC.
generalize (n_element_subset_sequence H2).
intro p; inversion_clear p.
inversion_clear H3.
assert (in_part (head x) (seq_set x0)).
apply in_part_comp_r with C; auto with algebra.
simpl in H3.
inversion_clear H3.
set (C' := seq_set (omit x0 x1)) in *.
assert (has_n_elements m C').
apply seq_set_n_element_subset.
exists (omit x0 x1); split; auto with algebra.
generalize omit_preserves_distinct.
intro p.
apply (p _ _ x0 H5 x1).
simpl in |- *.
apply f_equal with (f := S); auto with algebra.
apply (IHn _ H1 _ _ H3).
assert (seq_set x =' seq_set x0 in _).
inversion_clear H.
apply Trans with B; auto with algebra.
apply Trans with C; auto with algebra.
unfold B', C' in |- *.
inversion_clear H.
clear H3 C' H4 HBC H7 H2 C H1 H0 B' H9 IHm HB B IHn.
move H10 after x1.
rename x1 into i.
change (eq_part (seq_set (Seqtl x)) (seq_set (omit x0 i))) in |- *.
red in |- *.
intro a; split; intros.
assert (~ a =' x0 i in _).
assert (~ a =' head x in _).
simpl in H.
inversion_clear H.
destruct x1.
intro.
unfold head in H.
assert
(x (Build_finiteT (lt_O_Sn n)) ='
x (Build_finiteT (lt_n_S index n in_range_prf)) in _).
apply Trans with a; auto with algebra.
red in H10.
red in H10.
apply
H10
with
(Build_finiteT (lt_O_Sn n))
(Build_finiteT (lt_n_S index n in_range_prf));
auto with algebra.
simpl in |- *.
auto.
intro; red in H0; (apply H0; auto with algebra).
apply Trans with (x0 i); auto with algebra.
assert (in_part a (seq_set x0)).
assert (in_part a (seq_set x)).
simpl in H.
inversion_clear H.
destruct x1.
apply in_part_comp_l with (x (Build_finiteT (lt_n_S index n in_range_prf)));
auto with algebra.
simpl in |- *.
exists (Build_finiteT (lt_n_S index n in_range_prf)); auto with algebra.
apply in_part_comp_r with (seq_set x); auto with algebra.
simpl in H1.
inversion_clear H1.
rename x1 into q.
assert (~ i =' q in fin _).
intro; red in H0; apply H0.
apply Trans with (x0 q); auto with algebra.
elim (omit_removes' x0 H1).
intros.
apply in_part_comp_l with (x0 q); auto with algebra.
apply in_part_comp_l with (omit x0 i x1); auto with algebra.
simpl in |- *; exists x1; auto with algebra.
Opaque omit.
simpl in H.
inversion_clear H.
rename x1 into q.
assert (~ a =' x0 i in _).
intro.
generalize (distinct_omit_removes_all H5 (i:=i) (j:=q)); intro p; red in p.
apply p.
apply Trans with a; auto with algebra.
assert (~ a =' head x in _).
red in H; intro; apply H.
apply Trans with (head x); auto with algebra.
assert (in_part a (seq_set x0)).
apply in_part_comp_l with (omit x0 i q); auto with algebra.
apply omit_seq_in_seq_set; auto with algebra.
assert (in_part a (seq_set x)).
apply in_part_comp_r with (seq_set x0); auto with algebra.
simpl in H3; simpl in |- *.
inversion_clear H3.
destruct x1.
destruct index as [| n0].
apply False_ind; auto with algebra.
red in H1; apply H1.
unfold head in |- *;
(apply Trans with (x (Build_finiteT in_range_prf)); auto with algebra);
simpl in |- *.
exists (Build_finiteT (lt_S_n _ _ in_range_prf)).
apply Trans with (x (Build_finiteT in_range_prf)); auto with algebra.
Qed.

Lemma has_extra_element :
forall (A : Setoid) (B C : part_set A) (m n : Nat),
has_n_elements n B ->
has_n_elements m C -> m < n -> exists a : A, in_part a B /\ ~ in_part a C.