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

Lemma has_n_elements_inj :
forall (A : Setoid) (n : Nat) (B : part_set A),
has_n_elements n B ->
forall (m : Nat) (C : part_set A),
has_n_elements m C -> B =' C in _ -> n =' m in _.