File: wig-wag.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (767 lines) | stat: -rw-r--r-- 27,330 bytes parent folder | download | duplicates (4)
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
;; Copyright (C) 2017, Regents of the University of Texas
;; Written by Cuong Chau
;; License: A 3-clause BSD license.  See the LICENSE file distributed with
;; ACL2.

;; Cuong Chau <ckcuong@cs.utexas.edu>
;; May 2019

(in-package "ADE")

(include-book "alt-branch")
(include-book "alt-merge")
(include-book "../store-n")

(local (include-book "arithmetic-3/top" :dir :system))

(local (in-theory (disable nth)))

;; ======================================================================

;;; Table of Contents:
;;;
;;; 1. DE Module Generator of WW
;;; 2. Multi-Step State Lemma
;;; 3. Single-Step-Update Property
;;; 4. Relationship Between the Input and Output Sequences

;; ======================================================================

;; 1. DE Module Generator of WW
;;
;; Construct a DE module generator for wig-wag circuits using the link-joint
;; model.  Prove the value and state lemmas for this module generator.

(defconst *wig-wag$go-num* (+ *alt-branch$go-num*
                              *alt-merge$go-num*))

(defun wig-wag$data-ins-len (data-size)
  (declare (xargs :guard (natp data-size)))
  (+ 2 (mbe :logic (nfix data-size)
            :exec  data-size)))

(defun wig-wag$ins-len (data-size)
  (declare (xargs :guard (natp data-size)))
  (+ (wig-wag$data-ins-len data-size)
     *wig-wag$go-num*))

;; DE module generator of WW.  The ALT-BRANCH joint in WW accepts input data
;; and places them alternately into links L0 and L1.  The ALT-MERGE joint takes
;; data alternately from links L0 and L1 and delivers them as outputs.

(module-generator
 wig-wag* (data-size)
 (si 'wig-wag data-size)
 (list* 'full-in 'empty-out- (append (sis 'data-in 0 data-size)
                                     (sis 'go 0 *wig-wag$go-num*)))
 (list* 'in-act 'out-act
        (sis 'data-out 0 data-size))
 '(l0 l1 br me)
 (list
  ;; LINKS
  ;; L0
  (list 'l0
        (list* 'l0-status (sis 'd0-out 0 data-size))
        (si 'link data-size)
        (list* 'br-act0 'me-act0 (sis 'data 0 data-size)))

  ;; L1
  (list 'l1
        (list* 'l1-status (sis 'd1-out 0 data-size))
        (si 'link data-size)
        (list* 'br-act1 'me-act1 (sis 'data 0 data-size)))

  ;; JOINTS
  ;; Alt-Branch
  (list 'br
        (list* 'in-act 'br-act0 'br-act1
               (sis 'data 0 data-size))
        (si 'alt-branch data-size)
        (list* 'full-in 'l0-status 'l1-status
               (append (sis 'data-in 0 data-size)
                       (sis 'go 0 *alt-branch$go-num*))))

  ;; Alt-Merge
  (list 'me
        (list* 'out-act 'me-act0 'me-act1
               (sis 'data-out 0 data-size))
        (si 'alt-merge data-size)
        (list* 'l0-status 'l1-status 'empty-out-
               (append (sis 'd0-out 0 data-size)
                       (sis 'd1-out 0 data-size)
                       (sis 'go *alt-branch$go-num* *alt-merge$go-num*)))))

 (declare (xargs :guard (natp data-size))))

(make-event
 `(progn
    ,@(state-accessors-gen 'wig-wag '(l0 l1 br me) 0)))

;; DE netlist generator.  A generated netlist will contain an instance of WW.

(defund wig-wag$netlist (data-size)
  (declare (xargs :guard (natp data-size)))
  (cons (wig-wag* data-size)
        (union$ (link$netlist data-size)
                (alt-branch$netlist data-size)
                (alt-merge$netlist data-size)
                :test 'equal)))

;; Recognizer for WW

(defund wig-wag& (netlist data-size)
  (declare (xargs :guard (and (alistp netlist)
                              (natp data-size))))
  (b* ((subnetlist (delete-to-eq (si 'wig-wag data-size) netlist)))
    (and (equal (assoc (si 'wig-wag data-size) netlist)
                (wig-wag* data-size))
         (link& subnetlist data-size)
         (alt-branch& subnetlist data-size)
         (alt-merge& subnetlist data-size))))

;; Sanity check

(local
 (defthm check-wig-wag$netlist-64
   (and (net-syntax-okp (wig-wag$netlist 64))
        (net-arity-okp (wig-wag$netlist 64))
        (wig-wag& (wig-wag$netlist 64) 64))))

;; Constraints on the state of WW

(defund wig-wag$st-format (st data-size)
  (b* ((l0 (nth *wig-wag$l0* st))
       (l1 (nth *wig-wag$l1* st)))
    (and (< 0 data-size)
         (link$st-format l0 data-size)
         (link$st-format l1 data-size))))

(defthm wig-wag$st-format=>constraint
  (implies (wig-wag$st-format st data-size)
           (posp data-size))
  :hints (("Goal" :in-theory (enable wig-wag$st-format)))
  :rule-classes :forward-chaining)

(defund wig-wag$valid-st (st data-size)
  (b* ((l0 (nth *wig-wag$l0* st))
       (l1 (nth *wig-wag$l1* st))
       (br (nth *wig-wag$br* st))
       (me (nth *wig-wag$me* st)))
    (and (wig-wag$st-format st data-size)

         (link$valid-st l0 data-size)
         (link$valid-st l1 data-size)

         (alt-branch$valid-st br)
         (alt-merge$valid-st me))))

(defthmd wig-wag$valid-st=>constraint
  (implies (wig-wag$valid-st st data-size)
           (posp data-size))
  :hints (("Goal" :in-theory (enable wig-wag$valid-st)))
  :rule-classes :forward-chaining)

(defthmd wig-wag$valid-st=>st-format
  (implies (wig-wag$valid-st st data-size)
           (wig-wag$st-format st data-size))
  :hints (("Goal" :in-theory (e/d (wig-wag$valid-st)
                                  ()))))

;; Extract the input and output signals for WW

(progn
  ;; Extract the input data

  (defun wig-wag$data-in (inputs data-size)
    (declare (xargs :guard (and (true-listp inputs)
                                (natp data-size))))
    (take (mbe :logic (nfix data-size)
               :exec  data-size)
          (nthcdr 2 inputs)))

  (defthm len-wig-wag$data-in
    (equal (len (wig-wag$data-in inputs data-size))
           (nfix data-size)))

  (in-theory (disable wig-wag$data-in))

  ;; Extract the inputs for joint ALT-BRANCH

  (defund wig-wag$br-inputs (inputs st data-size)
    (b* ((full-in (nth 0 inputs))
         (data-in (wig-wag$data-in inputs data-size))
         (go-signals (nthcdr (wig-wag$data-ins-len data-size) inputs))

         (br-go-signals (take *alt-branch$go-num* go-signals))

         (l0 (nth *wig-wag$l0* st))
         (l0.s (nth *link$s* l0))
         (l1 (nth *wig-wag$l1* st))
         (l1.s (nth *link$s* l1)))

      (list* full-in (f-buf (car l0.s)) (f-buf (car l1.s))
             (append data-in br-go-signals))))

  ;; Extract the inputs for joint ALT-MERGE

  (defund wig-wag$me-inputs (inputs st data-size)
    (b* ((empty-out- (nth 1 inputs))
         (go-signals (nthcdr (wig-wag$data-ins-len data-size) inputs))

         (me-go-signals (take *alt-merge$go-num*
                              (nthcdr *alt-branch$go-num* go-signals)))

         (l0 (nth *wig-wag$l0* st))
         (l0.s (nth *link$s* l0))
         (l0.d (nth *link$d* l0))
         (l1 (nth *wig-wag$l1* st))
         (l1.s (nth *link$s* l1))
         (l1.d (nth *link$d* l1)))

      (list* (f-buf (car l0.s)) (f-buf (car l1.s)) empty-out-
             (append (v-threefix (strip-cars l0.d))
                     (v-threefix (strip-cars l1.d))
                     me-go-signals))))

  ;; Extract the "in-act" signal

  (defund wig-wag$in-act (inputs st data-size)
    (b* ((br-inputs (wig-wag$br-inputs inputs st data-size))
         (br (nth *wig-wag$br* st)))
      (alt-branch$act br-inputs br data-size)))

  (defthm wig-wag$in-act-inactive
    (implies (not (nth 0 inputs))
             (not (wig-wag$in-act inputs st data-size)))
    :hints (("Goal" :in-theory (enable wig-wag$br-inputs
                                       wig-wag$in-act))))

  ;; Extract the "out-act" signal

  (defund wig-wag$out-act (inputs st data-size)
    (b* ((me-inputs (wig-wag$me-inputs inputs st data-size))
         (me (nth *wig-wag$me* st)))
      (alt-merge$act me-inputs me data-size)))

  (defthm wig-wag$out-act-inactive
    (implies (equal (nth 1 inputs) t)
             (not (wig-wag$out-act inputs st data-size)))
    :hints (("Goal" :in-theory (enable wig-wag$me-inputs
                                       wig-wag$out-act))))

  ;; Extract the output data

  (defund wig-wag$data-out (st)
    (b* ((l0 (nth *wig-wag$l0* st))
         (l0.d (nth *link$d* l0))
         (l1 (nth *wig-wag$l1* st))
         (l1.d (nth *link$d* l1))
         (me (nth *wig-wag$me* st))
         (me-select (nth *alt-merge$select* me))
         (me-select.d (nth *link1$d* me-select)))
      (fv-if (car me-select.d)
             (strip-cars l1.d)
             (strip-cars l0.d))))

  (defthm len-wig-wag$data-out-1
    (implies (wig-wag$st-format st data-size)
             (equal (len (wig-wag$data-out st))
                    data-size))
    :hints (("Goal" :in-theory (enable wig-wag$st-format
                                       wig-wag$data-out))))

  (defthm len-wig-wag$data-out-2
    (implies (wig-wag$valid-st st data-size)
             (equal (len (wig-wag$data-out st))
                    data-size))
    :hints (("Goal" :in-theory (enable wig-wag$valid-st
                                       wig-wag$data-out))))

  (defthm bvp-wig-wag$data-out
    (implies (and (wig-wag$valid-st st data-size)
                  (wig-wag$out-act inputs st data-size))
             (bvp (wig-wag$data-out st)))
    :hints (("Goal" :in-theory (enable f-and3
                                       f-and
                                       joint-act
                                       wig-wag$valid-st
                                       wig-wag$st-format
                                       wig-wag$out-act
                                       wig-wag$data-out
                                       wig-wag$me-inputs
                                       alt-merge$valid-st
                                       alt-merge$act
                                       alt-merge$act0
                                       alt-merge$act1))))

  (defun wig-wag$outputs (inputs st data-size)
    (list* (wig-wag$in-act inputs st data-size)
           (wig-wag$out-act inputs st data-size)
           (wig-wag$data-out st)))
  )

;; The value lemma for WW

(defthm wig-wag$value
  (b* ((inputs (list* full-in empty-out- (append data-in go-signals))))
    (implies (and (wig-wag& netlist data-size)
                  (true-listp data-in)
                  (equal (len data-in) data-size)
                  (true-listp go-signals)
                  (equal (len go-signals) *wig-wag$go-num*)
                  (wig-wag$st-format st data-size))
             (equal (se (si 'wig-wag data-size) inputs st netlist)
                    (wig-wag$outputs inputs st data-size))))
  :hints (("Goal"
           :do-not-induct t
           :expand (:free (inputs data-size)
                          (se (si 'wig-wag data-size) inputs st netlist))
           :in-theory (e/d (de-rules
                            wig-wag&
                            wig-wag*$destructure
                            wig-wag$data-in
                            wig-wag$st-format
                            wig-wag$in-act
                            wig-wag$out-act
                            wig-wag$data-out
                            wig-wag$br-inputs
                            wig-wag$me-inputs)
                           (de-module-disabled-rules)))))

;; This function specifies the next state of WW.

(defun wig-wag$step (inputs st data-size)
  (b* ((data-in (wig-wag$data-in inputs data-size))

       (l0 (nth *wig-wag$l0* st))
       (l1 (nth *wig-wag$l1* st))
       (br (nth *wig-wag$br* st))
       (me (nth *wig-wag$me* st))

       (br-inputs (wig-wag$br-inputs inputs st data-size))
       (me-inputs (wig-wag$me-inputs inputs st data-size))

       (br-act0 (alt-branch$act0 br-inputs br data-size))
       (br-act1 (alt-branch$act1 br-inputs br data-size))
       (me-act0 (alt-merge$act0 me-inputs me data-size))
       (me-act1 (alt-merge$act1 me-inputs me data-size))

       (l0-inputs (list* br-act0 me-act0 data-in))
       (l1-inputs (list* br-act1 me-act1 data-in)))
    (list
     ;; L0
     (link$step l0-inputs l0 data-size)
     ;; L1
     (link$step l1-inputs l1 data-size)

     ;; Joint ALT-BRANCH
     (alt-branch$step br-inputs br data-size)
     ;; Joint ALT-MERGE
     (alt-merge$step me-inputs me data-size))))

;; The state lemma for WW

(defthm wig-wag$state
  (b* ((inputs (list* full-in empty-out- (append data-in go-signals))))
    (implies (and (wig-wag& netlist data-size)
                  (true-listp data-in)
                  (equal (len data-in) data-size)
                  (true-listp go-signals)
                  (equal (len go-signals) *wig-wag$go-num*)
                  (wig-wag$st-format st data-size))
             (equal (de (si 'wig-wag data-size) inputs st netlist)
                    (wig-wag$step inputs st data-size))))
  :hints (("Goal"
           :do-not-induct t
           :expand (:free (inputs data-size)
                          (de (si 'wig-wag data-size) inputs st netlist))
           :in-theory (e/d (de-rules
                            wig-wag&
                            wig-wag*$destructure
                            wig-wag$st-format
                            wig-wag$data-in
                            wig-wag$br-inputs
                            wig-wag$me-inputs)
                           (de-module-disabled-rules)))))

(in-theory (disable wig-wag$step))

;; ======================================================================

;; 2. Multi-Step State Lemma

;; Conditions on the inputs

(defund wig-wag$input-format (inputs data-size)
  (declare (xargs :guard (and (true-listp inputs)
                              (natp data-size))))
  (b* ((full-in    (nth 0 inputs))
       (empty-out- (nth 1 inputs))
       (data-in    (wig-wag$data-in inputs data-size))
       (go-signals (nthcdr (wig-wag$data-ins-len data-size) inputs)))
    (and
     (booleanp full-in)
     (booleanp empty-out-)
     (or (not full-in) (bvp data-in))
     (true-listp go-signals)
     (= (len go-signals) *wig-wag$go-num*)
     (equal inputs
            (list* full-in empty-out- (append data-in go-signals))))))

(local
 (defthm wig-wag$input-format=>br$input-format
   (implies (and (wig-wag$input-format inputs data-size)
                 (wig-wag$valid-st st data-size))
            (alt-branch$input-format
             (wig-wag$br-inputs inputs st data-size)
             data-size))
   :hints (("Goal"
            :in-theory (e/d (wig-wag$input-format
                             alt-branch$input-format
                             alt-branch$data-in
                             wig-wag$valid-st
                             wig-wag$st-format
                             wig-wag$br-inputs)
                            ())))))

(local
 (defthm wig-wag$input-format=>me$input-format
   (implies (and (wig-wag$input-format inputs data-size)
                 (wig-wag$valid-st st data-size))
            (alt-merge$input-format
             (wig-wag$me-inputs inputs st data-size)
             data-size))
   :hints (("Goal"
            :in-theory (e/d (wig-wag$input-format
                             alt-merge$input-format
                             alt-merge$data0-in
                             alt-merge$data1-in
                             wig-wag$valid-st
                             wig-wag$st-format
                             wig-wag$me-inputs)
                            ())))))

(defthm booleanp-wig-wag$in-act
  (implies (and (wig-wag$input-format inputs data-size)
                (wig-wag$valid-st st data-size))
           (booleanp (wig-wag$in-act inputs st data-size)))
  :hints (("Goal"
           :in-theory (enable wig-wag$valid-st
                              wig-wag$in-act)))
  :rule-classes (:rewrite :type-prescription))

(defthm booleanp-wig-wag$out-act
  (implies (and (wig-wag$input-format inputs data-size)
                (wig-wag$valid-st st data-size))
           (booleanp (wig-wag$out-act inputs st data-size)))
  :hints (("Goal"
           :in-theory (enable wig-wag$valid-st
                              wig-wag$out-act)))
  :rule-classes (:rewrite :type-prescription))

(simulate-lemma wig-wag)

;; ======================================================================

;; 3. Single-Step-Update Property

;; The extraction function for WW that extracts the future output sequence from
;; the current state.

(defund wig-wag$extract (st)
  (b* ((l0 (nth *wig-wag$l0* st))
       (l1 (nth *wig-wag$l1* st))
       (me (nth *wig-wag$me* st))

       (me-select       (nth *alt-merge$select* me))
       (me-select.s     (nth *link1$s* me-select))
       (me-select.d     (nth *link1$d* me-select))
       (me-select-buf   (nth *alt-merge$select-buf* me))
       (me-select-buf.d (nth *link1$d* me-select-buf))
       (valid-me-select (if (fullp me-select.s)
                            (car me-select.d)
                          (car me-select-buf.d))))

    (if valid-me-select
        (extract-valid-data (list l0 l1))
      (extract-valid-data (list l1 l0)))))

(defthm wig-wag$extract-not-empty
  (implies (and (wig-wag$out-act inputs st data-size)
                (wig-wag$valid-st st data-size))
           (< 0 (len (wig-wag$extract st))))
  :hints (("Goal"
           :in-theory (e/d (f-and3
                            alt-merge$act
                            alt-merge$act0
                            alt-merge$act1
                            wig-wag$me-inputs
                            wig-wag$valid-st
                            wig-wag$extract
                            wig-wag$out-act)
                           ())))
  :rule-classes :linear)

;; Specify and prove a state invariant

(progn
  (defund wig-wag$inv (st)
    (b* ((l0 (nth *wig-wag$l0* st))
         (l0.s (nth *link$s* l0))
         (l1 (nth *wig-wag$l1* st))
         (l1.s (nth *link$s* l1))
         (br (nth *wig-wag$br* st))
         (me (nth *wig-wag$me* st))

         (br-select       (nth *alt-branch$select* br))
         (br-select.s     (nth *link1$s* br-select))
         (br-select.d     (nth *link1$d* br-select))
         (br-select-buf   (nth *alt-branch$select-buf* br))
         (br-select-buf.d (nth *link1$d* br-select-buf))
         (valid-br-select (if (fullp br-select.s)
                              (car br-select.d)
                            (car br-select-buf.d)))

         (me-select       (nth *alt-merge$select* me))
         (me-select.s     (nth *link1$s* me-select))
         (me-select.d     (nth *link1$d* me-select))
         (me-select-buf   (nth *alt-merge$select-buf* me))
         (me-select-buf.d (nth *link1$d* me-select-buf))
         (valid-me-select (if (fullp me-select.s)
                              (car me-select.d)
                            (car me-select-buf.d))))

      (and (alt-branch$inv br)
           (alt-merge$inv me)
           (or (and (equal l0.s l1.s)
                    (equal valid-br-select valid-me-select))
               (and (fullp l0.s)
                    (emptyp l1.s)
                    valid-br-select
                    (not valid-me-select))
               (and (emptyp l0.s)
                    (fullp l1.s)
                    (not valid-br-select)
                    valid-me-select)))))

  (local
   (defthm wig-wag$input-format-lemma-1
     (implies (wig-wag$input-format inputs data-size)
              (booleanp (nth 0 inputs)))
     :hints (("Goal" :in-theory (enable wig-wag$input-format)))
     :rule-classes (:rewrite :type-prescription)))

  (local
   (defthm wig-wag$input-format-lemma-2
     (implies (wig-wag$input-format inputs data-size)
              (booleanp (nth 1 inputs)))
     :hints (("Goal" :in-theory (enable wig-wag$input-format)))
     :rule-classes (:rewrite :type-prescription)))

  (local
   (defthm wig-wag$input-format-lemma-3
     (implies (and (wig-wag$input-format inputs data-size)
                   (nth 0 inputs))
              (bvp (wig-wag$data-in inputs data-size)))
     :hints (("Goal" :in-theory (enable wig-wag$input-format)))))

  (defthm wig-wag$inv-preserved
    (implies (and (wig-wag$input-format inputs data-size)
                  (wig-wag$valid-st st data-size)
                  (wig-wag$inv st))
             (wig-wag$inv (wig-wag$step inputs st data-size)))
    :hints (("Goal"
             :in-theory (e/d (f-sr
                              wig-wag$valid-st
                              wig-wag$inv
                              wig-wag$step
                              wig-wag$in-act
                              wig-wag$out-act
                              wig-wag$br-inputs
                              wig-wag$me-inputs
                              alt-branch$valid-st
                              alt-branch$inv
                              alt-branch$step
                              alt-branch$act
                              alt-branch$act0
                              alt-branch$act1
                              alt-merge$valid-st
                              alt-merge$inv
                              alt-merge$step
                              alt-merge$act
                              alt-merge$act0
                              alt-merge$act1)
                             ()))))
  )

;; The extracted next-state function for WW.  Note that this function avoids
;; exploring the internal computation of WW.

(defund wig-wag$extracted-step (inputs st data-size)
  (b* ((data (wig-wag$data-in inputs data-size))
       (extracted-st (wig-wag$extract st))
       (n (1- (len extracted-st))))
    (cond
     ((equal (wig-wag$out-act inputs st data-size) t)
      (cond
       ((equal (wig-wag$in-act inputs st data-size) t)
        (cons data (take n extracted-st)))
       (t (take n extracted-st))))
     (t (cond
         ((equal (wig-wag$in-act inputs st data-size) t)
          (cons data extracted-st))
         (t extracted-st))))))

;; The single-step-update property

(defthm wig-wag$extracted-step-correct
  (b* ((next-st (wig-wag$step inputs st data-size)))
    (implies (and (wig-wag$input-format inputs data-size)
                  (wig-wag$valid-st st data-size)
                  (wig-wag$inv st))
             (equal (wig-wag$extract next-st)
                    (wig-wag$extracted-step inputs st data-size))))
  :hints (("Goal"
           :in-theory (e/d (f-sr
                            joint-act
                            wig-wag$extracted-step
                            wig-wag$valid-st
                            wig-wag$inv
                            wig-wag$step
                            wig-wag$in-act
                            wig-wag$out-act
                            wig-wag$extract
                            wig-wag$br-inputs
                            wig-wag$me-inputs
                            alt-branch$valid-st
                            alt-branch$inv
                            alt-branch$act
                            alt-branch$act0
                            alt-branch$act1
                            alt-merge$valid-st
                            alt-merge$inv
                            alt-merge$step
                            alt-merge$act
                            alt-merge$act0
                            alt-merge$act1)
                           ()))))

;; ======================================================================

;; 4. Relationship Between the Input and Output Sequences

;; Prove that wig-wag$valid-st is an invariant.

(encapsulate
  ()

  (local
   (defthm wig-wag$br-act0-inactive
     (implies (equal (nth *link$s*
                          (nth *wig-wag$l0* st))
                     '(t))
              (not (alt-branch$act0
                    (wig-wag$br-inputs inputs st data-size)
                    (nth *wig-wag$br* st)
                    data-size)))
     :hints (("Goal"
              :in-theory (enable wig-wag$br-inputs)))))

  (local
   (defthm wig-wag$br-act1-inactive
     (implies (equal (nth *link$s*
                          (nth *wig-wag$l1* st))
                     '(t))
              (not (alt-branch$act1
                    (wig-wag$br-inputs inputs st data-size)
                    (nth *wig-wag$br* st)
                    data-size)))
     :hints (("Goal"
              :in-theory (enable wig-wag$br-inputs)))))

  (local
   (defthm wig-wag$me-act0-inactive
     (implies (equal (nth *link$s*
                          (nth *wig-wag$l0* st))
                     '(nil))
              (not (alt-merge$act0
                    (wig-wag$me-inputs inputs st data-size)
                    (nth *wig-wag$me* st)
                    data-size)))
     :hints (("Goal"
              :in-theory (enable wig-wag$me-inputs)))))

  (local
   (defthm wig-wag$me-act1-inactive
     (implies (equal (nth *link$s*
                          (nth *wig-wag$l1* st))
                     '(nil))
              (not (alt-merge$act1
                    (wig-wag$me-inputs inputs st data-size)
                    (nth *wig-wag$me* st)
                    data-size)))
     :hints (("Goal"
              :in-theory (enable wig-wag$me-inputs)))))

  (local
   (defthm wig-wag$br-acts-inactive
     (b* ((br-inputs (wig-wag$br-inputs inputs st data-size))
          (br (nth *wig-wag$br* st)))
       (implies (not (nth 0 inputs))
                (and (not (alt-branch$act0 br-inputs br data-size))
                     (not (alt-branch$act1 br-inputs br data-size)))))
     :hints (("Goal" :in-theory (enable wig-wag$br-inputs)))))

  (defthm wig-wag$valid-st-preserved
    (implies (and (wig-wag$input-format inputs data-size)
                  (wig-wag$valid-st st data-size))
             (wig-wag$valid-st (wig-wag$step inputs st data-size)
                               data-size))
    :hints (("Goal"
             :use (wig-wag$input-format=>br$input-format
                   wig-wag$input-format=>me$input-format)
             :in-theory (e/d (f-sr
                              wig-wag$input-format
                              wig-wag$valid-st
                              wig-wag$st-format
                              wig-wag$step)
                             (wig-wag$input-format=>br$input-format
                              wig-wag$input-format=>me$input-format)))))
  )

(defthm wig-wag$extract-lemma
  (implies (and (wig-wag$valid-st st data-size)
                (wig-wag$out-act inputs st data-size))
           (equal (list (wig-wag$data-out st))
                  (nthcdr (1- (len (wig-wag$extract st)))
                          (wig-wag$extract st))))
  :hints (("Goal"
           :do-not-induct t
           :in-theory (enable f-and3
                              f-and
                              joint-act
                              wig-wag$valid-st
                              wig-wag$extract
                              wig-wag$out-act
                              wig-wag$data-out
                              wig-wag$me-inputs
                              alt-merge$valid-st
                              alt-merge$act
                              alt-merge$act0
                              alt-merge$act1))))

;; Extract the accepted input sequence

(seq-gen wig-wag in in-act 0
         (wig-wag$data-in inputs data-size))

;; Extract the valid output sequence

(seq-gen wig-wag out out-act 1
         (wig-wag$data-out st)
         :netlist-data (nthcdr 2 outputs))

;; The multi-step input-output relationship

(in-out-stream-lemma wig-wag :inv t)