-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.v
More file actions
355 lines (313 loc) · 7.15 KB
/
SQL.v
File metadata and controls
355 lines (313 loc) · 7.15 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
From Coq Require Import Init.Nat.
From Coq Require Import Lists.List.
Definition compose {A B C : Type} (f : A -> B) (g : B -> C) : A -> C :=
fun x => g (f x).
(* Setup Lists *)
Open Scope list_scope.
Import ListNotations.
Inductive result (A : Type) :=
| Result : A -> result A
| No_Result : result A
| Empty_Cursor : result A.
Fixpoint iter {A B : Type} (f : A -> (B * A)) n c acc :=
match n with
| 0 => (c, acc)
| (S n') =>
let (x, c') := (f c) in
iter f n' c' (c :: acc)
end.
Class Cursor c (elt : Type) :=
{
next : c -> result elt * c;
has_next : c -> Prop;
reset : c -> c;
collection : c -> list elt;
visited : c -> list elt;
coherent : c -> Prop;
ubound : c -> nat;
}.
Class SafeCursor c elt `{Cursor c elt} :=
{
next_collection :
forall c,
coherent c ->
collection (snd (next c)) = collection c;
next_visited_Result : forall a c c',
coherent c ->
next c = (Result elt a, c') ->
visited c' = a :: (visited c);
next_visited_No_Result : forall c c',
coherent c ->
next c = (No_Result elt, c') ->
visited c' = visited c;
next_visited_Empty_Cursor : forall c c',
coherent c ->
next c = (Empty_Cursor elt, c') ->
visited c' = visited c;
next_coherent : forall c,
coherent c -> coherent (snd (next c));
has_next_spec : forall c,
coherent c -> ~ has_next c ->
(collection c) = (rev (visited c));
has_next_next_neg : forall c,
coherent c ->
(has_next c <-> fst (next c) <> (Empty_Cursor elt));
reset_collection : forall c,
collection (reset c) = collection c;
reset_visited : forall c,
visited (reset c) = [];
reset_coherent : forall c,
coherent (reset c);
ubound_complate : forall c acc,
coherent c ->
~ has_next (fst (iter next (ubound c) c acc));
}.
Record Seq (elt : Type) : Type :=
{
total : list elt;
to_visit : list elt;
vs : list elt;
}.
Instance seqCursor (elt : Type) : Cursor (Seq elt) elt :=
{
next c := match c with
| {| total := t; to_visit := tv; vs := vs |} =>
match tv with
| [] => (Empty_Cursor elt, c)
| (x::xs) => (Result elt x, {| total := t; to_visit := xs; vs := x::vs |})
end
end;
has_next c := match to_visit elt c with
| [] => False
| (_::_) => True
end;
reset c := let t := total elt c in {| total := t; to_visit := t; vs := [] |};
collection c := total elt c;
visited c := vs elt c;
coherent c := total elt c = (rev (vs elt c)) ++ (to_visit elt c);
ubound c := length (to_visit elt c)
}.
Theorem seq_next_collection (elt : Type) :
forall (c : Seq elt) , coherent c ->
collection (snd (next c)) = collection c.
Proof.
intros.
induction c.
simpl.
induction to_visit0.
auto.
simpl.
auto.
Qed.
Lemma seq_next_result elt : forall (c : Seq elt) c' x,
coherent c ->
next c = (Result elt x, c') ->
visited c' = x :: (visited c).
Proof.
intros.
destruct c.
destruct to_visit0.
- simpl.
simpl in H0. injection H0 as H'. discriminate H'.
- simpl.
simpl in H0.
injection H0.
intros.
rewrite <- H1.
simpl.
rewrite H2.
auto.
Qed.
Lemma seq_next_visited_No_Result elt : forall c c',
coherent c ->
next c = (No_Result elt, c') ->
visited c' = visited c.
Proof.
intros.
destruct c.
- destruct to_visit0.
+ simpl in H0.
injection H0.
intros.
rewrite H1.
auto.
+ simpl in H0.
injection H0.
intros.
discriminate H2.
Qed.
Lemma seq_next_visited_Empty_Cursor elt : forall c c',
coherent c ->
next c = (Empty_Cursor elt, c') ->
visited c' = visited c.
Proof.
intros.
destruct c.
destruct to_visit0.
- simpl in H0.
injection H0.
simpl.
intros.
rewrite <- H1.
simpl.
auto.
- simpl in H0.
injection H0.
intros.
simpl in *.
discriminate H2.
Qed.
Lemma no_result_same_seq (elt : Type) : forall (c c' : Seq elt),
coherent c ->
(next c = (No_Result elt, c')) \/ (next c = (Empty_Cursor elt, c')) ->
c = c'.
Proof.
intros.
destruct H0;
simpl;
inversion H0;
destruct c;
destruct to_visit0;
injection H2;
auto;
injection H2;
intros;
discriminate H3.
Qed.
Lemma total_unchanged (elt : Type) : forall (c : Seq elt),
coherent c ->
total elt c = total elt (snd (next c)).
Proof.
intros.
destruct (next c) eqn:Heq.
rename s into c'.
destruct r;
simpl;
destruct c;
destruct c';
inversion Heq;
destruct to_visit0;
simpl;
inversion Heq;
auto.
Qed.
Lemma seq_next_coherent_result (elt : Type) : forall (c c' : Seq elt) x,
coherent c ->
next c = (Result elt x, c') ->
coherent c'.
Proof.
intros.
inversion H.
simpl in H0.
destruct c.
destruct to_visit0.
injection H0.
intros.
discriminate H3.
rename to_visit0 into es.
simpl in H0.
simpl in H2.
simpl.
injection H0.
intros.
rewrite <- H1.
simpl.
rewrite H2.
remember (rev vs0) as r.
assert (((r ++ [e]) ++ es) = r ++ ([e] ++ es)).
rewrite app_assoc.
auto.
rewrite H4.
auto.
Qed.
Lemma seq_next_coherent (elt : Type) : forall (c : Seq elt),
coherent c -> coherent (snd (next c)).
Proof.
intros.
destruct (next c) eqn:Heq.
destruct r.
- rename s into c'.
rename e into x.
eapply seq_next_coherent_result; eauto.
- assert (c = s).
apply no_result_same_seq; auto.
rewrite <- H0.
auto.
- assert (c = s).
apply no_result_same_seq; auto.
rewrite <- H0.
auto.
Qed.
Lemma seq_has_next_spec (elt : Type) : forall (c : Seq elt),
coherent c ->
~ has_next c ->
(collection c) = (rev (visited c)).
Proof.
intros.
inversion H.
simpl.
destruct c.
simpl in *.
destruct to_visit0.
- rewrite H2.
rewrite app_nil_r.
auto.
- simpl.
unfold not in H0.
exfalso.
apply H0.
tauto.
Qed.
Lemma seq_has_next_next_neg (elt : Type) : forall c,
coherent c ->
(has_next c <-> fst (next c) <> (Empty_Cursor elt)).
Proof.
intros.
split.
- intros.
simpl.
destruct c.
destruct to_visit0.
+ auto.
+ discriminate.
- intros.
destruct c.
destruct to_visit0;
simpl in *;
auto.
Qed.
Lemma seq_ubound_complate (elt : Type) : forall (c : Seq elt) acc,
coherent c ->
~ has_next (fst (iter next (ubound c) c acc)).
Proof.
intros.
destruct c.
induction to_visit0.
- unfold not.
intros.
simpl in *.
auto.
- unfold not.
intros.
simpl in H.
Instance seqsafe elt : SafeCursor (Seq elt) elt.
Proof.
constructor; intros; auto.
- apply seq_next_collection.
auto.
-
apply seq_next_result; auto.
- intros.
apply seq_next_visited_No_Result; auto.
- intros.
apply seq_next_visited_Empty_Cursor; auto.
- intros.
apply seq_next_coherent.
auto.
- apply seq_has_next_spec.
auto.
auto.
- apply seq_has_next_next_neg.
auto.
- simpl. auto.
Qed.