Library Coq.FSets.FSetInterface
Finite set library
Set interfaces, inspired by the one of Ocaml. When compared with
Ocaml, the main differences are:
- the lack of iter function, useless since Coq is purely functional
- the use of option types instead of Not_found exceptions
- the use of nat instead of int for the cardinal function
Several variants of the set interfaces are available:
- WSfun : functorial signature for weak sets, non-dependent style
- WS : self-contained version of WSfun
- Sfun : functorial signature for ordered sets, non-dependent style
- S : self-contained version of Sfun
- Sdep : analog of S written using dependent style
If unsure,
S is probably what you're looking for: other signatures
are subsets of it, apart from
Sdep which is isomorphic to
S (see
FSetBridge).
Non-dependent signatures
The following signatures presents sets as purely informative
programs together with axioms
Functorial signature for weak sets
Weak sets are sets without ordering on base elements, only
a decidable equality.
the abstract type of sets
Logical predicates
The empty set.
Test whether a set is empty or not.
mem x s tests whether x belongs to the set s.
add x s returns a set containing all elements of s,
plus x. If x was already in s, s is returned unchanged.
singleton x returns the one-element set containing only x.
remove x s returns a set containing all elements of s,
except x. If x was not in s, s is returned unchanged.
Set union.
Set intersection.
Set difference.
equal s1 s2 tests whether the sets s1 and s2 are
equal, that is, contain equal elements.
subset s1 s2 tests whether the set s1 is a subset of
the set s2.
Parameter fold :
forall A :
Type, (
elt ->
A ->
A) ->
t ->
A ->
A.
fold f s a computes (f xN ... (f x2 (f x1 a))...),
where x1 ... xN are the elements of s.
The order in which elements of s are presented to f is
unspecified.
for_all p s checks if all elements of the set
satisfy the predicate p.
exists p s checks if at least one element of
the set satisfies the predicate p.
filter p s returns the set of all elements in s
that satisfy predicate p.
partition p s returns a pair of sets (s1, s2), where
s1 is the set of all the elements of s that satisfy the
predicate p, and s2 is the set of all the elements of
s that do not satisfy p.
Return the number of elements of a set.
Return the list of all elements of the given set, in any order.
Return one element of the given set, or None if
the set is empty. Which element is chosen is unspecified.
Equal sets could return different elements.
Specification of In
Specification of eq
Specification of mem
Specification of equal
Specification of subset
Specification of empty
Specification of is_empty
Specification of add
Specification of remove
Specification of singleton
Specification of union
Specification of inter
Specification of diff
Specification of fold
Specification of cardinal
Specification of filter
Specification of for_all
Specification of exists
Specification of partition
Specification of elements
When compared with ordered sets, here comes the only
property that is really weaker:
Specification of choose
Static signature for weak sets
Similar to the functorial signature
SW, except that the
module
E of base elements is incorporated in the signature.
Functorial signature for sets on ordered elements
Based on
WSfun, plus ordering on sets and
min_elt and
max_elt
and some stronger specifications for other functions.
Total ordering between sets. Can be used as the ordering function
for doing sets of sets.
Return the smallest element of the given set
(with respect to the E.compare ordering),
or None if the set is empty.
Same as min_elt, but returns the largest element of the
given set.
Specification of lt
Additional specification of elements
Remark: since
fold is specified via
elements, this stronger
specification of
elements has an indirect impact on
fold,
which can now be proved to receive elements in increasing order.
Specification of
min_elt
Specification of max_elt
Additional specification of choose
Static signature for sets on ordered elements
Similar to the functorial signature
Sfun, except that the
module
E of base elements is incorporated in the signature.
Some subtyping tests
WSfun ---> WS
| |
| |
V V
Sfun ---> S
Module S_WS (M : S) <: WS := M.
Module Sfun_WSfun (E:OrderedType)(M : Sfun E) <: WSfun E := M.
Module S_Sfun (M : S) <: Sfun M.E := M.
Module WS_WSfun (M : WS) <: WSfun M.E := M.
Dependent signature
Signature
Sdep presents ordered sets using dependent types
Module Type Sdep.
Declare Module E :
OrderedType.
Definition elt :=
E.t.
Parameter t :
Type.
Parameter In :
elt ->
t ->
Prop.
Definition Equal s s´ :=
forall a :
elt,
In a s <-> In a s´.
Definition Subset s s´ :=
forall a :
elt,
In a s ->
In a s´.
Definition Add x s s´ :=
forall y,
In y s´ <-> E.eq x y \/ In y s.
Definition Empty s :=
forall a :
elt,
~ In a s.
Definition For_all (
P :
elt ->
Prop)
s :=
forall x,
In x s ->
P x.
Definition Exists (
P :
elt ->
Prop)
s :=
exists x, In x s /\ P x.
Notation "s [=] t" := (
Equal s t) (
at level 70,
no associativity).
Definition eq :
t ->
t ->
Prop :=
Equal.
Parameter lt :
t ->
t ->
Prop.
Parameter compare :
forall s s´ :
t,
Compare lt eq s s´.
Parameter eq_refl :
forall s :
t,
eq s s.
Parameter eq_sym :
forall s s´ :
t,
eq s s´ ->
eq s´ s.
Parameter eq_trans :
forall s s´ s´´ :
t,
eq s s´ ->
eq s´ s´´ ->
eq s s´´.
Parameter lt_trans :
forall s s´ s´´ :
t,
lt s s´ ->
lt s´ s´´ ->
lt s s´´.
Parameter lt_not_eq :
forall s s´ :
t,
lt s s´ ->
~ eq s s´.
Parameter eq_In :
forall (
s :
t) (
x y :
elt),
E.eq x y ->
In x s ->
In y s.
Parameter empty :
{s : t | Empty s}.
Parameter is_empty :
forall s :
t,
{Empty s} + {~ Empty s}.
Parameter mem :
forall (
x :
elt) (
s :
t),
{In x s} + {~ In x s}.
Parameter add :
forall (
x :
elt) (
s :
t),
{s´ : t | Add x s s´}.
Parameter
singleton :
forall x :
elt,
{s : t | forall y :
elt,
In y s <-> E.eq x y}.
Parameter
remove :
forall (
x :
elt) (
s :
t),
{s´ : t | forall y :
elt,
In y s´ <-> ~ E.eq x y /\ In y s}.
Parameter
union :
forall s s´ :
t,
{s´´ : t | forall x :
elt,
In x s´´ <-> In x s \/ In x s´}.
Parameter
inter :
forall s s´ :
t,
{s´´ : t | forall x :
elt,
In x s´´ <-> In x s /\ In x s´}.
Parameter
diff :
forall s s´ :
t,
{s´´ : t | forall x :
elt,
In x s´´ <-> In x s /\ ~ In x s´}.
Parameter equal :
forall s s´ :
t,
{s[=]s´} + {~ s[=]s´}.
Parameter subset :
forall s s´ :
t,
{Subset s s´} + {~ Subset s s´}.
Parameter
filter :
forall (
P :
elt ->
Prop) (
Pdec :
forall x :
elt,
{P x} + {~ P x})
(
s :
t),
{s´ : t | compat_P E.eq P ->
forall x :
elt,
In x s´ <-> In x s /\ P x}.
Parameter
for_all :
forall (
P :
elt ->
Prop) (
Pdec :
forall x :
elt,
{P x} + {~ P x})
(
s :
t),
{compat_P E.eq P ->
For_all P s} + {compat_P E.eq P ->
~ For_all P s}.
Parameter
exists_ :
forall (
P :
elt ->
Prop) (
Pdec :
forall x :
elt,
{P x} + {~ P x})
(
s :
t),
{compat_P E.eq P ->
Exists P s} + {compat_P E.eq P ->
~ Exists P s}.
Parameter
partition :
forall (
P :
elt ->
Prop) (
Pdec :
forall x :
elt,
{P x} + {~ P x})
(
s :
t),
{partition : t * t |
let (
s1,
s2) :=
partition in
compat_P E.eq P ->
For_all P s1 /\
For_all (
fun x =>
~ P x)
s2 /\
(forall x :
elt,
In x s <-> In x s1 \/ In x s2)}.
Parameter
elements :
forall s :
t,
{l : list elt |
sort E.lt l /\ (forall x :
elt,
In x s <-> InA E.eq x l)}.
Parameter
fold :
forall (
A :
Type) (
f :
elt ->
A ->
A) (
s :
t) (
i :
A),
{r : A | let (
l,
_) :=
elements s in
r = fold_left (
fun a e =>
f e a)
l i}.
Parameter
cardinal :
forall s :
t,
{r : nat | let (
l,
_) :=
elements s in r = length l }.
Parameter
min_elt :
forall s :
t,
{x : elt | In x s /\ For_all (
fun y =>
~ E.lt y x)
s} + {Empty s}.
Parameter
max_elt :
forall s :
t,
{x : elt | In x s /\ For_all (
fun y =>
~ E.lt x y)
s} + {Empty s}.
Parameter choose :
forall s :
t,
{x : elt | In x s} + {Empty s}.
The choose_3 specification of S cannot be packed
in the dependent version of choose, so we leave it separate.