This file is indexed.

/usr/share/acl2-6.5/books/cgen/splitnat.lisp is in acl2-books-source 6.5-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#|$ACL2s-Preamble$;
(ld ;; Newline to fool ACL2/cert.pl dependency scanner
 "portcullis.lsp")

(acl2::begin-book);$ACL2s-Preamble$|#


(in-package "DEFDATA")

(set-verify-guards-eagerness 2)
(set-state-ok t)

(include-book "num-list-fns")

(local (include-book "num-list-thms"))

(local (include-book "rem-and-floor"))

(defun weighted-split-nat-step (weights x old-results)
  (declare (xargs :guard (and (2+-listp weights)
                              (natp x)
                              (naturals-listp old-results)
                              (= (len weights) (len old-results)))))
  (if (mbe :logic (or (endp weights)
                      (endp old-results))
           :exec (endp weights))
    nil
    (let ((weight (car weights)))
      (cons (+ (* weight (car old-results))
               (rem x weight))
            (weighted-split-nat-step (cdr weights)
                                     (floor x weight)
                                     (cdr old-results))))))


(local (defthm weighted-split-nat-step--true-listp
         (true-listp (weighted-split-nat-step w x r))
         :rule-classes (:type-prescription :rewrite)))

(local (defthm weighted-split-nat-step--len
         (equal (len (weighted-split-nat-step w x r))
                (min (len w) (len r)))))

(local (defthm weighted-split-nat-step--nat-listp
         (implies (and (naturals-listp w)
                       (integerp x)
                       (<= 0 x)
                       (naturals-listp r))
                  (naturals-listp (weighted-split-nat-step w x r)))
         :rule-classes (:type-prescription :rewrite)))

(local (defthm weighted-split-nat-step--consp
         (implies (and (consp w)
                       (consp r))
                  (consp (weighted-split-nat-step w x r)))
         :rule-classes (:type-prescription :rewrite)))


(local (defthm weighted-split-nat-step--bound-old
         (implies (and (2+-listp w)
                       (integerp x)
                       (<= 0 x)
                       (naturals-listp r)
                       (equal (len w) (len r)))
                  (<=-lists (weighted-split-nat-step w x r)
                            (+-lists
                             (*-lists r
                                      w)
                             w)))
         :rule-classes (:rewrite)))


(local
 (encapsulate nil
   (local (include-book "arithmetic-5/top" :dir :system))
   
   (local (SET-DEFAULT-HINTS '((acl2::NONLINEARP-DEFAULT-HINT
                                acl2::STABLE-UNDER-SIMPLIFICATIONP
                                acl2::HIST
                                acl2::PSPV))))

  (defthm <=-lists--scale
           (implies (and (rationalp v)
                         (<= 0 v)
                         (rationalp w)
                         (<= v w)
                         (naturals-listp l))
                    (<=-lists (scale l v)
                              (scale l w))))

  (defthm <=-lists--scale2
           (implies (and (<=-lists l1
                                   (scale l2 v))
                         (rationalp v)
                         (<= 0 v)
                         (rationalp w)
                         (<= v w)
                         (naturals-listp l2))
                    (<=-lists l1
                              (scale l2 w))))

  (defthm <=-lists--scale3
           (implies (and (rationalp v)
                         (<= 0 v)
                         (naturals-listp l1)
                         (naturals-listp l2)
                         (<=-lists l1 l2))
                    (<=-lists (scale l1 v)
                              (scale l2 v))))

  (defthm <=-lists--scale4
           (implies (and (rationalp v1)
                         (<= 0 v1)
                         (rationalp v2)
                         (<= 0 v2)
                         (naturals-listp l1)
                         (naturals-listp l2)
                         (<=-lists l1 l2)
                         (<= v1 v2))
                    (<=-lists (scale l1 v1)
                              (scale l2 v2))))

  
  (defthm <=-lists--shift
    (implies (and (rationalp v)
                  (rationalp w)
                  (<= v w)
                  (rational-listp l))
             (<=-lists (shift l v)
                       (shift l w))))

  (defthm <=-lists--shift2
    (implies (and (<=-lists l1
                            (shift l2 v))
                  (rationalp v)
                  (rationalp w)
                  (<= v w)
                  (rational-listp l2))
             (<=-lists l1
                       (shift l2 w))))

  (defthm <=-lists--shift3
           (implies (and (rationalp v)
                         (rational-listp l1)
                         (rational-listp l2)
                         (<=-lists l1 l2))
                    (<=-lists (shift l1 v)
                              (shift l2 v))))

  (defthm <=-lists--shift4
           (implies (and (rationalp v1)
                         (rationalp v2)
                         (rational-listp l1)
                         (rational-listp l2)
                         (<=-lists l1 l2)
                         (<= v1 v2))
                    (<=-lists (shift l1 v1)
                              (shift l2 v2))))
  ))

(local (defthm shift--<=
         (equal (<=-lists (shift l v1)
                          (shift l v2))
                (or (endp l)
                    (<= v1 v2)))))

(local (defthm weighted-split-nat-step--bound
         (implies (and (2+-listp w)
                       (integerp x)
                       (<= 0 x)
                       (naturals-listp r)
                       (equal (len w) (len r)))
                  (<=-lists (weighted-split-nat-step w x r)
                            (shift
                             (*-lists r w)
                             x)))))

(local
 (encapsulate nil
   (local (include-book "arithmetic-5/top" :dir :system))
   
   (local (SET-DEFAULT-HINTS '((acl2::NONLINEARP-DEFAULT-HINT
                                acl2::STABLE-UNDER-SIMPLIFICATIONP
                                acl2::HIST
                                acl2::PSPV))))
   
   (defthm weighted-split-nat-step--bound2--lemma
     (implies (and (naturals-listp r)
                   (2+-listp w)
                   (equal (len r) (len w)))
              (<=-lists (*-lists r w)
                        (scale r (product-list w)))))))


(local
 (encapsulate nil
   (local (defthm <=-lists--transitive--force
            (implies (and (<=-lists a b)
                          (force (<=-lists b c)))
                     (<=-lists a c))
            :rule-classes ((:rewrite :match-free :all))))

  (local (defthm <=-lists--scale4--force
           (implies (and (rationalp v1)
                         (<= 0 v1)
                         (rationalp v2)
                         (<= 0 v2)
                         (naturals-listp l1)
                         (naturals-listp l2)
                         (force (<=-lists l1 l2))
                         (force (<= v1 v2)))
                    (<=-lists (scale l1 v1)
                              (scale l2 v2)))))

  (local (defthm <=-lists--shift4--force
           (implies (and (rationalp v1)
                         (rationalp v2)
                         (rational-listp l1)
                         (rational-listp l2)
                         (force (<=-lists l1 l2))
                         (force (<= v1 v2)))
                    (<=-lists (shift l1 v1)
                              (shift l2 v2)))))
   
  (local (include-book "arithmetic-5/top" :dir :system))
  
  (local (SET-DEFAULT-HINTS '((acl2::NONLINEARP-DEFAULT-HINT
                               acl2::STABLE-UNDER-SIMPLIFICATIONP
                               acl2::HIST
                               acl2::PSPV))))

   (defthm weighted-split-nat-step--bound2
     (implies (and (2+-listp w)
                   (integerp x)
                   (<= 0 x)
                   (naturals-listp r)
                   (equal (len w) (len r)))
              (<=-lists (weighted-split-nat-step w x r)
                        (shift
                         (scale r (product-list w))
                         x))))))


(local
 (defthm get-weights-factor-->=2
   (implies (and (2+-listp x)
                 (consp x))
            (<= 2 (product-list x)))
   :rule-classes (:linear :rewrite)))


(defun rot-list (x)
  (declare (xargs :guard (and (true-listp x)
                              (consp x))))
  (append (cdr x) (list (car x))))

(local
 (defthm rot-list--consp
   (implies (consp x)
            (consp (rot-list x)))
   :rule-classes (:type-prescription :rewrite)))

(local (defthm consp-cdr-append
         (implies (and (consp x)
                       (consp y))
                  (consp (cdr (append x y))))))

(local
 (defthm rot-list--consp-cdr
   (implies (and (consp x)
                 (consp (cdr x)))
            (consp (cdr (rot-list x))))
   :rule-classes (:type-prescription :rewrite)))

(local
 (defthm len-append-single
   (equal (len (append x (list y)))
          (+ 1 (len x)))))

(local
 (defthm rot-list--len
   (implies (consp x)
            (equal (len (rot-list x))
                   (len x)))))

(local (defthm rot-list--nat-listp
         (implies (and (naturals-listp x)
                       (consp x))
                  (naturals-listp (rot-list x)))))

(local (defthm rot-list--2+-listp
         (implies (and (2+-listp x)
                       (consp x))
                  (2+-listp (rot-list x)))))

(local (defthm rot-list--product-list
         (implies (consp x)
                  (equal (product-list (rot-list x))
                         (product-list x)))))

(local (defthm all-<=--rot-list
         (implies (consp l)
                  (equal (all-<= (rot-list l)
                                 x)
                         (all-<= l
                                 x)))))

(local (in-theory (disable rot-list)))

(defun weighted-split-nat1 (weights weights-factor x)
  (declare (xargs :measure (nfix x)
                  :verify-guards nil
                  :guard (and (2+-listp weights)
                              (equal weights-factor (product-list weights))
                              (consp weights)
                              (natp x))))
  (if (mbe :logic (or (zp x)
                      (endp weights)
                      (not (equal weights-factor (product-list weights)))
                      (not (2+-listp weights)))
           :exec (zp x))
    (make-list (len weights) :initial-element 0)
    (weighted-split-nat-step weights
                             (rem x weights-factor)
                             (rot-list
                              (weighted-split-nat1
                               (rot-list weights)
                               weights-factor
                               (floor x weights-factor))))))

(local
 (defthm weighted-split-nat1--consp
   (implies (consp weights)
            (consp (weighted-split-nat1 weights weights-factor x)))))

(local
 (defthm weighted-split-nat1--len
   (equal (len (weighted-split-nat1 weights weights-factor x))
          (len weights))))

(local
 (defthm weighted-split-nat1--nat-listp
   (implies (and (2+-listp weights)
                 (consp weights)
                 (equal weights-factor (product-list weights))
                 (integerp x)
                 (<= 0 x))
            (naturals-listp (weighted-split-nat1 weights weights-factor x)))
   :rule-classes ((:rewrite)
                  (:rewrite :corollary
                   (implies (and (2+-listp weights)
                                 (consp weights)
                                 (equal weights-factor (product-list weights))
                                 (integerp x)
                                 (<= 0 x))
                            (true-listp (weighted-split-nat1 weights weights-factor x)))))))

(verify-guards weighted-split-nat1)

(local
 (defthm weighted-split-nat1--<=-induction-step1
   (implies (and (2+-listp weights)
                 (consp weights)
                 (integerp x)
                 (<= 0 x))
            (<=-lists (weighted-split-nat-step
                       weights
                       (rem x (product-list weights))
                       (rot-list (weighted-split-nat1 (rot-list weights)
                                                      (product-list weights)
                                                      (floor x (product-list weights)))))
                      (shift
                       (scale (rot-list (weighted-split-nat1 (rot-list weights)
                                                             (product-list weights)
                                                             (floor x (product-list weights))))
                              (product-list weights))
                       (rem x (product-list weights)))))
   :rule-classes nil))

(local
 (encapsulate nil
   (local (include-book "arithmetic-3/top" :dir :system))
   
   (local (SET-DEFAULT-HINTS '((acl2::NONLINEARP-DEFAULT-HINT
                                acl2::STABLE-UNDER-SIMPLIFICATIONP
                                acl2::HIST
                                acl2::PSPV))))
   
   (local (defthm all-<=--shift
            (equal (all-<= (shift l v)
                           x)
                   (all-<= l
                           (- x v)))))
   
   (local (defthm all-<=--scale
            (implies (and (rationalp v)
                          (< 0 v))
                     (equal (all-<= (scale l v)
                                    x)
                            (all-<= l
                                    (/ x v))))))

   (local (defthm blah
            (implies (and (equal n (len l)) 
                          (<=-lists l
                                    (make-list-logic x n))
                          (force (<= x y)))
                     (all-<= l
                             y))))

   (defthm weighted-split-nat1--<=-induction-step2
     (implies (and (2+-listp weights)
                   (consp weights)
                   (integerp x)
                   (<= 0 x)
                   (all-<= (weighted-split-nat1 (rot-list weights)
                                                (product-list weights)
                                                (floor x (product-list weights)))
                           (floor x (product-list weights))))
              (all-<= (shift
                       (scale (rot-list (weighted-split-nat1 (rot-list weights)
                                                             (product-list weights)
                                                             (floor x (product-list weights))))
                              (product-list weights))
                       (rem x (product-list weights)))
                      x))
     :rule-classes nil)))

(local
 (defthm weighted-split-nat1--<=--consp
   (implies (and (2+-listp weights)
                 (consp weights)
                 (equal weights-factor (product-list weights))
                 (integerp x)
                 (<= 0 x))
            (all-<= (weighted-split-nat1 weights weights-factor x) x))
   :hints (("Goal" :in-theory (disable len 2+-listp product-list)
                   :do-not '(eliminate-destructors)
                   :induct t)
           ("Subgoal *1/2.1"
            :use ((:instance weighted-split-nat1--<=-induction-step1)
                  (:instance weighted-split-nat1--<=-induction-step2))))))

(local
 (defthm weighted-split-nat1--<=--endp
   (implies (not (consp weights))
            (all-<= (weighted-split-nat1 weights weights-factor x) x))))
 
(local
 (defthm weighted-split-nat1--<=
   (implies (and (2+-listp weights)
                 (equal weights-factor (product-list weights))
                 (integerp x)
                 (<= 0 x))
            (all-<= (weighted-split-nat1 weights weights-factor x) x))
   :hints (("Goal" :cases ((consp weights))))))

#|
(defun pos-list-fix (x)
  (if (atom x)
    nil
    (cons
     (if (posp (car x))
       (car x)
       1)
     (pos-list-fix (cdr x)))))
|#

(defthm pos-listp--pos-list-fix
  (pos-listp (pos-list-fix x))
  :rule-classes (:rewrite :type-prescription))

(defthm len--pos-list-fix
  (equal (len (pos-list-fix x))
         (len x)))

(defthm pos-list-fix--shortcut
  (implies (pos-listp x)
           (equal (pos-list-fix x)
                  x)))

(defun non-empty-pos-list-fix (x)
  (if (atom x)
    (list 1)
    (pos-list-fix x)))

(defthm len--non-empty-pos-list-fix
  (equal (len (non-empty-pos-list-fix x))
         (max 1 (len x))))

(defthm consp--non-empty-pos-list-fix
  (consp (non-empty-pos-list-fix x))
  :rule-classes (:rewrite :type-prescription))

(defthm pos-listp--non-empty-pos-list-fix
  (pos-listp (non-empty-pos-list-fix x))
  :rule-classes (:rewrite :type-prescription))

(defthm non-empty-pos-list-fix--shortcut
  (implies (and (pos-listp x)
                (consp x))
           (equal (non-empty-pos-list-fix x)
                  x)))

(in-theory (disable non-empty-pos-list-fix))

(defun weighted-split-nat (weights x)
  (declare (xargs :measure (nfix x)
                  :guard (and (pos-listp weights)
                              (consp weights)
                              (natp x))))
  (mbe :exec
       (let ((2+-weights (scale weights 2)))
         (weighted-split-nat1 2+-weights (product-list 2+-weights) x))
       :logic
       (let* ((weights (non-empty-pos-list-fix weights))
              (x (nfix x))
              (2+-weights (scale weights 2)))
         (weighted-split-nat1 2+-weights (product-list 2+-weights) x))))

(local ; weighted-split-nat will later be automatically rewritten, so these
       ; become useless 
 (defthm weighted-split-nat--len
   (equal (len (weighted-split-nat weights x))
          (max 1 (len weights)))))

(local
 (defthm weighted-split-nat--consp
   (consp (weighted-split-nat weights x))))


 (defthm weighted-split-nat--nat-listp
   (naturals-listp (weighted-split-nat weights x)))

(local (defthm scale--pos-listp--2+-listp
         (implies (pos-listp l)
                  (2+-listp (scale l 2)))
         :rule-classes (:forward-chaining :type-prescription)))

(local
 (defthm weighted-split-nat--bound
   (implies (and (integerp x)
                 (<= 0 x))
            (all-<= (weighted-split-nat weights x) x))))

(local
 (defthm car--nat-list--integer
   (implies (and (consp x)
                 (naturals-listp x))
            (integerp (car x)))))

(local
 (defthm weighted-split-nat--car-integer
   (INTEGERP (CAR (WEIGHTED-SPLIT-NAT WEIGHTS X)))))

(local
 (defthm car--nat-list-->=0
   (implies (and (consp x)
                 (naturals-listp x))
            (<= 0 (car x)))))

(local
 (defthm weighted-split-nat--car->=0
   (<= 0 (CAR (WEIGHTED-SPLIT-NAT WEIGHTS X)))))

(local
(defthm not-nat-implies-weighted-split-nat-is-zero
  (implies (and (not (natp x))
                (pos-listp ws)
                (>= (len ws) 1))
           (equal (weighted-split-nat ws x)
                  (make-list (len ws) :initial-element 0)))))


(in-theory (disable weighted-split-nat))




;(set-verify-guards-eagerness 0)
(defun nth-weighted-split-nat (i weights x)
  (declare (xargs :guard nil)) ; logic only
  ;(declare (xargs :verify-guards nil))
  (if (and (integerp i)
           (<= 0 i)
           (< i (len weights)))
    (nth i (weighted-split-nat weights x))
    (car (weighted-split-nat weights x))))

(defun nthcdr-weighted-split-nat (i weights x)
  (declare (xargs :guard nil)) ; logic only
  ;(declare (xargs :verify-guards nil))
  (nthcdr i (weighted-split-nat weights x)))



(defthm nth-weighted-split-nat--bound
  (implies (and (integerp x)
                (<= 0 x))
           (<= (nth-weighted-split-nat i weights x)
               x))
  ;:hints (("Goal" :cases ((all-<= (weighted-split-nat weights x) x))))
  :rule-classes (:rewrite :linear))

(defthm nat-listp--nth--integerp
         (implies (and (naturals-listp l)
                       (integerp i)
                       (<= 0 i)
                       (< i (len l)))
                  (integerp (nth i l)))
         :rule-classes (:rewrite :type-prescription))

(defthm nth-weighted-split-nat--integerp
  (integerp (nth-weighted-split-nat i weights x))
  :rule-classes (:rewrite :type-prescription))

(defthm nat-listp--nth-->=0
         (implies (and (naturals-listp l)
                       (integerp i)
                       (<= 0 i)
                       (< i (len l)))
                  (<= 0 (nth i l)))
         :rule-classes (:rewrite :linear))

(defthm nth-weighted-split-nat-->=0
  (<= 0 (nth-weighted-split-nat i weights x))
  :rule-classes (:rewrite :linear))


(local
 (defthm nth--nthcdr--decomp
   (implies (consp l)
            (equal (cons (nth 0 l)
                         (nthcdr 1 l))
                   l))))

(local
 (defthm len--nthcdr--toobig
   (implies (and (integerp i)
                 (<= (len l) i))
            (equal (len (nthcdr i l))
                   0))))

(local
 (defthm len--nthcdr
   (implies (and (integerp i)
                 (<= 0 i)
                 (<= i (len l)))
            (equal (len (nthcdr i l))
                   (- (len l) i)))))

(defthm nthcdr-weighted-split-nat--len
  (equal (len (nthcdr-weighted-split-nat i weights x))
         (if (zp i)
           (max 1 (len weights))
           (if (<= i (len weights))
             (- (len weights) i)
             0))))

(local
 (defthm consp--nthcdr
   (equal (consp (nthcdr i l))
          (and (consp l)
               (implies (integerp i)
                        (< i (len l)))))))

(defthm nthcdr-weighted-split-nat--consp
           (equal (consp (nthcdr-weighted-split-nat i weights x))
                  (implies (integerp i)
                           (< i (max 1 (len weights))))))

(local
 (defthm naturals-listp--nthcdr
   (implies (naturals-listp l)
            (naturals-listp (nthcdr i l)))
   :rule-classes (:rewrite :type-prescription)))

(defthm nthcdr-weighted-split-nat--naturals-listp
  (naturals-listp (nthcdr-weighted-split-nat i weights x)))


(defthm weighted-split-nat--to--nthcdr-weighted-split-nat
  (implies (and (pos-listp weights)
                (consp weights)
                (integerp x)
                (<= 0 x))
           (equal (weighted-split-nat weights x)
                  (nthcdr-weighted-split-nat 0 weights x))))



(local
 (encapsulate nil
   (local (include-book "arithmetic-5/top" :dir :system))

   (local
    (defthm nthcdr--cdr
      (implies (and (integerp i)
                    (<= 0 i))
               (equal (nthcdr i (cdr l))
                      (nthcdr (+ 1 i) l)))
      :hints (("Goal" :expand (nthcdr (+ 1 i) l)))))

   (defthm nth--nthcdr--decomp2
     (implies (and (integerp i)
                   (<= 0 i)
                   (< i (len l)))
              (equal (cons (nth i l)
                           (nthcdr (+ 1 i) l))
                     (nthcdr i l))))))

(defthm nthcdr-weighted-split-nat--deflike
  (implies (and (integerp i)
                (<= 0 i)
                (< i (len weights)))
           (equal (nthcdr-weighted-split-nat i weights x)
                  (cons (nth-weighted-split-nat i weights x)
                        (nthcdr-weighted-split-nat (+ 1 i) weights x))))
  :hints (("Goal" :in-theory (disable nth nthcdr)))
  :rule-classes ((:definition :controller-alist ((nthcdr-weighted-split-nat t nil nil)))))

(in-theory (disable nth-weighted-split-nat nthcdr-weighted-split-nat)) 

(defthm nthcdr-weighted-split-nat--car
  (implies (and (integerp i)
                (<= 0 i)
                (< i (len weights)))
           (equal (car (nthcdr-weighted-split-nat i weights x))
                  (nth-weighted-split-nat i weights x)))
  :hints (("Goal" :expand (nthcdr-weighted-split-nat i weights x))))

(defthm nthcdr-weighted-split-nat--cdr
  (implies (and (integerp i)
                (<= 0 i)
                (< i (len weights)))
           (equal (cdr (nthcdr-weighted-split-nat i weights x))
                  (nthcdr-weighted-split-nat (+ 1 i) weights x)))
  :hints (("Goal" :expand (nthcdr-weighted-split-nat i weights x))))

(local
 (defthm len-exceeding-return-nil
   (implies (and (natp i)
                 (<= (len l) i))
            (equal (nth i l) nil))))
(local 
 (defthm make-list-logic-len
   (implies (natp i)
            (equal (len (make-list-logic a i))
                   i))))

(defun nth-returns-elem-of-make-list-ind-scheme (i x)
  (declare (xargs :guard (and (natp i) (natp x))))
  (if (or (zp i) (zp x))
    0
    (nth-returns-elem-of-make-list-ind-scheme (- i 1) (- x 1))))

(local
 (defthm nth-returns-elem-of-make-list
   (implies (and (natp i)
                 (natp x)
                 (< i x))
            (equal (nth i (make-list-logic a x)) a))
 :hints (("Goal" :induct 
                 (nth-returns-elem-of-make-list-ind-scheme i x)))))
#|
;Above lemmas should help prove this
(defthm nth-i-split-nat-<=
  (implies (and (pos-listp ws)
                (>= (len ws) 1))
       (<= (nth i (weighted-split-nat ws x)) (nfix x)))
  :hints (("Goal" :cases ((< i (len ws)))))
 :rule-classes (:rewrite :linear))
|#

;ADDED some hack defthms to help termination of recursive records with
;more than 3 fields.
(defthm nth-i-split-nat-3-<=
 ;(implies (and (integerp x)
 ;              (<= 0 x))
          (<= (nth i (weighted-split-nat '(1 1 1) (nfix x))) (nfix x))
 :rule-classes (:rewrite :linear))

(defthm nth-i-split-nat-4-<=
 ;(implies (and (integerp x)
 ;              (<= 0 x))
          (<= (nth i (weighted-split-nat '(1 1 1 1) (nfix x))) (nfix x))
 :rule-classes (:rewrite :linear))

(defthm nth-i-split-nat-5-<=
 ;(implies (and (integerp x)
 ;              (<= 0 x))
          (<= (nth i (weighted-split-nat '(1 1 1 1 1) (nfix x))) (nfix x))
 :rule-classes (:rewrite :linear))

(defthm nth-i-split-nat-6-<=
 ;(implies (and (integerp x)
 ;              (<= 0 x))
          (<= (nth i (weighted-split-nat '(1 1 1 1 1 1) (nfix x))) (nfix x))
 :rule-classes (:rewrite :linear))

; testing theorems

#|
(thm
 (implies (natp x)
          (natp (cadr (weighted-split-nat '(3 2 4) x)))))
(thm
 (implies (and (natp n) (natp x))
          (<= (nth i (weighted-split-nat '(1 1 1) x)) x)))
;|#

; testing

#|
(defun weighted-split-nat-downfrom (weights n)
  (declare (xargs :guard (and (pos-listp weights)
                              (consp weights)
                              (natp n))))
  (if (zp n)
    (list (list 0 '-> (weighted-split-nat weights 0)))
    (cons (list n '-> (weighted-split-nat weights n))
          (weighted-split-nat-downfrom weights (- n 1)))))

(defun weighted-split-nat-upto (weights n)
  (declare (xargs :guard (and (pos-listp weights)
                              (consp weights)
                              (natp n))))
  (reverse (weighted-split-nat-downfrom weights n)))

;(trace$ weighted-split-nat-step-tail)
(weighted-split-nat-upto '(1 1) 33)

;|#


; alternative interface

(defthm pos-listp--list-expt--2
  (implies (naturals-listp l)
           (pos-listp (list-expt 2 l)))
  :rule-classes (:rewrite :type-prescription))

(defun pow-weighted-split-nat (pow-weights x)
  (declare (xargs :guard (and (pos-listp pow-weights)
                              (consp pow-weights)
                              (natp x))))
  (let ((2**weights (list-expt 2 pow-weights)))
    (weighted-split-nat1 2**weights (product-list 2**weights) x)))

(defun split-nat (nways x)
  (declare (xargs :guard (and (posp nways)
                              (natp x))))
  (weighted-split-nat (make-list nways :initial-element 1) x))

(defthm split-nat--naturals-listp
  (naturals-listp (split-nat nways x))
   :rule-classes :type-prescription)


(defthm naturals-listp--true-listp
  (implies (naturals-listp x)
           (true-listp x))
   :rule-classes (:rewrite :forward-chaining))#|ACL2s-ToDo-Line|#