(* 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.

Lemma seq_set_n_element_subset :
forall (A : Setoid) (n : Nat) (B : part_set A),
(exists v : seq n A, B =' seq_set v in _ /\ distinct v) ->
has_n_elements n B.