Commit 7d7c9871 authored by Robbert Krebbers's avatar Robbert Krebbers

Set Hint Mode for all classes in `base.v`.

This provides significant robustness against looping type class search.

As a consequence, at many places throughout the library we had to add
additional typing information to lemmas. This was to be expected, since
most of the old lemmas were ambiguous. For example:

  Section fin_collection.
    Context `{FinCollection A C}.

    size_singleton (x : A) : size {[ x ]} = 1.

In this case, the lemma does not tell us which `FinCollection` with
elements `A` we are talking about. So, `{[ x ]}` could not only refer to
the singleton operation of the `FinCollection A C` in the section, but
also to any other `FinCollection` in the development. To make this lemma
unambigious, it should be written as:

  Lemma size_singleton (x : A) : size ({[ x ]} : C) = 1.

In similar spirit, lemmas like the one below were also ambiguous:

  Lemma lookup_alter_None {A} (f : A → A) m i j :
    alter f i m !! j = None  m !! j = None.

It is not clear which finite map implementation we are talking about.
To make this lemma unambigious, it should be written as:

  Lemma lookup_alter_None {A} (f : A → A) (m : M A) i j :
    alter f i m !! j = None  m !! j = None.

That is, we have to specify the type of `m`.
parent 24aef2fe
Pipeline #4386 failed with stage
in 5 minutes and 53 seconds
This diff is collapsed.
......@@ -427,7 +427,7 @@ Proof.
rewrite !coPset_finite_spec; destruct X as [t Ht]; simpl; clear Ht.
induction t as [[]|]; simpl; rewrite ?coPset_finite_node, ?andb_True; tauto.
Qed.
Lemma coPset_split X :
Lemma coPset_split (X : coPset) :
¬set_finite X
X1 X2, X = X1 X2 X1 X2 = ¬set_finite X1 ¬set_finite X2.
Proof.
......
This diff is collapsed.
......@@ -63,14 +63,14 @@ Proof.
intros y; rewrite elem_of_elements, elem_of_union, elem_of_singleton.
by rewrite elem_of_cons, elem_of_elements.
Qed.
Lemma elements_singleton x : elements {[ x ]} = [x].
Lemma elements_singleton x : elements ({[ x ]} : C) = [x].
Proof.
apply Permutation_singleton. by rewrite <-(right_id () {[x]}),
elements_union_singleton, elements_empty by set_solver.
Qed.
Lemma elements_submseteq X Y : X Y elements X + elements Y.
Proof.
intros; apply NoDup_submseteq; auto using NoDup_elements.
intros; apply NoDup_submseteq; eauto using NoDup_elements.
intros x. rewrite !elem_of_elements; auto.
Qed.
......@@ -106,7 +106,7 @@ Proof.
contradict Hsz. rewrite HX, size_empty; lia.
Qed.
Lemma size_singleton (x : A) : size {[ x ]} = 1.
Lemma size_singleton (x : A) : size ({[ x ]} : C) = 1.
Proof. unfold size, collection_size. simpl. by rewrite elements_singleton. Qed.
Lemma size_singleton_inv X x y : size X = 1 x X y X x = y.
Proof.
......@@ -200,9 +200,9 @@ Proof.
{ destruct IH as (x' & Hx' & Hmin); [set_solver|].
destruct (decide (R x x')).
- exists x; split; [set_solver|].
eauto using union_minimal, singleton_minimal, minimal_weaken.
eauto using (union_minimal (C:=C)), (singleton_minimal (C:=C)), minimal_weaken.
- exists x'; split; [set_solver|].
auto using union_minimal, singleton_minimal_not_above. }
eauto using (union_minimal (C:=C)), (singleton_minimal_not_above (C:=C)). }
exists x; split; [set_solver|].
rewrite HX, (right_id _ ()). apply singleton_minimal.
Qed.
......
......@@ -61,7 +61,7 @@ Proof. rewrite (dom_insert _). set_solver. Qed.
Lemma dom_insert_subseteq_compat_l {A} (m : M A) i x X :
X dom D m X dom D (<[i:=x]>m).
Proof. intros. trans (dom D m); eauto using dom_insert_subseteq. Qed.
Lemma dom_singleton {A} (i : K) (x : A) : dom D {[i := x]} {[ i ]}.
Lemma dom_singleton {A} (i : K) (x : A) : dom D ({[i := x]} : M A) {[ i ]}.
Proof. rewrite <-insert_empty, dom_insert, dom_empty; set_solver. Qed.
Lemma dom_delete {A} (m : M A) i : dom D (delete i m) dom D m {[ i ]}.
Proof.
......@@ -100,7 +100,7 @@ Proof.
unfold is_Some. setoid_rewrite lookup_difference_Some.
destruct (m2 !! i); naive_solver.
Qed.
Lemma dom_fmap {A B} (f : A B) m : dom D (f <$> m) dom D m.
Lemma dom_fmap {A B} (f : A B) (m : M A) : dom D (f <$> m) dom D m.
Proof.
apply elem_of_equiv. intros i.
rewrite !elem_of_dom, lookup_fmap, <-!not_eq_None_Some.
......@@ -109,7 +109,8 @@ Qed.
Lemma dom_finite {A} (m : M A) : set_finite (dom D m).
Proof.
induction m using map_ind; rewrite ?dom_empty, ?dom_insert;
eauto using empty_finite, union_finite, singleton_finite.
eauto using (empty_finite (C:=D)), (union_finite (C:=D)),
(singleton_finite (C:=D)).
Qed.
Context `{!LeibnizEquiv D}.
......@@ -121,7 +122,7 @@ Lemma dom_alter_L {A} f (m : M A) i : dom D (alter f i m) = dom D m.
Proof. unfold_leibniz; apply dom_alter. Qed.
Lemma dom_insert_L {A} (m : M A) i x : dom D (<[i:=x]>m) = {[ i ]} dom D m.
Proof. unfold_leibniz; apply dom_insert. Qed.
Lemma dom_singleton_L {A} (i : K) (x : A) : dom D {[i := x]} = {[ i ]}.
Lemma dom_singleton_L {A} (i : K) (x : A) : dom D ({[i := x]} : M A) = {[ i ]}.
Proof. unfold_leibniz; apply dom_singleton. Qed.
Lemma dom_delete_L {A} (m : M A) i : dom D (delete i m) = dom D m {[ i ]}.
Proof. unfold_leibniz; apply dom_delete. Qed.
......@@ -132,6 +133,6 @@ Lemma dom_intersection_L {A} (m1 m2 : M A) :
Proof. unfold_leibniz; apply dom_intersection. Qed.
Lemma dom_difference_L {A} (m1 m2 : M A) : dom D (m1 m2) = dom D m1 dom D m2.
Proof. unfold_leibniz; apply dom_difference. Qed.
Lemma dom_fmap_L {A B} (f : A B) m : dom D (f <$> m) = dom D m.
Lemma dom_fmap_L {A B} (f : A B) (m : M A) : dom D (f <$> m) = dom D m.
Proof. unfold_leibniz; apply dom_fmap. Qed.
End fin_map_dom.
This diff is collapsed.
......@@ -136,8 +136,10 @@ Definition gmap_uncurry `{Countable K1, Countable K2} {A} :
Section curry_uncurry.
Context `{Countable K1, Countable K2} {A : Type}.
(* FIXME: the type annotations `option (gmap K2 A)` are silly. Maybe these are
a consequence of Coq bug #5735 *)
Lemma lookup_gmap_curry (m : gmap K1 (gmap K2 A)) i j :
gmap_curry m !! (i,j) = m !! i = (!! j).
gmap_curry m !! (i,j) = (m !! i : option (gmap K2 A)) = (!! j).
Proof.
apply (map_fold_ind (λ mr m, mr !! (i,j) = m !! i = (!! j))).
{ by rewrite !lookup_empty. }
......@@ -154,7 +156,7 @@ Section curry_uncurry.
Qed.
Lemma lookup_gmap_uncurry (m : gmap (K1 * K2) A) i j :
gmap_uncurry m !! i = (!! j) = m !! (i, j).
(gmap_uncurry m !! i : option (gmap K2 A)) = (!! j) = m !! (i, j).
Proof.
apply (map_fold_ind (λ mr m, mr !! i = (!! j) = m !! (i, j))).
{ by rewrite !lookup_empty. }
......@@ -229,7 +231,7 @@ Qed.
(* This is pretty ad-hoc and just for the case of [gset positive]. We need a
notion of countable non-finite types to generalize this. *)
Instance gset_positive_fresh : Fresh positive (gset positive) := λ X,
let 'Mapset (GMap m _) := X in fresh (dom _ m).
let 'Mapset (GMap m _) := X in fresh (dom Pset m).
Instance gset_positive_fresh_spec : FreshSpec positive (gset positive).
Proof.
split.
......
......@@ -25,27 +25,27 @@ Section definitions.
Definition multiplicity (x : A) (X : gmultiset A) : nat :=
match gmultiset_car X !! x with Some n => S n | None => 0 end.
Instance gmultiset_elem_of : ElemOf A (gmultiset A) := λ x X,
Global Instance gmultiset_elem_of : ElemOf A (gmultiset A) := λ x X,
0 < multiplicity x X.
Instance gmultiset_subseteq : SubsetEq (gmultiset A) := λ X Y, x,
Global Instance gmultiset_subseteq : SubsetEq (gmultiset A) := λ X Y, x,
multiplicity x X multiplicity x Y.
Instance gmultiset_elements : Elements A (gmultiset A) := λ X,
Global Instance gmultiset_elements : Elements A (gmultiset A) := λ X,
let (X) := X in '(x,n) map_to_list X; replicate (S n) x.
Instance gmultiset_size : Size (gmultiset A) := length elements.
Global Instance gmultiset_size : Size (gmultiset A) := length elements.
Instance gmultiset_empty : Empty (gmultiset A) := GMultiSet .
Instance gmultiset_singleton : Singleton A (gmultiset A) := λ x,
Global Instance gmultiset_empty : Empty (gmultiset A) := GMultiSet .
Global Instance gmultiset_singleton : Singleton A (gmultiset A) := λ x,
GMultiSet {[ x := 0 ]}.
Instance gmultiset_union : Union (gmultiset A) := λ X Y,
Global Instance gmultiset_union : Union (gmultiset A) := λ X Y,
let (X) := X in let (Y) := Y in
GMultiSet $ union_with (λ x y, Some (S (x + y))) X Y.
Instance gmultiset_difference : Difference (gmultiset A) := λ X Y,
Global Instance gmultiset_difference : Difference (gmultiset A) := λ X Y,
let (X) := X in let (Y) := Y in
GMultiSet $ difference_with (λ x y,
let z := x - y in guard (0 < z); Some (pred z)) X Y.
Instance gmultiset_dom : Dom (gmultiset A) (gset A) := λ X,
Global Instance gmultiset_dom : Dom (gmultiset A) (gset A) := λ X,
let (X) := X in dom _ X.
End definitions.
......@@ -54,27 +54,6 @@ Typeclasses Opaque gmultiset_elements gmultiset_size gmultiset_empty.
Typeclasses Opaque gmultiset_singleton gmultiset_union gmultiset_difference.
Typeclasses Opaque gmultiset_dom.
(** These instances are declared using [Hint Extern] to avoid too
eager type class search. *)
Hint Extern 1 (ElemOf _ (gmultiset _)) =>
eapply @gmultiset_elem_of : typeclass_instances.
Hint Extern 1 (SubsetEq (gmultiset _)) =>
eapply @gmultiset_subseteq : typeclass_instances.
Hint Extern 1 (Empty (gmultiset _)) =>
eapply @gmultiset_empty : typeclass_instances.
Hint Extern 1 (Singleton _ (gmultiset _)) =>
eapply @gmultiset_singleton : typeclass_instances.
Hint Extern 1 (Union (gmultiset _)) =>
eapply @gmultiset_union : typeclass_instances.
Hint Extern 1 (Difference (gmultiset _)) =>
eapply @gmultiset_difference : typeclass_instances.
Hint Extern 1 (Elements _ (gmultiset _)) =>
eapply @gmultiset_elements : typeclass_instances.
Hint Extern 1 (Size (gmultiset _)) =>
eapply @gmultiset_size : typeclass_instances.
Hint Extern 1 (Dom (gmultiset _) _) =>
eapply @gmultiset_dom : typeclass_instances.
Section lemmas.
Context `{Countable A}.
Implicit Types x y : A.
......
......@@ -18,18 +18,18 @@ Arguments hashset_car {_ _} _ : assert.
Section hashset.
Context `{EqDecision A} (hash : A Z).
Instance hashset_elem_of: ElemOf A (hashset hash) := λ x m, l,
Global Instance hashset_elem_of: ElemOf A (hashset hash) := λ x m, l,
hashset_car m !! hash x = Some l x l.
Program Instance hashset_empty: Empty (hashset hash) := Hashset _.
Global Program Instance hashset_empty: Empty (hashset hash) := Hashset _.
Next Obligation. by intros n X; simpl_map. Qed.
Program Instance hashset_singleton: Singleton A (hashset hash) := λ x,
Global Program Instance hashset_singleton: Singleton A (hashset hash) := λ x,
Hashset {[ hash x := [x] ]} _.
Next Obligation.
intros x n l [<- <-]%lookup_singleton_Some.
rewrite Forall_singleton; auto using NoDup_singleton.
Qed.
Program Instance hashset_union: Union (hashset hash) := λ m1 m2,
Global Program Instance hashset_union: Union (hashset hash) := λ m1 m2,
let (m1,Hm1) := m1 in let (m2,Hm2) := m2 in
Hashset (union_with (λ l k, Some (list_union l k)) m1 m2) _.
Next Obligation.
......@@ -38,7 +38,7 @@ Next Obligation.
split; [apply Forall_list_union|apply NoDup_list_union];
first [by eapply Hm1; eauto | by eapply Hm2; eauto].
Qed.
Program Instance hashset_intersection: Intersection (hashset hash) := λ m1 m2,
Global Program Instance hashset_intersection: Intersection (hashset hash) := λ m1 m2,
let (m1,Hm1) := m1 in let (m2,Hm2) := m2 in
Hashset (intersection_with (λ l k,
let l' := list_intersection l k in guard (l' []); Some l') m1 m2) _.
......@@ -48,7 +48,7 @@ Next Obligation.
split; [apply Forall_list_intersection|apply NoDup_list_intersection];
first [by eapply Hm1; eauto | by eapply Hm2; eauto].
Qed.
Program Instance hashset_difference: Difference (hashset hash) := λ m1 m2,
Global Program Instance hashset_difference: Difference (hashset hash) := λ m1 m2,
let (m1,Hm1) := m1 in let (m2,Hm2) := m2 in
Hashset (difference_with (λ l k,
let l' := list_difference l k in guard (l' []); Some l') m1 m2) _.
......@@ -58,10 +58,10 @@ Next Obligation.
split; [apply Forall_list_difference|apply NoDup_list_difference];
first [by eapply Hm1; eauto | by eapply Hm2; eauto].
Qed.
Instance hashset_elems: Elements A (hashset hash) := λ m,
Global Instance hashset_elements: Elements A (hashset hash) := λ m,
map_to_list (hashset_car m) = snd.
Global Instance: FinCollection A (hashset hash).
Global Instance hashset_fin_collection : FinCollection A (hashset hash).
Proof.
split; [split; [split| |]| |].
- intros ? (?&?&?); simplify_map_eq/=.
......@@ -98,13 +98,13 @@ Proof.
assert (x list_difference l k) by (by rewrite elem_of_list_difference).
exists (list_difference l k); split; [right; exists l,k|]; split_and?; auto.
by rewrite option_guard_True by eauto using elem_of_not_nil.
- unfold elem_of at 2, hashset_elem_of, elements, hashset_elems.
- unfold elem_of at 2, hashset_elem_of, elements, hashset_elements.
intros [m Hm] x; simpl. setoid_rewrite elem_of_list_bind. split.
{ intros ([n l]&Hx&Hn); simpl in *; rewrite elem_of_map_to_list in Hn.
cut (hash x = n); [intros <-; eauto|].
eapply (Forall_forall (λ x, hash x = n) l); eauto. eapply Hm; eauto. }
intros (l&?&?). exists (hash x, l); simpl. by rewrite elem_of_map_to_list.
- unfold elements, hashset_elems. intros [m Hm]; simpl.
- unfold elements, hashset_elements. intros [m Hm]; simpl.
rewrite map_Forall_to_list in Hm. generalize (NoDup_fst_map_to_list m).
induction Hm as [|[n l] m' [??]];
csimpl; inversion_clear 1 as [|?? Hn]; [constructor|].
......@@ -120,23 +120,6 @@ End hashset.
Typeclasses Opaque hashset_elem_of.
(** These instances are declared using [Hint Extern] to avoid too
eager type class search. *)
Hint Extern 1 (ElemOf _ (hashset _)) =>
eapply @hashset_elem_of : typeclass_instances.
Hint Extern 1 (Empty (hashset _)) =>
eapply @hashset_empty : typeclass_instances.
Hint Extern 1 (Singleton _ (hashset _)) =>
eapply @hashset_singleton : typeclass_instances.
Hint Extern 1 (Union (hashset _)) =>
eapply @hashset_union : typeclass_instances.
Hint Extern 1 (Intersection (hashset _)) =>
eapply @hashset_intersection : typeclass_instances.
Hint Extern 1 (Difference (hashset _)) =>
eapply @hashset_difference : typeclass_instances.
Hint Extern 1 (Elements _ (hashset _)) =>
eapply @hashset_elems : typeclass_instances.
Section remove_duplicates.
Context `{EqDecision A} (hash : A Z).
......
......@@ -311,7 +311,7 @@ Instance list_subseteq {A} : SubsetEq (list A) := λ l1 l2, ∀ x, x ∈ l1 →
Section list_set.
Context `{dec : EqDecision A}.
Global Instance elem_of_list_dec (x : A) : l, Decision (x l).
Global Instance elem_of_list_dec (x : A) : l : list A, Decision (x l).
Proof.
refine (
fix go l :=
......@@ -2857,6 +2857,7 @@ Proof. induction l; f_equal/=; auto. Qed.
Section fmap.
Context {A B : Type} (f : A B).
Implicit Types l : list A.
Lemma list_fmap_compose {C} (g : B C) l : g f <$> l = g <$> f <$> l.
Proof. induction l; f_equal/=; auto. Qed.
......@@ -2975,24 +2976,24 @@ Section fmap.
Lemma Exists_fmap (P : B Prop) l : Exists P (f <$> l) Exists (P f) l.
Proof. split; induction l; inversion 1; constructor; by auto. Qed.
Lemma Forall2_fmap_l {C} (P : B C Prop) l1 l2 :
Forall2 P (f <$> l1) l2 Forall2 (P f) l1 l2.
Lemma Forall2_fmap_l {C} (P : B C Prop) l k :
Forall2 P (f <$> l) k Forall2 (P f) l k.
Proof.
split; revert l2; induction l1; inversion_clear 1; constructor; auto.
split; revert k; induction l; inversion_clear 1; constructor; auto.
Qed.
Lemma Forall2_fmap_r {C} (P : C B Prop) l1 l2 :
Forall2 P l1 (f <$> l2) Forall2 (λ x, P x f) l1 l2.
Lemma Forall2_fmap_r {C} (P : C B Prop) k l :
Forall2 P k (f <$> l) Forall2 (λ x, P x f) k l.
Proof.
split; revert l1; induction l2; inversion_clear 1; constructor; auto.
split; revert k; induction l; inversion_clear 1; constructor; auto.
Qed.
Lemma Forall2_fmap_1 {C D} (g : C D) (P : B D Prop) l1 l2 :
Forall2 P (f <$> l1) (g <$> l2) Forall2 (λ x1 x2, P (f x1) (g x2)) l1 l2.
Proof. revert l2; induction l1; intros [|??]; inversion_clear 1; auto. Qed.
Lemma Forall2_fmap_2 {C D} (g : C D) (P : B D Prop) l1 l2 :
Forall2 (λ x1 x2, P (f x1) (g x2)) l1 l2 Forall2 P (f <$> l1) (g <$> l2).
Lemma Forall2_fmap_1 {C D} (g : C D) (P : B D Prop) l k :
Forall2 P (f <$> l) (g <$> k) Forall2 (λ x1 x2, P (f x1) (g x2)) l k.
Proof. revert k; induction l; intros [|??]; inversion_clear 1; auto. Qed.
Lemma Forall2_fmap_2 {C D} (g : C D) (P : B D Prop) l k :
Forall2 (λ x1 x2, P (f x1) (g x2)) l k Forall2 P (f <$> l) (g <$> k).
Proof. induction 1; csimpl; auto. Qed.
Lemma Forall2_fmap {C D} (g : C D) (P : B D Prop) l1 l2 :
Forall2 P (f <$> l1) (g <$> l2) Forall2 (λ x1 x2, P (f x1) (g x2)) l1 l2.
Lemma Forall2_fmap {C D} (g : C D) (P : B D Prop) l k :
Forall2 P (f <$> l) (g <$> k) Forall2 (λ x1 x2, P (f x1) (g x2)) l k.
Proof. split; auto using Forall2_fmap_1, Forall2_fmap_2. Qed.
Lemma list_fmap_bind {C} (g : B list C) l : (f <$> l) = g = l = g f.
......@@ -3081,7 +3082,7 @@ Section ret_join.
Lemma elem_of_list_ret (x y : A) : x @mret list _ A y x = y.
Proof. apply elem_of_list_singleton. Qed.
Lemma elem_of_list_join (x : A) (ls : list (list A)) :
x mjoin ls l, x l l ls.
x mjoin ls l : list A, x l l ls.
Proof. by rewrite list_join_bind, elem_of_list_bind. Qed.
Lemma join_nil (ls : list (list A)) : mjoin ls = [] Forall (= []) ls.
Proof.
......
......@@ -12,14 +12,14 @@ Arguments Listset {_} _ : assert.
Section listset.
Context {A : Type}.
Instance listset_elem_of: ElemOf A (listset A) := λ x l, x listset_car l.
Instance listset_empty: Empty (listset A) := Listset [].
Instance listset_singleton: Singleton A (listset A) := λ x, Listset [x].
Instance listset_union: Union (listset A) := λ l k,
Global Instance listset_elem_of: ElemOf A (listset A) := λ x l, x listset_car l.
Global Instance listset_empty: Empty (listset A) := Listset [].
Global Instance listset_singleton: Singleton A (listset A) := λ x, Listset [x].
Global Instance listset_union: Union (listset A) := λ l k,
let (l') := l in let (k') := k in Listset (l' ++ k').
Global Opaque listset_singleton listset_empty.
Global Instance: SimpleCollection A (listset A).
Global Instance listset_simple_collection : SimpleCollection A (listset A).
Proof.
split.
- by apply not_elem_of_nil.
......@@ -40,20 +40,21 @@ Defined.
Context `{!EqDecision A}.
Instance listset_intersection: Intersection (listset A) := λ l k,
Global Instance listset_intersection: Intersection (listset A) := λ l k,
let (l') := l in let (k') := k in Listset (list_intersection l' k').
Instance listset_difference: Difference (listset A) := λ l k,
Global Instance listset_difference: Difference (listset A) := λ l k,
let (l') := l in let (k') := k in Listset (list_difference l' k').
Instance: Collection A (listset A).
Instance listset_collection: Collection A (listset A).
Proof.
split.
- apply _.
- intros [?] [?]. apply elem_of_list_intersection.
- intros [?] [?]. apply elem_of_list_difference.
Qed.
Instance listset_elems: Elements A (listset A) := remove_dups listset_car.
Global Instance: FinCollection A (listset A).
Global Instance listset_elements: Elements A (listset A) :=
remove_dups listset_car.
Global Instance listset_fin_collection : FinCollection A (listset A).
Proof.
split.
- apply _.
......@@ -62,23 +63,6 @@ Proof.
Qed.
End listset.
(** These instances are declared using [Hint Extern] to avoid too
eager type class search. *)
Hint Extern 1 (ElemOf _ (listset _)) =>
eapply @listset_elem_of : typeclass_instances.
Hint Extern 1 (Empty (listset _)) =>
eapply @listset_empty : typeclass_instances.
Hint Extern 1 (Singleton _ (listset _)) =>
eapply @listset_singleton : typeclass_instances.
Hint Extern 1 (Union (listset _)) =>
eapply @listset_union : typeclass_instances.
Hint Extern 1 (Intersection (listset _)) =>
eapply @listset_intersection : typeclass_instances.
Hint Extern 1 (Difference (listset _)) =>
eapply @listset_difference : typeclass_instances.
Hint Extern 1 (Elements _ (listset _)) =>
eapply @listset_elems : typeclass_instances.
Instance listset_ret: MRet listset := λ A x, {[ x ]}.
Instance listset_fmap: FMap listset := λ A B f l,
let (l') := l in Listset (f <$> l').
......@@ -86,7 +70,7 @@ Instance listset_bind: MBind listset := λ A B f l,
let (l') := l in Listset (mbind (listset_car f) l').
Instance listset_join: MJoin listset := λ A, mbind id.
Instance: CollectionMonad listset.
Instance listset_collection_monad : CollectionMonad listset.
Proof.
split.
- intros. apply _.
......
......@@ -14,18 +14,18 @@ Arguments mapset_car {_} _ : assert.
Section mapset.
Context `{FinMap K M}.
Instance mapset_elem_of: ElemOf K (mapset M) := λ x X,
Global Instance mapset_elem_of: ElemOf K (mapset M) := λ x X,
mapset_car X !! x = Some ().
Instance mapset_empty: Empty (mapset M) := Mapset .
Instance mapset_singleton: Singleton K (mapset M) := λ x,
Global Instance mapset_empty: Empty (mapset M) := Mapset .
Global Instance mapset_singleton: Singleton K (mapset M) := λ x,
Mapset {[ x := () ]}.
Instance mapset_union: Union (mapset M) := λ X1 X2,
Global Instance mapset_union: Union (mapset M) := λ X1 X2,
let (m1) := X1 in let (m2) := X2 in Mapset (m1 m2).
Instance mapset_intersection: Intersection (mapset M) := λ X1 X2,
Global Instance mapset_intersection: Intersection (mapset M) := λ X1 X2,
let (m1) := X1 in let (m2) := X2 in Mapset (m1 m2).
Instance mapset_difference: Difference (mapset M) := λ X1 X2,
Global Instance mapset_difference: Difference (mapset M) := λ X1 X2,
let (m1) := X1 in let (m2) := X2 in Mapset (m1 m2).
Instance mapset_elems: Elements K (mapset M) := λ X,
Global Instance mapset_elements: Elements K (mapset M) := λ X,
let (m) := X in (map_to_list m).*1.
Lemma mapset_eq (X1 X2 : mapset M) : X1 = X2 x, x X1 x X2.
......@@ -35,7 +35,7 @@ Proof.
f_equal. apply map_eq. intros i. apply option_eq. intros []. by apply E.
Qed.
Instance: Collection K (mapset M).
Instance mapset_collection: Collection K (mapset M).
Proof.
split; [split | | ].
- unfold empty, elem_of, mapset_empty, mapset_elem_of.
......@@ -54,17 +54,17 @@ Proof.
intros [m1] [m2] ?. simpl. rewrite lookup_difference_Some.
destruct (m2 !! x) as [[]|]; intuition congruence.
Qed.
Global Instance: LeibnizEquiv (mapset M).
Global Instance mapset_leibniz : LeibnizEquiv (mapset M).
Proof. intros ??. apply mapset_eq. Qed.
Global Instance: FinCollection K (mapset M).
Global Instance mapset_fin_collection : FinCollection K (mapset M).
Proof.
split.
- apply _.
- unfold elements, elem_of at 2, mapset_elems, mapset_elem_of.
- unfold elements, elem_of at 2, mapset_elements, mapset_elem_of.
intros [m] x. simpl. rewrite elem_of_list_fmap. split.
+ intros ([y []] &?& Hy). subst. by rewrite <-elem_of_map_to_list.
+ intros. exists (x, ()). by rewrite elem_of_map_to_list.
- unfold elements, mapset_elems. intros [m]. simpl.
- unfold elements, mapset_elements. intros [m]. simpl.
apply NoDup_fst_map_to_list.
Qed.
......@@ -127,21 +127,4 @@ Proof.
Qed.
End mapset.
(** These instances are declared using [Hint Extern] to avoid too
eager type class search. *)
Hint Extern 1 (ElemOf _ (mapset _)) =>
eapply @mapset_elem_of : typeclass_instances.
Hint Extern 1 (Empty (mapset _)) =>
eapply @mapset_empty : typeclass_instances.
Hint Extern 1 (Singleton _ (mapset _)) =>
eapply @mapset_singleton : typeclass_instances.
Hint Extern 1 (Union (mapset _)) =>
eapply @mapset_union : typeclass_instances.
Hint Extern 1 (Intersection (mapset _)) =>
eapply @mapset_intersection : typeclass_instances.
Hint Extern 1 (Difference (mapset _)) =>
eapply @mapset_difference : typeclass_instances.
Hint Extern 1 (Elements _ (mapset _)) =>
eapply @mapset_elems : typeclass_instances.
Arguments mapset_eq_dec : simpl never.
......@@ -207,7 +207,7 @@ Proof. by destruct mx. Qed.
Lemma option_fmap_ext {A B} (f g : A B) mx :
( x, f x = g x) f <$> mx = g <$> mx.
Proof. intros; destruct mx; f_equal/=; auto. Qed.
Lemma option_fmap_equiv_ext `{Equiv A, Equiv B} (f g : A B) mx :
Lemma option_fmap_equiv_ext `{Equiv A, Equiv B} (f g : A B) (mx : option A) :
( x, f x g x) f <$> mx g <$> mx.
Proof. destruct mx; constructor; auto. Qed.
Lemma option_fmap_bind {A B C} (f : A B) (g : B option C) mx :
......@@ -306,13 +306,15 @@ Section union_intersection_difference.
Proof. by intros [?|]. Qed.
Global Instance union_with_right_id : RightId (=) None (union_with f).
Proof. by intros [?|]. Qed.
Global Instance union_with_comm : Comm (=) f Comm (=) (union_with f).
Global Instance union_with_comm :
Comm (=) f Comm (=) (union_with (M:=option A) f).
Proof. by intros ? [?|] [?|]; compute; rewrite 1?(comm f). Qed.
Global Instance intersection_with_left_ab : LeftAbsorb (=) None (intersection_with f).
Proof. by intros [?|]. Qed.
Global Instance intersection_with_right_ab : RightAbsorb (=) None (intersection_with f).
Proof. by intros [?|]. Qed.
Global Instance difference_with_comm : Comm (=) f Comm (=) (intersection_with f).
Global Instance difference_with_comm :
Comm (=) f Comm (=) (intersection_with (M:=option A) f).
Proof. by intros ? [?|] [?|]; compute; rewrite 1?(comm f). Qed.
Global Instance difference_with_right_id : RightId (=) None (difference_with f).
Proof. by intros [?|]. Qed.
......
......@@ -24,7 +24,7 @@ Instance set_difference {A} : Difference (set A) := λ X1 X2,
Instance set_collection : Collection A (set A).
Proof. split; [split | |]; by repeat intro. Qed.
Lemma elem_of_top {A} (x : A) : x True.
Lemma elem_of_top {A} (x : A) : x ( : set A) True.
Proof. done. Qed.
Lemma elem_of_mkSet {A} (P : A Prop) x : x {[ x | P x ]} P x.
Proof. done. Qed.
......@@ -40,7 +40,7 @@ Instance set_bind : MBind set := λ A B (f : A → set B) (X : set A),
Instance set_fmap : FMap set := λ A B (f : A B) (X : set A),
{[ b | a, b = f a a X ]}.
Instance set_join : MJoin set := λ A (XX : set (set A)),
{[ a | X, a X X XX ]}.
{[ a | X : set A, a X X XX ]}.
Instance set_collection_monad : CollectionMonad set.
Proof. by split; try apply _. Qed.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment