-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_sort.v
More file actions
393 lines (351 loc) · 6.24 KB
/
Copy pathtree_sort.v
File metadata and controls
393 lines (351 loc) · 6.24 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
(*
Author: Cenobio Moisés Vázquez Reyes
This module is a formal verification of the sorting algorithm 'tree sort'.
*)
Require Import List.
Require Import Coq.Unicode.Utf8_core.
Require Import Coq.Arith.Lt.
(* Definition: x ∊ L, where 'L' is a simple list. *)
Fixpoint in_list (x:nat) (l:list nat) :=
match l with
| nil => False
| y::ys => x = y \/ in_list x ys
end.
(* Sort list definition. *)
Fixpoint list_is_sort (l:list nat) :=
match l with
| nil => True
| x::xs => (∀ y, in_list y xs -> x <= y) /\ list_is_sort xs
end.
(* Definition: Binary tree. *)
Inductive btree :=
| Void : btree
| N : nat->btree->btree->btree.
(* Definition: x ∊ T, where 'T' is a binary tree. *)
Fixpoint in_tree n t :=
match t with
| Void => False
| N m t1 t2 => n = m \/ in_tree n t1 \/ in_tree n t2
end.
(* Definition: Binary search tree. *)
Fixpoint bstree t :=
match t with
| Void => True
| N n l r => (∀ m, in_tree m l -> m < n) /\
(∀ m, in_tree m r -> n <= m) /\
bstree l /\ bstree r
end.
(* Definition: In-order tree traversal. *)
Fixpoint in_order (t:btree) :=
match t with
| Void => nil
| N n l r => in_order l ++ (n::in_order r)
end.
Definition leaf n := N n Void Void.
(* Definition: Insert a number in a BST. *)
Fixpoint insert_BST x t :=
match t with
| Void => leaf x
| N n l r => match Nat.ltb x n with
| true => (N n (insert_BST x l) r)
| false => (N n l (insert_BST x r))
end
end.
(* Definition: insert a list in a BST. *)
Fixpoint insert_list_in_BST l t :=
match l with
| nil => t
| x::xs => insert_list_in_BST xs (insert_BST x t)
end.
(* List to BST. *)
Definition list_to_BST l := insert_list_in_BST l Void.
Proposition in_insertBST : ∀ m x t, in_tree m (insert_BST x t) ->
(m = x \/ in_tree m t).
Proof.
intros.
induction t.
(* base case *)
simpl in H; intuition.
(* inductive case *)
simpl in H.
case (Nat.ltb x n) in H.
destruct H.
right.
simpl.
left; trivial.
destruct H.
apply IHt1 in H.
destruct H.
left; trivial.
right.
simpl.
right; left; trivial.
right.
simpl.
right; right; trivial.
destruct H.
right.
simpl.
left; trivial.
destruct H.
right.
simpl.
right; left; trivial.
apply IHt2 in H.
destruct H.
left; trivial.
right.
simpl.
right; right; trivial.
Qed.
Lemma insertBST : ∀ x t, bstree t -> bstree (insert_BST x t).
Proof.
(* base case *)
intros.
induction t.
simpl; intuition.
(* inductive case *)
simpl.
(* case: x < n = true *)
destruct (Bool.bool_dec (Nat.ltb x n) true).
rewrite e.
simpl.
split.
intros.
assert (∀ x n, Nat.ltb x n = true -> x < n).
intro.
induction x0.
destruct n0.
intros.
inversion H1.
intros.
apply Lt.neq_0_lt.
intuition.
inversion H2.
destruct n0.
intros.
inversion H1.
intros.
simpl in H1.
apply Lt.lt_n_S.
apply IHx0; trivial.
apply in_insertBST in H0.
destruct H0.
rewrite <- H0 in e.
apply H1 in e.
trivial.
apply H.
trivial.
split.
intros.
apply H; trivial.
split.
apply IHt1; apply H.
apply H.
(* x < n = false *)
apply Bool.not_true_is_false in n0.
rewrite n0.
simpl.
split.
intros.
inversion H.
intuition.
split.
intros.
apply in_insertBST in H0.
destruct H0.
apply PeanoNat.Nat.ltb_ge in n0.
rewrite H0; trivial.
inversion H.
apply H2; trivial.
split.
apply H.
apply IHt2.
apply H.
Qed.
Lemma insertListToBST : ∀ l t, bstree t -> bstree (insert_list_in_BST l t).
Proof.
induction l.
(* base case *)
intros.
simpl; trivial.
(* inductive case *)
intros.
simpl.
apply IHl.
apply insertBST; trivial.
Qed.
Lemma tailSort : ∀ l, list_is_sort l -> list_is_sort (tail l).
Proof.
induction l.
(* base case *)
intros.
trivial.
(* inductive case *)
intros.
simpl.
simpl in H.
apply H.
Qed.
Lemma InAppend : ∀ x l1 l2, in_list x (l1 ++ l2) -> (in_list x l1 \/ in_list x l2).
Proof.
intros.
induction l1.
(* base case *)
right.
exact H.
(* inductive case *)
simpl in H.
destruct H.
left.
simpl.
left; trivial.
apply IHl1 in H.
destruct H.
left.
simpl.
right; trivial.
right; trivial.
Qed.
Lemma sortAppend : ∀ l1 l2 n, list_is_sort l1 ->
list_is_sort (n::l2) ->
(∀ m, in_list m l1 -> m <= n) ->
list_is_sort (l1 ++ (n::l2)).
Proof.
intro.
induction l1.
(* base case *)
intros.
simpl.
split.
intros.
apply H0.
trivial.
apply tailSort in H0; trivial.
(* inductive case *)
intros.
split.
intros.
apply InAppend in H2.
destruct H2.
apply H; trivial.
inversion H.
inversion H0.
destruct H2.
rewrite H2.
apply H1.
left; trivial.
apply (PeanoNat.Nat.le_trans a n y).
apply H1.
left; trivial.
apply H5; intuition.
apply IHl1.
apply H.
exact H0.
intros.
apply H1.
right; trivial.
Qed.
Lemma InTree_iff_InList : ∀ x t, in_tree x t <-> in_list x (in_order t).
Proof.
(* -> *)
split.
induction t.
(* base case *)
intros.
inversion H.
(* inductive case *)
intros.
simpl.
assert (∀ x l1 l2, (in_list x l1 \/ in_list x l2) -> in_list x (l1 ++ l2)).
intros.
induction l1.
simpl.
destruct H0.
inversion H0.
trivial.
simpl.
destruct H0.
simpl in H0.
destruct H0.
left.
trivial.
right.
apply IHl1.
left.
trivial.
right.
apply IHl1.
right.
trivial.
apply (H0 x (in_order t1) (n :: in_order t2)).
destruct H.
right.
simpl.
left.
trivial.
destruct H.
left.
apply IHt1.
trivial.
right.
simpl.
right.
apply IHt2.
trivial.
(* <- *)
induction t.
(* base case *)
intros.
simpl in H.
exfalso;trivial.
(* inductive case *)
intros.
simpl.
simpl in H.
apply InAppend in H.
destruct H.
right; left.
apply IHt1; trivial.
simpl in H.
destruct H.
left; trivial.
right; right.
apply IHt2; trivial.
Qed.
Lemma inOrderSort : ∀ t, bstree t -> list_is_sort (in_order t).
Proof.
induction t.
(* base case *)
intros.
trivial.
(* inductive case *)
intros.
simpl.
apply sortAppend.
apply IHt1.
apply H.
simpl list_is_sort.
split.
intros.
apply InTree_iff_InList in H0.
apply H.
trivial.
apply IHt2.
apply H.
intros.
apply InTree_iff_InList in H0.
inversion H.
apply H1 in H0.
apply PeanoNat.Nat.lt_le_incl in H0.
exact H0.
Qed.
Definition tree_sort l := in_order (list_to_BST l).
Theorem treeSort_correct : ∀ l, list_is_sort (tree_sort l).
Proof.
intros.
unfold tree_sort.
apply inOrderSort.
apply insertListToBST.
simpl; trivial.
Qed.