• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Community
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Recursion-and-induction
      • Hons-and-memoization
      • Events
      • Parallelism
      • History
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Mutual-recursion
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Defconst
        • Fast-alists
        • Defmacro
        • Loop$-primer
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
          • Let
          • Declare
          • Mbe
          • Apply$
          • Fmt
          • Loop$
          • Return-last
          • Mv-let
          • Df
          • Pseudo-termp
          • Defwarrant
          • Er
          • Mbt
          • Time$
          • Msg
          • Ec-call
          • Mv-nth
          • Sys-call
          • Value-triple
          • O-p
          • Comp
          • Member
          • Cw
          • O<
          • Flet
          • Or
          • Hons
          • Cbd
          • Mv
          • Defbadge
          • Append
          • *ACL2-exports*
          • Comment
          • Eql
          • List
          • Unsigned-byte-p
          • Posp
          • Natp
          • The
          • Nth
            • Std/lists/nth
              • Equal-by-nths
            • Nth-aliases-table
            • Equal-by-nths
            • First
            • Rest
            • Third
            • Tenth
            • Sixth
            • Seventh
            • Second
            • Ninth
            • Fourth
            • Fifth
            • Eighth
          • And
          • Len
          • Time-tracker
          • Term-order
          • True-listp
          • Msgp
          • Booleanp
          • <
          • If
          • Pseudo-term-listp
          • +
          • Not
          • With-global-stobj
          • Bitp
          • Equal
          • Cdr
          • Car
          • String-listp
          • Nat-listp
          • Implies
          • Iff
          • Character-listp
          • Alistp
          • Cons
          • Symbol-listp
          • Macrolet
          • Characterp
          • Quote
          • Integerp
          • Consp
          • State-global-let*
          • Compress1
          • Symbolp
          • Stringp
          • *common-lisp-symbols-from-main-lisp-package*
          • True-list-listp
          • Prog2$
          • *
          • Last-prover-steps
          • Hons-acons
          • Let*
          • Eq
          • With-guard-checking
          • @
          • Length
          • With-local-stobj
          • Hard-error
          • Zp
          • -
          • Search
          • Intersection$
          • Assign
          • Aset1
          • Symbol-name
          • Union$
          • Set-gc-strategy
          • In-tau-intervalp
          • Cons-with-hint
          • Break-on-error
          • Pand
          • Case-match
          • Fast-alist-fork
          • Sys-call+
          • Signed-byte-p
          • ACL2-count
          • Remove-duplicates
          • With-serialize-character
          • Observation
          • Hons-resize
          • Make-tau-interval
          • Assoc
          • Logbitp
          • Termp
          • Position
          • *standard-co*
          • Hons-acons!
          • Update-nth
          • Aref1
          • Read-run-time
          • Non-exec
          • Take
          • Symbol-package-name
          • Hons-wash
          • Expt
          • Coerce
          • Get-internal-time
          • Keywordp
          • Intern
          • Standard-co
          • Case
          • Standard-oi
          • Good-bye
          • Formula
          • Error1
          • Atom
          • Without-evisc
          • Symbol-alistp
          • Rationalp
          • Ash
          • With-local-state
          • Spec-mv-let
          • Intern-in-package-of-symbol
          • Binary-+
          • <=
          • With-fast-alist
          • Set-difference$
          • Hons-clear
          • Flush-compress
          • Por
          • Rassoc
          • Logand
          • Remove-assoc
          • =
          • Nthcdr
          • Pargs
          • Nfix
          • Hons-copy
          • Alphorder
          • Subsetp
          • Floor
          • Remove1-assoc
          • No-duplicatesp
          • Mv-list
          • Fmt-to-comment-window
          • Canonical-pathname
          • Aset2
          • Serialize-read
          • Random$
          • F-put-global
          • Compress2
          • Concatenate
          • Swap-stobjs
          • Fast-alist-clean
          • Assert$
          • Remove
          • Remove1
          • Intersectp
          • Endp
          • Put-assoc
          • Princ$
          • Primitive
          • Keyword-value-listp
          • True-list-fix
          • Integer-listp
          • Illegal
          • Hons-get
          • With-output-lock
          • Setenv$
          • Open-output-channel!
          • Fast-alist-free
          • Er-progn
          • Cw-print-base-radix
          • Reverse
          • Cond
          • Complex
          • Add-to-set
          • Truncate
          • Digit-char-p
          • Code-char
          • Char-code
          • Set-print-case
          • Set-print-base
          • Read-ACL2-oracle
          • Print-object$
          • Plet
          • Logior
          • Integer-length
          • Zip
          • With-live-state
          • Hons-assoc-equal
          • Extend-pathname
          • In-package
          • With-guard-checking-event
          • Term-listp
          • Print-object$+
          • Fmx-cw
          • String
          • Mod
          • Unary--
          • Set-print-radix
          • Resize-list
          • Pkg-witness
          • Revappend
          • Null
          • Term-list-listp
          • Make-fast-alist
          • Header
          • Boole$
          • Subseq
          • Logxor
          • With-guard-checking-error-triple
          • /
          • Make-list
          • Char-upcase
          • Char-downcase
          • Strip-cars
          • Set-fmt-hard-right-margin
          • Make-ord
          • Integer-range-p
          • Ifix
          • Fast-alist-free-on-exit
          • F-get-global
          • Aref2
          • Standard-char-p
          • Lognot
          • Must-be-equal
          • Getenv$
          • Binary-append
          • Er-hard
          • Eqlablep
          • Cpu-core-count
          • Boolean-listp
          • Allocate-fixnum-range
          • ACL2-numberp
          • Butlast
          • Pairlis$
          • Mod-expt
          • Hons-equal
          • Gc$
          • Substitute
          • Ceiling
          • With-stolen-alist
          • Mv?-let
          • String-upcase
          • String-downcase
          • Round
          • Evenp
          • Count
          • Char
          • Sys-call-status
          • Progn$
          • Pprogn
          • Lexorder
          • Hons-summary
          • Break$
          • Assert*
          • Lower-case-p
          • Alpha-char-p
          • Strip-cdrs
          • Serialize-write
          • Keyword-listp
          • Upper-case-p
          • Logeqv
          • List*
          • Last
          • Proofs-co
          • Maximum-length
          • Fix
          • Explode-nonnegative-integer
          • Eqlable-listp
          • Dimensions
          • Default
          • Arity
          • Sublis
          • Max
          • Explode-atom
          • Change
          • Zerop
          • String<
          • String-equal
          • Abs
          • Set-print-base-radix
          • Print-base-p
          • Nonnegative-integer-quotient
          • Intern$
          • Getprop
          • Binary-*
          • Aset1-trusted
          • Symbol<
          • String-append
          • Rfix
          • Mv?
          • Logic-fns-list-listp
          • Fast-alist-fork!
          • Er-hard?
          • Char-equal
          • 1+
          • *standard-oi*
          • Sys-call*
          • Mbt*
          • Hons-wash!
          • Hons-clear!
          • Signum
          • Rem
          • Real/rationalp
          • Rational-listp
          • Pos-listp
          • O-first-coeff
          • Logic-fnsp
          • Logic-fns-listp
          • Hons-copy-persistent
          • Gc-strategy
          • Fast-alist-summary
          • String>=
          • String<=
          • Acons
          • O-first-expt
          • Getpropc
          • Fast-alist-clean!
          • >=
          • >
          • Subst
          • Logcount
          • Evens
          • Er-soft
          • Digit-to-char
          • Comp-gcl
          • Atom-listp
          • Arities-okp
          • ACL2-number-listp
          • /=
          • Cadr
          • *standard-ci*
          • Unary-/
          • The-true-list
          • Realfix
          • O-rst
          • Fast-alist-len
          • Complex/complex-rationalp
          • Array2p
          • Array1p
          • Logtest
          • Logandc1
          • Char<
          • 1-
          • Trace-co
          • Putprop
          • Get-real-time
          • Eqlable-alistp
          • Count-keys
          • Assoc-string-equal
          • Logorc1
          • Logandc2
          • Denominator
          • Packn
          • Logic-termp
          • Logic-term-list-listp
          • Fmt!
          • Fms
          • Cw!
          • Assoc-keyword
          • String>
          • Numerator
          • Logorc2
          • Lognor
          • Char>=
          • Update-nth-array
          • The-number
          • Odds
          • Makunbound-global
          • Make-character-list
          • Make
          • List$
          • Int=
          • Get-cpu-time
          • Fmt-to-comment-window!
          • Fms!
          • F-boundp-global
          • Complex-rationalp
          • Alist-to-doublets
          • Access
          • Min
          • Lognand
          • Listp
          • Identity
          • Char>
          • Char<=
          • Zpf
          • Standard-char-listp
          • O-finp
          • Number-subtrees
          • Logic-term-listp
          • Last-cdr
          • Fmt1!
          • Fmt-to-comment-window!+
          • Character-alistp
          • Oddp
          • Minusp
          • Imagpart
          • Conjugate
          • Xor
          • Unquote
          • String-alistp
          • Packn-pos
          • Maybe-flush-and-compress1
          • Kwote
          • Fmt1
          • Fmt-to-comment-window+
          • Cw-print-base-radix!
          • Alist-keys-subsetp
          • Realpart
          • Plusp
          • First
          • Symbol-name-lst
          • R-symbol-alistp
          • R-eqlable-alistp
          • Fmx
          • Cw!+
          • Cons-subtrees
          • Cons-count-bounded
          • Cddr
          • Caar
          • Proper-consp
          • Kwote-lst
          • Improper-consp
          • Cw+
          • Rest
          • Standard-char-p+
          • Mbe1
          • Caddr
          • Pairlis-x2
          • Pairlis-x1
          • O>=
          • O<=
          • O-infp
          • Merge-sort-lexorder
          • Fix-true-list
          • Cdddr
          • Set-fmt-soft-right-margin
          • Real-listp
          • O>
          • Cddar
          • Cdar
          • Cdadr
          • Cdaar
          • Cadar
          • Caadr
          • Caaar
          • Cadddr
          • Caddar
          • Third
          • Tenth
          • Sixth
          • Seventh
          • Second
          • Ninth
          • Fourth
          • Fifth
          • Eighth
          • Cddddr
          • Cdddar
          • Cddadr
          • Cddaar
          • Cdaddr
          • Cdadar
          • Cdaadr
          • Cdaaar
          • Cadadr
          • Cadaar
          • Caaddr
          • Caadar
          • Caaadr
          • Caaaar
          • Hons-shrink-alist!
          • Hons-shrink-alist
          • Flush-hons-get-hash-table-link
          • Delete-assoc
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Efficiency
        • Irrelevant-formals
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Oracle-eval
        • Defmacro-untouchable
        • <<
        • Primitive
        • Revert-world
        • Unmemoize
        • Set-duplicate-keys-action
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Fake-oracle-eval
        • Defopen
        • Sleep
      • Operational-semantics
      • Real
      • Start-here
      • Miscellaneous
      • Output-controls
      • Bdd
      • Macros
      • Installation
      • Mailing-lists
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Math
    • Testing-utilities
  • Std/lists
  • Nth

Std/lists/nth

Lemmas about nth available in the std/lists library.

Definitions and Theorems

Trivial reductions

Theorem: nth-when-atom

(defthm nth-when-atom
  (implies (atom x)
           (equal (nth n x) nil)))

Theorem: nth-when-zp

(defthm nth-when-zp
  (implies (zp n)
           (equal (nth n x) (car x))))

Theorem: nth-of-nil

(defthm nth-of-nil
  (equal (nth n nil) nil))

Theorem: nth-of-list-fix

(defthm nth-of-list-fix
  (equal (nth n (list-fix x)) (nth n x)))

Theorem: nth-of-nfix

(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: nth-when-too-large

(defthm nth-when-too-large
  (implies (<= (len x) (nfix n))
           (equal (nth n x) nil)))

Theorem: nth-when-too-large-cheap

(defthm nth-when-too-large-cheap
  (implies (<= (len x) (nfix n))
           (equal (nth n x) nil))
  :rule-classes ((:rewrite :backchain-limit-lst 1)))

Lemmas about ACL2-count of nth

Theorem: acl2-count-of-nth-linear

(defthm acl2-count-of-nth-linear
  (implies (consp x)
           (< (acl2-count (nth i x))
              (acl2-count x)))
  :rule-classes :linear)

Theorem: acl2-count-of-nth-linear-weak

(defthm acl2-count-of-nth-linear-weak
  (<= (acl2-count (nth i x))
      (acl2-count x))
  :rule-classes :linear)

Theorem: acl2-count-of-nth-rewrite

(defthm acl2-count-of-nth-rewrite
  (equal (< (acl2-count (nth i x))
            (acl2-count x))
         (or (consp x) (> (acl2-count x) 0))))

Nth with other basic list functions

Note that we don't prove a rule about update-nth, because nth-update-nth is an ACL2 builtin.

Theorem: member-of-nth

(defthm member-of-nth
  (implies (< (nfix n) (len x))
           (member (nth n x) x)))

Theorem: nth-of-append

(defthm nth-of-append
  (equal (nth n (append x y))
         (if (< (nfix n) (len x))
             (nth n x)
           (nth (- n (len x)) y))))

Theorem: nth-of-revappend

(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: nth-of-rev

(defthm nth-of-rev
  (equal (nth n (rev x))
         (if (< (nfix n) (len x))
             (nth (- (len x) (+ 1 (nfix n))) x)
           nil)))

Theorem: nth-of-reverse

(defthm nth-of-reverse
  (equal (nth n (reverse x))
         (if (< (nfix n) (len x))
             (nth (- (len x) (+ 1 (nfix n))) x)
           nil)))

Theorem: nth-of-take

(defthm nth-of-take
  (equal (nth i (take n l))
         (if (< (nfix i) (nfix n))
             (nth i l)
           nil)))

Theorem: nth-of-make-list-ac

(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: nth-of-repeat

(defthm nth-of-repeat
  (equal (nth n (repeat m a))
         (if (< (nfix n) (nfix m)) a nil)))

Theorem: nth-of-nthcdr

(defthm nth-of-nthcdr
  (equal (nth n (nthcdr m x))
         (nth (+ (nfix n) (nfix m)) x)))

Theorem: nth-of-last

(defthm nth-of-last
  (equal (nth n (last x))
         (if (zp n) (car (last x)) nil)))

Theorem: nth-of-butlast

(defthm nth-of-butlast
  (equal (nth n (butlast x m))
         (if (< (nfix n) (- (len x) (nfix m)))
             (nth n x)
           nil)))

Nth of element lists and projections

These are for use with std::deflist and std::defprojection.

Theorem: element-p-of-nth-when-element-list-p-when-nil-element

(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: element-p-of-nth-when-element-list-p-when-nil-unknown

(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: element-p-of-nth-when-element-list-p-when-nil-not-element-non-negated

(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: element-p-of-nth-when-element-list-p-when-nil-not-element-negated

(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: nth-of-elementlist-projection-when-nil-preservingp

(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: nth-of-elementlist-projection-when-not-nil-preservingp

(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: nth-of-element-list-fix-when-nil-element

(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: nth-of-element-list-fix-unless-nil-element

(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)

Subtopics

Equal-by-nths
Proof technique: show two lists are equal by showing that their nth elements are always the same.