-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.v
156 lines (126 loc) · 4.29 KB
/
Map.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
From Coq Require Import Strings.String.
Local Definition map' (A : Type) := string -> A. (* total map *)
Local Definition empty' {A : Type} v : map' A := (fun _ => v).
Local Definition update' {A : Type} (m : map' A) k v :=
fun k' => if string_dec k k' then v else m k'.
Definition map (A : Type) := map' (option A). (* partial map *)
Definition empty {A : Type} : map A := empty' None.
Definition update {A : Type} (m : map A) k v :=
update' m k (Some v).
Definition lookup {A : Type} (m : map A) k := m k.
Notation "m '[' k '<==' v ']'" := (update m k v)
(at level 9, k at next level, v at next level).
(* ------------------------------------------------------------------------- *)
(* proofs *)
(* ------------------------------------------------------------------------- *)
Lemma lookup_update_eq : forall {A} (m : map A) k v,
m[k <== v] k = Some v.
Proof.
intros. unfold lookup, update, update'. eauto.
destruct string_dec; trivial; contradiction.
Qed.
Lemma lookup_update_neq : forall {A} (m : map A) k k' v,
k' <> k ->
m[k' <== v] k = m k.
Proof.
intros. unfold update, update'.
destruct string_dec; trivial; subst. contradiction.
Qed.
(* ------------------------------------------------------------------------- *)
(* equivalence *)
(* ------------------------------------------------------------------------- *)
Definition map_eqv {A} (m1 m2 : map A) :=
forall k, m1 k = m2 k.
Notation " m1 === m2 " := (map_eqv m1 m2)
(at level 70, no associativity).
Module MapEqv.
Lemma refl : forall {A} (m : map A),
m === m.
Proof.
unfold map_eqv. intros. eauto.
Qed.
Lemma trans : forall {A} (m1 m2 m3 : map A),
m1 === m2 ->
m2 === m3 ->
m1 === m3.
Proof.
unfold map_eqv. intros * H1 H2 ?.
rewrite (H1 _). rewrite (H2 _). trivial.
Qed.
Lemma sym : forall {A} (m1 m2 : map A),
m1 === m2 ->
m2 === m1.
Proof.
unfold map_eqv. intros. eauto.
Qed.
Lemma lookup : forall {A} (m1 m2 : map A) k v,
m1 === m2 ->
m1 k = v ->
m2 k = v.
Proof.
intros * Heq ?. specialize (Heq k). inversion Heq. trivial.
Qed.
Lemma update_eqv : forall {A} (m1 m2 : map A) k v,
m1 === m2 ->
m1[k <== v] === m2[k <== v].
Proof.
unfold map_eqv, update, update'. intros.
destruct string_dec; trivial.
Qed.
Lemma update_permutation : forall {A} (m : map A) k1 k2 v1 v2,
k1 <> k2 ->
m[k1 <== v1][k2 <== v2] === m[k2 <== v2][k1 <== v1].
Proof.
unfold map_eqv, update, update'. intros.
do 2 (destruct string_dec; subst); trivial.
contradiction.
Qed.
Lemma update_overwrite : forall {A} (m : map A) k v v',
m[k <== v] === m[k <== v'][k <== v].
Proof.
unfold map_eqv, update, update'. intros.
destruct string_dec; intros; trivial.
Qed.
End MapEqv.
(* ------------------------------------------------------------------------- *)
(* inclusion *)
(* ------------------------------------------------------------------------- *)
(* m includes m' *)
Definition inclusion {A} (m m' : map A) := forall k v,
m' k = Some v -> m k = Some v.
Infix "includes" := inclusion (at level 70, no associativity).
Module MapInc.
Lemma refl : forall {A} (m : map A),
m includes m.
Proof.
unfold inclusion. intros. eauto.
Qed.
Lemma trans : forall {A} (m1 m2 m3 : map A),
m1 includes m2 ->
m2 includes m3 ->
m1 includes m3.
Proof.
unfold inclusion. intros * H1 H2 ? ? ?.
rewrite (H1 k v); trivial. rewrite (H2 k v); trivial.
Qed.
Lemma update_inclusion : forall {A} (m m' : map A) k v,
m includes m' ->
m[k <== v] includes m'[k <== v].
Proof.
unfold inclusion, update, update'. intros.
destruct string_dec; eauto.
Qed.
Lemma update_permutation : forall {A} (m : map A) k1 k2 v1 v2,
k1 <> k2 ->
m[k1 <== v1][k2 <== v2] includes m[k2 <== v2][k1 <== v1].
Proof.
unfold inclusion, update, update'. intros.
do 2 (destruct string_dec; subst); trivial. contradiction.
Qed.
Lemma update_overwrite : forall {A} (m : map A) k v v',
m[k <== v] includes m[k <== v'][k <== v].
Proof.
unfold inclusion, update, update'. intros.
destruct string_dec; trivial.
Qed.
End MapInc.