Library Coq.FSets.FSetPositive
Efficient implementation of
FSetInterface.S for positive keys,
inspired from the
FMapPositive module.
This module was adapted by Alexandre Ren, Damien Pous, and Thomas
Braibant (2010, LIG, CNRS, UMR 5217), from the
FMapPositive
module of Pierre Letouzey and Jean-Christophe Filliâtre, which in
turn comes from the
FMap framework of a work by Xavier Leroy and
Sandrine Blazy (used for building certified compilers).
helper function to avoid creating empty trees that are not leaves
reverses y and concatenate it with x
the additional argument, i, records the current path, in
reverse order (this should be more efficient: we reverse this argument
only at present nodes only, rather than at each node of the tree).
we also use this convention in all functions below
uses a to accumulate values rather than doing a lot of concatenations
would it be more efficient to use a path like in the above functions ?
lexicographic product, defined using a notation to keep things lazy
Notation lex u v :=
match u with Eq =>
v |
Lt =>
Lt |
Gt =>
Gt end.
Definition compare_bool a b :=
match a,
b with
|
false,
true =>
Lt
|
true,
false =>
Gt
|
_,
_ =>
Eq
end.
Fixpoint compare_fun (
m m´:
t):
comparison :=
match m,
m´ with
|
Leaf,
_ =>
if is_empty m´ then Eq else Lt
|
_,
Leaf =>
if is_empty m then Eq else Gt
|
Node l o r,
Node l´ o´ r´ =>
lex (
compare_bool o o´) (
lex (
compare_fun l l´) (
compare_fun r r´))
end.
Definition In i t :=
mem i t = true.
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 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).
Notation "s [<=] t" := (
Subset s t) (
at level 70,
no associativity).
Definition eq :=
Equal.
Definition lt m m´ :=
compare_fun m m´ = Lt.
Specification of In
Specification of eq
Specification of mem
Additional lemmas for mem
Specification of empty
Specification of node
Specification of is_empty
Specification of subset
Specification of equal (via subset)
(Specified) definition of compare
Lemma lex_Opp:
forall u v u´ v´,
u = CompOpp u´ ->
v = CompOpp v´ ->
lex u v = CompOpp (
lex u´ v´).
Lemma compare_bool_inv:
forall b b´,
compare_bool b b´ = CompOpp (
compare_bool b´ b).
Lemma compare_inv:
forall s s´,
compare_fun s s´ = CompOpp (
compare_fun s´ s).
Lemma lex_Eq:
forall u v,
lex u v = Eq <-> u=Eq /\ v=Eq.
Lemma compare_bool_Eq:
forall b1 b2,
compare_bool b1 b2 = Eq <-> eqb b1 b2 = true.
Lemma compare_equal:
forall s s´,
compare_fun s s´ = Eq <-> equal s s´ = true.
Lemma compare_gt:
forall s s´,
compare_fun s s´ = Gt ->
lt s´ s.
Lemma compare_eq:
forall s s´,
compare_fun s s´ = Eq ->
eq s s´.
Lemma compare :
forall s s´ :
t,
Compare lt eq s s´.
Section lt_spec.
Inductive ct:
comparison ->
comparison ->
comparison ->
Prop :=
|
ct_xxx:
forall x,
ct x x x
|
ct_xex:
forall x,
ct x Eq x
|
ct_exx:
forall x,
ct Eq x x
|
ct_glx:
forall x,
ct Gt Lt x
|
ct_lgx:
forall x,
ct Lt Gt x.
Lemma ct_cxe:
forall x,
ct (
CompOpp x)
x Eq.
Lemma ct_xce:
forall x,
ct x (
CompOpp x)
Eq.
Lemma ct_lxl:
forall x,
ct Lt x Lt.
Lemma ct_gxg:
forall x,
ct Gt x Gt.
Lemma ct_xll:
forall x,
ct x Lt Lt.
Lemma ct_xgg:
forall x,
ct x Gt Gt.
Local Hint Constructors ct:
ct.
Local Hint Resolve ct_cxe ct_xce ct_lxl ct_xll ct_gxg ct_xgg:
ct.
Ltac ct :=
trivial with ct.
Lemma ct_lex:
forall u v w u´ v´ w´,
ct u v w ->
ct u´ v´ w´ ->
ct (
lex u u´) (
lex v v´) (
lex w w´).
Lemma ct_compare_bool:
forall a b c,
ct (
compare_bool a b) (
compare_bool b c) (
compare_bool a c).
Lemma compare_x_Leaf:
forall s,
compare_fun s Leaf = if is_empty s then Eq else Gt.
Lemma compare_empty_x:
forall a,
is_empty a = true ->
forall b,
compare_fun a b = if is_empty b then Eq else Lt.
Lemma compare_x_empty:
forall a,
is_empty a = true ->
forall b,
compare_fun b a = if is_empty b then Eq else Gt.
Lemma ct_compare_fun:
forall a b c,
ct (
compare_fun a b) (
compare_fun b c) (
compare_fun a c).
End lt_spec.
Lemma lt_trans:
forall s s´ s´´,
lt s s´ ->
lt s´ s´´ ->
lt s s´´.
Lemma lt_not_eq:
forall s s´,
lt s s´ ->
~ eq s s´.
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
Specification of choose
Specification of min_elt
Specification of max_elt