fin_map_dom.v 6.43 KB
Newer Older
1
(* Copyright (c) 2012-2019, Coq-std++ developers. *)
2 3 4 5
(* This file is distributed under the terms of the BSD license. *)
(** This file provides an axiomatization of the domain function of finite
maps. We provide such an axiomatization, instead of implementing the domain
function in a generic way, to allow more efficient implementations. *)
6
From stdpp Require Export collections fin_maps.
7
Set Default Proof Using "Type*".
8

9
Class FinMapDom K M D `{ A, Dom (M A) D, FMap M,
10
     A, Lookup K A (M A),  A, Empty (M A),  A, PartialAlter K A (M A),
11
    OMap M, Merge M,  A, FinMapToList K A (M A), EqDecision K,
12
    ElemOf K D, Empty D, Singleton K D,
13
    Union D, Intersection D, Difference D} := {
14 15 16 17 18
  finmap_dom_map :>> FinMap K M;
  finmap_dom_collection :>> Collection K D;
  elem_of_dom {A} (m : M A) i : i  dom D m  is_Some (m !! i)
}.

19
Section fin_map_dom.
20 21
Context `{FinMapDom K M D}.

22 23 24 25 26 27 28
Lemma dom_map_filter {A} (P : K * A  Prop) `{! x, Decision (P x)} (m : M A):
  dom D (filter P m)  dom D m.
Proof.
  intros ?. rewrite 2!elem_of_dom.
  destruct 1 as [?[Eq _]%map_filter_lookup_Some]. by eexists.
Qed.

29 30
Lemma elem_of_dom_2 {A} (m : M A) i x : m !! i = Some x  i  dom D m.
Proof. rewrite elem_of_dom; eauto. Qed.
31
Lemma not_elem_of_dom {A} (m : M A) i : i  dom D m  m !! i = None.
32
Proof. by rewrite elem_of_dom, eq_None_not_Some. Qed.
33
Lemma subseteq_dom {A} (m1 m2 : M A) : m1  m2  dom D m1  dom D m2.
34
Proof.
35 36
  rewrite map_subseteq_spec.
  intros ??. rewrite !elem_of_dom. inversion 1; eauto.
37
Qed.
38
Lemma subset_dom {A} (m1 m2 : M A) : m1  m2  dom D m1  dom D m2.
39
Proof.
40 41 42
  intros [Hss1 Hss2]; split; [by apply subseteq_dom |].
  contradict Hss2. rewrite map_subseteq_spec. intros i x Hi.
  specialize (Hss2 i). rewrite !elem_of_dom in Hss2.
43
  destruct Hss2; eauto. by simplify_map_eq.
44 45 46
Qed.
Lemma dom_empty {A} : dom D (@empty (M A) _)  .
Proof.
47
  intros x. rewrite elem_of_dom, lookup_empty, <-not_eq_None_Some. set_solver.
48
Qed.
49
Lemma dom_empty_inv {A} (m : M A) : dom D m    m = .
50 51
Proof.
  intros E. apply map_empty. intros. apply not_elem_of_dom.
52
  rewrite E. set_solver.
53
Qed.
54 55 56
Lemma dom_alter {A} f (m : M A) i : dom D (alter f i m)  dom D m.
Proof.
  apply elem_of_equiv; intros j; rewrite !elem_of_dom; unfold is_Some.
57
  destruct (decide (i = j)); simplify_map_eq/=; eauto.
58 59
  destruct (m !! j); naive_solver.
Qed.
60
Lemma dom_insert {A} (m : M A) i x : dom D (<[i:=x]>m)  {[ i ]}  dom D m.
61
Proof.
62 63
  apply elem_of_equiv. intros j. rewrite elem_of_union, !elem_of_dom.
  unfold is_Some. setoid_rewrite lookup_insert_Some.
64
  destruct (decide (i = j)); set_solver.
65
Qed.
66
Lemma dom_insert_subseteq {A} (m : M A) i x : dom D m  dom D (<[i:=x]>m).
67
Proof. rewrite (dom_insert _). set_solver. Qed.
68
Lemma dom_insert_subseteq_compat_l {A} (m : M A) i x X :
69
  X  dom D m  X  dom D (<[i:=x]>m).
70
Proof. intros. trans (dom D m); eauto using dom_insert_subseteq. Qed.
71
Lemma dom_singleton {A} (i : K) (x : A) : dom D ({[i := x]} : M A)  {[ i ]}.
72
Proof. rewrite <-insert_empty, dom_insert, dom_empty; set_solver. Qed.
73
Lemma dom_delete {A} (m : M A) i : dom D (delete i m)  dom D m  {[ i ]}.
74
Proof.
75
  apply elem_of_equiv. intros j. rewrite elem_of_difference, !elem_of_dom.
76
  unfold is_Some. setoid_rewrite lookup_delete_Some. set_solver.
77 78 79 80 81 82 83
Qed.
Lemma delete_partial_alter_dom {A} (m : M A) i f :
  i  dom D m  delete i (partial_alter f i m) = m.
Proof. rewrite not_elem_of_dom. apply delete_partial_alter. Qed.
Lemma delete_insert_dom {A} (m : M A) i x :
  i  dom D m  delete i (<[i:=x]>m) = m.
Proof. rewrite not_elem_of_dom. apply delete_insert. Qed.
84
Lemma map_disjoint_dom {A} (m1 m2 : M A) : m1 ## m2  dom D m1 ## dom D m2.
85
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
86
  rewrite map_disjoint_spec, elem_of_disjoint.
87
  setoid_rewrite elem_of_dom. unfold is_Some. naive_solver.
88
Qed.
89
Lemma map_disjoint_dom_1 {A} (m1 m2 : M A) : m1 ## m2  dom D m1 ## dom D m2.
90
Proof. apply map_disjoint_dom. Qed.
91
Lemma map_disjoint_dom_2 {A} (m1 m2 : M A) : dom D m1 ## dom D m2  m1 ## m2.
92
Proof. apply map_disjoint_dom. Qed.
93
Lemma dom_union {A} (m1 m2 : M A) : dom D (m1  m2)  dom D m1  dom D m2.
94
Proof.
95 96 97
  apply elem_of_equiv. intros i. rewrite elem_of_union, !elem_of_dom.
  unfold is_Some. setoid_rewrite lookup_union_Some_raw.
  destruct (m1 !! i); naive_solver.
98
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
99
Lemma dom_intersection {A} (m1 m2: M A) : dom D (m1  m2)  dom D m1  dom D m2.
100
Proof.
101 102
  apply elem_of_equiv. intros i. rewrite elem_of_intersection, !elem_of_dom.
  unfold is_Some. setoid_rewrite lookup_intersection_Some. naive_solver.
103
Qed.
104
Lemma dom_difference {A} (m1 m2 : M A) : dom D (m1  m2)  dom D m1  dom D m2.
105
Proof.
106 107 108
  apply elem_of_equiv. intros i. rewrite elem_of_difference, !elem_of_dom.
  unfold is_Some. setoid_rewrite lookup_difference_Some.
  destruct (m2 !! i); naive_solver.
109
Qed.
110
Lemma dom_fmap {A B} (f : A  B) (m : M A) : dom D (f <$> m)  dom D m.
111 112 113 114 115
Proof.
  apply elem_of_equiv. intros i.
  rewrite !elem_of_dom, lookup_fmap, <-!not_eq_None_Some.
  destruct (m !! i); naive_solver.
Qed.
116 117 118
Lemma dom_finite {A} (m : M A) : set_finite (dom D m).
Proof.
  induction m using map_ind; rewrite ?dom_empty, ?dom_insert;
119 120
    eauto using (empty_finite (C:=D)), (union_finite (C:=D)),
    (singleton_finite (C:=D)).
121
Qed.
122 123 124 125 126 127 128 129 130 131

Context `{!LeibnizEquiv D}.
Lemma dom_empty_L {A} : dom D (@empty (M A) _) = .
Proof. unfold_leibniz; apply dom_empty. Qed.
Lemma dom_empty_inv_L {A} (m : M A) : dom D m =   m = .
Proof. by intros; apply dom_empty_inv; unfold_leibniz. Qed.
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.
132
Lemma dom_singleton_L {A} (i : K) (x : A) : dom D ({[i := x]} : M A) = {[ i ]}.
133 134 135 136 137 138 139 140 141 142
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.
Lemma dom_union_L {A} (m1 m2 : M A) : dom D (m1  m2) = dom D m1  dom D m2.
Proof. unfold_leibniz; apply dom_union. Qed.
Lemma dom_intersection_L {A} (m1 m2 : M A) :
  dom D (m1  m2) = dom D m1  dom D m2.
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.
143
Lemma dom_fmap_L {A B} (f : A  B) (m : M A) : dom D (f <$> m) = dom D m.
144
Proof. unfold_leibniz; apply dom_fmap. Qed.
145
End fin_map_dom.