File: csort.lisp

package info (click to toggle)
acl2 7.2dfsg-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 198,968 kB
  • ctags: 182,300
  • sloc: lisp: 2,415,261; ansic: 5,675; perl: 5,577; xml: 3,576; sh: 3,255; cpp: 2,835; makefile: 2,440; ruby: 2,402; python: 778; ml: 763; yacc: 709; csh: 355; php: 171; lex: 162; tcl: 44; java: 24; asm: 23; haskell: 17
file content (590 lines) | stat: -rw-r--r-- 16,881 bytes parent folder | download | duplicates (6)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
; Copyright (C) 1997  Computational Logic, Inc.
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.
; Authors: Bishop Brock and J Strother Moore

; This book is the ACL2 script accompanying the paper

; A Mechanically Checked Proof of a Comparator Sort Algorithm
;         Bishop Brock and J Strother Moore

; Abstract

; We describe a mechanically checked correctness proof for the
; comparator sort algorithm underlying a microcode program in a
; commercially designed digital signal processing chip.  The abstract
; algorithm uses an unlimited number of systolic comparator modules to
; sort a stream of data.  In addition to proving that the algorithm
; produces an ordered permutation of its input, we prove two theorems
; that are important to verifying the microcode implementation.  These
; theorems describe how positive and negative ``infinities'' can be
; streamed into the array of comparators to achieve certain effects.
; Interesting generalizations are necessary in order to prove these
; theorems inductively.  The mechanical proofs were carried out with
; the ACL2 theorem prover.  We find these proofs both mathematically
; interesting and illustrative of the kind of mathematics that must be
; done to verify software.

; This file is divided into six ``chapters'' by comments to that effect.
; See the above paper for a tour through this file.

; The basis for this work was done at Computational Logic, Inc. under
; a contract to model the Motorola CAP Digitial Signal Processor.  The
; original version of this script was distributed as the ACL2 book
; v2-3/books/cli-misc/comparator-sort.lisp, which was copyrighted by
; Computational Logic, Inc., 1997.  The present script was developed
; as a simplified version of that one.

(in-package "ACL2")

; We start with basic facts unrelated to comparator sorting.

; Chapter 1:  Elementary Stuff

; List Processing

(defun firstn (n l)
  "The sublist of L consisting of its first N elements."
  (declare (xargs :guard (and (true-listp l)
                              (integerp n)
                              (<= 0 n))))
  (cond ((endp l) nil)
        ((zp n) nil)
        (t (cons (car l)
                 (firstn (1- n) (cdr l))))))

(defthm member-equal-car-firstn
  (implies (and (integerp n)
		(<= 1 n)
		(consp lst))
	   (member-equal (car (firstn n lst)) lst)))

(defthm firstn-too-long
  (implies (and (true-listp lst)
                (integerp n)
                (<= (len lst) n))
           (equal (firstn n lst) lst)))

(defun repeat (n x)
  (cond ((zp n) nil)
        (t (cons x (repeat (1- n) x)))))

(defun evnp (n)
  (cond ((zp n) t)
        ((zp (1- n)) nil)
        (t (evnp (1- (1- n))))))

(defun rev (x)
  (cond ((endp x) nil)
        (t (append (rev (cdr x)) (list (car x))))))

(defthm car-append
  (equal (car (append a b))
         (if (consp a) (car a) (car b))))

(defthm assoc-of-append
  (equal (append (append a b) c)
         (append a (append b c))))

(defthm true-listp-append-rewrite
  (equal (true-listp (append a b))
         (true-listp b)))

(defthm len-append
  (equal (len (append x y))
         (+ (len x) (len y))))


; Arithmetic.

(defthm +-constant-folding
  (implies (syntaxp (and (quotep x) (quotep y)))
	   (equal (+ x (+ y z)) (+ (+ x y) z))))

; Facts about Perm

(defun delete-equal (x lst)
  (cond ((endp lst) nil)
        ((equal x (car lst)) (cdr lst))
        (t (cons (car lst) (delete-equal x (cdr lst))))))

(defun perm (lst1 lst2)
  (cond ((endp lst1) (endp lst2))
        ((member-equal (car lst1) lst2)
         (perm (cdr lst1) (delete-equal (car lst1) lst2)))
        (t nil)))

(defthm perm-reflexive
  (perm x x))

(defthm perm-cons
  (implies (member-equal a x)
           (equal (perm x (cons a y))
                  (perm (delete-equal a x) y)))
  :hints (("Goal" :induct (perm x y))))

(defthm perm-symmetric
  (implies (perm x y) (perm y x)))

(defthm member-equal-delete-equal
  (implies (member-equal a (delete-equal b x))
           (member-equal a x)))

(defthm perm-member-equal
  (implies (and (perm x y)
                (member-equal a x))
           (member-equal a y)))

(defthm member-equal-delete-equal2
  (implies (and (member-equal a x)
                (not (equal a b)))
           (member-equal a (delete-equal b x))))

(defthm comm-delete-equal
  (equal (delete-equal a (delete-equal b x))
         (delete-equal b (delete-equal a x))))

(defthm perm-delete-equal
  (implies (perm x y)
           (perm (delete-equal a x) (delete-equal a y))))

(defthm perm-transitive
  (implies (and (perm x y) (perm y z)) (perm x z)))

(defequiv perm)

(defcong perm perm (cons x y) 2)

(defcong perm iff (member-equal x y) 2)

(defthm atom-perm
  (implies (not (consp x)) (perm x nil))
  :rule-classes :forward-chaining)

; Apologia: The following lemma wasn't necessary until we fixed the
; controller-alist bug.  Of course, this is a pretty reasonable lemma
; to have.

(defthm member-equal-append
  (iff (member-equal e (append x y))
       (or (member-equal e x)
           (member-equal e y))))

(defcong perm perm (append x y) 1)

(defcong perm perm (append x y) 2)

(defthm perm-append-cons
  (perm (append x (cons i y))
        (cons i (append x y))))

(defcong perm equal (len x) 1)

(defthm perm-rev
  (perm (rev x) x))

(defthm perm-append-nil
  (perm (append x nil) x))

(defthm append-repeat
  (equal (append (repeat n x) (list x))
         (cons x (repeat n x))))

(defthm rev-repeat
  (equal (rev (repeat n x)) (repeat n x)))

(defthm len-repeat
  (equal (len (repeat n x)) (nfix n)))

(defthm consp-repeat
  (equal (consp (repeat n x))
         (not (zp n)))
  :hints (("Goal" :expand (REPEAT 1 X))))

(defthm car-repeat
  (equal (car (repeat n x))
         (if (zp n) nil x))
  :hints (("Goal" :expand (REPEAT 1 X))))

; Chapter 2: All-gte, Orderings, etc

(defun data (pair)
  (cdr pair))

(in-theory (disable data))

(defun ordered (lst)
  (cond ((endp lst) t)
        ((endp (cdr lst)) t)
        ((>= (data (car lst)) (data (cadr lst)))
         (ordered (cdr lst)))
        (t nil)))

(defun all-gte (pair lst)
  (cond ((endp lst) t)
        ((and (>= (data pair) (data (car lst)))
              (all-gte pair (cdr lst))))
        (t nil)))

(defthm all-gte-delete-equal-1
  (implies (all-gte pair lst)
           (all-gte pair (delete-equal x lst))))

(defthm all-gte-delete-equal-2
  (implies (and (all-gte pair (delete-equal x lst))
                (>= (data pair) (data x)))
           (all-gte pair lst)))

(encapsulate nil
             (local (defthm lemma
                      (implies (and (perm lst lst-equiv)
                                    ;; Added for mod to ACL2 v2-8 that does
                                    ;; better matching for calls of equivalence
                                    ;; relations against the current context:
                                    (syntaxp (not (term-order lst lst-equiv)))
                                    (all-gte pair lst))
                               (all-gte pair lst-equiv))))
             (defcong perm equal (all-gte pair lst) 2))

(defthm all-gte-implies-gte-member-equal
  (implies (and (all-gte pair1 a)
                (member-equal pair2 a))
           (<= (data pair2) (data pair1)))
  :rule-classes :linear)

(defthm gte-car-firstn
  (implies (and (all-gte pair a)
		(perm a b)
		(integerp n)
		(<= 1 n)
		(consp b))
	   (<= (data (car (firstn n b))) (data pair)))
  :rule-classes nil)

(defthm all-gte-helper
  (implies (and (all-gte pair1 lst)
                (<= (data pair1) (data pair2)))
           (all-gte pair2 lst)))

(defun all-all-gte (lst1 lst2)
  (cond ((endp lst1) t)
        (t (and (all-gte (car lst1) lst2)
                (all-all-gte (cdr lst1) lst2)))))

(defcong perm equal (all-all-gte lst1 lst2) 1)

(defcong perm equal (all-all-gte lst1 lst2) 2)

(defthm all-all-gte-cdr
  (implies (all-all-gte lst acc)
           (all-all-gte lst (cdr acc))))

(defthm ordered-cons
  (implies (all-gte pair1 s)
           (equal (ordered (cons pair1 s))
                  (ordered s))))

(defthm all-gte-append-cons
  (equal (all-gte pair1 (append s (list pair2)))
         (and (all-gte pair1 s)
              (>= (data pair1) (data pair2)))))

(defthm ordered-append
  (equal (ordered (append lst1 lst2))
         (and (ordered lst1)
              (ordered lst2)
              (all-all-gte lst1 lst2))))

(defthm ordered-cons-append-cons
  (implies (and (all-gte pair1 s)
                (>= (data pair1) (data pair2))
                (all-all-gte s (list pair2))
                (ordered s))
           (ordered (CONS pair1 (APPEND S (list pair2)))))
  :rule-classes nil)

(defthm all-all-gte-cons
  (implies (syntaxp (not (equal acc ''nil)))
           (equal (all-all-gte s (cons pair acc))
                  (and (all-all-gte s (list pair))
                       (all-all-gte s acc)))))

(defthm all-all-gte-append1
  (equal (all-all-gte s (append lst1 lst2))
         (and (all-all-gte s lst1)
              (all-all-gte s lst2)))
  :hints (("Goal" :induct (append lst1 lst2))))

(defthm all-all-gte-append2
  (equal (all-all-gte (append lst1 lst2) lst3)
         (and (all-all-gte lst1 lst3)
              (all-all-gte lst2 lst3))))

(defthm all-all-gte-nil
  (all-all-gte lst nil))

(defthm ordered-repeat
  (ordered (repeat n x)))

(defthm all-all-gte-repeat
  (equal (all-all-gte (repeat n maxpair) acc)
         (or (zp n)
             (all-gte maxpair acc)))
  :hints (("Goal" :induct (repeat n maxpair))))

(defthm all-all-gte-repeat-2
  (implies (all-all-gte lst (list minpair))
           (all-all-gte lst (repeat n minpair))))

; Chapter 3: comparator sort.

(defun max-pair (pair1 pair2)
  (cond ((<= (data pair1) (data pair2)) pair2)
        (t pair1)))

(defun min-pair (pair1 pair2)
  (cond ((<= (data pair1) (data pair2)) pair1)
        (t pair2)))

(defun cstep (acc)
  (cond ((endp acc) nil)
        ((endp (cdr acc)) acc)
        (t (cons (max-pair (cadr acc) (car acc))
                 (cons (min-pair (cadr acc) (car acc))
                       (cstep (cddr acc)))))))

(defun cfeed (lst acc)
  (cond ((endp lst) acc)
        (t (cfeed (cdr lst)
                  (cstep (cons (car lst) acc))))))

(defun cdrain (n acc)
  (cond ((zp n) acc)
        (t (cons (car acc)
                 (cdrain (1- n) (cstep (cdr acc)))))))

(defun csort (lst)
  (cdrain (len lst)
          (cfeed lst nil)))

; Chapter 4:  Basic Properties of the Subroutines

(defthm perm-cstep
  (perm (cstep acc) acc))

(defthm perm-cfeed
  (perm (cfeed lst acc) (append lst acc))
  :hints (("Goal" :induct (cfeed lst acc))))

(defthm perm-cdrain
  (implies (<= n (len acc))
           (perm (cdrain n acc) acc)))

; One might wonder why we need the next lemma (and several others like
; it).  The proof of this lemma is obvious given that perm is a congruence
; relation for len.  So one is tempted to conclude that we'll prove it
; whenever we need it.  Wrong.

; The lemma is used to relieve a hyp of the form (<= ... (len a)).  In
; the application, a is bound to (cstep acc), which was rewritten
; under equiv EQUAL.  If we were to retrieve the binding of a and
; rewrite it under equiv PERM, we would simplify (len a) to (len acc).
; But we don't rewrite the binding.  So we have to prove a rewrite
; rule.

(defthm len-cstep
  (equal (len (cstep acc)) (len acc)))

(defthm len-cfeed
  (equal (len (cfeed lst acc))
         (+ (len lst) (len acc))))

(defthm true-listp-cstep
  (implies (true-listp acc)
           (true-listp (cstep acc)))
  :hints (("Goal" :induct (cstep acc))))

(defthm true-listp-cfeed
  (implies (true-listp acc)
           (true-listp (cfeed lst acc))))

(defthm true-listp-cdrain
  (implies (true-listp acc)
           (true-listp (cdrain n acc))))

(defthm len-cdrain
  (implies (and (integerp n)
                (<= n (len acc)))
           (equal (len (cdrain n acc))
                  (len acc))))

(defthm cfeed-opener
  (equal (cfeed (cons pair lst) acc)
         (cfeed lst (cstep (cons pair acc)))))

(defthm cstep-append1
  (implies (evnp (len a))
           (equal (cstep (append a b))
                  (append (cstep a)
                          (cstep b))))
  :rule-classes nil
  :hints (("Goal" :induct (cstep a))))

(defthm cstep-noop
  (implies (and (true-listp acc)
                (ordered acc))
           (equal (cstep acc) acc))
  :hints (("Goal" :induct (cstep acc))))

(defthm cstep-append2
  (implies (and (true-listp b)
                (ordered b)
                (all-all-gte a b))
           (equal (cstep (append a b))
                  (append (cstep a) b)))
  :hints (("Goal" :induct (cstep a))))




; Chapter 5:  Invariant work

(defun phi (acc)
  (cond ((endp acc) t)
        ((endp (cdr acc)) t)
        (t (and (all-gte (car acc) (cdr acc))
                (phi (cddr acc))))))

(defthm all-gte-cstep
  (implies (all-gte x acc)
           (all-gte x (cstep acc)))
  :hints (("Goal" :induct (cstep acc))))

(defthm phi-cstep
  (implies (phi (cdr acc))
           (phi (cstep acc))))

(defthm phi-cons
  (equal (phi (cons pair acc))
         (or (endp acc)
             (and (all-gte pair acc)(phi (cdr acc))))))

(defthm phi-cfeed
  (implies (phi acc)
           (phi (cfeed lst acc)))
  :hints (("Goal" :induct (cfeed lst acc)
           :in-theory (disable cstep))))

; Chapter 6:  The Main Theorems

(defthm perm-csort
  (perm (csort acc) acc))


; Here is the proof that csort orders.

(defthm ordered-firstn-cdrain
  (implies (and (integerp n)
		(<= n (len acc))
		(phi acc))
	   (ordered (firstn (+ 2 n) (cdrain n acc))))
  :hints
  (("Subgoal *1/5"
    :use (:instance gte-car-firstn
		    (a (cdr acc))
		    (b (cDRAIN (+ -1 N)
                               (cSTEP (CDR ACC))))
		    (pair (car acc))
		    (n (+ 1 n))))
   ("Subgoal *1/1"
    :expand ((FIRSTN (+ 2 N) ACC)
	     (FIRSTN (+ 1 N) (cdr ACC))))))

(defthm ordered-csort
  (ordered (csort lst))
  :hints
  (("Goal" :use (:instance ordered-firstn-cdrain
                           (n (len lst))
                           (acc (cfeed lst nil))))))

; Here is the proof of the positive infinity property.  The whole
; key is the following rewrite rule and the induction hint.

(defthm bridge
  (implies
   (and (all-gte pair1 s)
        (>= (data pair1) (data pair2))
        (all-all-gte s (list pair2))
        (evnp (len s))
        (ordered s))
   (equal
    (cstep (cons pair1 (append s (cons pair2 acc))))
    (cons pair1 (append s (cons pair2 (cstep acc))))))
  :hints
  (("Goal"
    :use ((:instance cstep-append1
                     (a (cons pair1 (append s (list pair2))))
                     (b acc))
          (:instance ordered-cons-append-cons
                     (pair1 pair1)
                     (s s)
                     (pair2 pair2))))))

(defun positive-infinity-hint (lst s acc)
  (cond
   ((endp lst) (list s acc))
   (t (positive-infinity-hint (cdr lst)
                              (cons (car lst)
                                    (append s (list (car acc))))
                              (cstep (cdr acc))))))

(defthm positive-infinity-gen
  (implies (and (<= (len lst) (len acc))
                (evnp (len s))
                (ordered s)
                (ordered (rev lst))
                (phi acc)
                (all-all-gte lst s)
                (all-all-gte lst acc)
                (all-all-gte s acc))
           (equal (cfeed lst (append s acc))
                  (append (rev lst)
                          s
                          (cdrain (len lst) acc))))
  :hints
  (("Goal" :induct (positive-infinity-hint lst s acc)))
  :rule-classes nil)

(defthm positive-infinity
  (implies (and (integerp n)
                (<= n (len acc))
                (all-gte maxpair acc)
                (phi acc))
           (equal (cfeed (repeat n maxpair) acc)
                  (append (repeat n maxpair)
                          (cdrain n acc))))
  :hints (("Goal"
           :use (:instance positive-infinity-gen
                                  (lst (repeat n maxpair))
                                  (s nil)
                                  (acc acc)))))
; The negative infinity property is easier.

(defthm negative-infinity-gen
  (implies (and (true-listp acc)
                (all-all-gte lst acc)
                (all-all-gte s acc)
                (ordered acc))
           (equal (cfeed lst (append s acc))
                  (append (cfeed lst s) acc)))
  :hints
  (("Goal" :induct (cfeed lst s)))
  :rule-classes nil)

(defthm negative-infinity
  (implies (all-all-gte lst (list minpair))
           (equal (cfeed lst (repeat n minpair))
                  (append (cfeed lst nil) (repeat n minpair))))
  :hints (("Goal" :use (:instance negative-infinity-gen
                                  (lst lst)
                                  (s nil)
                                  (acc (repeat n minpair))))))