hashset.v 7.86 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
(* Copyright (c) 2012-2015, Robbert Krebbers. *)
2 3 4 5
(* This file is distributed under the terms of the BSD license. *)
(** This file implements finite set using hash maps. Hash sets are represented
using radix-2 search trees. Each hash bucket is thus indexed using an binary
integer of type [Z], and contains an unordered list without duplicates. *)
6 7
From stdpp Require Export fin_maps listset.
From stdpp Require Import zmap.
8 9 10 11 12 13 14 15 16 17

Record hashset {A} (hash : A  Z) := Hashset {
  hashset_car : Zmap (list A);
  hashset_prf :
    map_Forall (λ n l, Forall (λ x, hash x = n) l  NoDup l) hashset_car
}.
Arguments Hashset {_ _} _ _.
Arguments hashset_car {_ _} _.

Section hashset.
18
Context `{ x y : A, Decision (x = y)} (hash : A  Z).
19 20 21 22 23 24 25

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  _.
Next Obligation. by intros n X; simpl_map. Qed.
Program Instance hashset_singleton: Singleton A (hashset hash) := λ x,
26
  Hashset {[ hash x := [x] ]} _.
27
Next Obligation.
28
  intros x n l [<- <-]%lookup_singleton_Some.
29 30 31 32 33 34
  rewrite Forall_singleton; auto using NoDup_singleton.
Qed.
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.
35
  intros _ _ m1 Hm1 m2 Hm2 n l'; rewrite lookup_union_with_Some.
36
  intros [[??]|[[??]|(l&k&?&?&?)]]; simplify_eq/=; auto.
37 38 39 40 41 42 43 44
  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,
  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) _.
Next Obligation.
45
  intros _ _ m1 Hm1 m2 Hm2 n l'. rewrite lookup_intersection_with_Some.
46
  intros (?&?&?&?&?); simplify_option_eq.
47 48 49 50 51 52 53 54
  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,
  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) _.
Next Obligation.
55
  intros _ _ m1 Hm1 m2 Hm2 n l'. rewrite lookup_difference_with_Some.
56
  intros [[??]|(?&?&?&?&?)]; simplify_option_eq; auto.
57 58 59 60 61 62 63 64 65
  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,
  map_to_list (hashset_car m) = snd.

Global Instance: FinCollection A (hashset hash).
Proof.
  split; [split; [split| |]| |].
66
  - intros ? (?&?&?); simplify_map_eq/=.
67
  - unfold elem_of, hashset_elem_of, singleton, hashset_singleton; simpl.
68 69 70
    intros x y. setoid_rewrite lookup_singleton_Some. split.
    { by intros (?&[? <-]&?); decompose_elem_of_list. }
    intros ->; eexists [y]. by rewrite elem_of_list_singleton.
71
  - unfold elem_of, hashset_elem_of, union, hashset_union.
72 73
    intros [m1 Hm1] [m2 Hm2] x; simpl; setoid_rewrite lookup_union_with_Some.
    split.
74
    { intros (?&[[]|[[]|(l&k&?&?&?)]]&Hx); simplify_eq/=; eauto.
75 76 77 78 79 80
      rewrite elem_of_list_union in Hx; destruct Hx; eauto. }
    intros [(l&?&?)|(k&?&?)].
    + destruct (m2 !! hash x) as [k|]; eauto.
      exists (list_union l k). rewrite elem_of_list_union. naive_solver.
    + destruct (m1 !! hash x) as [l|]; eauto 6.
      exists (list_union l k). rewrite elem_of_list_union. naive_solver.
81
  - unfold elem_of, hashset_elem_of, intersection, hashset_intersection.
82 83
    intros [m1 ?] [m2 ?] x; simpl.
    setoid_rewrite lookup_intersection_with_Some. split.
84
    { intros (?&(l&k&?&?&?)&Hx); simplify_option_eq.
85 86 87
      rewrite elem_of_list_intersection in Hx; naive_solver. }
    intros [(l&?&?) (k&?&?)]. assert (x  list_intersection l k)
      by (by rewrite elem_of_list_intersection).
88
    exists (list_intersection l k); split; [exists l, k|]; split_and?; auto.
89
    by rewrite option_guard_True by eauto using elem_of_not_nil.
90
  - unfold elem_of, hashset_elem_of, intersection, hashset_intersection.
91 92
    intros [m1 ?] [m2 ?] x; simpl.
    setoid_rewrite lookup_difference_with_Some. split.
93
    { intros (l'&[[??]|(l&k&?&?&?)]&Hx); simplify_option_eq;
94 95 96 97
        rewrite ?elem_of_list_difference in Hx; naive_solver. }
    intros [(l&?&?) Hm2]; destruct (m2 !! hash x) as [k|] eqn:?; eauto.
    destruct (decide (x  k)); [destruct Hm2; eauto|].
    assert (x  list_difference l k) by (by rewrite elem_of_list_difference).
98
    exists (list_difference l k); split; [right; exists l,k|]; split_and?; auto.
99
    by rewrite option_guard_True by eauto using elem_of_not_nil.
100
  - unfold elem_of at 2, hashset_elem_of, elements, hashset_elems.
101 102 103 104 105
    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.
106
  - unfold elements, hashset_elems. intros [m Hm]; simpl.
107 108
    rewrite map_Forall_to_list in Hm. generalize (NoDup_fst_map_to_list m).
    induction Hm as [|[n l] m' [??]];
109
      csimpl; inversion_clear 1 as [|?? Hn]; [constructor|].
110
    apply NoDup_app; split_and?; eauto.
111 112 113 114 115 116 117 118 119
    setoid_rewrite elem_of_list_bind; intros x ? ([n' l']&?&?); simpl in *.
    assert (hash x = n  hash x = n') as [??]; subst.
    { split; [eapply (Forall_forall (λ x, hash x = n) l); eauto|].
      eapply (Forall_forall (λ x, hash x = n') l'); eauto.
      rewrite Forall_forall in Hm. eapply (Hm (_,_)); eauto. }
    destruct Hn; rewrite elem_of_list_fmap; exists (hash x, l'); eauto.
Qed.
End hashset.

120 121
Typeclasses Opaque hashset_elem_of.

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
(** 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.
138 139 140 141 142 143 144 145 146

Section remove_duplicates.
Context `{ x y : A, Decision (x = y)} (hash : A  Z).

Definition remove_dups_fast (l : list A) : list A :=
  match l with
  | [] => []
  | [x] => [x]
  | _ =>
Robbert Krebbers's avatar
Robbert Krebbers committed
147
     let n : Z := length l in
148
     elements (foldr (λ x, ({[ x ]} ))  l :
Robbert Krebbers's avatar
Robbert Krebbers committed
149
       hashset (λ x, hash x `mod` (2 * n))%Z)
150 151 152 153 154
  end.
Lemma elem_of_remove_dups_fast l x : x  remove_dups_fast l  x  l.
Proof.
  destruct l as [|x1 [|x2 l]]; try reflexivity.
  unfold remove_dups_fast; generalize (x1 :: x2 :: l); clear l; intros l.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
  generalize (λ x, hash x `mod` (2 * length l))%Z; intros f.
156
  rewrite elem_of_elements; split.
157
  - revert x. induction l as [|y l IH]; intros x; simpl.
158 159
    { by rewrite elem_of_empty. }
    rewrite elem_of_union, elem_of_singleton. intros [->|]; [left|right]; eauto.
160
  - induction 1; set_solver.
161 162 163 164 165 166 167 168 169 170 171 172 173
Qed.
Lemma NoDup_remove_dups_fast l : NoDup (remove_dups_fast l).
Proof.
  unfold remove_dups_fast; destruct l as [|x1 [|x2 l]].
  apply NoDup_nil_2. apply NoDup_singleton. apply NoDup_elements.
Qed.
Definition listset_normalize (X : listset A) : listset A :=
  let (l) := X in Listset (remove_dups_fast l).
Lemma listset_normalize_correct X : listset_normalize X  X.
Proof.
  destruct X as [l]. apply elem_of_equiv; intro; apply elem_of_remove_dups_fast.
Qed.
End remove_duplicates.