(* 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 :  counting\_elements.v }%*)
Set Implicit Arguments.
Unset Strict Implicit.
From Algebra Require Export Diff.
From Algebra Require Export Singleton.
Require Export has_n_elements.
From Algebra Require Export Union.
Require Export const.
(** - Once we have has_n_elements, it would seem to be easy to define has_at_least_n_elements and has_at_most_n_elements. But one has to take a little care. We could not just say, e.g. [  Definition has_at_least_n_elements := ] [    [n:nat;A:Setoid](EXT m:nat | (le n m) /\ (has_n_elements m A)).  ] since the set may be infinite, and then there is no such m to be found.  - But we may adapt the definition of has_n_elements slightly. It said that we can make a sequence [v] of length [n] with all elements distinct---[(distinct v)]; covering the whole set---[(seq_set v)='(full v)]. Now we just drop this last requirement so we only need to point out a sequence of $n$ different elements:  %\label{hasatleastnelements}% *)
Definition has_at_least_n_elements (n : Nat) (A : Setoid) :=
exists v : seq n A, distinct v.
(** - Note that, contrarily, we may not just drop the other requirement to obtain  [Definition has_at_most_n_elements_wrong := [n:nat;A:Setoid](EXT v:(seq n A) | (full A)='(seq_set v)).]  since this doesn't work out well for empty sets: it has at most n elements for any n, but with this definition we'd need to exhibit an n-length sequence, which we can't do for $n>0$.*)
Definition has_at_most_n_elements (n : Nat) (A : Setoid) :=
exists m : nat, m <= n /\ has_n_elements m A.
intros.
red in |- *.
red in H.
inversion_clear H.
rename x into Bsize.
inversion_clear H2.
exists Bsize.
split.
generalize (eq_ind _ (fun n => Bsize <= n) H _ H1).
auto.
apply has_n_elements_comp with Bsize B; auto with algebra.
Qed.
intros.
rewrite <- H1.
red in |- *.
red in H.
inversion_clear H.
rename x into Bseq.
exists (Map_to_equal_subsets H0 Bseq).
red in |- *; intros; intro; red in H2; (apply (H2 i j); auto with algebra).
simpl in |- *.
red in |- *.
apply Trans with (subtype_elt (Bseq i)); auto with algebra.
apply Trans with (subtype_elt (Bseq j)); auto with algebra.
apply Trans with (subtype_elt (Map_to_equal_subsets H0 Bseq i));
auto with algebra.
apply Trans with (subtype_elt (Map_to_equal_subsets H0 Bseq j));
auto with algebra.
Qed.
intros.
inversion_clear H.
exists x.
inversion_clear H0; auto.
Qed.

Lemma has_n_elements_then_has_at_most_n_elements :
forall (A : Setoid) (n : Nat),
has_n_elements n A -> forall m : Nat, n <= m -> has_at_most_n_elements m A.