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
The guard for a call of
See equality-variants for a discussion of the relation between
(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
Macro:
(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:
(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:
(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:
(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)))))))