Collect the set of variables in an s-expression.
(4v-sexpr-vars x) is one way to produce a list of all the
variables that occur in the sexpr
Before deciding to collect variables, it may be worth considering whether you really need to compute the exact set of variables for a sexpr. It is often possible to efficiently overapproximate the set of all variables, e.g., if you have just carried out an esim run, the variables you assigned to the input and state bits is probably readily available.
You can often use overapproximations in scenarios such as
because in these cases it is perfectly fine for the alists to include extraneous bindings for variables that aren't in the sexpr.
Depending on your application, 4v-sexpr-vars-1pass may be a more
efficient way to collect variables than
The basic approach taken by
This might be a reasonable strategy when you care about the precise set of variables for a large number of closely-related sexprs. However, it is very memory intensive, and can be very slow unless you are really getting a lot of reuse out of the variable sets being computed. Moreover, computing the precise set of variables for every subexpression may be far more work than you really need to do, so 4v-sexpr-vars-1pass might be more appropriate.
See also 4v-nsexpr-vars, which only works when the sexprs involved have natural-numbered variables, but can use a sparse bitset representation that generally performs very well.
Function:
(defun 4v-sexpr-vars (x) (declare (xargs :guard t)) (if (atom x) (and x (mbe :logic (set::insert x nil) :exec (hons x nil))) (4v-sexpr-vars-list (cdr x))))
Function:
(defun 4v-sexpr-vars-list (x) (declare (xargs :guard t)) (if (atom x) nil (mbe :logic (set::union (4v-sexpr-vars (car x)) (4v-sexpr-vars-list (cdr x))) :exec (hons-alphorder-merge (4v-sexpr-vars (car x)) (4v-sexpr-vars-list (cdr x))))))
Theorem:
(defthm atom-listp-4v-sexpr-vars (atom-listp (4v-sexpr-vars x)))
Theorem:
(defthm atom-listp-4v-sexpr-vars-list (atom-listp (4v-sexpr-vars-list x)))
Theorem:
(defthm true-listp-4v-sexpr-vars (true-listp (4v-sexpr-vars x)) :rule-classes (:rewrite :type-prescription))
Theorem:
(defthm true-listp-4v-sexpr-vars-list (true-listp (4v-sexpr-vars-list x)) :rule-classes (:rewrite :type-prescription))
Theorem:
(defthm setp-4v-sexpr-vars (set::setp (4v-sexpr-vars x)))
Theorem:
(defthm setp-4v-sexpr-vars-list (set::setp (4v-sexpr-vars-list x)))
Function:
(defun 4v-sexpr-vars-memoize-condition (x) (declare (ignorable x) (xargs :guard 't)) (and (consp x) (consp (cdr x))))
Theorem:
(defthm not-member-4v-sexpr-vars-lookup-when-not-member-vars-alist-vals (implies (not (member-equal k (4v-sexpr-vars-list (alist-vals al)))) (not (member-equal k (4v-sexpr-vars (cdr (hons-assoc-equal x al)))))))
Theorem:
(defthm 4v-sexpr-vars-4v-sexpr-compose (implies (not (member-equal v (4v-sexpr-vars-list (alist-vals al)))) (not (member-equal v (4v-sexpr-vars (4v-sexpr-compose x al))))))
Theorem:
(defthm 4v-sexpr-vars-list-4v-sexpr-compose-list (implies (not (member-equal v (4v-sexpr-vars-list (alist-vals al)))) (not (member-equal v (4v-sexpr-vars-list (4v-sexpr-compose-list x al))))))
Theorem:
(defthm 4v-sexpr-vars-4v-sexpr-restrict (implies (and (not (member-equal k (4v-sexpr-vars-list (alist-vals al)))) (not (and (member-equal k (4v-sexpr-vars x)) (not (member-equal k (alist-keys al)))))) (not (member-equal k (4v-sexpr-vars (4v-sexpr-restrict x al))))))
Theorem:
(defthm 4v-sexpr-vars-list-4v-sexpr-restrict-list (implies (and (not (member-equal k (4v-sexpr-vars-list (alist-vals al)))) (not (and (member-equal k (4v-sexpr-vars-list x)) (not (member-equal k (alist-keys al)))))) (not (member-equal k (4v-sexpr-vars-list (4v-sexpr-restrict-list x al))))))