2
(include-book ;; Newline to fool ACL2/cert.pl dependency scanner
4
(acl2::begin-book t);$ACL2s-Preamble$|#
6
;; [Jared] Marking this book as non-acl2(r) because it attempts to prove:
8
;; (DEFTHM COMMON-LISP::COMPLEX-CONSTRUCTOR-DESTRUCTORS
9
;; (IMPLIES (ACL2-NUMBERP X)
10
;; (AND (RATIONALP (REALPART X))
11
;; (RATIONALP (IMAGPART X))))
15
;; which, I think, is not a theorem in ACL2(r).
17
; cert_param: (non-acl2r)
21
(include-book "splitnat")
22
(include-book "switchnat")
24
(include-book "defdata-core")
25
(include-book "random-state")
26
(include-book "enumerators-gen")
28
(include-book "library-support")
30
(include-book "register-data-constructor")
31
(include-book "register-type")
33
(include-book "register-combinator")
34
(include-book "listof")
35
(include-book "alistof")
36
(include-book "tau-characterization")
38
(include-book "tools/rulesets" :dir :system)
40
(include-book "var-book")
42
;; (make-event ;TODO make sure to get this working
44
;; (defdata::set-acl2s-defdata-verbose t)
45
;; (value '(value-triple :invisible)))
46
;; :check-expansion t)
49
(declare (xargs :mode :logic
54
;; (defthm allp-is-tau-predicate
55
;; (booleanp (allp x))
56
;; :rule-classes :tau-system)
60
;; :rule-classes (:rewrite))
62
(in-theory (disable allp))
65
; ALLP aliases TAU explicitly disallows predicates that hide equality to a
66
; constant. In particular it does not like the everywhere-true and
67
; everywhere-false predicates. So we built special support for these. We will
68
; store all aliases of allp in a table. This will be used by subtype-p and
69
; disjoint-p functions. Recall that we have given up homebrew datatype
70
; relationship graphs in favor of its implicit coding in TAU-DATABASE.
71
(table allp-aliases nil)
72
(table allp-aliases 'allp 'all :put) ;all will be registered as a defdata type below.
77
;; NOTE: ALL should not be used in subtype/disjoint relations
78
;; because of a caveat in tau
80
;;type constructors == product types
81
;;rational number constructor
83
(register-data-constructor (consp cons)
84
((allp car) (allp cdr))
89
;;jared's oset implementation
90
;; (defun set::non-empty-setp (x)
91
;; (declare (xargs :guard t))
93
;; (not (set::empty x))))
95
;; (register-data-constructor (SET::non-empty-setp SET::insert)
96
;; ((allp SET::head) (set::setp SET::tail))
101
(register-data-constructor (symbolp intern$)
102
((stringp symbol-name) (stringp symbol-package-name))
104
:proper nil) ;package name destructor fails
108
(register-data-constructor (rationalp /)
109
((integerp numerator) (posp denominator))
116
;;complex number type
117
(register-data-constructor (acl2-numberp complex)
118
((rationalp realpart) (rationalp imagpart))
126
(declare (xargs :guard (natp x)))
134
(declare (xargs :guard (natp x)))
141
(equal (succ (pred x)) x)))
143
(register-data-constructor (posp succ)
150
(defconst *character-values* *standard-chars*)
152
(defconst *len-character-values* (len *character-values*))
154
(defconst *alpha-chars*
155
'(#\A #\B #\C #\D #\E #\F #\G
156
#\H #\I #\J #\K #\L #\M #\N #\O #\P #\Q
157
#\R #\S #\T #\U #\V #\W #\X #\Y #\Z
159
#\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
160
#\n #\o #\p #\q #\r #\s #\t #\u #\v #\w
163
(defconst *len-alpha-chars* (len *alpha-chars*))
165
(defconst *alpha-low-chars*
167
#\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
168
#\n #\o #\p #\q #\r #\s #\t #\u #\v #\w
172
(defconst *len-alpha-low-chars* (len *alpha-low-chars*))
174
(defconst *alpha-up-chars*
175
'(#\A #\B #\C #\D #\E #\F #\G
176
#\H #\I #\J #\K #\L #\M #\N #\O #\P #\Q
177
#\R #\S #\T #\U #\V #\W #\X #\Y #\Z
180
(defconst *len-alpha-up-chars* (len *alpha-up-chars*))
182
(defconst *alpha-num-chars*
183
'(#\A #\B #\C #\D #\E #\F #\G
184
#\H #\I #\J #\K #\L #\M #\N #\O #\P #\Q
185
#\R #\S #\T #\U #\V #\W #\X #\Y #\Z
186
#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
188
#\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
189
#\n #\o #\p #\q #\r #\s #\t #\u #\v #\w
193
(defconst *len-alpha-num-chars* (len *alpha-num-chars*))
197
(implies (and (integerp m)
199
(integerp (mod m n)))
200
:rule-classes (:rewrite :type-prescription))
204
(local (include-book "arithmetic-5/top" :dir :system))
206
(defthm mod-nonnegative-integer-args
207
(implies (and (integerp x)
211
:rule-classes ((:rewrite :backchain-limit-lst 1)
212
(:type-prescription)))
215
(defun get-character-list-from-positions (l)
216
(declare (xargs :guard (nat-listp l)))
217
;:verify-guards nil))
220
(let* ((pos (mod (car l) (len *alpha-num-chars*)))
221
(char (nth pos *alpha-num-chars*)))
222
(cons char (get-character-list-from-positions (cdr l))))))
224
(defun get-standard-char-list-from-positions (l)
225
(declare (xargs :guard (nat-listp l)))
228
(let* ((pos (mod (car l) (len *standard-chars*)))
229
(char (nth pos *standard-chars*)))
230
(cons char (get-standard-char-list-from-positions (cdr l))))))
232
(defthm get-character-list-from-positions--character-listp
233
(implies (nat-listp l)
234
(and (character-listp (get-character-list-from-positions l))
235
(character-listp (get-standard-char-list-from-positions l))))
236
:hints (("goal" :in-theory (enable standard-char-listp))))
239
(in-theory (disable mod))
242
(defconst *boolean-values* '(t nil))
243
(defun nth-boolean (n) (nth (mod n 2) *boolean-values*))
244
;(define-enumeration-type boolean '(t nil))
248
;-------- define some enumerators --------;
251
(declare (xargs :guard (natp n)))
255
(declare (xargs :guard (natp n)))
258
(defthm nth-nat-index
259
(equal (nat-index (nth-nat n))
262
(defthm nat-index-nth
263
(equal (nth-nat (nat-index n))
268
(declare (xargs :guard (natp n)))
275
(defthm nth-pos-is-posp
278
:hints (("goal" :in-theory (enable nth-pos)))
279
:rule-classes (:rewrite :type-prescription))
281
(defexec pos-index (i)
282
(declare (xargs :guard (posp i)))
289
(defthm nth-pos-index
290
(equal (pos-index (nth-pos n))
293
(defthm pos-index-nth
294
(implies (and (integerp i)
296
(equal (nth-pos (pos-index i))
300
(defun pos-multiple-of-threep (v)
304
(defun nth-pos-multiple-of-three (n)
309
(defun pos-multiple-of-three-index (i)
310
(if (pos-multiple-of-threep i)
315
(defun nth-integer (n)
316
(declare (xargs :guard (natp n)))
317
(let* (;(n (mod n 1000))
324
(defun nth-integer-between (n lo hi)
325
(declare (xargs :guard (and (natp n)
328
(let ((range (nfix (- hi lo))))
329
(+ lo (mod n (1+ range)))))
331
(defun integer-index (i)
332
(declare (xargs :guard (integerp i)))
334
(1+ (* (- (1+ i)) 2))
339
(include-book "arithmetic-5/top" :dir :system))
341
(defthm nth-pos-multiple-three-type
342
(pos-multiple-of-threep (nth-pos-multiple-of-three n)))
345
(defthm nth-pos-multiple-of-three-index
347
(equal (pos-multiple-of-three-index (nth-pos-multiple-of-three n))
350
(defthm pos-multiple-of-three-index-nth
351
(implies (pos-multiple-of-threep i)
352
(equal (nth-pos-multiple-of-three (pos-multiple-of-three-index i))
356
(defthm nth-integer-index
360
(equal (integer-index (nth-integer n))
362
(defthm integer-index-nth
365
(equal (nth-integer (integer-index i))
369
;;only strings upto len 1 to 8
370
(defun nth-string (n)
371
(declare (xargs :guard (natp n)))
372
;:verify-guards nil))
373
(let* ((str-len (1+ (mod n 7)))
374
(char-pos-list (defdata::split-nat str-len n))
375
(charlist (get-character-list-from-positions char-pos-list)))
376
(coerce charlist 'string)))
378
(defun standard-stringp (x)
379
(declare (xargs :guard t))
381
(standard-char-listp (coerce x 'list))))
383
(defun nth-standard-string (n)
384
(declare (xargs :guard (natp n)))
385
;:verify-guards nil))
386
(let* ((str-len (1+ (mod n 7)))
387
(char-pos-list (defdata::split-nat str-len n))
388
(charlist (get-standard-char-list-from-positions char-pos-list)))
389
(coerce charlist 'string)))
392
((nth-symbol (n) t :guard (natp n)))
393
(local (defun nth-symbol (n)
394
(declare (xargs :guard (natp n)))
398
(defun nth-symbol-builtin (n)
399
(declare (xargs :guard (natp n)))
400
;:verify-guards nil))
401
(intern$ (nth-string n) "ACL2"))
403
(defattach nth-symbol nth-symbol-builtin)
406
((nth-character (n) t :guard (natp n)))
407
(local (defun nth-character (n)
408
(declare (xargs :guard (natp n)))
412
(defun nth-character-builtin (n)
413
(declare (xargs :guard (natp n)))
414
(nth (mod n *len-character-values*) *character-values*))
416
(defattach nth-character nth-character-builtin)
418
(defun nth-alpha-num-character (n)
419
(declare (xargs :guard (natp n)))
420
(nth (mod n *len-alpha-num-chars*) *alpha-num-chars*))
423
(defun positive-ratiop (x)
424
(declare (xargs :guard t))
430
(defun nth-positive-ratio (n)
431
(declare (xargs :guard (natp n)))
432
(mbe :logic (if (natp n)
433
(let* ((two-n-list (defdata::split-nat 2 n))
434
(alpha (car two-n-list))
435
(beta (cadr two-n-list))
437
(num (+ (floor beta den) beta)))
440
:exec (let* ((two-n-list (defdata::split-nat 2 n))
441
(alpha (car two-n-list))
442
(beta (cadr two-n-list))
444
(num (+ (floor beta den) beta)))
449
(defun negative-ratiop (x)
450
(declare (xargs :guard t))
457
(defun nth-negative-ratio (n)
458
(declare (xargs :guard (natp n)))
459
(let* ((two-n-list (defdata::split-nat 2 n))
460
(alpha (car two-n-list))
461
(beta (cadr two-n-list))
463
(num (+ (floor beta den) beta)))
467
;(defdata rat (oneof 0 positive-ratio negative-ratio))
468
;DOESNT WORK so positive/negative ratio are not consistent types ;TODO
469
;(local (include-book "arithmetic-5/top" :dir :system))
470
;(thm (nat-listp (defdata::split-nat 2 n)))
471
;(thm (positive-ratiop (nth-positive-ratio n)))
475
(declare (xargs :guard t))
480
(declare (xargs :guard (natp n)))
484
(defdata int (oneof 0 pos neg))
485
(thm (iff (integerp x) (intp x)))
488
(defun nth-positive-rational (n)
489
(declare (xargs :guard (natp n)))
490
(let* ((two-n-list (defdata::split-nat 2 n))
491
(num (nth-pos (car two-n-list)))
492
(den (nth-pos (cadr two-n-list))))
495
(defun nth-negative-rational (n)
496
(declare (xargs :guard (natp n)))
497
(let* ((two-n-list (defdata::split-nat 2 n))
498
(num (nth-neg (car two-n-list)))
499
(den (nth-pos (cadr two-n-list))))
501
(defun positive-rationalp (x)
502
(declare (xargs :guard t))
506
(defun negative-rationalp (x)
507
(declare (xargs :guard t))
512
;(defdata rational (oneof 0 positive-rational negative-rational))
513
(defun nth-rational (n)
514
(declare (xargs :guard (natp n)))
515
(let* ((two-n-list (defdata::split-nat 2 n))
516
(num (nth-integer (car two-n-list)))
517
(den (nth-pos (cadr two-n-list))))
522
(defthm nth-rat-is-ratp
524
(rationalp (nth-rational x)))
525
:rule-classes (:rewrite :type-prescription))
527
;lo included, hi included
530
(defun nth-rational-between (n lo hi);inclusive
531
(declare (xargs :guard (and (natp n)
535
(let* ((two-n-list (defdata::split-nat 2 n))
536
(den (nth-pos (car two-n-list)))
537
(num (nth-integer-between (cadr two-n-list) 0 (1+ den)))
539
(+ lo (* (/ num den) range))))
542
(defun nth-complex-rational (n)
543
(declare (xargs :guard (natp n)))
544
(let* ((two-n-list (defdata::split-nat 2 n))
545
(rpart (nth-rational (defdata::nfixg (car two-n-list))))
546
(ipart (nth-rational (defdata::nfixg (cadr two-n-list)))))
547
(complex rpart ipart)))
550
((nth-acl2-number (n) t :guard (natp n)))
551
(local (defun nth-acl2-number (n)
552
(declare (xargs :guard (natp n)))
556
(defun nth-acl2-number-builtin (n)
557
(declare (xargs :guard (natp n)))
558
(b* (((mv choice seed)
559
(defdata::switch-nat 4 n)))
562
(1 (nth-integer seed))
563
(2 (nth-rational seed))
564
(t (nth-complex-rational seed)))))
566
(defattach nth-acl2-number nth-acl2-number-builtin)
568
;(defdata character-list (listof character))
569
;;only strings upto len 1 to 8
570
(defun nth-character-list (n)
571
(declare (xargs :guard (natp n)))
572
;:verify-guards nil))
573
(let* ((str-len (1+ (mod n 7)))
574
(char-pos-list (defdata::split-nat str-len n))
575
(charlist (get-character-list-from-positions char-pos-list)))
578
(defun nth-standard-char-list (n)
579
(declare (xargs :guard (natp n)))
580
;:verify-guards nil))
581
(let* ((str-len (1+ (mod n 7)))
582
(char-pos-list (defdata::split-nat str-len n))
583
(charlist (get-standard-char-list-from-positions char-pos-list)))
587
(defconst *base-types* '((BOOLEAN 2 *BOOLEAN-VALUES* . BOOLEANP)
588
(CHARACTER-LIST T NTH-CHARACTER-LIST . CHARACTER-LISTP)
589
(SYMBOL T NTH-SYMBOL . SYMBOLP)
590
(STRING T NTH-STRING . STRINGP)
591
(CHARACTER 62 *CHARACTER-VALUES* . CHARACTERP)
592
(ACL2-NUMBER T NTH-ACL2-NUMBER . ACL2-NUMBERP)
593
(COMPLEX-RATIONAL T NTH-COMPLEX-RATIONAL . COMPLEX-RATIONALP)
594
(RATIONAL T NTH-RATIONAL . RATIONALP)
595
(POS T NTH-POS . POSP)
596
(NAT T NTH-NAT . NATP)
597
(INTEGER T NTH-INTEGER . INTEGERP)))
599
(declare (xargs :guard (natp n))
601
(let* ((num-types (len *base-types*))
602
(two-n-list (defdata::split-nat 2 n))
603
(choice (mod (car two-n-list) num-types))
604
(seed (cadr two-n-list))
605
(type-info (cdr (nth choice *base-types*)))
606
(type-size (car type-info))
607
(type-enum (cadr type-info)))
608
(if (eq type-size 't) ;inf
610
`(nth ,(mod seed type-size) ,type-enum))))||#
613
(defconst *number-testing-limit* 1000)
615
;ADDED restricted testing enumerators for all number types
616
(defun nth-nat-testing (n)
617
(declare (xargs :guard (natp n)))
618
(let ((n-small (mod n *number-testing-limit*)))
620
(defun nth-pos-testing (n)
621
(declare (xargs :guard (natp n)))
622
(let ((n-small (mod n *number-testing-limit*)))
624
(defun nth-neg-testing (n)
625
(declare (xargs :guard (natp n)))
626
(let ((n-small (mod n *number-testing-limit*)))
629
(defun nth-integer-testing (n)
630
(declare (xargs :guard (natp n)))
631
(let ((n-small (mod n *number-testing-limit*)))
632
(nth-integer n-small)))
634
(defun nth-positive-ratio-testing (n)
635
(declare (xargs :guard (natp n)))
636
(let ((n-small (mod n *number-testing-limit*)))
637
(nth-positive-ratio n-small)))
638
(defun nth-negative-ratio-testing (n)
639
(declare (xargs :guard (natp n)))
640
(let ((n-small (mod n *number-testing-limit*)))
641
(nth-negative-ratio n-small)))
642
(defun nth-rational-testing (n)
643
(declare (xargs :guard (natp n)))
644
(let ((n-small (mod n *number-testing-limit*)))
645
(nth-rational n-small)))
646
(defun nth-positive-rational-testing (n)
647
(declare (xargs :guard (natp n)))
648
(let ((n-small (mod n *number-testing-limit*)))
649
(nth-positive-rational n-small)))
650
(defun nth-negative-rational-testing (n)
651
(declare (xargs :guard (natp n)))
652
(let ((n-small (mod n *number-testing-limit*)))
653
(nth-negative-rational n-small)))
654
(defun nth-acl2-number-testing (n)
655
(declare (xargs :guard (natp n)))
656
(let ((n-small (mod n *number-testing-limit*)))
657
(nth-acl2-number n-small)))
658
(defun nth-complex-rational-testing (n)
659
(declare (xargs :guard (natp n)))
660
(let ((n-small (mod n *number-testing-limit*)))
661
(nth-complex-rational n-small)))
665
;; (declare (xargs :guard t))
669
(declare (xargs :guard (natp n)))
670
(b* (((mv choice seed)
671
(defdata::weighted-switch-nat
688
(2 (nth-nat-testing seed));smaller numbers
689
(3 (nth-symbol seed))
690
(4 (nth-string seed))
691
(5 (nth-alpha-num-character seed)) ;(nth-character seed))
692
(6 (nth-acl2-number seed))
693
(7 (nth-rational seed))
694
(8 (nth-pos-testing seed))
696
(t (nth-integer-testing seed)))))
698
;(defdata atom (oneof acl2-number character symbol string))
703
; register-type ought to also test if not prove the following:
704
; TODO:Note it does not prove that type is sound neither that is is complete
705
; By Type Soundness i mean (thm (implies (natp n) (Tp (nth-T n)))
706
; By Type Completeness i mean (thm (implies (Tp x)
707
; (equal x (nth-T (T-index x))))
708
; where (nth-T (T-index x)) = x
710
(defmacro register-custom-type (typename typesize enum pred &key verbose)
711
`(defdata::register-type ,typename :size ,typesize :predicate ,pred :enumerator ,enum :verbose ,verbose))
713
(register-custom-type nat t nth-nat natp)
715
(register-custom-type pos t nth-pos posp)
717
(register-custom-type neg t nth-neg negp )
718
(register-custom-type integer t nth-integer integerp )
719
(register-custom-type positive-ratio t nth-positive-ratio positive-ratiop)
720
(register-custom-type negative-ratio t nth-negative-ratio negative-ratiop )
721
(register-custom-type positive-rational t nth-positive-rational positive-rationalp )
722
(register-custom-type negative-rational t nth-negative-rational negative-rationalp )
723
(register-custom-type rational t nth-rational rationalp )
724
(register-custom-type complex-rational t nth-complex-rational complex-rationalp )
725
(register-custom-type acl2-number t nth-acl2-number acl2-numberp )
726
(register-custom-type boolean 2 nth-boolean booleanp );taken care of by define-enumeration-type
727
(register-custom-type symbol t nth-symbol symbolp)
729
(verify-termination acl2::legal-constantp)
730
(verify-guards acl2::legal-constantp)
731
(defun proper-symbolp (x)
732
(declare (xargs :guard t))
734
(not (or (keywordp x);a keyword
735
(booleanp x);t or nil
736
(acl2::legal-constantp x)))))
738
(in-theory (disable acl2::legal-constantp))
740
(defconst *nice-symbol-names*
741
'(x y z a b c i j k p q r s u v w l d e f g h m n))
743
(defun nth-proper-symbol (n)
744
(declare (xargs :guard (natp n)))
745
(let ((psym (nth-symbol n)))
746
(if (proper-symbolp psym)
748
(nth (mod n (len *nice-symbol-names*)) *nice-symbol-names*))))
750
(register-custom-type proper-symbol t nth-proper-symbol proper-symbolp)
752
(defun nth-character-uniform (m seed)
753
(declare (ignorable m))
754
(declare (type (unsigned-byte 31) seed))
755
(declare (xargs :guard (and (natp m)
756
(unsigned-byte-p 31 seed))))
758
(defdata::random-natural-seed seed)
759
(mv (nth-character n) (the (unsigned-byte 31) seed))))
761
(defdata::register-type character :size 62 :enumerator nth-character :predicate characterp :enum/acc nth-character-uniform)
766
;(define-enumeration-type standard-char *standard-chars*)
767
(defun nth-standard-char (n)
768
(declare (xargs :guard (natp n)))
769
(nth (mod n 96) *standard-chars*))
770
(register-custom-type standard-char 96 nth-standard-char standard-char-p)
771
(register-custom-type string t nth-string stringp)
772
(register-custom-type standard-string t nth-standard-string standard-stringp)
773
(register-custom-type atom t nth-atom atom);instead of atomp Exception
775
;added the above atom primitive types in the data-type graph using register-custom-type
778
(defconst *z-values* '(0 -1 "a" 1/3 :a)) ;for zp
780
(declare (xargs :guard (natp n)))
781
(nth (mod n 5) *z-values*))
782
(defun nth-z-uniform (m seed)
783
(declare (ignorable m))
784
(declare (type (unsigned-byte 31) seed))
785
(declare (xargs :guard (and (natp m)
786
(unsigned-byte-p 31 seed))))
788
(defdata::random-natural-seed seed)
789
(mv (nth-z n) (the (unsigned-byte 31) seed))))
790
(defdata::register-type z :size t :enumerator nth-z :predicate zp :enum/acc nth-z-uniform)
793
;Subtype relations betweem the above
794
;pos is a subtype of nat (Note the direction)
795
(defdata-subtype pos nat)
797
;nat is a subtype of integer
798
(defdata-subtype nat integer)
799
(defdata-subtype neg integer)
800
(defdata-subtype integer rational)
801
(defdata-subtype positive-ratio rational)
802
(defdata-subtype positive-rational rational) ;Aug 18 2011
803
(defdata-subtype negative-ratio rational)
804
(defdata-subtype negative-rational rational) ;Aug 18 2011
805
(defdata-subtype complex-rational acl2-number)
806
(defdata-subtype rational acl2-number)
807
(defdata-subtype acl2-number atom)
808
(defdata-subtype boolean symbol)
809
(defdata-subtype proper-symbol symbol)
811
(defdata-subtype standard-char character :hints (("Goal" :in-theory (enable standard-char-p))))
812
(defdata-subtype character atom)
813
(defdata-subtype string atom)
814
(defdata-subtype symbol atom)
816
(defdata ratio (oneof positive-ratio negative-ratio))
819
(defdata-subtype ratio rational)
822
(defdata-subtype neg negative-rational)
823
(defdata-subtype pos positive-rational)
824
(defdata-subtype negative-rational z)
825
(defdata-subtype ratio z)
826
(defdata-subtype complex-rational z)
827
(defdata-subtype symbol z)
828
(defdata-subtype character z)
829
(defdata-subtype string z)
830
(defdata-disjoint pos z)
832
(defdata-subtype standard-string string)
835
;Disjoint relations between the above types
836
(defdata-disjoint acl2-number character)
837
(defdata-disjoint acl2-number symbol)
838
(defdata-disjoint character string)
839
(defdata-disjoint character symbol)
840
(defdata-disjoint string symbol)
841
(defdata-disjoint boolean proper-symbol)
844
(defthm termination-tree-enum-cdr
846
(and (< (acl2-count (cdr x))
848
(< (acl2-count (car x))
850
(defthm termination-tree-enum-dec
851
(implies (< (acl2-count x1) (acl2-count x2))
852
(and (< (acl2-count (car x1)) (acl2-count x2))
853
(< (acl2-count (cdr x1)) (acl2-count x2)))))
854
(defthm terminination-tree-enum-nth
855
(<= (acl2-count (nth i x))
857
:rule-classes (:rewrite :linear))
859
(defthm termination-tree-enum-dec2
860
(implies (< (acl2-count x1) (acl2-count x2))
861
(< (acl2-count (nth i x1)) (acl2-count x2)))
862
:hints (("Goal" :in-theory (disable nth))))
866
; IMPORTANT: PROPER-CONS is put ahead of all lists, so that in the
867
; event of intersecting it with lists, the lists are given preference,
868
; by the virtue of appearing earlier in the reverse chronological
869
; order of type-metadata table. Lists of various types are certainly
870
; more amenable to Cgen/Enumeration than proper-conses.
872
;; (defdata list-a (list atom))
873
;; (defdata list-aa (list atom atom))
874
;; (defdata list-aaa (list atom atom atom))
877
(defdata cons-atom (cons atom atom))
879
(defdata-disjoint cons-atom atom)
882
(defdata cons-ca-ca (cons cons-atom cons-atom))
883
(defdata cons-cca-cca (cons cons-ca-ca cons-ca-ca) )
885
;; (defdata list-a-ca (list atom cons-atom) )
886
;; (defdata list-aa-ca (list atom atom atom cons-atom) )
887
;; (defdata list-aa-cca (list atom atom cons-ca-ca) )
888
;; (defdata list-aaaa-cccca (list cons-atom cons-cca-cca) )
889
;; (defdata list-ca-cca (list cons-atom cons-ca-ca) )
890
;; (defdata list-la-la (list list-aa list-aa) )
893
;MAJOR CHANGE June 6th 2010, now we have no guards in any enumerators
894
(defun nth-proper-cons (n)
895
; (declare (xargs :guard (natp n)))
896
(declare (xargs :mode :program))
897
(b* (((mv choice seed)
898
(defdata::weighted-switch-nat
914
(0 (list (nth-atom seed)))
915
(1 (b* (((list i1 i2) (defdata::split-nat 2 seed))) (list (nth-atom i1) (nth-atom i2))))
916
(2 (b* (((list i1 i2 i3) (defdata::split-nat 3 seed))) (list (nth-atom i1) (nth-atom i2) (nth-atom i3))))
917
(3 (b* (((list i1 i2 i3) (defdata::split-nat 3 seed))) (list (nth-atom i1) (cons (nth-atom i2) (nth-atom i3))))) ;(nth-list-a-ca seed))
918
(4 (b* (((list i1 i2 i3 i4 i5) (defdata::split-nat 5 seed)))
919
(list (nth-atom i1) (nth-atom i2) (nth-atom i3) (cons (nth-atom i4) (nth-atom i5))))) ;(nth-list-aa-ca seed))
920
(5 (b* (((list i1 i2 i3 i4 i5 i6) (defdata::split-nat 6 seed)))
921
(list (nth-atom i1) (nth-atom i2) (cons (cons (nth-atom i3) (nth-atom i4))
922
(cons (nth-atom i5) (nth-atom i6)))))) ;(list-aa-cca seed))
923
(6 (b* (((list i1 i2 i3 i4 i5) (defdata::split-nat 5 seed)));(list-aaaa-cccca seed))
924
(list (nth-cons-atom i1) (cons (cons (nth-cons-atom i2) (nth-cons-atom i3))
925
(cons (nth-cons-atom i4) (nth-cons-atom i5))))))
926
(7 (b* (((list i1 i2 i3 i4 i5) (defdata::split-nat 5 seed))) ;(list-ca-cca seed))
927
(list (nth-cons-atom i1) (cons (cons (nth-atom i2) (nth-atom i3))
928
(cons (nth-atom i4) (nth-atom i5))))))
929
(8 (b* (((list i1 i2 i3 i4 i5) (defdata::split-nat 5 seed)))
930
(list (nth-atom i1) (nth-atom i2) (nth-atom i3) (nth-atom i4) (nth-atom i5))))
933
(register-custom-type proper-cons t nth-proper-cons proper-consp)
938
(defdata nat-list (listof nat))
940
;; (verify-termination pos-listp) ; pos-listp is in program mode, so we need this.
941
;; (verify-guards pos-listp)
943
(defdata pos-list (listof pos))
945
(defdata integer-list (listof integer) )
946
(defdata rational-list (listof rational) )
947
(defdata complex-rational-list (listof complex-rational) )
949
(defdata acl2-number-list (listof acl2-number) )
950
(defdata boolean-list (listof boolean) )
951
(defdata symbol-list (listof symbol) )
952
(defdata::register-type character-list
954
:predicate character-listp
955
:enumerator nth-character-list
956
:prettyified-def (listof character))
958
(defdata::register-type standard-char-list
960
:predicate standard-char-listp
961
:enumerator nth-standard-char-list
962
:prettyified-def (listof standard-char))
964
; TAU characterization of standard-char-list (copied and string/replaced from def=>String-list)
966
def=>standard-char-list
967
(and (implies (and (equal v1 'nil))
968
(standard-char-listp v1))
969
(implies (and (standard-char-p v11)
970
(standard-char-listp v12))
971
(standard-char-listp (cons v11 v12))))
972
:hints (("goal" :in-theory (e/d (standard-char-listp) (standard-char-p))))
973
:rule-classes (:tau-system :rewrite))
975
standard-char-list=>def
976
(and (implies (and (standard-char-listp v1)
980
(and (standard-char-listp v1)
982
(and (standard-char-p (car v1))
983
(standard-char-listp (cdr v1)))))
984
:hints (("goal" :in-theory (e/d (standard-char-listp) (standard-char-p))))
985
:rule-classes (:tau-system (:forward-chaining :trigger-terms ((standard-char-listp v1)))))
988
(defdata string-list (listof string))
989
(defdata atom-list (listof atom))
990
(defdata-subtype pos-list nat-list)
991
(defdata-subtype nat-list integer-list)
992
(defdata-subtype integer-list rational-list)
993
(defdata-subtype complex-rational-list acl2-number-list)
994
(defdata-subtype rational-list acl2-number-list )
995
(defdata-subtype acl2-number-list atom-list)
996
(defdata-subtype boolean-list symbol-list)
997
(defdata-subtype standard-char-list character-list)
998
(defdata-subtype character-list atom-list)
999
(defdata-subtype string-list atom-list)
1001
(defdata-subtype symbol-list atom-list)
1005
;TODO.NOTE: Note that all the enumerators defined below are purely heuristic and
1006
;are not consistent/complete with their respective predicates.
1009
;; (verify-guards NTH-NAT-LIST)
1010
;; (verify-guards NTH-RATIONAL-LIST)
1011
;; (verify-guards NTH-SYMBOL-LIST)
1012
;; (verify-guards NTH-CONS-ATOM)
1013
;; (verify-guards NTH-CONS-CA-CA)
1014
;; (verify-guards NTH-STRING-LIST)
1015
;; (verify-guards NTH-ATOM-LIST)
1018
(declare (xargs :mode :program))
1019
(declare (xargs :guard (natp n)))
1022
;;:verify-guards nil))
1023
(b* (((mv choice seed)
1024
(defdata::weighted-switch-nat
1051
(3 (nth-integer-testing seed))
1052
(4 (nth-rational seed))
1053
(5 (nth-nat-list seed))
1054
(6 (nth-symbol seed))
1055
(7 (nth-string seed))
1056
(8 (nth-alpha-num-character seed)) ;(nth-character seed))
1057
(9 (nth-acl2-number seed))
1058
(10 (nth (mod seed 2) *boolean-values*))
1059
(11 (nth-nat-testing seed))
1060
(12 (nth-pos-testing seed))
1061
(13 (nth-rational-list seed))
1062
(14 (nth-symbol-list seed))
1063
(15 (nth-cons-atom seed))
1064
(16 (nth-character-list seed))
1065
(17 (b* (((list i1 i2) (defdata::split-nat 2 seed)))
1066
(cons (nth-cons-atom i1) (nth-cons-atom i2)))) ;(cons-ca-ca seed))
1067
(18 (nth-string-list seed))
1068
(19 (nth-atom-list seed))
1069
(t 'nil)))) ;this case should not come up
1072
(register-custom-type all t nth-all allp)
1075
;We will also name a special type, the empty type, which has no elements in its typeset.
1076
(defconst *empty-values* '())
1077
(defun nth-empty (x)
1078
(declare (ignore x) (xargs :guard (natp x)))
1079
(er hard? 'nth-empty "~| Empty enumerator~%"))
1080
;TODO - if type is already registered, then we should be able to pick the predicate
1081
;from the table instead of syntactically from the type.
1083
(declare (ignore x) (xargs :guard t))
1086
(defthm emptyp-is-tau-predicate
1087
(booleanp (emptyp x))
1088
:rule-classes :tau-system)
1090
(register-custom-type empty 0 nth-empty emptyp)
1091
;NOTE: empty is a special type, so we treat it specially and seperately, rather than the
1092
;usual way of going through the data type graph, and it might lead to inconsistency
1093
;with the ACL2 axioms about datatypes.
1095
(defdata cons (cons all all))
1099
(defdata acons (cons (cons all all) all))
1101
;;associated key-value pairs
1102
;; (defun aconsp (x)
1103
;; (declare (xargs :guard t))
1104
;; (and (consp x) (consp (car x))))
1105
(defun acons-caar (x) (declare (xargs :guard (aconsp x))) (caar x))
1106
(defun acons-cdar (x) (declare (xargs :guard (aconsp x))) (cdar x))
1107
(defun acons-cdr (x) (declare (xargs :guard (aconsp x))) (cdr x))
1109
(defthm acons-acl2-count-lemma
1110
(equal (acl2-count (acons x1 x2 x3))
1111
(+ 2 (acl2-count x1) (acl2-count x2) (acl2-count x3))))
1113
(in-theory (enable aconsp))
1114
(register-data-constructor (aconsp acons)
1115
((allp acons-caar) (allp acons-cdar) (allp acons-cdr))
1116
:rule-classes (:rewrite)
1119
(defthm acons-alist-lemma
1121
(alistp (acons x1 x2 x)))
1122
:rule-classes ((:rewrite :backchain-limit-lst 1)))
1124
; (in-theory (disable acons aconsp acons-caar acons-cdar acons-cdr)) TODO --
1125
; Whats the point of acons as a constructor if these functions are not disabled
1126
; -- Ohh well, lets ride on cons for now. [2014-09-24 Wed]
1131
(defdata list (oneof cons nil))
1133
(DEFUNS (NTH-TRUE-LIST
1135
(declare (xargs :mode :program))
1136
(DECLARE (XARGS :guard (natp x) :verify-guards nil
1138
(IF (OR (NOT (INTEGERP X)) (< X 1))
1141
(LET ((INFXLST (DEFDATA::SPLIT-NAT 2 X)))
1142
(CONS (LET ((X (NTH 0 INFXLST))) (NTH-ALL X))
1143
(LET ((X (NTH 1 INFXLST)))
1144
(NTH-TRUE-LIST X))))))))
1146
(defdata::register-type true-list
1148
:predicate true-listp
1149
:enumerator nth-true-list
1150
:prettyified-def (listof all))
1154
(defdata alist (listof (cons all all)))
1155
(defdata symbol-alist (alistof symbol all))
1156
(verify-termination character-alistp)
1157
(defdata character-alist (alistof character all))
1158
(defdata r-symbol-alist (alistof all symbol))
1159
(defdata-subtype symbol-alist alist)
1160
(defdata-subtype character-alist alist)
1161
(defdata-subtype r-symbol-alist alist)
1163
;TODO standard-string-alist has very poor theory support!!
1164
;(defdata standard-string-list (listof standard-string))
1165
;(defdata-subtype standard-string-list string-list)
1166
;(defdata standard-string-alist (alistof standard-string all))
1167
;(defdata-subtype standard-string-alist alist)
1169
;(verify-guards nth-true-list)
1170
(defdata true-list-list (listof true-list))
1171
(defdata-subtype true-list-list true-list)
1176
(defdata-subtype cons z)
1177
(defdata-subtype list z)
1180
(defun all-but-zero-nil-tp (x)
1181
(declare (xargs :guard t))
1182
(and (not (equal x 0))
1184
(not (equal x 'nil))))
1186
(defun nth-all-but-zero-nil-t (n)
1187
(declare (xargs :mode :program))
1188
(declare (xargs :guard (natp n)))
1190
(b* (((mv choice seed)
1191
(defdata::weighted-switch-nat
1211
(0 (nth-integer-testing seed))
1212
(1 (nth-character-list seed))
1213
(2 (nth-symbol seed))
1214
(3 (nth-string seed))
1215
(4 (nth-character seed))
1216
(5 (nth-pos-testing seed))
1217
(6 (nth-positive-ratio seed))
1218
(7 (nth-negative-ratio seed))
1219
(8 (nth-complex-rational seed))
1220
(9 (nth-rational-list seed))
1221
(10 (nth-symbol-list seed))
1222
(11 (nth-cons-atom seed))
1223
(12 (nth-nat-list seed))
1224
(13 (b* (((list i1 i2) (defdata::split-nat 2 seed)))
1225
(cons (nth-cons-atom i1) (nth-cons-atom i2)))) ;(cons-ca-ca seed))
1226
(14 (nth-string-list seed))
1227
(15 (nth-atom-list seed))
1230
(register-custom-type all-but-zero-nil-t t nth-all-but-zero-nil-t all-but-zero-nil-tp)
1233
(defun nth-wf-key (n) ;;since nth-all-but-zero-nil-t has strings of length less than 8, it cannot include the ill-formed-key
1234
(declare (xargs :guard (natp n)))
1235
(declare (xargs :mode :program))
1236
(nth-all-but-zero-nil-t n))
1238
(register-custom-type wf-key t nth-wf-key wf-keyp)
1240
;; Same problem as in sets. A nil is also a good-map!
1242
;; (defun non-empty-good-map (x)
1243
;; (declare (xargs :guard t))
1248
(defun all-but-nilp (x)
1249
(declare (xargs :guard t))
1250
(not (equal x 'nil)))
1252
; TODO: this is a major hiccup of our map and record implementation, disallowing nil explicitly!!
1253
;; (register-data-constructor (non-empty-good-map mset)
1254
;; ((wf-keyp caar) (all-but-nilp cdar) (good-map cdr))
1258
(register-data-constructor (good-map mset)
1259
((wf-keyp caar) (allp cdar) (good-map cdr))
1260
:hints (("Goal" :in-theory (enable good-map)))
1263
(defun nth-all-but-nil (n)
1264
(declare (xargs :mode :program))
1265
(declare (xargs :guard (natp n)))
1268
(t (nth-all-but-zero-nil-t n))))
1270
(register-custom-type all-but-nil t nth-all-but-nil all-but-nilp)
1272
(defdata-subtype all-but-zero-nil-t all)
1273
(defdata-subtype all-but-nil all) ;make this the pseudo top type!
1275
(defdata-subtype acons cons :hints (("Goal" :in-theory (enable aconsp))))
1276
(defdata-subtype cons all-but-nil)
1277
(defdata-subtype atom all)
1278
(defdata-subtype atom-list true-list)
1279
(defdata-subtype alist true-list)
1280
(defdata-subtype list all)
1281
(defdata-subtype true-list list)
1286
(defdata cons-cccca-cccca (cons cons-cca-cca cons-cca-cca) )
1287
(defdata cons-a-ca (cons atom cons-atom) )
1288
(defdata cons-a-cca (cons atom cons-ca-ca) )
1289
(defdata cons-a-cccca (cons atom cons-cca-cca))
1290
(defdata cons-ca-cca (cons cons-atom cons-ca-ca))
1292
(defdata cons-ca-cccca (cons cons-atom cons-cca-cca) )
1293
;(verify-guards allp)
1294
(defdata cons-all-all-but-zero-nil-t (cons all all-but-zero-nil-t) )
1296
(defun nth-improper-cons (n)
1297
; (declare (xargs :guard (natp n)))
1298
(declare (xargs :mode :program))
1299
(b* (((mv choice seed)
1300
(defdata::weighted-switch-nat
1302
1 ;cons-all-all-but-zero-nil-t
1313
(0 (nth-cons-all-all-but-zero-nil-t seed))
1314
(1 (nth-cons-ca-ca seed))
1315
(2 (nth-cons-a-ca seed))
1316
(3 (nth-cons-a-cca seed))
1317
(4 (nth-cons-a-cccca seed))
1318
(5 (nth-cons-cccca-cccca seed))
1319
(6 (nth-cons-ca-cca seed))
1320
(7 (nth-cons-ca-cccca seed))
1323
(register-custom-type improper-cons t nth-improper-cons improper-consp)
1325
(defdata-subtype improper-cons cons)
1328
(defdata-subtype proper-cons cons)
1330
;this was missing before and so we werent inferring proper-consp when
1331
;type-alist knew both true-listp and proper-consp, and this is common in ACL2
1332
(defdata-subtype proper-cons true-list)
1334
(defdata-disjoint proper-cons improper-cons)
1335
(defdata-disjoint atom cons)
1340
;; (defmacro disjoint-p (T1 T2)
1341
;; ":Doc-Section DATA-DEFINITIONS
1342
;; top-level query wether two types are disjoint~/
1343
;; ~c[(disjoint-p T1 T2)] asks the question
1344
;; are T1, T2 disjoint? This call makes a quick
1345
;; lookup into the internal data type graph where
1346
;; disjoint relation information provided by the user
1347
;; in the past is stored and used to compute the
1348
;; disjoint relation closure. If they are pairwise
1349
;; disjoint (according to the computed information)
1350
;; then we get back an affirmative , i.e ~c[t]. otherwise
1351
;; it returns ~c[nil].
1355
;; (disjoint-p cons list)
1356
;; (disjoint-p pos acl2-number)
1357
;; (disjoint-p integer complex)
1361
;; (disjoint-p <Type-name1> <Type-name2>)
1364
;; `(trans-eval '(defdata::is-disjoint$$ ',t1 ',t2 defdata::R$ defdata::types-ht$) 'disjoint-p state nil))
1365
; `(is-disjoint ',T1 ',T2 R$ types-ht$))
1368
;; (defmacro show-all-defdata-types ()
1369
;; `(table-alist 'defdata::types-info-table (w state)))
1371
;; (defmacro subtype-p (T1 T2)
1372
;; ":Doc-Section DATA-DEFINITIONS
1373
;; top-level query wether two types are disjoint~/
1374
;; ~c[(subtype-p T1 T2)] asks the question
1375
;; is T1 a subtype of T2? This call makes a quick
1376
;; lookup into the internal data type graph where
1377
;; subtype relation information provided by the user
1378
;; in the past is stored and used to compute the
1379
;; subtype relation closure. If T1 is indeed a subtype
1380
;; of T2 (according to the computed information)
1381
;; then we get back an affirmative , i.e ~c[t]. otherwise
1382
;; it returns ~c[nil].
1386
;; (subtype-p boolean atom)
1387
;; (subtype-p character string)
1388
;; (subtype-p list cons)
1392
;; (subtype-p <Type-name1> <Type-name2>)
1395
;; `(trans-eval '(defdata::is-subtype$$ ',t1 ',t2 defdata::R$ defdata::types-ht$) 'subtype-p state nil))
1396
;`(is-subtype$$ ',T1 ',T2 R$ types-ht$))
1401
; TODO 29 March 2014
1402
; - add oddp and evenp (but do this consistently, these definitions are only valid when we additionally know that v is a integer.
1404
(declare (xargs :guard (natp n)))
1405
(* 2 (nth-integer n)))
1407
(defdata::register-type even
1409
:enumerator nth-even)
1412
(declare (xargs :guard (natp n)))
1417
;(defun nth-odd (n) (1+ (* 2 (nth-integer))))
1418
(defdata::register-type odd
1420
:enumerator nth-odd)
1422
(defdata-subtype var symbol)
1424
(in-theory (disable varp))