-
Notifications
You must be signed in to change notification settings - Fork 85
/
insertSortProgScript.sml
491 lines (479 loc) · 14.9 KB
/
insertSortProgScript.sml
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
(*
In-place insertion sort on a polymorphic array.
*)
open preamble semanticPrimitivesTheory
ml_translatorTheory ml_translatorLib ml_progLib cfLib
basisFunctionsLib ArrayProofTheory
val _ = new_theory "insertSortProg";
val _ = translation_extends"ArrayProg";
fun basis_st () = get_ml_prog_state ()
val insertsort = process_topdecs `
fun insertsort cmp a =
let
fun outer_loop prefix =
if prefix < Array.length a - 1 then
let val c = Array.sub a (prefix + 1)
fun inner_loop i =
if i >= 0 then
let val ai = Array.sub a i in
if cmp c ai then
(Array.update a (i+1) ai;
inner_loop (i - 1))
else
Array.update a (i + 1) c
end
else
Array.update a (i + 1) c
in
(inner_loop prefix;
outer_loop (prefix+1))
end
else
()
in
if Array.length a = 0 then () else outer_loop 0
end;
`;
val insertsort_st = ml_progLib.add_prog insertsort ml_progLib.pick_name (basis_st());
Triviality list_rel_perm_help:
!l1 l2.
PERM l1 l2
⇒
!l3 l4.
LIST_REL r (MAP FST l1) (MAP SND l1)
⇒
LIST_REL r (MAP FST l2) (MAP SND l2)
Proof
ho_match_mp_tac PERM_IND >>
rw []
QED
Theorem list_rel_perm:
!r l1 l2 l3 l4.
LENGTH l3 = LENGTH l4 ∧
LIST_REL r l1 l2 ∧
PERM (ZIP (l1,l2)) (ZIP (l3,l4))
⇒
LIST_REL r l3 l4
Proof
rw [] >>
drule list_rel_perm_help >>
imp_res_tac LIST_REL_LENGTH >>
rw [MAP_ZIP]
QED
Theorem list_rel_front:
!r l1 l2.
l1 ≠ [] ∧ l2 ≠ [] ⇒
(LIST_REL r l1 l2
⇔
LIST_REL r (FRONT l1) (FRONT l2) ∧ r (LAST l1) (LAST l2))
Proof
Induct_on `l1` >>
rw [] >>
Cases_on `l2` >>
fs [FRONT_DEF, LAST_DEF] >>
rw [] >>
metis_tac []
QED
Theorem zip_append_sing:
!l1 l2 x y.
LENGTH l1 = LENGTH l2
⇒
ZIP (l1,l2) ++ [(x, y)] = ZIP (l1++[x], l2++[y])
Proof
rw [] >>
`[(x,y)] = ZIP ([x], [y])` by rw [] >>
metis_tac [ZIP_APPEND, LENGTH]
QED
Triviality arith:
!x:num. x ≠ 0 ⇒ &(x-1) = &x - 1:int
Proof
rw [int_arithTheory.INT_NUM_SUB]
QED
val eq_num_v_thm =
MATCH_MP
(DISCH_ALL mlbasicsProgTheory.eq_v_thm)
(ml_translatorTheory.EqualityType_NUM_BOOL |> CONJUNCT1)
Theorem insertsort_spec:
!ffi_p cmp cmp_v arr_v elem_vs elems.
LIST_REL a elems elem_vs ∧
(a --> a --> BOOL) cmp cmp_v ∧
(!x y. cmp x y ⇒ ~cmp y x)
⇒
app (ffi_p:'ffi ffi_proj) ^(fetch_v "insertsort" insertsort_st)
[cmp_v; arr_v]
(ARRAY arr_v elem_vs)
(POSTv u.
SEP_EXISTS elem_vs'.
ARRAY arr_v elem_vs' *
&(?elems'.
PERM (ZIP (elems', elem_vs')) (ZIP (elems, elem_vs)) ∧
SORTED (\x y. ¬(cmp y x)) elems'))
Proof
rpt strip_tac >>
xcf "insertsort" insertsort_st >>
xfun_spec `outer_loop`
`!elem_vs2 elems1 elems2 elem_vs1 prefix_v.
elem_vs1 ≠ [] ∧
LIST_REL a elems1 elem_vs1 ∧
LIST_REL a elems2 elem_vs2 ∧
INT (&(LENGTH elem_vs1 - 1)) prefix_v ∧
SORTED (\x y. ¬(cmp y x)) elems1
⇒
app (ffi_p:'ffi ffi_proj) outer_loop
[prefix_v]
(ARRAY arr_v (elem_vs1++elem_vs2))
(POSTv u.
SEP_EXISTS elem_vs'.
ARRAY arr_v elem_vs' *
&(?elems'.
LENGTH elems' = LENGTH elem_vs' ∧
PERM (ZIP (elems', elem_vs')) (ZIP (elems1++elems2, elem_vs1++elem_vs2)) ∧
SORTED (\x y. ¬(cmp y x)) elems'))`
>- (
gen_tac >>
Induct_on `LENGTH elem_vs2` >>
rw []
>- ( (* Base case: we've come to the end of the array *)
xapp >>
xlet `POSTv len_v.
ARRAY arr_v (elem_vs1) *
&INT (&LENGTH (elem_vs1)) len_v`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def]) >>
xlet `POSTv x.
ARRAY arr_v elem_vs1 *
&INT (&(LENGTH elem_vs1-1)) x`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def, arith]) >>
xlet `POSTv b_v. ARRAY arr_v elem_vs1 * &BOOL F b_v`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def, BOOL_def]) >>
xif >>
qexists_tac `F` >>
rw [] >>
xret >>
xsimpl >>
fs [] >>
qexists_tac `elems1` >>
rw [] >>
metis_tac [LIST_REL_LENGTH]) >>
(* Start going through the loop *)
last_x_assum xapp_spec >>
xlet `POSTv len_v.
ARRAY arr_v (elem_vs1 ++ elem_vs2) *
&INT (&LENGTH (elem_vs1++elem_vs2)) len_v`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def]) >>
xlet `POSTv x.
ARRAY arr_v (elem_vs1 ++ elem_vs2) *
&INT (&(LENGTH (elem_vs1++elem_vs2)-1)) x`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def, arith]) >>
xlet `POSTv b_v. ARRAY arr_v (elem_vs1 ++ elem_vs2) * &BOOL T b_v`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def, BOOL_def] >>
Cases_on`LENGTH elem_vs1` \\ fs[]) >>
xif >>
qexists_tac `T` >>
rw [] >>
xlet
`POSTv pre1_v.
ARRAY arr_v (elem_vs1 ++ elem_vs2) *
&INT (&(LENGTH elem_vs1)) pre1_v`
>- (
xapp >>
xsimpl >>
fs [INT_def, arith]) >>
xlet `POSTv cc_v. ARRAY arr_v (elem_vs1 ++ elem_vs2) * &(cc_v = HD elem_vs2)`
>- (
xapp >>
xsimpl >>
qexists_tac `LENGTH elem_vs1` >>
simp [EL_APPEND2] >>
fs [INT_def, NUM_def]) >>
xfun_spec `inner_loop`
`!sorted_vs1 sorted1 sorted2 sorted_vs2 i_v junk.
LIST_REL a sorted1 sorted_vs1 ∧
LIST_REL a sorted2 sorted_vs2 ∧
INT (&(LENGTH sorted_vs1) - 1) i_v ∧
EVERY (cmp (HD elems2)) sorted2 ∧
SORTED (\x y. ¬(cmp y x)) (sorted1++sorted2)
⇒
app (ffi_p:'ffi ffi_proj) inner_loop
[i_v]
(ARRAY arr_v (sorted_vs1++[junk]++sorted_vs2++TL elem_vs2))
(POSTv u.
SEP_EXISTS sorted_vs'.
ARRAY arr_v (sorted_vs' ++ sorted_vs2 ++ TL elem_vs2) *
&(?sorted'.
LENGTH sorted' = LENGTH sorted_vs' ∧
PERM (ZIP (sorted', sorted_vs'))
(ZIP (sorted1++[HD elems2], sorted_vs1++[cc_v])) ∧
SORTED (\x y. ¬(cmp y x)) (sorted'++sorted2)))`
>- (
gen_tac >>
Induct_on `LENGTH sorted_vs1` >>
rw [] >>
last_x_assum xapp_spec
>- ( (* Base case, we've run off the array *)
xlet `POSTv b_v2. ARRAY arr_v (junk :: sorted_vs2 ++ TL elem_vs2) * &BOOL F b_v2`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def]) >>
xif >>
qexists_tac `F` >>
fs [] >>
xlet `POSTv i1_v. ARRAY arr_v (junk :: sorted_vs2 ++ TL elem_vs2) * &INT 0 i1_v`
>- (
xapp >>
xsimpl >>
fs [INT_def]) >>
xapp >>
xsimpl >>
qexists_tac `0` >>
simp [] >>
fs [INT_def, NUM_def] >>
rw [] >>
qexists_tac `[HD elem_vs2]` >>
simp [LUPDATE_def] >>
qexists_tac `[HD elems2]` >>
simp [] >>
Cases_on `sorted2` >>
simp [SORTED_DEF] >>
fs []) >>
(* We haven't hit the end *)
xlet `POSTv b_v2. ARRAY arr_v (sorted_vs1 ++ [junk] ++ sorted_vs2 ++ TL elem_vs2) * &BOOL T b_v2`
>- (
xapp >>
xsimpl >>
fs [INT_def, NUM_def, BOOL_def] >>
intLib.ARITH_TAC) >>
xif >>
qexists_tac `T` >>
rw [] >>
xlet `POSTv ai_v. ARRAY arr_v (sorted_vs1 ++ [junk] ++ sorted_vs2 ++ TL elem_vs2) *
&(ai_v = LAST sorted_vs1)`
>- (
xapp >>
xsimpl >>
qexists_tac `&(LENGTH sorted_vs1 - 1)` >>
simp [] >>
qpat_x_assum`_ = LENGTH sorted_vs1`(assume_tac o SYM) \\ fs[] >>
fs[EL_APPEND1, NUM_def, INT_def, ADD1, arith, LAST_EL, PRE_SUB1] >>
conj_tac >- intLib.COOPER_TAC >>
Q.ISPEC_THEN`sorted_vs1`mp_tac LAST_EL >> simp[PRE_SUB1] >>
impl_tac >- (strip_tac \\ fs[]) \\ simp[]) >>
xlet `POSTv b_v3. ARRAY arr_v (sorted_vs1 ++ [junk] ++ sorted_vs2 ++ TL elem_vs2) *
&BOOL (cmp (HD elems2) (LAST sorted1)) b_v3`
>- (
xapp >>
xsimpl >>
MAP_EVERY qexists_tac [`LAST sorted1`, `HD elems2`, `cmp`, `a`] >>
simp [] >>
fs [LIST_REL_EL_EQN] >>
rw [] >>
`0 < LENGTH elems2 ∧ LENGTH sorted_vs1 ≠ 0 ∧ PRE (LENGTH sorted1) < LENGTH sorted_vs1` by decide_tac >>
metis_tac [EL, LAST_EL, LENGTH_NIL]) >>
xif
>- ( (* The item to insert is too small. Keep going *)
xlet `POSTv i1_v. ARRAY arr_v (sorted_vs1 ++ [junk] ++ sorted_vs2 ++ TL elem_vs2) *
&INT (&LENGTH sorted_vs1) i1_v`
>- (
xapp >>
xsimpl >>
fs [INT_def, arith]) >>
xlet `POSTv u_v. ARRAY arr_v (sorted_vs1 ++ [LAST sorted_vs1] ++ sorted_vs2 ++ TL elem_vs2)`
>- (
xapp >>
xsimpl >>
qexists_tac `LENGTH sorted_vs1` >>
fs [NUM_def, INT_def] >>
metis_tac [lupdate_append2, APPEND_ASSOC]) >>
xlet `POSTv i2_v. ARRAY arr_v (sorted_vs1 ++ [LAST sorted_vs1] ++ sorted_vs2 ++ TL elem_vs2) *
&INT (&LENGTH sorted_vs1 − 2) i2_v`
>- (
xapp >>
xsimpl >>
fs [INT_def] >>
intLib.ARITH_TAC) >>
(* Prepare inductive hyp *)
`LENGTH sorted_vs1 ≠ 0` by decide_tac >>
first_x_assum (qspec_then `FRONT sorted_vs1` mp_tac) >>
impl_keep_tac
>- (
fs [LENGTH_NIL] >>
simp [LENGTH_FRONT]) >>
disch_then (qspecl_then [`FRONT sorted1`, `LAST sorted1::sorted2`,
`LAST sorted_vs1::sorted_vs2`, `i2_v`, `LAST sorted_vs1`] mp_tac) >>
simp [] >>
fs [] >>
impl_tac
>- (
`sorted1 ≠ []` by metis_tac [LENGTH_NIL, LIST_REL_LENGTH] >>
fs [list_rel_front, LENGTH_NIL] >>
fs [INT_def, LENGTH_FRONT, PRE_SUB1] >>
rw [] >> fs [ arith]
>- intLib.ARITH_TAC >>
metis_tac [LENGTH_NIL, APPEND_FRONT_LAST, APPEND, APPEND_ASSOC]) >>
disch_then xapp_spec >>
xsimpl >>
rw []
>- metis_tac [LENGTH_NIL, APPEND_FRONT_LAST] >>
qexists_tac `sorted'++[LAST sorted1]` >>
rw []
>- (
imp_res_tac LIST_REL_LENGTH >>
simp [GSYM ZIP_APPEND] >>
`PERM (ZIP (sorted',x') ++ [(LAST sorted1,LAST sorted_vs1)])
(ZIP (FRONT sorted1 ++ [HD elems2], FRONT sorted_vs1 ++ [HD elem_vs2]) ++
[(LAST sorted1,LAST sorted_vs1)])`
by metis_tac [PERM_APPEND_IFF] >>
pop_assum mp_tac >>
simp [zip_append_sing] >>
rw [] >>
irule PERM_TRANS >>
qexists_tac `ZIP (FRONT sorted1 ++ [HD elems2], FRONT sorted_vs1 ++ [HD elem_vs2]) ++ [(LAST sorted1,LAST sorted_vs1)]` >>
`sorted_vs1 ≠ [] ∧ sorted1 ≠ []` by metis_tac [LENGTH_NIL] >>
simp [GSYM ZIP_APPEND, LENGTH_FRONT] >>
irule PERM_TRANS >>
qexists_tac `ZIP (FRONT sorted1,FRONT sorted_vs1) ++ [(LAST sorted1,LAST sorted_vs1)] ++ [(HD elems2,HD elem_vs2)]` >>
rw []
>- metis_tac [PERM_APPEND, PERM_APPEND_IFF, APPEND_ASSOC] >>
simp [zip_append_sing, LENGTH_FRONT, ZIP_APPEND, APPEND_FRONT_LAST])
>- metis_tac [APPEND, APPEND_ASSOC])
>- ( (* We found the item's spot *)
xlet `POSTv i1_v. ARRAY arr_v (sorted_vs1 ++ [junk] ++ sorted_vs2 ++ TL elem_vs2) *
&INT (&LENGTH sorted_vs1) i1_v`
>- (
xapp >>
xsimpl >>
fs [INT_def]) >>
xapp >>
xsimpl >>
qexists_tac `LENGTH sorted_vs1` >>
simp [] >>
fs [INT_def, NUM_def] >>
rw [] >>
qexists_tac `sorted_vs1++[HD elem_vs2]` >>
rw []
>- (
qexists_tac `sorted1++[HD elems2]` >>
imp_res_tac LIST_REL_LENGTH >>
fs [SORTED_APPEND_GEN]
>- metis_tac [LENGTH_NIL, DECIDE ``SUC v ≠ 0``] >>
Cases_on `sorted2` >>
simp [] >>
`elems2 ≠ []` by metis_tac [LENGTH_NIL, DECIDE ``SUC v ≠ 0``] >>
Cases_on `elems2` >>
fs []) >>
metis_tac [lupdate_append2, APPEND_ASSOC])) >>
(* Call the inner loop from the outer loop *)
xlet `POSTv u.
SEP_EXISTS sorted_vs'.
ARRAY arr_v (sorted_vs' ++ TL elem_vs2) *
&(?sorted'.
LENGTH sorted' = LENGTH sorted_vs' ∧
PERM (ZIP (sorted', sorted_vs'))
(ZIP (elems1++[HD elems2], elem_vs1++[cc_v])) ∧
SORTED (\x y. ¬(cmp y x)) sorted')`
>- (
xapp >>
xsimpl >>
MAP_EVERY qexists_tac [`[]`, `elem_vs1`, `[]`, `elems1`, `HD elem_vs2`] >>
fs [INT_def] >>
simp [arith] >>
Cases_on `elem_vs2` >>
fs []) >>
xlet `POSTv p1_v. ARRAY arr_v (sorted_vs' ++ TL elem_vs2) *
&INT (&LENGTH elem_vs1) p1_v`
>- (
xapp >>
xsimpl >>
fs [INT_def] >>
simp [arith]) >>
(* Call the outer loop recursively *)
xapp >>
xsimpl >>
MAP_EVERY qexists_tac [`TL elems2`, `sorted'`, `TL elem_vs2`, `sorted_vs'`] >>
simp [] >>
imp_res_tac LIST_REL_LENGTH >>
drule PERM_LENGTH >>
simp [] >>
strip_tac >>
`LENGTH sorted_vs' ≠ 0` by decide_tac >>
fs [LENGTH_NIL] >>
rw [LENGTH_TL]
>- (
irule list_rel_perm >>
simp [] >>
ONCE_REWRITE_TAC [PERM_SYM] >>
qexists_tac `elems1 ++ [HD elems2]` >>
qexists_tac `elem_vs1 ++ [HD elem_vs2]` >>
simp [] >>
Cases_on `elems2` >>
Cases_on `elem_vs2` >>
fs [])
>- (
Cases_on `elems2` >>
Cases_on `elem_vs2` >>
fs []) >>
qexists_tac `elems'` >>
rw [] >>
irule PERM_TRANS >>
qexists_tac `ZIP (sorted' ++ TL elems2,sorted_vs' ++ TL elem_vs2)` >>
simp [] >>
`PERM (ZIP (sorted',sorted_vs') ++ ZIP (TL elems2, TL elem_vs2))
(ZIP (elems1 ++ [HD elems2],elem_vs1 ++ [HD elem_vs2]) ++ ZIP (TL elems2, TL elem_vs2))`
by simp [PERM_APPEND_IFF] >>
pop_assum mp_tac >>
simp [ZIP_APPEND, LENGTH_TL] >>
Cases_on `elems2` >>
Cases_on `elem_vs2` >>
fs [] >>
metis_tac [APPEND, APPEND_ASSOC]) >>
(* The initial stuff *)
xlet `POSTv l_v. ARRAY arr_v elem_vs * &INT (&LENGTH elem_vs) l_v`
>- (
xapp >>
xsimpl >>
fs [NUM_def, INT_def]) >>
xlet `POSTv b_v. ARRAY arr_v elem_vs * &BOOL (LENGTH elem_vs = 0) b_v`
>- (
xapp_spec eq_num_v_thm >>
xsimpl >>
fs [INT_def, NUM_def]) >>
xif
>- (
xret >>
xsimpl >>
qexists_tac `elems` >>
rw [] >>
Cases_on `elems` >>
fs [] >>
rfs [])
>- (
xapp >>
xsimpl >>
MAP_EVERY qexists_tac [`TL elems`, `[HD elems]`, `TL elem_vs`, `[HD elem_vs]`] >>
simp [] >>
Cases_on `elems` >>
Cases_on `elem_vs` >>
fs [] >>
rw [] >>
qexists_tac `elems'` >>
simp [])
QED
val _ = export_theory ();