Lemmas about nth available in the std/lists library.
Theorem:
(defthm nth-when-atom (implies (atom x) (equal (nth n x) nil)))
Theorem:
(defthm nth-when-zp (implies (zp n) (equal (nth n x) (car x))))
Theorem:
(defthm nth-of-nil (equal (nth n nil) nil))
Theorem:
(defthm nth-of-list-fix (equal (nth n (list-fix x)) (nth n x)))
Theorem:
(defthm nth-of-nfix (equal (nth (nfix n) x) (nth n x)))
Note: Matt Kaufmann reported that the following lemma got expensive in one of his books, so we now disable it by default and instead leave enabled a -cheap rule with a backchain limit.
Theorem:
(defthm nth-when-too-large (implies (<= (len x) (nfix n)) (equal (nth n x) nil)))
Theorem:
(defthm nth-when-too-large-cheap (implies (<= (len x) (nfix n)) (equal (nth n x) nil)) :rule-classes ((:rewrite :backchain-limit-lst 1)))
Theorem:
(defthm acl2-count-of-nth-linear (implies (consp x) (< (acl2-count (nth i x)) (acl2-count x))) :rule-classes :linear)
Theorem:
(defthm acl2-count-of-nth-linear-weak (<= (acl2-count (nth i x)) (acl2-count x)) :rule-classes :linear)
Theorem:
(defthm acl2-count-of-nth-rewrite (equal (< (acl2-count (nth i x)) (acl2-count x)) (or (consp x) (> (acl2-count x) 0))))
Note that we don't prove a rule about update-nth, because
Theorem:
(defthm member-of-nth (implies (< (nfix n) (len x)) (member (nth n x) x)))
Theorem:
(defthm nth-of-append (equal (nth n (append x y)) (if (< (nfix n) (len x)) (nth n x) (nth (- n (len x)) y))))
Theorem:
(defthm nth-of-revappend (equal (nth n (revappend x y)) (if (< (nfix n) (len x)) (nth (- (len x) (+ 1 (nfix n))) x) (nth (- n (len x)) y))))
Theorem:
(defthm nth-of-rev (equal (nth n (rev x)) (if (< (nfix n) (len x)) (nth (- (len x) (+ 1 (nfix n))) x) nil)))
Theorem:
(defthm nth-of-reverse (equal (nth n (reverse x)) (if (< (nfix n) (len x)) (nth (- (len x) (+ 1 (nfix n))) x) nil)))
Theorem:
(defthm nth-of-take (equal (nth i (take n l)) (if (< (nfix i) (nfix n)) (nth i l) nil)))
Theorem:
(defthm nth-of-make-list-ac (equal (nth n (make-list-ac m val acc)) (if (< (nfix n) (nfix m)) val (nth (- n (nfix m)) acc))))
Theorem:
(defthm nth-of-repeat (equal (nth n (repeat m a)) (if (< (nfix n) (nfix m)) a nil)))
Theorem:
(defthm nth-of-nthcdr (equal (nth n (nthcdr m x)) (nth (+ (nfix n) (nfix m)) x)))
Theorem:
(defthm nth-of-last (equal (nth n (last x)) (if (zp n) (car (last x)) nil)))
Theorem:
(defthm nth-of-butlast (equal (nth n (butlast x m)) (if (< (nfix n) (- (len x) (nfix m))) (nth n x) nil)))
These are for use with std::deflist and std::defprojection.
Theorem:
(defthm element-p-of-nth-when-element-list-p-when-nil-element (implies (and (element-p nil) (element-list-p x)) (element-p (nth n x))) :rule-classes :rewrite)
Theorem:
(defthm element-p-of-nth-when-element-list-p-when-nil-unknown (implies (and (element-list-p x) (< (nfix n) (len x))) (element-p (nth n x))) :rule-classes :rewrite)
Theorem:
(defthm element-p-of-nth-when-element-list-p-when-nil-not-element-non-negated (implies (and (not (element-p nil)) (element-list-p x)) (iff (element-p (nth n x)) (< (nfix n) (len x)))) :rule-classes :rewrite)
Theorem:
(defthm element-p-of-nth-when-element-list-p-when-nil-not-element-negated (implies (and (not (element-p nil)) (element-list-p x)) (iff (non-element-p (nth n x)) (<= (len x) (nfix n)))) :rule-classes :rewrite)
Theorem:
(defthm nth-of-elementlist-projection-when-nil-preservingp (implies (equal (element-xformer nil) nil) (equal (nth n (elementlist-projection x)) (element-xformer (nth n x)))) :rule-classes :rewrite)
Theorem:
(defthm nth-of-elementlist-projection-when-not-nil-preservingp (equal (nth n (elementlist-projection x)) (and (< (nfix n) (len x)) (element-xformer (nth n x)))) :rule-classes :rewrite)
Theorem:
(defthm nth-of-element-list-fix-when-nil-element (implies (element-p nil) (equal (nth n (element-list-fix x)) (element-fix (nth n x)))) :rule-classes :rewrite)
Theorem:
(defthm nth-of-element-list-fix-unless-nil-element (equal (nth n (element-list-fix x)) (and (< (nfix n) (len x)) (element-fix (nth n x)))) :rule-classes :rewrite)