• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • 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$
        • Defmacro
        • Loop$-primer
        • Fast-alists
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • 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
          • Member
          • Append
          • List
          • Nth
          • Len
          • True-listp
          • String-listp
          • Nat-listp
          • Character-listp
          • Symbol-listp
          • True-list-listp
          • Length
          • Search
          • Intersection$
          • Union$
          • Remove-duplicates
          • Position
            • Listpos
          • Update-nth
          • Take
          • Nthcdr
          • Set-difference$
          • Subsetp
          • No-duplicatesp
          • Concatenate
          • Remove
          • Remove1
          • Intersectp
          • Endp
          • Keyword-value-listp
          • Integer-listp
          • Reverse
          • Add-to-set
          • List-utilities
          • Set-size
          • Revappend
          • Subseq
          • Make-list
          • Last
          • Lists-light
          • Boolean-listp
          • Butlast
          • Pairlis$
          • Substitute
          • Count
          • Keyword-listp
          • List*
          • Eqlable-listp
          • Pos-listp
          • Integer-range-listp
          • Rational-listp
          • Evens
          • Atom-listp
          • ACL2-number-listp
          • Typed-list-utilities
          • Odds
          • List$
          • Listp
          • Standard-char-listp
          • Last-cdr
          • Pairlis
          • Proper-consp
          • Improper-consp
          • Pairlis-x2
          • Pairlis-x1
          • Merge-sort-lexorder
          • Fix-true-list
          • Real-listp
        • 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
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Math
    • Testing-utilities
  • Lists
  • Strings
  • ACL2-built-ins

Position

Position of an item in a string or a list

General Forms:
(position x seq)
(position x seq :test 'eql)   ; same as above (eql as equality test)
(position x seq :test 'eq)    ; same, but eq is equality test
(position x seq :test 'equal) ; same, but equal is equality test

(Position x seq) is the least index (zero-based) of the element x in the string or list seq, if x is an element of seq. Otherwise (position x seq) is nil. The optional keyword, :TEST, has no effect logically, but provides the test (default eql) used for comparing x with items of seq.

The guard for a call of position depends on the test. In all cases, the second argument must satisfy stringp or true-listp. If the test is eql, then either the first argument must be suitable for eql (see eqlablep) or the second argument must satisfy eqlable-listp. If the test is eq, then either the first argument must be a symbol or the second argument must satisfy symbol-listp.

See equality-variants for a discussion of the relation between position and its variants:

(position-eq x seq) is equivalent to (position x seq :test 'eq);

(position-equal x seq) is equivalent to (position x seq :test 'equal).

In particular, reasoning about any of these primitives reduces to reasoning about the function position-equal.

Position is defined by Common Lisp. See any Common Lisp documentation for more information.

Macro: position

(defmacro position (x seq &key (test ''eql))
  (declare (xargs :guard (or (equal test ''eq)
                             (equal test ''eql)
                             (equal test ''equal))))
  (cond ((equal test ''eq)
         (cons 'let-mbe
               (cons (cons (cons 'x (cons x 'nil))
                           (cons (cons 'seq (cons seq 'nil)) 'nil))
                     '(:logic (position-equal x seq)
                              :exec (position-eq-exec x seq)))))
        ((equal test ''eql)
         (cons 'let-mbe
               (cons (cons (cons 'x (cons x 'nil))
                           (cons (cons 'seq (cons seq 'nil)) 'nil))
                     '(:logic (position-equal x seq)
                              :exec (position-eql-exec x seq)))))
        (t (cons 'position-equal
                 (cons x (cons seq 'nil))))))

Function: position-equal

(defun position-equal (x seq)
  (declare (xargs :guard (or (stringp seq) (true-listp seq))))
  (if (stringp seq)
      (position-ac x (coerce seq 'list) 0)
    (position-equal-ac x seq 0)))

Function: position-equal-ac

(defun position-equal-ac (item lst acc)
  (declare (xargs :guard (and (true-listp lst)
                              (acl2-numberp acc))))
  (cond ((endp lst) nil)
        ((equal item (car lst))
         (mbe :exec acc :logic (fix acc)))
        (t (position-equal-ac item (cdr lst)
                              (1+ acc)))))

Macro: position-ac

(defmacro position-ac (item lst acc &key (test ''eql))
 (declare (xargs :guard (or (equal test ''eq)
                            (equal test ''eql)
                            (equal test ''equal))))
 (cond
  ((equal test ''eq)
   (cons 'let-mbe
         (cons (cons (cons 'item (cons item 'nil))
                     (cons (cons 'lst (cons lst 'nil))
                           (cons (cons 'acc (cons acc 'nil))
                                 'nil)))
               '(:logic (position-equal-ac item lst acc)
                        :exec (position-ac-eq-exec item lst acc)))))
  ((equal test ''eql)
   (cons
        'let-mbe
        (cons (cons (cons 'item (cons item 'nil))
                    (cons (cons 'lst (cons lst 'nil))
                          (cons (cons 'acc (cons acc 'nil))
                                'nil)))
              '(:logic (position-equal-ac item lst acc)
                       :exec (position-ac-eql-exec item lst acc)))))
  (t (cons 'position-equal-ac
           (cons item (cons lst (cons acc 'nil)))))))

Subtopics

Listpos
(listpos x y) returns the starting position of the first occurrence of the sublist x within the list y, or NIL if there is no such occurrence.