Library FinSet.FinSet

Finite sets of countable types can be be defined such that extensional set equality is strict equality (using Cyril Cohen's quotient construction).

Require Coq.Setoids.Setoid.
Require Import Coq.Classes.RelationClasses.
Require Import Coq.Classes.Morphisms.
Require Import FinSet.Lib.ListSet.
Require Import FinSet.Quotients.UList.
Require Import FinSet.Lib.DProp.
Require Import FinSet.Quotients.Countable.
Require Import FinSet.Quotients.Retract.
Require Import FinSet.Quotients.Quotient.

Definition T (A:Type) {_:Countable A} : Type := Quotient (eq_set_ulist eq_countable).

Instance set_countable (A:Type) {_:Countable A} : Countable (T A).

Low level interface


Definition listset {A} {_:Countable A} : Retract (list A) (T A) :=
  Retract.compose ulist (quotient (eq_set_ulist eq_countable)).

Lemma set_quotient A (_:Countable A) : x,
  eq_set_list eq_countable (listset.(inj) (listset.(proj) x)) x.
Lemma set_unique A (_:Countable A) :
   x, nodup eq_countable (listset.(inj) x).

Quantifiers


Section Quantifiers.

 Context {A} {CA:Countable A}.

Set membership. A decidable proposition.
 Definition mem (x:A) (u:T A) : DProp := mem_list eq_countable x (listset.(inj) u).
 Local Notation "x ∈ u" := (mem x u) (at level 10).

 Lemma mem_spec x u : xu mem_list eq_countable x (listset.(inj) u).

 Lemma mem_spec_in x u : xu List.In x (listset.(inj) u).

 Global Instance in_eq_set : Proper (eq==>eq_set_list eq_countable==>iff) (@List.In A).

Bounded existential quantifier on decidable propositions.
 Program Definition dexists (u:T A) (P:ADProp) : DProp := {|
   Holds := x, xu P x ;
   Denies := x, xu DNot (P x) ;
   dec := dec_lift (dec (ListSet.dexists P u))
 |}.

Bounded existential quantifier on decidable propositions.
 Program Definition dforall (u:T A) (P:ADProp) : DProp :=
   DNot (dexists u (fun xDNot (P x))).

End Quantifiers.


Module MembershipNotation.

 Notation "x ∈ u" := (mem x u) (at level 10).

End MembershipNotation.
Import MembershipNotation.

Set-theoretical operations


Import List.ListNotations.
Local Open Scope list_scope.

Generic set builders


Section SetOperations.

 Context {A} {CA:Countable A}.
 Context {B} {CB:Countable B}.

 Definition empty : T A := listset.(proj) [].

 Lemma empty_spec : x, ¬xempty.

 Lemma empty_spec_iff : x, xempty False.

 Definition singleton (a:A) : T A := listset.(proj) [a].

 Lemma singleton_spec : x a, x∈(singleton a) x=a.

 Corollary singleton_self : a, a∈(singleton a).

 Definition union (u₁ u₂:T A) : T A := listset.(proj) (listset.(inj) u₁++ listset.(inj) u₂).

 Lemma union_spec : x u₁ u₂, x∈(union u₁ u₂) xu₁ xu₂.

Union over a finite set of finite sets. Together with singleton forms a monad (restricted to countable types). This monad allows to build sets by comprehension.
 Definition Union (u:T A) (F:AT B) : T B :=
   listset.(proj) (List.flat_map (fun xlistset.(inj) (F x)) (listset.(inj) u)).

 Lemma Union_spec u F x : x (Union u F) y, yu x (F y).

End SetOperations.


guard P is the last component of the comprehension syntax. It lifts a proposition to a subset of the singleton type. It is used to implement the "such that" clause of comprehension.
Definition guard (P:DProp) : T unit :=
  if (dec P) then (listset).(proj) [tt] else empty.

Lemma guard_spec_tt P : tt∈(guard P) P.

Lemma guard_spec P : x, x∈(guard P) P.

Lemma guard_Union A (_:Countable A) P (u:T A) :
   x, x∈(Union (guard P) (fun _u)) xu P.

Notations for comprehension


Module ComprehensionNotations.

 Delimit Scope comprehension with comprehension.

 Notation "u ´⍪´ x ´∈´ v" := (Union v (fun xu))
       (at level 50, x ident, v at level 20, right associativity) : comprehension.
 Notation "u ´st´ p" := (Union (guard p) (fun _u))
       (at level 50, p at level 20, right associativity) : comprehension.
 Notation "a ´\´ " := (singleton a) (at level 20) : comprehension.
 Notation "´⦃´ x ´⦄´" := (x%comprehension).

End ComprehensionNotations.

Import ComprehensionNotations.

Derived operations


Definition inter {A} {_:Countable A} (u₁ u₂:T A) : T A := x \ xu₂ xu₁ .

Lemma inter_spec A (_:Countable A) (u₁ u₂:T A) :
   x, x (inter u₁ u₂) xu₁ xu₂.

Extensionality


Definition subset {A} {_:Countable A} (u v:T A) : DProp := dforall u (fun xxv).

Lemma double_inclusion A (_:Countable A) (u v:T A) :
  subset u v subset v u u = v.

Instance PreOrder A (_:Countable A) : PreOrder subset.

Lemma set_ext A (_:Countable A) (u v:T A) : ( x, xu xv) u=v.

Standard notations


Module SetNotations.

  Notation "∅" := empty.
  Notation "u₁ ∪ u₂" := (union u₁ u₂) (at level 40, left associativity).
  Notation "u₁ ∩ u₂" := (inter u₁ u₂) (at level 40, left associativity).

  Notation "u ⊆ v" := (subset u v) (at level 70, no associativity).

End SetNotations.

Import SetNotations.

Algebraic properties


Hint Rewrite union_spec inter_spec empty_spec_iff singleton_spec : set_simplify.
Hint Rewrite guard_Union Union_spec : set_simplify.

Ltac set_intuition :=
  intros **;
  apply set_ext; intros ?; autorewrite with set_simplify in *; intuition eauto.

Section Algebraic.

 Context (A:Type) (cA:Countable A).

Equational laws


 Lemma union_assoc (v₁ v₂ v₃:T A) : v₁∪(v₂v₃) = (v₁v₂)∪v₃.

 Lemma union_empty_left_unit (v:T A) : v = v.

 Lemma union_empty_right_unit (v:T A) : v = v.

 Lemma union_commutative (v₁ v₂:T A) : v₁v₂ = v₂v₁.

 Lemma union_idempotent (v:T A) : vv = v.

Note: intersection does not, in general, have a neutral element. Indeed, it would be a set with all the elements of the type, which is not necessarily finite.

Order-related laws


 Lemma empty_bottom : u, u.

 Lemma union_sup (v₁ v₂:T A) : u, v₁v₂ u v₁u v₂u.

 Lemma inter_inf (u₁ u₂:T A) : v, v u₁u₂ vu₁ vu₂.

End Algebraic.

Categorical combinators

Laws yet to prove: map id u = u and map (fg) u = map f (map g u).
Definition map {A B} {_:Countable A} {_:Countable B} (f:AB) (u:T A) : T B :=
   f x \ xu .

Laws yet to prove: join (singleton u) = u, join (map singleton u) = u and join (join u) = join (map join u).
Definition join {A} {_:Countable A} (u:T (T A)) : T A :=
   x \ xy yu .

Notations


Module Notations.
  Export MembershipNotation.
  Export SetNotations.
  Export ComprehensionNotations.
End Notations.