2
;;; Some stuff about lists, sets, sequences and association lists.
3
;;; Created: 6-8-99 Last revison: 05-12-2000
4
;;; =============================================================================
6
#| To certify this book:
10
(certify-book "basic")
18
;;; *******************************************************************
19
;;; BASIC PROPERTIES ABOUT LISTS, SETS, SEQUENCES AND ASSOCIATION LISTS
20
;;; *******************************************************************
22
;;; REMARK: This is book is not intended to be a complete tratement of
23
;;; such data structures, but and "ad hoc" collection of definition and
24
;;; properties, needed for further development of the theory of first
27
;;; ============================================================================
29
;;; ============================================================================
34
(defthm append-associative
35
(equal (append (append x y) z)
36
(append x (append y z))))
39
(iff (member x (append a b))
40
(or (member x a) (member x b))))
44
(implies (true-listp l)
45
(equal (append l nil) l)))
53
(append (revlist (cdr l)) (list (car l)))))
55
(defthm revlist-true-listp
56
(true-listp (revlist l)))
59
(defthm revappend-revlist
60
(equal (revappend l m) (append (revlist l) m))))
62
(defthm reverse-revlist
63
(implies (true-listp l)
64
(equal (reverse l) (revlist l))))
68
(defun delete-one (x l)
70
((equal x (car l)) (cdr l))
71
(t (cons (car l) (delete-one x (cdr l))))))
73
(defthm delete-one-less-length
75
(< (len (delete-one x m)) (len m))))
77
(defthm member-delete-one
78
(implies (and (member x m) (not (equal x y)))
79
(member x (delete-one y m))))
81
(defthm delete-one-conmute
82
(equal (delete-one x (delete-one y l))
83
(delete-one y (delete-one x l))))
85
(defthm subsetp-delete-one
86
(implies (not (member x l))
87
(not (member x (delete-one y l)))))
89
;;; ======= ELIMINATE (like remove, but different guard, using equal)
91
(defun eliminate (x l)
92
(declare (xargs :guard (true-listp l)))
94
((equal x (car l)) (eliminate x (cdr l)))
95
(t (cons (car l) (eliminate x (cdr l))))))
100
(< (len (eliminate x l)) (len l)))
101
:rule-classes :linear)
103
;;; ====== NATURAL-TRUE-LISTP
104
(defun positive-integer-true-listp (l)
105
(declare (xargs :guard t))
108
(and (integerp (car l)) (> (car l) 0)
109
(positive-integer-true-listp (cdr l)))))
111
;;; ====== REPLACE-LIST
113
(defun replace-list (l n x)
114
(declare (xargs :guard
115
(and (true-listp l) (integerp n) (>= n 0))))
120
(cons (car l) (replace-list (cdr l) (- n 1) x)))))
123
(defthm nth-replace-list-same-position
124
(implies (and (integerp i)
127
(equal (nth i (replace-list l i x)) x)))
130
(defthm replace-list-same-length
131
(equal (len (replace-list l i x)) (len l)))
134
(defthm nth-replace-list-different-position
135
(implies (and (integerp i) (integerp k)
137
(< i (len l)) (< k (len l))
139
(equal (nth i (replace-list l k x)) (nth i l))))
144
(defthm replace-list-different-position
145
(implies (and (integerp i) (integerp k)
147
(< i (len l)) (< k (len l))
150
(replace-list (replace-list l i x) k y)
151
(replace-list (replace-list l k y) i x))))
155
(defthm replace-list-same-position
156
(implies (and (integerp i)
160
(replace-list (replace-list term i x) i y)
161
(replace-list term i y))))
169
(defun pair-args (l1 l2)
170
(declare (xargs :guard (and (true-listp l1) (true-listp l2))))
171
(cond ((endp l1) (if (equal l1 l2) (mv nil t) (mv nil nil)))
172
((endp l2) (mv nil nil))
173
(t (mv-let (pair-rest bool)
174
(pair-args (cdr l1) (cdr l2))
176
(mv (cons (cons (car l1) (car l2))
181
;;; **** Guard verification
182
(defthm pair-args-true-listp
183
(true-listp (car (pair-args l1 l2))))
188
;;; REMARK: I prefer to deal with first,second, third, etc... than with
191
(defthm mv-nth-0-first
192
(equal (mv-nth 0 l) (first l)))
194
(defthm mv-nth-1-second
195
(equal (mv-nth 1 l) (second l)))
197
(defthm mv-nth-2-third
198
(equal (mv-nth 2 l) (third l)))
200
(defthm mv-nth-3-fourth
201
(equal (mv-nth 3 l) (fourth l)))
206
;;; ============================================================================
208
;;; ============================================================================
211
;;; A true-list without repeated elements.
216
(and (not (member (car l) (cdr l)))
221
(local (defthm member-eliminate
222
(implies (member y (eliminate x l))
225
(defthm eliminate-preserves-setp
227
(setp (eliminate x l)))))
234
(defun not-subsetp-witness (l1 l2)
237
(if (member (car l1) l2)
238
(not-subsetp-witness (cdr l1) l2)
241
(defthm not-subsetp-witness-lemma
242
(equal (subsetp l1 l2)
243
(implies (member (not-subsetp-witness l1 l2) l1)
244
(member (not-subsetp-witness l1 l2) l2))))
247
(in-theory (disable not-subsetp-witness-lemma))
249
(defthm subsetp-append-1
250
(equal (subsetp (append x y) z)
251
(and (subsetp x z) (subsetp y z))))
253
(defthm subsetp-append-2
254
(implies (subsetp c a)
255
(subsetp c (append b a))))
257
(defthm subsetp-append-3
258
(implies (subsetp b c)
259
(subsetp b (append c a))))
263
(implies (subsetp l m)
264
(subsetp l (cons x m))))
266
(defthm subsetp-reflexive
269
(defthm delete-one-subsetp
270
(subsetp (delete-one x l) l))
272
(defun subset-induction (l m)
273
(if (or (atom l) (atom m) (not (member (car l) m)))
275
(subset-induction (cdr l) (eliminate (car l) m))))
278
substp-preserved-when-deleting-a-non-member
279
(implies (and (subsetp z m) (not (member x z)))
280
(subsetp z (eliminate x m))))
285
;;; No common elements
287
(defun disjointp (l1 l2)
290
(and (not (member (car l1) l2))
291
(disjointp (cdr l1) l2))))
294
(defthm disjointp-cons
295
(equal (disjointp l (cons x m))
296
(and (not (member x l))
299
(defthm disjointp-append-1
300
(equal (disjointp (append l m) n)
304
(defthm disjointp-append-2
305
(equal (disjointp m (append l n))
309
(defthm disjointp-conmutative
310
(equal (disjointp l1 l2) (disjointp l2 l1)))
312
(defthm disjointp-nil
318
;;; Eliminate duplicates.
323
(if (member (car l) (cdr l))
325
(cons (car l) (make-set (cdr l))))))
327
(defthm member-make-set
328
(iff (member x (make-set l)) (member x l)))
330
(defthm setp-make-set
333
(defthm make-set-of-a-setp-is-the-same
335
(equal (make-set l) (fix-true-list l))))
337
(defthm len-fix-true-list
338
(equal (len (fix-true-list l)) (len l)))
340
(defthm make-set-lessp-length-if-not-setp
341
(implies (not (setp l))
342
(< (len (make-set l))
344
:rule-classes :linear)
347
(defthm length-make-set-leq
350
:rule-classes :linear)
352
(defthm make-set-fix-true-list
353
(equal (fix-true-list (make-set l)) (make-set l)))
356
;;; ============ EQUAL-SET: CONGRUENCE AND REWRITE RULES
358
(defun equal-set (x y)
359
(and (subsetp x y) (subsetp y x)))
362
(defthm subsetp-transitive
363
(implies (and (subsetp l m)
371
(defcong equal-set iff (subsetp x y) 1)
372
(defcong equal-set iff (subsetp x y) 2)
373
(defcong equal-set equal (consp l) 1)
378
(defthm subsetp-disjoint-1
379
(implies (and (subsetp x x-equiv)
380
(disjointp x-equiv y))
383
(defcong equal-set iff (disjointp x y) 1))
388
(defthm subsetp-disjoint-2
389
(implies (and (subsetp y y-equiv)
390
(disjointp x y-equiv))
393
(defcong equal-set iff (disjointp x y) 2))
398
(local (defthm equal-set-member-cong-lemma-1
399
(implies (and (not (member x y))
401
(not (member x y-equiv)))))
403
(local (defthm equal-set-member-cong-lemma-2
404
(implies (and (member x y)
406
(member x y-equiv))))
407
(defcong equal-set iff (member x y) 2))
410
(defthm equal-set-make-set
411
(equal-set (make-set l) l))
413
;;; REMARK: The following rule is needed in
414
;;; equal-size-and-not-inverse-subsumption-implies-not-renaming-almost
415
;;; I don't know why the previous rule is not used (monitoring is not
416
;;; possible, because it is a "simple"(?) rule). So I think I need the
417
;;; following rule, which is redundant (?)
420
(defthm subsetp-make-set-provisional
421
(iff (subsetp a (make-set b)) (subsetp a b)))
426
;;; ====== PERM: CONGRUENCE AND REWRITE RULES
429
(defun perm (lst1 lst2)
430
(cond ((atom lst1) (atom lst2))
431
((member (car lst1) lst2)
432
(perm (cdr lst1) (delete-one (car lst1) lst2)))
439
(defthm perm-reflexive
443
(defthm perm-main-lemma
444
(implies (and (member x a)
446
(perm (delete-one x a) (delete-one x b)))
450
(defthm perm-symmetric
451
(implies (perm x y) (perm y x))))
455
(defthm perm-transitive-reverse
456
(implies (and (perm a b)
462
(defthm perm-transitive
463
(implies (and (perm a b)
467
((:instance perm-symmetric
469
(:instance perm-transitive-reverse
475
(local (defthm perm-implies-subsetp
476
(implies (perm x y) (subsetp x y))
477
:hints (("Goal" :induct (perm x y)))))
480
(defrefinement perm equal-set)
484
(defthm perm-fix-true-list
485
(perm (fix-true-list l) l))
488
;;; ============================================================================
490
;;; ============================================================================
492
;;; Sequences of natural numbers will be used to represent positions of
497
(defun prefix (p1 p2)
500
((equal (car p1) (car p2))
501
(prefix (cdr p1) (cdr p2)))
504
;;; ===== DIFFERENCE-POS
506
(defun difference-pos (p q)
509
(difference-pos (cdr p) (cdr q))))
512
(defthm difference-the-same
513
(implies (true-listp l)
514
(equal (difference-pos l l) nil)))
516
(defthm difference-pos-common-prefix
517
(equal (difference-pos l (append l m))
520
(defthm difference-pos-common-prefix-append
521
(equal (difference-pos (append l m) (append l n))
522
(difference-pos m n)))
525
;;; ===== DISJOINT-POSITIONS
527
(defun disjoint-positions (p1 p2)
529
((or (endp p1) (endp p2)) nil)
530
((equal (car p1) (car p2))
531
(disjoint-positions (cdr p1) (cdr p2)))
536
;;; ============================================================================
537
;;; 4. Association lists
538
;;; ============================================================================
541
;;; REMARK: Association lists represent finite domain functions in the
543
;;; - Every atom object represents the identity function.
544
;;; - If an atom object is member of an association list will be
545
;;; considered as (nil . nil).
546
;;; - If several pairs (x . t1), (x . t2),... are member of an
547
;;; association list only the first one is considered.
548
;;; As a consecuence of this representation, different objects represent
549
;;; the same finite domain function. Thus, we cannot use equal as a
550
;;; predicate to talk about equality of finite domain functions.
552
;;; REMARK: We do not use the book alist-theory.list
553
;;; provided by the distribution because we need our own version of
554
;;; assoc (see the function val). We want the association list to behave
555
;;; like identity outside its domain.
561
(declare (xargs :guard (if (eqlablep x)
563
(eqlable-alistp sigma))))
566
(if (eql x (caar sigma)) (cdar sigma) (val x (cdr sigma)))))
569
; [Removed by Matt K. to handle changes to member, assoc, etc. after ACL2 4.2.]
571
;;; REMARK: For execution, I will use assoc when the variable is in the
572
;;; domain. Sometimes, I have to use assoc-equal (see renamings.lisp).
573
;;; The following rule rewrites assoc-equal to assoc. In this way, we
574
;;; can build one set of rules about assoc. WARNING: this is different
575
;;; rewriting strategy than the one given in the book alist-defthms.lisp
576
;;; in the ACL2 distribution.
578
(defthm assoc-equal-assoc-equal
579
(equal (assoc-equal x a) (assoc x a)))
583
;;; This relation between val and assoc is expressed
584
;;; here. For reasonig, I prefer to use val (that's the explanation for
585
;;; the orientation of the following rule):
588
(equal (cdr (assoc x l)) (val x l))))
590
;;; ====== RESTRICTION
591
;;; The list of pairs (x. sigma(x)), for all x in l.
593
(defun restriction (sigma l)
596
(cons (cons (car l) (val (car l) sigma))
597
(restriction sigma (cdr l)))))
600
;;; domain of association lists (the list of first components of sigma).
601
;;; Note: as we said above, the atom elements of sigma are considered
602
;;; as (nil . nil). This remark also applies to the following definitions.
604
(defun domain (sigma)
605
(declare (xargs :guard (alistp sigma)))
608
(cons (caar sigma) (domain (cdr sigma)))))
611
(defthm domain-restriction
612
(equal (domain (restriction sigma l)) (fix-true-list l)))
615
(defthm domain-append
616
(equal (domain (append l1 l2))
617
(append (domain l1) (domain l2))))
620
(equal (consp (domain sigma))
624
(defthm assoc-member-domain
625
(implies (alistp sigma)
626
(iff (member x (domain sigma))
631
;;; co-domain of association lists (the list of first components of sigma).
633
(defun co-domain (sigma)
636
(cons (cdar sigma) (co-domain (cdr sigma)))))
641
;;; The association list obtained reversing the pairs.
643
(defun inverse (sigma)
646
(cons (cons (cdar sigma) (caar sigma))
647
(inverse (cdr sigma)))))
650
(defthm domain-of-inverse-is-co-domain
651
(equal (domain (inverse sigma)) (co-domain sigma)))
653
(defthm co-domain-of-inverse-is-domain
654
(equal (co-domain (inverse sigma)) (domain sigma)))
656
(defthm same-length-co-domain-and-domain
657
(equal (len (domain sigma))
658
(len (co-domain sigma))))
660
(in-theory (disable same-length-co-domain-and-domain))
664
;;; ====== ALISTP (for guard verification)
666
(defthm assoc-consp-or-nil
668
(or (consp (assoc x a))
669
(equal (assoc x a) nil)))
670
:rule-classes :type-prescription)
673
(defun alistp-acl2-numberp (l)
674
(declare (xargs :guard t))
677
(and (consp (car l)) (acl2-numberp (cdar l))
678
(alistp-acl2-numberp (cdr l)))))