-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeline.v
286 lines (260 loc) · 9.13 KB
/
Timeline.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
Require Import Coq.ZArith.ZArith.
Require Import ByteData.
Require Import DiskSubset.
Require Import File.
Require Import FileData.
Require Import FileIds.
Require Import Util.
Open Local Scope bool.
Open Local Scope N.
Inductive Event: Type :=
| FileAccess: N -> FileId -> Event
| FileModification: N -> FileId -> Event
| FileCreation: N -> FileId -> Event
| FileDeletion: N -> FileId -> Event
.
Definition eqb (lhs rhs: Event) :=
match (lhs, rhs) with
| (FileAccess l lfs, FileAccess r rfs) =>
andb (FileIds.eqb lfs rfs) (N.eqb l r)
| (FileModification l lfs, FileModification r rfs) =>
andb (FileIds.eqb lfs rfs) (N.eqb l r)
| (FileCreation l lfs, FileCreation r rfs) =>
andb (FileIds.eqb lfs rfs) (N.eqb l r)
| (FileDeletion l lfs, FileDeletion r rfs) =>
andb (FileIds.eqb lfs rfs) (N.eqb l r)
| _ => false
end.
Definition Timeline: Type := list Event.
Definition timestampOf (event: Event) : Exc N :=
match event with
| FileAccess timestamp _ => value timestamp
| FileModification timestamp _ => value timestamp
| FileCreation timestamp _ => value timestamp
| FileDeletion timestamp _ => value timestamp
end.
Definition beforeOrConcurrent (lhs rhs: Event) :=
match (timestampOf lhs, timestampOf rhs) with
| (value lhs_time, value rhs_time) => lhs_time <=? rhs_time
| _ => false
end.
Definition foundOn (event: Event) (disk: Disk) :=
match event with
| FileAccess timestamp fs => exists (file: File),
isOnDisk file disk
/\ fs = file.(fileId)
/\ file.(lastAccess) = value timestamp
| FileModification timestamp fs => exists (file: File),
isOnDisk file disk
/\ fs = file.(fileId)
/\ file.(lastModification) = value timestamp
| FileCreation timestamp fs => exists (file: File),
isOnDisk file disk
/\ fs = file.(fileId)
/\ file.(lastCreated) = value timestamp
| FileDeletion timestamp fs => exists (file: File),
isOnDisk file disk
/\ fs = file.(fileId)
/\ file.(lastDeleted) = value timestamp
end.
Definition foundOn_compute (event: Event) (disk: Disk) (file: File) :=
match event with
| FileAccess timestamp fs =>
isOnDisk_compute file disk
&& FileIds.eqb fs file.(fileId)
&& optN_eqb file.(lastAccess) (value timestamp)
| FileModification timestamp fs =>
isOnDisk_compute file disk
&& FileIds.eqb fs file.(fileId)
&& optN_eqb file.(lastModification) (value timestamp)
| FileCreation timestamp fs =>
isOnDisk_compute file disk
&& FileIds.eqb fs file.(fileId)
&& optN_eqb file.(lastCreated) (value timestamp)
| FileDeletion timestamp fs =>
isOnDisk_compute file disk
&& FileIds.eqb fs file.(fileId)
&& optN_eqb file.(lastDeleted) (value timestamp)
end.
Lemma foundOn_reflection (event: Event) (disk: Disk) (file: File) :
foundOn_compute event disk file = true -> foundOn event disk.
Proof.
intros.
unfold foundOn_compute in H. unfold foundOn.
destruct event.
apply Bool.andb_true_iff in H; destruct H;
apply Bool.andb_true_iff in H; destruct H;
exists file;
split;
[ apply isOnDisk_reflection; auto
| split;
[ apply FileIds.eqb_reflection; auto
| apply optN_eqb_reflection; auto]].
apply Bool.andb_true_iff in H; destruct H;
apply Bool.andb_true_iff in H; destruct H;
exists file;
split;
[ apply isOnDisk_reflection; auto
| split;
[ apply FileIds.eqb_reflection; auto
| apply optN_eqb_reflection; auto]].
apply Bool.andb_true_iff in H; destruct H;
apply Bool.andb_true_iff in H; destruct H;
exists file;
split;
[ apply isOnDisk_reflection; auto
| split;
[ apply FileIds.eqb_reflection; auto
| apply optN_eqb_reflection; auto]].
apply Bool.andb_true_iff in H; destruct H;
apply Bool.andb_true_iff in H; destruct H;
exists file;
split;
[ apply isOnDisk_reflection; auto
| split;
[ apply FileIds.eqb_reflection; auto
| apply optN_eqb_reflection; auto]].
Qed.
Lemma foundOn_subset:
forall (sub super: Disk) (event: Event),
sub ⊆ super ->
foundOn event sub ->
foundOn event super.
Proof.
intros sub super event subset.
unfold foundOn.
intros H.
destruct event; repeat (
destruct H as [file H]; exists file;
destruct H; split;
[ apply isOnDisk_subset with (1:=subset); assumption
| assumption]).
Qed.
Definition isSoundPair (disk: Disk) (eventPair: Event*Event) :=
let (lhsEvent, rhsEvent) := eventPair in
foundOn lhsEvent disk
/\ foundOn rhsEvent disk
/\ beforeOrConcurrent lhsEvent rhsEvent = true.
Definition isSoundPair_compute (disk: Disk)
(pairPair: (Event*Event)*(File*File)) :=
let (eventPair, filePair) := pairPair in
let (lhsEvent, rhsEvent) := eventPair in
let (lhsFile, rhsFile) := filePair in
foundOn_compute lhsEvent disk lhsFile
&& foundOn_compute rhsEvent disk rhsFile
&& beforeOrConcurrent lhsEvent rhsEvent.
Lemma isSoundPair_reflection (disk: Disk)
(eventPair: Event*Event) (filePair: File*File) :
isSoundPair_compute disk (eventPair, filePair) = true ->
isSoundPair disk eventPair.
Proof.
intros.
destruct eventPair as [lhsEvent rhsEvent].
destruct filePair as [lhsFile rhsFile].
unfold isSoundPair_compute in H.
unfold isSoundPair.
apply Bool.andb_true_iff in H. destruct H.
apply Bool.andb_true_iff in H. destruct H.
split.
apply foundOn_reflection with (file := lhsFile). auto.
split.
apply foundOn_reflection with (file := rhsFile). auto.
auto.
Qed.
Lemma isSoundPair_subset:
forall (sub super: Disk) (eventPair: Event*Event),
sub ⊆ super ->
isSoundPair sub eventPair ->
isSoundPair super eventPair.
Proof.
intros sub super eventPair subset.
destruct eventPair as [lhs rhs].
unfold isSoundPair.
intros H. destruct H. destruct H0.
split. apply foundOn_subset with (1:=subset). assumption.
split. apply foundOn_subset with (1:=subset). assumption.
assumption.
Qed.
Definition isSound (timeline: Timeline) (disk: Disk) :=
let staggeredEvents := combine timeline (skipn 1 timeline) in
forall (pair: Event*Event),
In pair staggeredEvents -> isSoundPair disk pair.
Lemma isSound_subset:
forall (sub super: Disk) (timeline: Timeline),
sub ⊆ super ->
isSound timeline sub ->
isSound timeline super.
Proof.
intros sub super timeline subset.
unfold isSound.
intros H pair Hin.
apply isSoundPair_subset with (1:=subset).
apply H. assumption.
Qed.
Definition isSound_tmp (timeline: Timeline) (disk: Disk) :=
exists (files: list File),
let staggeredEvents := combine timeline (skipn 1 timeline) in
let staggeredFiles := combine files (skipn 1 files) in
let combined := combine staggeredEvents staggeredFiles in
length staggeredEvents = length staggeredFiles
/\ forall (pairPair: (Event*Event)*(File*File)),
In pairPair combined -> isSoundPair disk (fst pairPair).
Lemma strip_list_l {L R: Type} (lhs:list L) (rhs: list R)
(prop:L->Prop) :
length lhs = length rhs
-> (forall (pair: L*R), In pair (combine lhs rhs) -> prop (fst pair))
-> (forall (l: L), In l lhs -> prop l).
Proof.
generalize rhs prop. clear rhs prop.
induction lhs.
intros. destruct rhs; [
contradict H1
| simpl in H; discriminate H].
intros. destruct rhs; [
simpl in H; discriminate H
|].
destruct H1.
specialize (H0 (l, r)).
simpl in H0. apply H0. left. subst. reflexivity.
apply IHlhs with (rhs := rhs).
inversion H. reflexivity.
intros pair inn.
assert (In pair (combine (a :: lhs) (r :: rhs))).
apply in_cons. assumption.
apply H0. assumption.
assumption.
Qed.
Lemma isSound_tmp_impl (timeline: Timeline) (disk: Disk) :
isSound_tmp timeline disk -> isSound timeline disk.
Proof.
intros.
unfold isSound_tmp in H. destruct H as [files]. destruct H.
unfold isSound.
remember (combine timeline (skipn 1 timeline)) as staggeredEvents.
remember (combine files (skipn 1 files)) as staggeredFiles.
apply strip_list_l with (rhs := staggeredFiles).
assumption. assumption.
Qed.
Definition isSound_compute (timeline: Timeline) (disk: Disk)
(files: list File) :=
let staggeredEvents := combine timeline (skipn 1 timeline) in
let staggeredFiles := combine files (skipn 1 files) in
let combined := combine staggeredEvents staggeredFiles in
beq_nat (length staggeredEvents) (length staggeredFiles)
&& forallb (isSoundPair_compute disk) combined.
Lemma isSound_reflection (timeline: Timeline) (disk: Disk)
(files: list File) :
isSound_compute timeline disk files = true ->
isSound timeline disk.
Proof.
intros. apply isSound_tmp_impl. unfold isSound_tmp. exists files.
unfold isSound_compute in H.
apply Bool.andb_true_iff in H. destruct H.
split. apply beq_nat_eq. auto.
rewrite forallb_forall in H0.
intros. apply isSoundPair_reflection with (filePair := snd pairPair).
assert (fst pairPair |-> snd pairPair = pairPair).
destruct pairPair. simpl. reflexivity.
rewrite H2.
auto.
Qed.