Library Lib_FinSetImpl


Require Import Eqdep_dec.
Require Import FSetList.
Require Import OrderedTypeEx.

Require Import Lib_ListFacts.
Require Import Lib_FinSet.

First, implement only FSetInterface.S.

Module MakeRaw (X : UsualOrderedType) <: FSetInterface.S with Module E := X.

 Module Raw := Raw X.
 Module E := X.

 Module OTFacts := OrderedTypeFacts E.

 Definition sort_bool (xs : Raw.t) :=
   (if (sort_dec E.lt OTFacts.lt_dec xs) then true else false) = true.

 Record slist : Set :=
  {this :> Raw.t;
   sorted : sort_bool this}.

 Definition from_sorted : forall (xs : Raw.t), sort E.lt xs -> sort_bool xs.

 Definition to_sorted : forall xs, sort_bool xs -> sort E.lt xs.

 Coercion to_sorted : sort_bool >-> sort.

 Definition Build_slist' (xs : Raw.t) (pf : sort E.lt xs) :=
   Build_slist (from_sorted pf).

 Definition t := slist.
 Definition elt := E.t.

 Definition In (x : elt) (s : t) : Prop := InA E.eq x s.(this).
 Definition Equal (s s':t) : Prop := forall a : elt, In a s <-> In a s'.
 Definition Subset (s s':t) : Prop := forall a : elt, In a s -> In a s'.
 Definition Empty (s:t) : Prop := forall a : elt, ~ In a s.
 Definition For_all (P : elt -> Prop)(s:t) : Prop := forall x, In x s -> P x.
 Definition Exists (P : elt -> Prop)(s:t) : Prop := exists x, In x s /\ P x.

 Definition mem (x : elt) (s : t) : bool := Raw.mem x s.
 Definition add (x : elt)(s : t) : t := Build_slist' (Raw.add_sort (sorted s) x).
 Definition remove (x : elt)(s : t) : t := Build_slist' (Raw.remove_sort (sorted s) x).
 Definition singleton (x : elt) : t := Build_slist' (Raw.singleton_sort x).
 Definition union (s s' : t) : t :=
   Build_slist' (Raw.union_sort (sorted s) (sorted s')).
 Definition inter (s s' : t) : t :=
   Build_slist' (Raw.inter_sort (sorted s) (sorted s')).
 Definition diff (s s' : t) : t :=
   Build_slist' (Raw.diff_sort (sorted s) (sorted s')).
 Definition equal (s s' : t) : bool := Raw.equal s s'.
 Definition subset (s s' : t) : bool := Raw.subset s s'.
 Definition empty : t := Build_slist' Raw.empty_sort.
 Definition is_empty (s : t) : bool := Raw.is_empty s.
 Definition elements (s : t) : list elt := Raw.elements s.
 Definition min_elt (s : t) : option elt := Raw.min_elt s.
 Definition max_elt (s : t) : option elt := Raw.max_elt s.
 Definition choose (s : t) : option elt := Raw.choose s.
 Definition fold (B : Set) (f : elt -> B -> B) (s : t) : B -> B := Raw.fold (B:=B) f s.
 Definition cardinal (s : t) : nat := Raw.cardinal s.
 Definition filter (f : elt -> bool) (s : t) : t :=
   Build_slist' (Raw.filter_sort (sorted s) f).
 Definition for_all (f : elt -> bool) (s : t) : bool := Raw.for_all f s.
 Definition exists_ (f : elt -> bool) (s : t) : bool := Raw.exists_ f s.
 Definition partition (f : elt -> bool) (s : t) : t * t :=
   let p := Raw.partition f s in
   (@Build_slist' (fst p) (Raw.partition_sort_1 (sorted s) f),
   @Build_slist' (snd p) (Raw.partition_sort_2 (sorted s) f)).
 Definition eq (s s' : t) : Prop := Raw.eq s s'.
 Definition lt (s s' : t) : Prop := Raw.lt s s'.

 Section Spec.
  Variable s s' s'': t.
  Variable x y : elt.

  Lemma In_1 : E.eq x y -> In x s -> In y s.

  Lemma mem_1 : In x s -> mem x s = true.
  Lemma mem_2 : mem x s = true -> In x s.

  Lemma equal_1 : Equal s s' -> equal s s' = true.
  Lemma equal_2 : equal s s' = true -> Equal s s'.

  Lemma subset_1 : Subset s s' -> subset s s' = true.
  Lemma subset_2 : subset s s' = true -> Subset s s'.

  Lemma empty_1 : Empty empty.

  Lemma is_empty_1 : Empty s -> is_empty s = true.
  Lemma is_empty_2 : is_empty s = true -> Empty s.

  Lemma add_1 : E.eq x y -> In y (add x s).
  Lemma add_2 : In y s -> In y (add x s).
  Lemma add_3 : ~ E.eq x y -> In y (add x s) -> In y s.

  Lemma remove_1 : E.eq x y -> ~ In y (remove x s).
  Lemma remove_2 : ~ E.eq x y -> In y s -> In y (remove x s).
  Lemma remove_3 : In y (remove x s) -> In y s.

  Lemma singleton_1 : In y (singleton x) -> E.eq x y.
  Lemma singleton_2 : E.eq x y -> In y (singleton x).

  Lemma union_1 : In x (union s s') -> In x s \/ In x s'.
  Lemma union_2 : In x s -> In x (union s s').
  Lemma union_3 : In x s' -> In x (union s s').

  Lemma inter_1 : In x (inter s s') -> In x s.
  Lemma inter_2 : In x (inter s s') -> In x s'.
  Lemma inter_3 : In x s -> In x s' -> In x (inter s s').

  Lemma diff_1 : In x (diff s s') -> In x s.
  Lemma diff_2 : In x (diff s s') -> ~ In x s'.
  Lemma diff_3 : In x s -> ~ In x s' -> In x (diff s s').

  Lemma fold_1 : forall (A : Set) (i : A) (f : elt -> A -> A),
      fold f s i = fold_left (fun a e => f e a) (elements s) i.

  Lemma cardinal_1 : cardinal s = length (elements s).

  Section Filter.

  Variable f : elt -> bool.

  Lemma filter_1 : compat_bool E.eq f -> In x (filter f s) -> In x s.
  Lemma filter_2 : compat_bool E.eq f -> In x (filter f s) -> f x = true.
  Lemma filter_3 :
      compat_bool E.eq f -> In x s -> f x = true -> In x (filter f s).

  Lemma for_all_1 :
      compat_bool E.eq f ->
      For_all (fun x => f x = true) s -> for_all f s = true.
  Lemma for_all_2 :
      compat_bool E.eq f ->
      for_all f s = true -> For_all (fun x => f x = true) s.

  Lemma exists_1 :
      compat_bool E.eq f ->
      Exists (fun x => f x = true) s -> exists_ f s = true.
  Lemma exists_2 :
      compat_bool E.eq f ->
      exists_ f s = true -> Exists (fun x => f x = true) s.

  Lemma partition_1 :
      compat_bool E.eq f -> Equal (fst (partition f s)) (filter f s).
  Lemma partition_2 :
      compat_bool E.eq f ->
      Equal (snd (partition f s)) (filter (fun x => negb (f x)) s).

  End Filter.

  Lemma elements_1 : In x s -> InA E.eq x (elements s).
  Lemma elements_2 : InA E.eq x (elements s) -> In x s.
  Lemma elements_3 : sort E.lt (elements s).

  Lemma min_elt_1 : min_elt s = Some x -> In x s.
  Lemma min_elt_2 : min_elt s = Some x -> In y s -> ~ E.lt y x.
  Lemma min_elt_3 : min_elt s = None -> Empty s.

  Lemma max_elt_1 : max_elt s = Some x -> In x s.
  Lemma max_elt_2 : max_elt s = Some x -> In y s -> ~ E.lt x y.
  Lemma max_elt_3 : max_elt s = None -> Empty s.

  Lemma choose_1 : choose s = Some x -> In x s.
  Lemma choose_2 : choose s = None -> Empty s.

  Lemma eq_refl : eq s s.
  Lemma eq_sym : eq s s' -> eq s' s.
  Lemma eq_trans : eq s s' -> eq s' s'' -> eq s s''.

  Lemma lt_trans : lt s s' -> lt s' s'' -> lt s s''.
  Lemma lt_not_eq : lt s s' -> ~ eq s s'.

  Definition compare : Compare lt eq s s'.

 End Spec.

End MakeRaw.

Now, implement the FinSet interface.

Module Make (X : UsualOrderedType) <: FinSet with Module E := X.

  Module Import E := X.
  Module Import S := MakeRaw X.

Define aliases.

  Definition fset := S.t.
  Definition elt := S.elt.

Equality on these sets is extensional.

  Lemma bool_dec : forall x y : bool,
    x = y \/ x <> y.

  Theorem eq_ext : forall s s' : t, (forall a, In a s <-> In a s') -> s = s'.

  Theorem eq_if_Equal : forall s s' : t, Equal s s' -> s = s'.

End Make.