-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaps.v
177 lines (160 loc) · 4.71 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
From Coq Require Import Arith.Arith.
From Coq Require Import Bool.Bool.
Require Export Coq.Strings.String.
From Coq Require Import Logic.FunctionalExtensionality.
From Coq Require Import Lists.List.
Import ListNotations.
Set Default Goal Selector "!".
Locate "+".
Print Init.Nat.add.
(* total maps *)
Definition total_map (A : Type) := string -> A.
Definition t_empty {A : Type} (v : A) : total_map A := (fun _ => v).
Definition t_update {A : Type} (m : total_map A) (x : string) (v : A) :=
fun x' => if String.eqb x x' then v else m x'.
Notation "'_' '!->' v" := (t_empty v)
(at level 100, right associativity).
Example example_empty := (_ !-> false).
Check example_empty.
Notation "x '!->' v ';' m" := (t_update m x v)
(at level 100, v at next level, right associativity).
Definition examplemap :=
( "bar" !-> true;
"foo" !-> true;
_ !-> false
).
Print examplemap.
Lemma t_apply_empty : forall (A : Type) (x : string) (v : A),
(_ !-> v) x = v.
Proof. intros. unfold t_empty. reflexivity. Qed.
Lemma t_update_eq : forall (A : Type) (m : total_map A) x v,
(x !-> v; m) x = v.
Proof. intros.
unfold t_update.
rewrite String.eqb_refl.
reflexivity.
Qed.
Lemma t_update_neq : forall (A : Type) (m : total_map A) x1 x2 v,
x1 <> x2 -> (x1 !-> v; m) x2 = m x2.
Proof.
intros.
unfold t_update.
apply String.eqb_neq in H.
destruct (String.eqb x1 x2) eqn:Ex.
- discriminate H.
- reflexivity.
Qed.
Lemma t_update_shadow : forall (A : Type) (m : total_map A) x v1 v2,
(x !-> v2; x !-> v1; m) = (x !-> v2 ; m).
Proof.
intros.
apply functional_extensionality.
intros y.
unfold t_update.
destruct (String.eqb x y).
- reflexivity.
- reflexivity.
Qed.
Theorem t_update_same : forall (A : Type) (m : total_map A) x,
(x !-> m x; m) = m.
Proof.
intros.
apply functional_extensionality.
intros y.
unfold t_update.
destruct (String.eqb x y) eqn:Exy.
- apply String.eqb_eq in Exy. rewrite Exy. reflexivity.
- reflexivity.
Qed.
Theorem t_update_permute : forall (A : Type) (m: total_map A)
v1 v2 x1 x2,
x2 <> x1 ->
(x1 !-> v1; x2 !-> v2; m) = (x2 !-> v2; x1 !-> v1; m).
Proof.
intros.
apply functional_extensionality.
intros.
unfold t_update.
destruct (String.eqb x1 x) eqn:Ex1.
- destruct (String.eqb x2 x) eqn:Ex2.
+ destruct H.
apply String.eqb_eq in Ex2.
apply String.eqb_eq in Ex1.
rewrite Ex2.
rewrite Ex1.
reflexivity.
+ reflexivity.
- destruct (String.eqb x2 x) eqn:Ex2.
+ reflexivity.
+ reflexivity.
Qed.
Definition paritial_map (A : Type) := total_map (option A).
Definition empty {A : Type} : paritial_map A := t_empty None.
Definition update {A : Type} (m : paritial_map A)
(x : string) (v : A) := (x !-> Some v; m).
Notation "x '|->' v ';' m" :=
(update m x v)
(at level 100, v at next level, right associativity).
Lemma apply_empty : forall (A : Type) (x : string), @empty A x = None.
Proof.
intros. unfold empty.
apply t_apply_empty.
Qed.
Lemma update_eq : forall (A : Type) (m : paritial_map A) x v,
(x |-> v; m) x = Some v.
Proof.
intros. unfold update.
apply t_update_eq.
Qed.
(* update_eq lemma is used very often in proofs. adding it to coq's
global "hint database" allows proof-automation tactics such as
_auto_ to find it *)
#[global] Hint Resolve update_eq : core.
Theorem update_neq : forall (A:Type) (m:paritial_map A) x1 x2 v,
x2 <> x1 -> (x2 |-> v; m) x1 = m x1.
Proof.
intros. unfold update.
apply t_update_neq. apply H.
Qed.
Lemma update_shadow : forall (A:Type) (m:paritial_map A) x v1 v2,
(x |-> v2; x |-> v1; m) = (x |-> v2; m).
Proof.
intros. unfold update.
apply t_update_shadow.
Qed.
Lemma update_same : forall (A: Type) (m:paritial_map A) x v,
m x = Some v -> (x |-> v; m) = m.
Proof.
intros. unfold update.
rewrite <- H.
rewrite t_update_same.
reflexivity.
Qed.
Lemma update_permute : forall (A:Type) (m:paritial_map A) x1 x2 v1 v2,
x2 <> x1 ->
(x1 |-> v1; x2 |-> v2; m) = (x2 |-> v2; x1 |-> v1; m).
Proof.
intros. unfold update.
apply t_update_permute.
apply H.
Qed.
(* i don't get this *)
Definition includedin {A : Type} (m m' : paritial_map A) :=
forall x v, m x = Some v -> m' x = Some v.
Lemma includedin_update : forall (A : Type) (m m' : paritial_map A)
(x : string) (vx : A),
includedin m m' -> includedin (x |-> vx; m) (x |-> vx; m').
Proof.
unfold includedin.
intros A m m' x vx H y vy.
destruct (eqb_spec x y).
- rewrite e.
rewrite update_eq.
rewrite update_eq.
intros H'. apply H'.
- rewrite update_neq.
+ rewrite update_neq.
{ intros H'. apply H. apply H'. }
apply n.
+ apply n.
Qed.