-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaps.v
191 lines (136 loc) · 4.36 KB
/
maps.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(*
** Maps used to represent environments in all three languages.
*)
Require Import Coq.Logic.Decidable.
Require Import PeanoNat.
Require Import Coq.Strings.String.
Require Import Ascii.
Require Import Bool.
Require Import Nat.
(*
** A Map maps strings to A.
*)
Inductive Map (A : Type) : Type :=
| MEmpty : Map A
| MCons : string -> A -> Map A -> Map A
.
Arguments MEmpty {A}.
Arguments MCons {A}.
Notation "v |=> T ; M" := (MCons v T M) (at level 40).
Ltac breakStrDec :=
simpl in *;
repeat match goal with
| [ |- context [string_dec ?v ?v'] ] =>
destruct (string_dec v v'); subst; try easy
| [ H: context [string_dec ?v ?v'] |- _ ] =>
destruct (string_dec v v') in H; subst; try easy
end.
(*
** Return 'M[var]' (None if absent)
*)
Fixpoint In {A} M var : option A :=
match M with
| MEmpty => None
| var' |=> T; M' => if string_dec var var' then Some T else In M' var
end.
Lemma InEq : forall A (M : Map A) var r, In (var |=> r; M) var = Some r.
Proof. intros; breakStrDec. Qed.
Lemma InEq' : forall A (M : Map A) var r r',
In (var |=> r; M) var = Some r' -> r = r'.
Proof. intros * H. rewrite InEq in H. injection H. trivial. Qed.
Lemma InNotEq' : forall A (M : Map A) var var' r,
var <> var' -> In (var' |=> r; M) var = In M var.
Proof. intros; breakStrDec. Qed.
Lemma InNotEq : forall A (M : Map A) var var' r r',
var <> var' -> In (var' |=> r; M) var = r' -> In M var = r'.
Proof. intros * HNeq Heq. rewrite InNotEq' in Heq; trivial. Qed.
Definition inclusion {A} (M : Map A) M' :=
forall x v, In M x = Some v -> In M' x = Some v.
Lemma inclusion_empty : forall A (M : Map A), inclusion MEmpty M.
Proof. unfold inclusion. inversion 1. Qed.
Lemma inclusion_refl : forall A (M : Map A), inclusion M M.
Proof. unfold inclusion. trivial. Qed.
Lemma inclusion_update : forall A (M : Map A) M' var tvar,
inclusion M M' -> inclusion (var |=> tvar ; M) (var |=> tvar ; M').
Proof.
unfold inclusion; intros; breakStrDec; auto.
Qed.
Lemma inclusion_shadow : forall A (M : Map A) var t1 t2,
inclusion (var |=> t1; (var |=> t2; M)) (var |=> t1; M).
Proof.
unfold inclusion; intros; breakStrDec.
Qed.
Lemma inclusion_shadow' : forall A (M : Map A) var t1 t2,
inclusion (var |=> t1; M) (var |=> t1; (var |=> t2; M)).
Proof.
unfold inclusion; intros; breakStrDec.
Qed.
Lemma inclusion_permute : forall A (M : Map A) var1 var2 t1 t2,
var1 <> var2 ->
inclusion (var1 |=> t1; (var2 |=> t2; M))
(var2 |=> t2; (var1 |=> t1; M)).
Proof.
unfold inclusion; intros; breakStrDec.
Qed.
Lemma InInclusionEq : forall A (M M' : Map A) var t,
inclusion (var |=> t; M) M' ->
In M' var = Some t.
Proof.
unfold inclusion; eauto using InEq.
Qed.
Lemma InInclusion : forall A (M M' : Map A) var t,
inclusion (var |=> t; M) M' ->
In M' var = Some t.
Proof.
unfold inclusion; intros; eauto using InEq.
Qed.
Definition map_eq {A} (M M' : Map A) := forall var, In M var = In M' var.
Lemma map_eq_In : forall {A} (M M': Map A) var v v',
In M' var = Some v ->
map_eq (var |=> v'; M) M' ->
v = v'.
Proof.
intros * HI HEq.
replace (In M' var) with (Some v') in *; try congruence.
specialize (HEq var). rewrite InEq in HEq. congruence.
Qed.
Lemma map_eq_incl : forall {A} (M M': Map A),
map_eq M M' -> inclusion M M'.
Proof.
unfold inclusion; unfold map_eq; intros; congruence.
Qed.
(* map_eq is an equivalence relation *)
Lemma map_eq_refl : forall A (M : Map A), map_eq M M.
Proof. unfold map_eq; trivial. Qed.
Lemma map_eq_sym : forall A (M M': Map A), map_eq M M' -> map_eq M' M.
Proof. unfold map_eq; auto. Qed.
Lemma map_eq_trans : forall A (M M' M'': Map A),
map_eq M M' ->
map_eq M' M'' ->
map_eq M M''.
Proof. unfold map_eq; intros; congruence. Qed.
Lemma eqeq_shadow : forall A (M M' : Map A) var v v',
map_eq (var |=> v'; M) M' ->
map_eq (var |=> v; M) (var |=> v; M').
Proof.
intros * H1.
intros var'.
breakStrDec.
specialize (H1 var').
rewrite InNotEq' in H1; trivial.
Qed.
Lemma eq_shadow : forall A (M : Map A) var v v',
map_eq (var |=> v; M) (var |=> v; (var |=> v'; M)).
Proof.
intros; eauto using eqeq_shadow, map_eq_refl.
Qed.
Lemma eqeq_permute : forall A (M M' : Map A) var var' v v',
map_eq (var |=> v; M) M' ->
var <> var' ->
map_eq (var |=> v; (var' |=> v'; M)) (var' |=> v'; M').
Proof.
intros * H ?.
intros var0.
specialize (H var0).
breakStrDec.
Qed.