File: preliminary-material.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (977 lines) | stat: -rw-r--r-- 30,745 bytes parent folder | download | duplicates (5)
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
; This file contains all the material prepared for the preliminary paper,
; main.pdf

#|
(defpkg  "M1"
  (set-difference-equal
   (union-eq '(defp measure l< delta preprocess fast-loop fast correct-loop correct
                pairlis-x2)
         (union-eq *common-lisp-symbols-from-main-lisp-package*
                   *acl2-exports*))
   '(PC PROGRAM ID PUSH POP STEP COMPILE)))

(certify-book "preliminary-material" 1)
jsm
|#

(in-package "M1")

(defun fact (n)
  (if (zp n)
      1
      (* n (fact (- n 1)))))

(defthm nth-nil
  (equal (nth n nil) nil))

(defthm acl2-count-nth
  (implies (consp x)
           (< (acl2-count (nth n x))
              (acl2-count x)))
  :rule-classes :linear)

(defun push (obj stack) (cons obj stack))
(defun top (stack) (car stack))
(defun pop (stack) (cdr stack))

(defun opcode (inst) (nth 0 inst))
(defun arg1 (inst) (nth 1 inst))
(defun arg2 (inst) (nth 2 inst))
(defun arg3 (inst) (nth 3 inst))

(defun make-state (pc locals stack program)
     (cons pc
           (cons locals
                 (cons stack
                       (cons program
                             nil)))))

(defun pc (s) (nth 0 s))
(defun locals (s) (nth 1 s))
(defun stack (s) (nth 2 s))
(defun program (s) (nth 3 s))

(defun next-inst (s) (nth (pc s) (program s)))

(defun execute-PUSH (inst s)
  (make-state (+ 1 (pc s))
              (locals s)
              (push (arg1 inst) (stack s))
              (program s)))

(defun execute-LOAD (inst s)
  (make-state (+ 1 (pc s))
              (locals s)
              (push (nth (arg1 inst)
                         (locals s))
                    (stack s))
              (program s)))

(defun execute-ADD (inst s)
  (declare (ignore inst))
  (make-state (+ 1 (pc s))
              (locals s)
              (push (+ (top (pop (stack s)))
                       (top (stack s)))
                    (pop (pop (stack s))))
              (program s)))

(defun execute-STORE (inst s)
  (make-state (+ 1 (pc s))
              (update-nth (arg1 inst) (top (stack s)) (locals s))
              (pop (stack s))
              (program s)))

(defun execute-SUB (inst s)
  (declare (ignore inst))
  (make-state (+ 1 (pc s))
              (locals s)
              (push (- (top (pop (stack s)))
                       (top (stack s)))
                    (pop (pop (stack s))))
              (program s)))

(defun execute-MUL (inst s)
  (declare (ignore inst))
  (make-state (+ 1 (pc s))
              (locals s)
              (push (* (top (pop (stack s)))
                       (top (stack s)))
                    (pop (pop (stack s))))
              (program s)))

(defun execute-GOTO (inst s)
  (make-state (+ (arg1 inst) (pc s))
              (locals s)
              (stack s)
              (program s)))

(defun execute-IFLE (inst s)
  (make-state (if (<= (top (stack s)) 0)
                  (+ (arg1 inst) (pc s))
                (+ 1 (pc s)))
              (locals s)
              (pop (stack s))
              (program s)))

; Boyer-Moore instructions

(defun execute-IFLT (inst s)
  (make-state (if (< (top (stack s)) 0)
                  (+ (arg1 inst) (pc s))
                (+ 1 (pc s)))
              (locals s)
              (pop (stack s))
              (program s)))

(defun execute-IFNE (inst s)
  (make-state (if (not (equal (top (stack s)) 0))
                  (+ (arg1 inst) (pc s))
                (+ 1 (pc s)))
              (locals s)
              (pop (stack s))
              (program s)))

(defun execute-IFANE (inst s)
  (make-state (if (not (equal (top (pop (stack s)))
                              (top (stack s))))
                  (+ (arg1 inst) (pc s))
                (+ 1 (pc s)))
              (locals s)
              (pop (pop (stack s)))
              (program s)))

(defun execute-ALOAD (inst s)
  (declare (ignore inst))
  (make-state (+ 1 (pc s))
              (locals s)
              (push (if (stringp (top (pop (stack s))))
                        (char-code (char (top (pop (stack s)))
                                         (top (stack s))))
                      (nth (top (stack s))
                           (top (pop (stack s)))))
                    (pop (pop (stack s))))
              (program s)))

(defun do-inst (inst s)
  (if (equal (opcode inst) 'PUSH)
      (execute-PUSH  inst s)
    (if (equal (opcode inst) 'LOAD)
        (execute-LOAD  inst s)
      (if (equal (opcode inst) 'STORE)
          (execute-STORE  inst s)
        (if (equal (opcode inst) 'ADD)
            (execute-ADD   inst s)
          (if (equal (opcode inst) 'SUB)
              (execute-SUB   inst s)
            (if (equal (opcode inst) 'MUL)
                (execute-MUL   inst s)
              (if (equal (opcode inst) 'GOTO)
                  (execute-GOTO   inst s)
                (if (equal (opcode inst) 'IFLE)
                    (execute-IFLE   inst s)
                  (if (equal (opcode inst) 'IFLT)
                      (execute-IFLT   inst s)
                    (if (equal (opcode inst) 'IFNE)
                        (execute-IFNE   inst s)
                      (if (equal (opcode inst) 'IFANE)
                          (execute-IFANE  inst s)
                        (if (equal (opcode inst) 'ALOAD)
                            (execute-ALOAD   inst s)
                          s)))))))))))))

(defun step (s)
  (do-inst (next-inst s) s))

(defun haltedp (s)
  (equal s (step s)))

(defun run (sched s)
  (if (endp sched)
      s
    (run (cdr sched) (step s))))

(defconst *ifact-program*

;    M1              M1      JVM    JVM            Java
;    code            pc       pc   bytecode

 '((PUSH 1)     ;   0         0   bipush_1 
   (STORE 1)    ;   1         1   istore_1      a = 1;
   (LOAD 0)     ;   2         2   iload_0       while (n>0){
   (IFLE 10)    ;   3         3   ifle    17
   (LOAD 0)     ;   4         6   iload_0
   (LOAD 1)     ;   5         7   iload_1  
   (MUL)        ;   6         8   imul      
   (STORE 1)    ;   7         9   istore_1        a = n*a;
   (LOAD 0)     ;   8        10   iload_0  
   (PUSH 1)     ;   9        11   bipush_1 
   (SUB)        ;  10        12   isub      
   (STORE 0)    ;  11        13   istore_0        n = n-1;
   (GOTO -10)   ;  12        14   goto    2       }
   (LOAD 1)     ;  13        17   iload_1
   (RETURN)     ;  14        18   ireturn        return a;  
   ))

(defun repeat (th n)
     (if (zp n)
         nil
       (cons th (repeat th (- n 1)))))

(defun ifact-loop-sched (n)
  (if (zp n)
      (repeat 0 4)
    (append (repeat 0 11)
            (ifact-loop-sched (- n 1)))))

(defun ifact-sched (n)
  (append (repeat 0 2)
          (ifact-loop-sched n)))

(defthm factorial-5-example
  (equal (top
          (stack
           (run
            (ifact-sched 5)
            (make-state
             0
             '(5 0)
             nil
             *ifact-program*))))
         (fact 5))
  :rule-classes nil)

(defun collect-at-end (x e)
  (if (member e x)
      x
    (append x (cons e nil))))

(defun collect-vars-in-expr (vars expr)
  (if (atom expr)
      (if (symbolp expr)
          (collect-at-end vars expr)
        vars)
    (collect-vars-in-expr
     (collect-vars-in-expr vars
                           (nth 0 expr))
     (nth 2 expr))))

(mutual-recursion

 (defun collect-vars-in-stmt* (vars stmt-list)
   (if (endp stmt-list)
       vars
     (collect-vars-in-stmt*
      (collect-vars-in-stmt vars (car stmt-list))
      (cdr stmt-list))))

 (defun collect-vars-in-stmt (vars stmt)
   (if (equal (nth 1 stmt) '=)
       (collect-vars-in-expr
        (collect-at-end vars (nth 0 stmt))
        (nth 2 stmt))
     (if (equal (nth 0 stmt) 'WHILE)
         (collect-vars-in-stmt*
          (collect-vars-in-expr vars (nth 1 stmt))
          (cdr (cdr stmt)))
       (if (equal (nth 0 stmt) 'RETURN)
           (collect-vars-in-expr vars (nth 1 stmt))
         vars))))
)

(defun index (vars var)
  (if (endp vars)
      0
    (if (equal var (car vars))
        0
      (+ 1 (index (cdr vars) var)))))

(defun OP! (op)
  (if (equal op '+)
      '((ADD))
    (if (equal op '-)
        '((SUB))
      (if (equal op '*)
          '((MUL))
        '((ILLEGAL))))))

(defun LOAD! (vars var)
  (cons (cons 'LOAD (cons (index vars var) nil))
        nil))

(defun PUSH! (n)
  (cons (cons 'PUSH (cons n nil))
        nil))

(defun expr! (vars expr)
  (if (atom expr)
      (if (symbolp expr)
          (LOAD! vars expr)
        (PUSH! expr))
    (append (expr! vars (nth 0 expr))
            (append (expr! vars (nth 2 expr))
                    (OP! (nth 1 expr))))))

(defun IFLE! (offset)
  (cons (cons 'IFLE (cons offset nil))
        nil))

(defun GOTO! (offset)
  (cons (cons 'GOTO (cons offset nil))
        nil))

(defun while! (test-code body-code)
  (append test-code
          (append (IFLE! (+ 2 (len body-code)))
                  (append body-code
                          (GOTO! (- (+ (len test-code)
                                       1
                                       (len body-code))))))))

(defun test! (vars test)
  (if (equal (nth 1 test) '>)
      (if (equal (nth 2 test) 0)
          (expr! vars (nth 0 test))
        (append (expr! vars (nth 0 test))
                (append (expr! vars (nth 2 test))
                        '((SUB)))))
    '((ILLEGAL))))

(defun STORE! (vars var)
  (cons (cons 'STORE (cons (index vars var) nil))
        nil))

(mutual-recursion

 (defun stmt*! (vars stmt-list)
   (if (endp stmt-list)
       nil
     (append (stmt! vars (car stmt-list))
             (stmt*! vars (cdr stmt-list)))))

 (defun stmt! (vars stmt)
   (if (equal (nth 1 stmt) '=)
       (append (expr! vars (nth 2 stmt))
               (STORE! vars (nth 0 stmt)))
     (if (equal (nth 0 stmt) 'WHILE)
         (while!
          (test! vars (nth 1 stmt))
          (stmt*! vars (cdr (cdr stmt))))
       (if (equal (nth 0 stmt) 'RETURN)
           (append (expr! vars (nth 1 stmt))
                   '((RETURN)))
         '((ILLEGAL))))))
 )

(defun compile (formals stmt-list)
  (stmt*! (collect-vars-in-stmt* formals stmt-list)
          stmt-list))

(defthm example-compilation-1
  (equal (compile '(n)
                  '((a = 1)
                    (while (n > 0)
                      (a = (n * a))
                      (n = (n - 1)))
                    (return a)))
         '((PUSH 1)
           (STORE 1)
           (LOAD 0)
           (IFLE 10)
           (LOAD 0)
           (LOAD 1)
           (MUL)
           (STORE 1)
           (LOAD 0)
           (PUSH 1)
           (SUB)
           (STORE 0)
           (GOTO -10)
           (LOAD 1)
           (RETURN)))
  :rule-classes nil)

(include-book "arithmetic/top-with-meta" :dir :system)

(defthm stacks
  (and (equal (top (push x s)) x)
       (equal (pop (push x s)) s)

       (equal (top (cons x s)) x)
       (equal (pop (cons x s)) s)))

(in-theory (disable push top pop))

(defthm states
  (and (equal (pc (make-state pc locals stack program)) pc)
       (equal (locals
               (make-state pc locals stack program))
              locals)
       (equal (stack
               (make-state pc locals stack program))
              stack)
       (equal (program
               (make-state pc locals stack program))
              program)

       (equal (pc (cons pc x)) pc)
       (equal (locals (cons pc (cons locals x))) locals)
       (equal (stack
               (cons pc (cons locals (cons stack x))))
              stack)
       (equal (program
               (cons pc 
                (cons locals (cons stack (cons program x)))))
              program)))

(in-theory (disable make-state pc locals stack program))

(defthm step-opener
  (implies (consp (next-inst s))
           (equal (step s)
                  (do-inst (next-inst s) s))))

(in-theory (disable step))

(defthm run-opener
  (and (equal (run nil s) s)
       (equal (run (cons th sched) s)
              (run sched (step s)))))

(defthm run-append
  (equal (run (append a b) s)
         (run b (run a s))))

(defthm nth-add1!
  (implies (natp n)
           (equal (nth (+ 1 n) list)
                  (nth n (cdr list)))))

(defthm update-nth-add1!
  (implies (natp n)
           (equal (update-nth (+ 1 n) v x)
                  (cons (car x) (update-nth n v (cdr x))))))

(defun ifact (n a)
  (if (zp n)
      a
    (ifact (- n 1) (* n a))))

(defthm ifact-loop-lemma
  (implies (and (natp n)
                (natp a))
           (equal (run (ifact-loop-sched n)
                       (make-state 2
                                   (cons n (cons a nil))
                                   stack
                                   *ifact-program*))
                  (make-state 14
                              (cons 0 (cons (ifact n a) nil))
                              (push (ifact n a) stack)
                              *ifact-program*))))

(defthm ifact-lemma
  (implies (natp n)
           (equal (run (ifact-sched n)
                       (make-state 0
                                   (cons n (cons a nil))
                                   stack
                                   *ifact-program*))
                  (make-state 14
                              (cons 0 (cons (ifact n 1) nil))
                              (push (ifact n 1) stack)
                              *ifact-program*))))

(in-theory (disable ifact-sched))

(defthm ifact-is-factorial
  (implies (and (natp n)
                (natp a))
           (equal (ifact n a)
                  (* (fact n) a))))

(defthm ifact-correct
  (implies (natp n)
           (equal (run (ifact-sched n)
                       (make-state 0
                                   (cons n (cons a nil)) 
                                   stack
                                   *ifact-program*))
                  (make-state 14
                              (cons 0 (cons (fact n) nil))
                              (push (fact n) stack)
                              *ifact-program*))))

(defthm ifact-correct-corollary-1
  (implies (natp n)
           (haltedp (run (ifact-sched n)
                         (make-state 0
                                     (cons n (cons a nil))
                                     stack
                                     *ifact-program*)))))

(defthm ifact-correct-corollary-2
  (implies (natp n)
           (equal (top
                   (stack
                    (run (ifact-sched n)
                         (make-state 0
                                     (cons n (cons a nil))
                                     stack
                                     *ifact-program*))))
                  (fact n))))

(defthm ifact-correct-corollary-3
  (implies (natp n)
           (equal (top
                   (stack
                    (run (ifact-sched n)
                         (make-state 0
                                     (cons n (cons a nil))
                                     stack
                                     (compile
                                      '(n)
                                      '((a = 1)
                                        (while (n > 0)
                                          (a = (n * a))
                                          (n = (n - 1)))
                                        (return a)))))))
                  (fact n))))

(include-book "misc/defp" :dir :system)

(defmacro defspec (name prog inputs pre-pc post-pc annotation-alist)
  (let ((Inv
         (intern-in-package-of-symbol
          (concatenate 'string (symbol-name name) "-INV")
          'run))
        (Inv-def
         (intern-in-package-of-symbol
          (concatenate 'string (symbol-name name) "-INV$DEF")
          'run))
        (Inv-opener
         (intern-in-package-of-symbol
          (concatenate 'string (symbol-name name) "-INV-OPENER")
          'run))
        (Inv-step
         (intern-in-package-of-symbol
          (concatenate 'string (symbol-name name) "-INV-STEP")
          'run))
        (Inv-run
         (intern-in-package-of-symbol
          (concatenate 'string (symbol-name name) "-INV-RUN")
          'run))
        (Correctness
         (intern-in-package-of-symbol
          (concatenate 'string "PARTIAL-CORRECTNESS-OF-PROGRAM-"
                       (symbol-name name))
          'run)))
    `(progn
       (defp ,Inv (,@inputs s)
         (if (member (pc s)
                     ',(strip-cars annotation-alist))
             (and (equal (program s)
                         ,prog)
                  (case (pc s)
                    ,@annotation-alist))
           (,Inv ,@inputs (step s))))
       (defthm ,Inv-opener
         (implies (and (equal pc (pc s))
                       (syntaxp (quotep pc))
                       (not
                        (member pc
                                ',(strip-cars annotation-alist))))
                  (equal (,Inv ,@inputs s)
                         (,Inv ,@inputs (step s)))))
       (defthm ,Inv-step
         (implies (,Inv ,@inputs s)
                  (,Inv ,@inputs (step s))))
       (defthm ,Inv-run
         (implies (,Inv ,@inputs s)
                  (,Inv ,@inputs (run sched s)))
         :rule-classes nil
         :hints (("Goal" :in-theory (e/d (run)(,Inv-def)))))
       (defthm ,Correctness
         (let* ((sk (run sched s0)))
           (implies
            (and (let ((s s0)) ,(cadr (assoc pre-pc annotation-alist)))
                 (equal (pc s0) ,pre-pc)
                 (equal (locals s0) (list* ,@inputs any))
                 (equal (program s0) ,prog)
                 (equal (pc sk) ,post-pc))
            (let ((s sk)) ,(cadr (assoc post-pc annotation-alist)))))

         :hints (("Goal" :use
                  (:instance ,Inv-run
                             ,@(pairlis$ inputs (pairlis-x2 inputs nil))
                             (s s0)
                             (sched sched))))
         :rule-classes nil))))

(defun n (s) (nth 0 (locals s)))
(defun a (s) (nth 1 (locals s)))

(defspec ifact *ifact-program* (n0 a0) 0 14
  ((0 (and (equal n0 (n s))
           (natp n0)))
   (2 (and (natp n0)
           (natp (n s))
           (natp (a s))
           (<= (n s) n0)
           (equal (fact n0) (* (fact (n s)) (a s)))))
   (14 (equal (top (stack s)) (fact n0)))))

(defthm partial-correctness-of-program-ifact-corrollary
  (implies (and (natp n0)
                (equal (pc s0) 0)
                (equal (locals s0)
                       (cons n0 (cons a0 nil)))
                (equal (program s0) *ifact-program*)
                (equal sk (run sched s0))
                (equal (pc sk) 14))
           (equal (top (stack sk))
                  (fact n0)))

  :hints (("goal" :use (:instance partial-correctness-of-program-ifact
                                  (n0 n0)
                                  (a0 a0)
                                  (sched sched)
                                  (any nil))))
  :rule-classes nil)


(include-book "fast")

(defconst *m1-boyer-moore-program*

; Allocation of locals

; pat   0 
; j     1
; txt   2
; i     3
; pmax  4 = (length pat)
; tmax  5 = (length txt)
; array 6 = (preprocess pat)
; c     7 = temp - last char read from txt

  '(

    (load 0)        ;  0    (load pat)
    (push "")       ;  1    (push "")
    (ifane 5)       ;  2    (ifane loop)        ; if pat/="", goto loop
    (load 2)        ;  3    (load txt)
    (push "")       ;  4    (push "")
    (ifane 40)      ;  5    (ifane win)         ; if txt/="", goto win
    (goto 43)       ;  6    (goto lose)         
; loop:             
    (load 1)        ;  7    (load j)     
    (iflt 37)       ;  8    (iflt win))         ; if j<0, goto win
    (load 5)        ;  9    (load tmax)  
    (load 3)        ; 10    (load i)     
    (sub)           ; 11    (sub)        
    (ifle 37)       ; 12    (ifle lose)         ; if (length txt)-i <= 0, goto lose
    (load 0)        ; 13    (load pat)   
    (load 1)        ; 14    (load j)     
    (aload)         ; 15    (aload)             ; pat[j]
    (load 2)        ; 16    (load txt)   
    (load 3)        ; 17    (load i)     
    (aload)         ; 18    (aload)             ; txt[i]
    (store 7)       ; 19    (store v)           ; (store into v)
    (load 7)        ; 20    (load v)     
    (sub)           ; 21    (sub)        
    (ifne 10)       ; 22    (ifne skip)         ; if pat[j] /= txt[i], goto skip
    (load 1)        ; 23    (load j)     
    (push 1)        ; 24    (push 1)    
    (sub)           ; 25    (sub)        
    (store 1)       ; 26    (store j)           ; j=j-1
    (load 3)        ; 27    (load i)     
    (push 1)        ; 28    (push 1)    
    (sub)           ; 29    (sub)        
    (store 3)       ; 30    (store i)           ; i=i-1
    (goto -24)      ; 31    (goto loop)         ; goto loop
; skip:
    (load 3)        ; 32    (load i)     
    (load 6)        ; 33    (load array)       
    (load 7)        ; 34    (load v)     
    (aload)         ; 35    (aload)      
    (load 1)        ; 36    (load j)     
    (aload)         ; 37    (aload)      
    (add)           ; 38    (add)
    (store 3)       ; 39    (store i)           ; i := i+array[c][j]
    (load 4)        ; 40    (load pmax)  
    (push 1)        ; 41    (push 1)    
    (sub)           ; 42    (sub)
    (store 1)       ; 43    (store j)           ; j := (length pat)-1
    (goto -37)      ; 44    (goto loop)   
; win:
    (load 3)        ; 45    (load i)     
    (push 1)        ; 46    (push 1)    
    (add)           ; 47    (add)        
    (return)        ; 48    (return)     
; lose:
    (push nil)      ; 49    (push nil) 
    (return) )      ; 50    (return))
  )

(in-theory (enable char length))

(defun m1-boyer-moore-loop-sched (pat j txt i)
  (declare (xargs :measure (measure pat j txt i)
                  :well-founded-relation l<))
  (cond
   ((not (and (stringp pat) (integerp j)
              (stringp txt) (integerp i)
              (<= -1 j) (< j (length pat))
              (<= j i)))
    nil)
   ((< j 0)
    (repeat 0 6))
   ((<= (length txt) i)
    (repeat 0 8))
   ((equal (char-code (char pat j)) (char-code (char txt i)))
    (append (repeat 0 25)
            (m1-boyer-moore-loop-sched pat (- j 1) txt (- i 1))))
   (t (append (repeat 0 29)
              (m1-boyer-moore-loop-sched pat
                                         (- (length pat) 1)
                                         txt
                                         (+ i (delta (char txt i)
                                                     j
                                                     pat)))))))

(defun m1-boyer-moore-sched (pat txt)
  (if (equal pat "")
      (if (equal txt "")
          (repeat 0 9)
        (repeat 0 10))
    (append (repeat 0 3)
            (m1-boyer-moore-loop-sched pat 
                                       (- (length pat) 1)
                                       txt
                                       (- (length pat) 1)))))
(defun m1-boyer-moore-loop-vars (pat j txt i v)
  (declare (xargs :measure (measure pat j txt i)
                  :well-founded-relation l<))
  (cond
   ((not (and (stringp pat) (integerp j)
              (stringp txt) (integerp i)
              (<= -1 j) (< j (length pat))
              (<= j i)))
    (list j i v))
   ((< j 0)
    (list j i v))
   ((<= (length txt) i)
    (list j i v))
   ((equal (char-code (char pat j)) (char-code (char txt i)))
    (m1-boyer-moore-loop-vars
     pat
     (- j 1)
     txt
     (- i 1)
     (char-code (char txt i))))
   (t (m1-boyer-moore-loop-vars
       pat
       (- (length pat) 1)
       txt
       (+ i (delta (char txt i)
                   j
                   pat))
       (char-code (char txt i))))))

(defthm m1-boyer-moore-loop-is-fast-loop
  (implies (and (stringp pat)
                (stringp txt)
                (integerp j)
                (<= -1 j)
                (< j (length pat))
                (integerp i)
                (<= j i))
           (equal (run (m1-boyer-moore-loop-sched pat j txt i)
                       (make-state 7
                                   (list pat
                                         j
                                         txt
                                         i
                                         (length pat)
                                         (length txt)
                                         (preprocess pat)
                                         v)
                                   nil
                                   *m1-boyer-moore-program*))
                  (if (fast-loop pat j txt i)
                      (make-state 48
                                  (list pat
                                        (nth 0 (m1-boyer-moore-loop-vars pat j txt i v))
                                        txt
                                        (nth 1 (m1-boyer-moore-loop-vars pat j txt i v))
                                        (length pat)
                                        (length txt)
                                        (preprocess pat)
                                        (nth 2 (m1-boyer-moore-loop-vars pat j txt i v)))
                                  (push (fast-loop pat j txt i) nil)
                                  *m1-boyer-moore-program*)
                    (make-state 50
                                (list pat
                                      (nth 0 (m1-boyer-moore-loop-vars pat j txt i v))
                                      txt
                                      (nth 1 (m1-boyer-moore-loop-vars pat j txt i v))
                                      (length pat)
                                      (length txt)
                                      (preprocess pat)
                                      (nth 2 (m1-boyer-moore-loop-vars pat j txt i v)))
                                (push nil nil)
                                *m1-boyer-moore-program*))))
  :hints (("Goal" :in-theory (enable preprocess))))

(in-theory (disable length))

(defthm m1-boyer-moore-is-fast
  (implies (and (stringp pat)
                (stringp txt))
           (equal (top
                   (stack
                    (run (m1-boyer-moore-sched pat txt)
                         (make-state 0
                                     (list pat
                                           (- (length pat) 1)
                                           txt
                                           (- (length pat) 1)
                                           (length pat)
                                           (length txt)
                                           (preprocess pat)
                                           0)
                                     nil
                                     *m1-boyer-moore-program*))))
                  (fast pat txt))))

(defthm m1-boyer-moore-halts
  (implies (and (stringp pat)
                (stringp txt))
           (haltedp
            (run (m1-boyer-moore-sched pat txt)
                 (make-state 0
                             (list pat
                                   (- (length pat) 1)
                                   txt
                                   (- (length pat) 1)
                                   (length pat)
                                   (length txt)
                                   (preprocess pat)
                                   0)
                             nil
                             *m1-boyer-moore-program*)))))

(defthm m1-boyer-moore-is-correct
  (implies (and (stringp pat)
                (stringp txt))
           (equal (top
                   (stack
                    (run (m1-boyer-moore-sched pat txt)
                         (make-state 0
                                     (list pat
                                           (- (length pat) 1)
                                           txt
                                           (- (length pat) 1)
                                           (length pat)
                                           (length txt)
                                           (preprocess pat)
                                           0)
                                     nil
                                     *m1-boyer-moore-program*))))
                  (correct pat txt))))




; Appendix:  A Universal Program?

(defconst *universal-program*
  '((PUSH 0)
    (PUSH 1)
    (ADD)
    (GOTO -2)))

(defun universal-sched-loop (k)
  (if (zp k)
      nil
    (append (repeat 0 3)
            (universal-sched-loop (- k 1)))))

(defun universal-sched (k)
  (append (repeat 0 1)
          (universal-sched-loop k)))

(defun induct-hint (k stack)
  (if (zp k)
      stack
    (induct-hint (- k 1)
                 (push (+ 1 (top stack))
                       (pop stack)))))

(defun universal-algorithm (k n)
  (if (zp k)
      n
    (universal-algorithm (- k 1) (+ 1 n))))

(defthm step-a-run-universal-loop
  (implies (and (natp k)
                (natp n))
           (equal (run (universal-sched-loop k)
                       (make-state 1
                                   locals
                                   (push n stack)
                                   *universal-program*))
                  (make-state 1
                              locals
                              (push (universal-algorithm k n)
                                    stack)
                              *universal-program*))))

(defthm step-a-run-universal
  (implies (natp k)
           (equal (run (universal-sched k)
                       (make-state 0
                                   locals
                                   stack
                                   *universal-program*))
                  (make-state 1
                              locals
                              (push (universal-algorithm k 0)
                                    stack)
                              *universal-program*))))

(defthm step-b
  (implies (and (natp k)
                (natp n))
           (equal (universal-algorithm k n) (+ k n))))

(defun new-fact-sched (n)
  (universal-sched (fact n)))

(defthm universal-computes-fact
  (equal (top
          (stack
           (run (new-fact-sched n)
                (make-state 0
                            locals
                            stack
                            *universal-program*))))
         (fact n)))


(defthm universal-never-halts-lemma
  (implies (and (member (pc s) '(0 1 2 3))
                (equal (program s) *universal-program*))
           (not (haltedp (run sched s)))))


(defthm universal-never-halts
  (not
   (haltedp
    (run sched
         (make-state 0
                     locals
                     stack
                     *universal-program*)))))