Applies inside-out rewriting to an s-expression using a user-provided set of rewrite rules.
A good (?) default set of rules is provided in *sexpr-rewrites*. It is a theorem that, if the rewrite rules are recognized by 4v-sexpr-rewrite-alistp, then the result is 4v-sexpr-equiv to the input. It is also a theorem that the variables of the result are a subset of those of the input.Function:
(defun sexpr-rewrite (x rewrites) (declare (xargs :guard t)) (if (atom x) x (b* ((args (sexpr-rewrite-list (cdr x) rewrites))) (sexpr-rewrite-fncall 100 (car x) args rewrites))))
Function:
(defun sexpr-rewrite-list (x rewrites) (declare (xargs :guard t)) (if (atom x) nil (hons (sexpr-rewrite (car x) rewrites) (sexpr-rewrite-list (cdr x) rewrites))))
Function:
(defun sexpr-rewrite-memoize-condition (x rewrites) (declare (ignorable x rewrites) (xargs :guard 't)) (consp x))
Function:
(defun sexpr-rewrite-flag (flag x rewrites) (declare (xargs :non-executable t)) (declare (ignorable x rewrites)) (prog2$ (throw-nonexec-error 'sexpr-rewrite-flag (list flag x rewrites)) (cond ((equal flag 'rw) (if (consp x) ((lambda (args rewrites x) (sexpr-rewrite-fncall '100 (car x) args rewrites)) (sexpr-rewrite-flag 'rw-list (cdr x) rewrites) rewrites x) x)) (t (if (consp x) (hons (sexpr-rewrite-flag 'rw (car x) rewrites) (sexpr-rewrite-flag 'rw-list (cdr x) rewrites)) 'nil)))))
Theorem:
(defthm sexpr-rewrite-flag-equivalences (and (equal (sexpr-rewrite-flag 'rw x rewrites) (sexpr-rewrite x rewrites)) (equal (sexpr-rewrite-flag 'rw-list x rewrites) (sexpr-rewrite-list x rewrites))))
Theorem:
(defthm sexpr-rewrite-correct (implies (4v-sexpr-rewrite-alistp rewrites) (4v-sexpr-equiv (sexpr-rewrite x rewrites) x)))
Theorem:
(defthm sexpr-rewrite-list-correct (implies (4v-sexpr-rewrite-alistp rewrites) (4v-sexpr-list-equiv (sexpr-rewrite-list x rewrites) x)))
Theorem:
(defthm sexpr-rewrite-vars (implies (not (member v (4v-sexpr-vars x))) (not (member v (4v-sexpr-vars (sexpr-rewrite x rewrites))))))
Theorem:
(defthm sexpr-rewrite-list-vars (implies (not (member v (4v-sexpr-vars-list x))) (not (member v (4v-sexpr-vars-list (sexpr-rewrite-list x rewrites))))))
Function:
(defun sexpr-rewrite-alist (x rewrites) (declare (xargs :guard t)) (if (atom x) nil (if (atom (car x)) (sexpr-rewrite-alist (cdr x) rewrites) (cons (cons (caar x) (sexpr-rewrite (cdar x) rewrites)) (sexpr-rewrite-alist (cdr x) rewrites)))))