Filtering...

defalist

books/data-structures/defalist

Included Books

other
(in-package "ACL2")
include-book
(include-book "alist-defuns")
include-book
(include-book "list-defuns")
include-book
(include-book "utilities")
domain-type-domain-elem-typeencapsulate
(encapsulate ((domain-elem-type (x) boolean) (domain-type (l) boolean)
    (range-elem-type (x) boolean)
    (range-type (l) boolean)
    (alist-type (a) boolean))
  (local (in-theory '(ground-zero)))
  (local (defun domain-elem-type (x) (symbolp x)))
  (local (defun domain-type (l) (symbol-listp l)))
  (local (defun range-elem-type (x) (integerp x)))
  (local (defun range-type (l) (integer-listp l)))
  (local (defun alist-type
      (a)
      (cond ((atom a) (eq a nil))
        (t (and (consp (car a))
            (domain-elem-type (caar a))
            (range-elem-type (cdar a))
            (alist-type (cdr a)))))))
  (defthm domain-type-domain-elem-type
    (and (domain-type nil)
      (equal (domain-type (cons x l))
        (and (domain-elem-type x) (domain-type l)))))
  (defthm range-type-range-elem-type
    (and (range-type nil)
      (equal (range-type (cons x l))
        (and (range-elem-type x) (range-type l)))))
  (defthm alist-type-defun
    (iff (alist-type l)
      (cond ((atom l) (eq l nil))
        (t (and (consp (car l))
            (domain-elem-type (caar l))
            (range-elem-type (cdar l))
            (alist-type (cdr l))))))
    :rule-classes ((:rewrite :corollary (implies (atom l) (equal (alist-type l) (null l)))) (:rewrite :corollary (equal (alist-type (cons x l))
          (and (consp x)
            (domain-elem-type (car x))
            (range-elem-type (cdr x))
            (alist-type l)))))))
replace-equalfunction
(defun replace-equal
  (a b l)
  (declare (xargs :guard (true-listp l)))
  (cond ((endp l) nil)
    ((equal (car l) a) (cons b (replace-equal a b (cdr l))))
    (t (cons (car l) (replace-equal a b (cdr l))))))
my-conjoinfunction
(defun my-conjoin
  (termlist1 termlist2)
  (declare (xargs :guard (and (true-listp termlist1)
        (true-listp termlist2)
        (or (consp termlist1) (consp termlist2)))))
  (let ((termlist (append termlist1 termlist2)))
    (cond ((= (len termlist) 1) (car termlist))
      (t (fcons-term 'and termlist)))))
my-conjunctsmutual-recursion
(mutual-recursion (defun my-conjuncts
    (term)
    (cond ((eq term t) nil)
      ((atom term) (list term))
      ((eq (car term) 'and) (my-conjuncts-list (cdr term)))
      (t (list term))))
  (defun my-conjuncts-list
    (termlist)
    (cond ((atom termlist) nil)
      (t (append (my-conjuncts (car termlist))
          (my-conjuncts-list (cdr termlist)))))))
alist-type-alistp-lemmamacro
(defmacro alist-type-alistp-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn
      ran-type-fn
      dom-elem-type-fn
      ran-elem-type-fn))
  `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
    (alistp l)))
alist-type-alistptheorem
(defthm alist-type-alistp
  (alist-type-alistp-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :rule-classes :forward-chaining :hints (("Goal" :induct t)))
in-theory
(in-theory (disable alist-type-alistp))
alist-type-acons-lemmamacro
(defmacro alist-type-acons-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn ran-type-fn))
  (let* ((vars (unique-symbols 2
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var1 (car vars))
      (var2 (cadr vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (,DOM-ELEM-TYPE-FN ,VAR1)
    (,RAN-ELEM-TYPE-FN ,VAR2)))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(ACONS ,VAR1 ,VAR2 L) FORMALS)))))
alist-type-aconstheorem
(defthm alist-type-acons
  (alist-type-acons-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :do-not-induct t
     :in-theory (enable alist-type-alistp acons))))
alist-type-append-lemmamacro
(defmacro alist-type-append-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn
      ran-type-fn
      dom-elem-type-fn
      ran-elem-type-fn))
  (let* ((vars (unique-symbols 2
         (intern-in-package-of-symbol "L" alist-type-fn)
         formals)) (var1 (car vars))
      (var2 (cadr vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L VAR1 FORMALS))
    (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L VAR2 FORMALS))))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(APPEND ,VAR1 ,VAR2) FORMALS)))))
alist-type-appendtheorem
(defthm alist-type-append
  (alist-type-append-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-bind-equal-lemmamacro
(defmacro alist-type-bind-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn ran-type-fn))
  (let* ((vars (unique-symbols 2
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var1 (car vars))
      (var2 (cadr vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (,DOM-ELEM-TYPE-FN ,VAR1)
    (,RAN-ELEM-TYPE-FN ,VAR2)))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(BIND-EQUAL ,VAR1 ,VAR2 L) FORMALS)))))
alist-type-bind-equaltheorem
(defthm alist-type-bind-equal
  (alist-type-bind-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-rembind-equal-lemmamacro
(defmacro alist-type-rembind-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn
      ran-type-fn
      dom-elem-type-fn
      ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(REMBIND-EQUAL ,VAR L) FORMALS)))))
alist-type-rembind-equaltheorem
(defthm alist-type-rembind-equal
  (alist-type-rembind-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-pairlis$-lemmamacro
(defmacro alist-type-pairlis$-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-elem-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 2
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var1 (car vars))
      (var2 (cadr vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((TRUE-LISTP ,VAR1) (,DOM-TYPE-FN ,VAR1) (,RAN-TYPE-FN ,VAR2)
    (EQL (LEN ,VAR1) (LEN ,VAR2))))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(PAIRLIS$ ,VAR1 ,VAR2) FORMALS)))))
alist-type-pairlis$theorem
(defthm alist-type-pairlis$
  (alist-type-pairlis$-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-bind-all-equal-lemmamacro
(defmacro alist-type-bind-all-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-elem-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 3
         (intern-in-package-of-symbol "L" alist-type-fn)
         formals)) (var1 (car vars))
      (var2 (cadr vars))
      (var3 (caddr vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,DOM-TYPE-FN ,@(REPLACE-EQUAL 'L VAR1 FORMALS))
    (,RAN-TYPE-FN ,@(REPLACE-EQUAL 'L VAR2 FORMALS))
    (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L VAR3 FORMALS))))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(BIND-ALL-EQUAL ,VAR1 ,VAR2 ,VAR3) FORMALS)))))
alist-type-bind-all-equaltheorem
(defthm alist-type-bind-all-equal
  (alist-type-bind-all-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-domain-restrict-equal-lemmamacro
(defmacro alist-type-domain-restrict-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn
      ran-type-fn
      dom-elem-type-fn
      ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "L" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(DOMAIN-RESTRICT-EQUAL ,VAR L) FORMALS)))))
alist-type-domain-restrict-equaltheorem
(defthm alist-type-domain-restrict-equal
  (alist-type-domain-restrict-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-rembind-all-equal-lemmamacro
(defmacro alist-type-rembind-all-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn
      ran-type-fn
      dom-elem-type-fn
      ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "L" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(REMBIND-ALL-EQUAL ,VAR L) FORMALS)))))
alist-type-rembind-all-equaltheorem
(defthm alist-type-rembind-all-equal
  (alist-type-rembind-all-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-bind-pairs-equal-lemmamacro
(defmacro alist-type-bind-pairs-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn
      ran-type-fn
      dom-elem-type-fn
      ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "PAIRS" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L VAR FORMALS))
    (,ALIST-TYPE-FN ,@FORMALS)))
      (,ALIST-TYPE-FN ,@(REPLACE-EQUAL 'L `(BIND-PAIRS-EQUAL ,VAR L) FORMALS)))))
alist-type-bind-pairs-equaltheorem
(defthm alist-type-bind-pairs-equal
  (alist-type-bind-pairs-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :induct t)))
alist-type-assoc-equal-lemmamacro
(defmacro alist-type-assoc-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn ran-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (NOT (,DOM-ELEM-TYPE-FN ,VAR))))
      (not (assoc-equal ,VAR l)))))
alist-type-assoc-equaltheorem
(defthm alist-type-assoc-equal
  (alist-type-assoc-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (enable alist-type-alistp assoc-equal))))
alist-type-bound?-equal-lemmamacro
(defmacro alist-type-bound?-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn ran-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (NOT (,DOM-ELEM-TYPE-FN ,VAR))))
      (not (bound?-equal ,VAR l)))))
alist-type-bound?-equaltheorem
(defthm alist-type-bound?-equal
  (alist-type-bound?-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (enable alist-type-alistp bound?-equal))))
alist-type-all-bound?-equal-lemmamacro
(defmacro alist-type-all-bound?-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore ran-type-fn dom-elem-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (TRUE-LISTP ,VAR) (NOT (,DOM-TYPE-FN ,VAR))))
      (not (all-bound?-equal ,VAR l)))))
alist-type-all-bound?-equaltheorem
(defthm alist-type-all-bound?-equal
  (alist-type-all-bound?-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (set-difference-theories (enable alist-type-alistp all-bound?-equal)
       '(bound?-equal)))))
alist-type-binding-equal-lemmamacro
(defmacro alist-type-binding-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn ran-type-fn dom-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (BOUND?-EQUAL ,VAR L)))
      (,RAN-ELEM-TYPE-FN (binding-equal ,VAR l)))))
alist-type-binding-equaltheorem
(defthm alist-type-binding-equal
  (alist-type-binding-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :rule-classes :forward-chaining :hints (("Goal" :in-theory (enable alist-type-alistp binding-equal bound?-equal))))
alist-type-cdr-assoc-equal-lemmamacro
(defmacro alist-type-cdr-assoc-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn ran-type-fn dom-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD)
  `((,ALIST-TYPE-FN ,@FORMALS) (ASSOC-EQUAL ,VAR L)))
      (,RAN-ELEM-TYPE-FN (cdr (assoc-equal ,VAR l))))))
alist-type-cdr-assoc-equaltheorem
(defthm alist-type-cdr-assoc-equal
  (alist-type-cdr-assoc-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :rule-classes :forward-chaining :hints (("Goal" :in-theory (enable alist-type-alistp assoc-equal bound?-equal))))
alist-type-domain-lemmamacro
(defmacro alist-type-domain-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore ran-type-fn dom-elem-type-fn ran-elem-type-fn))
  `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
    (,DOM-TYPE-FN (domain l))))
alist-type-domaintheorem
(defthm alist-type-domain
  (alist-type-domain-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (enable alist-type-alistp domain))))
alist-type-range-lemmamacro
(defmacro alist-type-range-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn dom-elem-type-fn ran-elem-type-fn))
  `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
    (,RAN-TYPE-FN (range l))))
alist-type-rangetheorem
(defthm alist-type-range
  (alist-type-range-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (enable alist-type-alistp range))))
alist-type-collect-bound-equal-lemmamacro
(defmacro alist-type-collect-bound-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore ran-type-fn dom-elem-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
      (,DOM-TYPE-FN ,@(REPLACE-EQUAL 'L `(COLLECT-BOUND-EQUAL ,VAR L) FORMALS)))))
alist-type-collect-bound-equaltheorem
(defthm alist-type-collect-bound-equal
  (alist-type-collect-bound-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (set-difference-theories (enable alist-type-alistp collect-bound-equal)
       '(bound?-equal)))))
alist-type-all-bindings-equal-lemmamacro
(defmacro alist-type-all-bindings-equal-lemma
  (alist-type-fn dom-type-fn
    ran-type-fn
    dom-elem-type-fn
    ran-elem-type-fn
    formals
    &optional
    (guard 't))
  (declare (ignore dom-type-fn dom-elem-type-fn ran-elem-type-fn))
  (let* ((vars (unique-symbols 1
         (intern-in-package-of-symbol "X" alist-type-fn)
         formals)) (var (car vars)))
    `(implies ,(MY-CONJOIN (MY-CONJUNCTS GUARD) `((,ALIST-TYPE-FN ,@FORMALS)))
      (,RAN-TYPE-FN ,@(REPLACE-EQUAL 'L `(ALL-BINDINGS-EQUAL ,VAR L) FORMALS)))))
alist-type-all-bindings-equaltheorem
(defthm alist-type-all-bindings-equal
  (alist-type-all-bindings-equal-lemma alist-type
    domain-type
    range-type
    domain-elem-type
    range-elem-type
    (l))
  :hints (("Goal" :in-theory (set-difference-theories (enable alist-type-alistp all-bindings-equal)
       '(bound?-equal binding-equal)))))
*defalist-true-fn*constant
(defconst *defalist-true-fn*
  '(lambda (x) (declare (ignore x)) t))
*defalist-theory-options*constant
(defconst *defalist-theory-options*
  '(acons alistp
    all-bindings-equal
    all-bound?-equal
    append
    assoc-equal
    bind-all-equal
    bind-equal
    bind-pairs-equal
    binding-equal
    bound?-equal
    collect-bound-equal
    domain
    domain-restrict-equal
    pairlis$
    range
    rembind-all-equal
    rembind-equal))
pack-intern-namesfunction
(defun pack-intern-names
  (name1 name2)
  (pack-intern name1 name1 "-" name2))
other
(defloop pack-intern-all-names
  (name l)
  (for ((x in l)) (collect (pack-intern-names name x))))
other
(defloop defalist-defthms
  (alist-type-fn formals
    dom-elem-type-fn
    ran-elem-type-fn
    dom-type-fn
    ran-type-fn
    guard
    theory
    binding-equal-rule-classes)
  (declare (xargs :guard (and (symbolp alist-type-fn)
        (arglistp formals)
        (consp formals)
        (symbol-listp theory))
      :mode :program))
  (for ((fn in theory))
    (append (let ((lemmaname (pack-intern-names alist-type-fn fn)) (lemma-macro-name (pack-intern alist-type-fn 'alist-type- fn '-lemma))
          (rule-classes (case fn
              (binding-equal binding-equal-rule-classes)
              (alistp '(:forward-chaining))
              (t '(:rewrite))))
          (hints (if (or (consp (my-conjuncts guard))
                (and (equal dom-type-fn *defalist-true-fn*)
                  (not (equal dom-elem-type-fn *defalist-true-fn*)))
                (and (equal ran-type-fn *defalist-true-fn*)
                  (not (equal ran-elem-type-fn *defalist-true-fn*)))
                (> (len formals) 1))
              `(("Goal" :in-theory (enable ,FN
                   ,@(IF (EQ FN 'BINDING-EQUAL)
      '(BOUND?-EQUAL)
      NIL))))
              (let ((domain-elem-type-instance (if (equal dom-elem-type-fn *defalist-true-fn*)
                     (remove-equal '(declare (ignore x)) dom-elem-type-fn)
                     (cond ((and (consp dom-elem-type-fn)
                          (eq (car dom-elem-type-fn) 'lambda)) dom-elem-type-fn)
                       (t `(lambda (x) (,DOM-ELEM-TYPE-FN x)))))) (domain-type-instance (if (equal dom-type-fn *defalist-true-fn*)
                      (remove-equal '(declare (ignore x)) dom-type-fn)
                      dom-type-fn))
                  (range-elem-type-instance (if (equal ran-elem-type-fn *defalist-true-fn*)
                      (remove-equal '(declare (ignore x)) ran-elem-type-fn)
                      (cond ((and (consp ran-elem-type-fn)
                           (eq (car ran-elem-type-fn) 'lambda)) ran-elem-type-fn)
                        (t `(lambda (x) (,RAN-ELEM-TYPE-FN x))))))
                  (range-type-instance (if (equal ran-type-fn *defalist-true-fn*)
                      (remove-equal '(declare (ignore x)) ran-type-fn)
                      ran-type-fn)))
                `(("Goal" :do-not-induct t
                   :use (:functional-instance ,(U::PACK-INTERN ALIST-TYPE-FN 'ALIST-TYPE- FN)
                     (domain-elem-type ,DOMAIN-ELEM-TYPE-INSTANCE)
                     (domain-type ,DOMAIN-TYPE-INSTANCE)
                     (range-elem-type ,RANGE-ELEM-TYPE-INSTANCE)
                     (range-type ,RANGE-TYPE-INSTANCE)
                     (alist-type ,ALIST-TYPE-FN))))))))
        `((defthm ,LEMMANAME
           (,LEMMA-MACRO-NAME ,ALIST-TYPE-FN
             ,DOM-TYPE-FN
             ,RAN-TYPE-FN
             ,DOM-ELEM-TYPE-FN
             ,RAN-ELEM-TYPE-FN
             ,FORMALS
             ,GUARD)
           :rule-classes ,RULE-CLASSES
           :hints ,HINTS) ,@(IF (AND (EQ FN 'ASSOC-EQUAL)
           (NOT (EQUAL RAN-ELEM-TYPE-FN *DEFALIST-TRUE-FN*)))
      `((DEFTHM ,(PACK-INTERN-NAMES ALIST-TYPE-FN (PACK-INTERN-NAMES 'CDR FN))
                (,(U::PACK-INTERN ALIST-TYPE-FN
                   'ALIST-TYPE-CDR-ASSOC-EQUAL-LEMMA)
                 ,ALIST-TYPE-FN ,DOM-TYPE-FN ,RAN-TYPE-FN ,DOM-ELEM-TYPE-FN
                 ,RAN-ELEM-TYPE-FN ,FORMALS ,GUARD)
                :RULE-CLASSES ,BINDING-EQUAL-RULE-CLASSES :HINTS
                ,(SUBST
                  (PACK-INTERN-NAMES 'ALIST-TYPE (PACK-INTERN-NAMES 'CDR FN))
                  (U::PACK-INTERN ALIST-TYPE-FN 'ALIST-TYPE- FN) HINTS)))
      NIL))))))
*defalist-options*constant
(defconst *defalist-options*
  '(:binding-equal-rule-classes :theory :omit-defun :domain-type :range-type :theory-name))
*forward-chaining-elem-types*constant
(defconst *forward-chaining-elem-types*
  '(integerp rationalp
    complex-rationalp
    symbolp
    true-listp
    stringp
    characterp
    alistp
    acl2-numberp))
defalist-check-syntaxfunction
(defun defalist-check-syntax
  (name formals body)
  "Return NIL if no errors, otherwise crash."
  (declare (xargs :mode :program))
  (cond ((not (symbolp name)) (bomb 'defalist
        "The function name must be a symbol, but ~p0 is not."
        name))
    ((not (true-listp formals)) (bomb 'defalist
        "The argument list ~p0 is not a true list."
        formals))
    ((not (arglistp formals)) (mv-let (elmt msg)
        (find-first-bad-arg formals)
        (bomb 'defalist
          "The argument list ~p0 is not valid because the ~
                          element ~p1 ~@2."
          formals
          elmt
          msg)))
    ((let* ((formal-strings (mapcar-string formals)) (l-tail (member-equal "L" formal-strings))
         (multiple-ls (member-equal "L" (cdr l-tail))))
       (or (not l-tail) multiple-ls)) (bomb 'defalist
        "The formal argument list to DEFALIST must be a valid ~
                       functional argument list that contains exactly 1 ~
                       symbol whose print-name is "L", but ~p0 is not."
        formals))
    ((null body) (bomb 'defalist "The function body is empty!"))
    (t (let* ((last-form (car (last body))) (options? (and (>= (len body) 2)
              (true-listp last-form)
              (eq (car last-form) :options)))
          (predicate (if options?
              (car (last (butlast body 1)))
              last-form)))
        (cond ((and (consp predicate)
             (let ((d (car predicate)) (r (cdr predicate)))
               (and (or (symbolp d)
                   (and (true-listp d)
                     (>= (len d) 3)
                     (eq (first d) 'lambda)
                     (arglistp (second d))
                     (equal (len (second d)) 1)))
                 (or (symbolp r)
                   (and (true-listp r)
                     (>= (len r) 3)
                     (eq (first r) 'lambda)
                     (arglistp (second r))
                     (equal (len (second r)) 1)))))) nil)
          (t (bomb 'defalist
              "The DEFALIST predicate designator must be a ~
                             pair (d . r) where d is either a function symbol
                             or a 1-argument LAMBDA function, and r ~
                             is either a function symbol or a 1-argument
                             LAMBDA function. ~p0 does not satisfy this
                             requirement."
              predicate)))))))
other
(deftheory minimal-theory-for-defalist
  (union-theories (current-theory 'ground-zero)
    (current-theory 'alist-defuns)))
other
(defloop filter-alist-theory
  (theory dom-elem-type-fn
    ran-elem-type-fn
    dom-type-fn
    ran-type-fn)
  (for ((fn in theory))
    (when (case fn
        (binding-equal (not (equal ran-elem-type-fn *defalist-true-fn*)))
        (bound?-equal (not (equal dom-elem-type-fn *defalist-true-fn*)))
        (domain (not (equal dom-type-fn *defalist-true-fn*)))
        (range (not (equal ran-type-fn *defalist-true-fn*)))
        (pairlis$ (and (not (equal dom-type-fn *defalist-true-fn*))
            (not (equal ran-type-fn *defalist-true-fn*))))
        (bind-all-equal (and (not (equal dom-type-fn *defalist-true-fn*))
            (not (equal ran-type-fn *defalist-true-fn*))))
        (all-bound?-equal (not (equal dom-type-fn *defalist-true-fn*)))
        (collect-bound-equal (not (equal dom-type-fn *defalist-true-fn*)))
        (all-bindings-equal (not (equal ran-type-fn *defalist-true-fn*)))
        (t t))
      (collect fn))))
other
(defsection defalist
  :parents (data-structures)
  :short "Define a new alist type, and a theory of the alist type."
  :long "Examples:

@({
  (defalist symbol-to-integer-alistp (l)
    "Recognizes an alist mapping symbols to integers."
    (symbolp . integerp))

  (defalist symbol-to-bnatural-alistp (l lub)
    "Recognizes an alists mapping symbols to  naturals bounded by lub."
    (symbolp . (lambda (x) (bnaturalp x lub))))

  (defalist symbol-alistp (l)
    "Define an alist theory alists from an unspecified domain type to
     symbols."
    ((lambda (x) t) . symbolp)
    (:options :omit-defun (:range-type symbol-listp)))

  (defalist string-to-integer-alistp (l)
    "Recognizes an alist mapping strings to integers. Produce a minimal
     theory, and store the BINDING-EQUAL lemma as a :TYPE-PRESCRIPTION."
    (stringp . integerp)
    (:options (:theory nth put) (:binding-equal-rule-classes :type-prescription)
              (:domain-type string-listp) (:range-type integer-listp)))
})

<p>Syntax:</p>

@({
   DEFALIST name arglist [documentation] {declaration}* type-pair [option-list]

   option-list ::= (:OPTIONS <<!options>>)

   options ::= !binding-equal-rule-classes-option |
               !omit-defun-option |
               !theory-option |
               !domain-type-option |
               !range-type-option |
               !theory-name-option

   theory-option ::= (:THEORY <<!alist-functions>>)

   theory-name-option ::= (:THEORY-NAME theory-name)

   alist-functions ::= acons | alistp | all-bindings-equal| all-bound?-equal | append |
                       assoc-equal | bind-all-equal | bind-equal | bind-pairs-equal |
                       binding-equal | bound?-equal | collect-bound-equal | domain |
                       domain-restrict-equal | pairlis$ | range | rembind-all-equal |
                       rembind-equal

   binding-equal-rule-classes-option ::= (:BINDING-EQUAL-RULE-CLASSES rule-classes)

   omit-defun-option ::= :OMIT-DEFUN
})

<p>Arguments and Values:</p>

@({
   arglist -- an argument list satisfying ACL2::ARGLISTP, and containing
     exactly one symbol whose `print-name' is "L".

   declaration -- any valid declaration.

   documentation -- a string; not evaluated.

   name -- a symbol.

   theory-name -- any symbol that is a legal name for a deftheory event.

   type-pair -- A pair (d . r) where d and r are either a function symbol
     or a one argument LAMBDA function or the constant T.
     d designates a predicate to be applied to each element of the domain
     of the alist, and r designates a predicate to be applied to each element
     of the range of the alist. T means no type restriction.

   rule-classes -- any form legal as an argument to the :RULE-CLASSES keyword
    of DEFTHM.

   Acl2-theory-expression -- Any legal Acl2 theory expression
})

<h3>Description:</h3>

<p>DEFALIST defines a recognizer for association lists whose pairs map
  keys of a given type to values of a given type, and by default creates
  an extensive theory for alists of the newly defined type.</p>

<p>To define an alist type with DEFALIST you must supply a name for the alist
  recognizer, an argument list for the recognizer, and predicate designator for
  elements of the alist's range. The name may be any symbol.  The argument list
  must be valid as a functional argument list, and must contain exactly one
  symbol whose `print-name'is "L".  By convention this is the alist argument
  recognized by the function defined by DEFALIST.</p>

<p>The type of the domain and range of the alist is given by a pair (d . r)
  where d identifies the type of an element of the alist's domain, and r
  specifies the type of an element of the alist's range. Either of these
  may be specified by a symbol which names a one-argument function (or macro)
  which tests the elements of the domain and range of L. Either of d and r may
  also be specified as a single-argument LAMBDA function. Finally, either of d
  and r may be specified as the constant t, indicating no type constraint.</p>

<p>Any number of other arguments to the alist functions may be supplied,
  but only the L argument will change in the recursive structure of the recognizer.</p>

<p>Note that DEFALIST does not create any guards for L or any other argument.
  Guards may be specified in the usual way since any number of DECLARE forms
  may preceed the predicate specification in the DEFALIST form.  Bear in mind
  that if you are defining a function to be used as a guard, then you are
  advised to consider what impact guarding the arguments of the function may
  have on its utility.  In general the most useful guard functions are those
  that are guard-free.</p>

<h3>Theory</h3>

<p>By default, DEFALIST creates an extensive theory for the recognized alists.
  This theory contains appropriate lemmas for all of the alist functions
  appearing in the `alist-functions' syntax description above.  One can select
  a subset of this theory to be generated by using the :THEORY option
  (see below).  DEFALIST always creates a :FORWARD-CHAINING rule from the
  recognizer to ALISTP.</p>

<p>DEFALIST also creates a DEFTHEORY event that lists all of the lemmas created
  by the DEFALIST.  The name of the theory is formed by concatenating the
  function name and the string "-THEORY", and INTERNing the resulting string
  in the package of the function name.</p>

<h3>Options</h3>

<p>DEFALIST options are specified with a special :OPTIONS list systax.  If
  present, the :OPTIONS list must appear as the last form in the body of the
  DEFALIST.</p>

<dl>
<dt>:OMIT-DEFUN</dt>
<dd>If the :OMIT-DEFUN keyword is present then the definition will not be
    created.  Instead, only the list theory for the function is
    generated. Use this option to create a list theory for recognizers
    defined elsewhere.</dd>

<dt>:THEORY</dt>
<dd>This option is used to specify that only a subset of the list theory be
   created.  In the STRINGP-LISTP example above we specify that only lemmas
   about STRINGP-LISTP viz-a-viz NTH and PUT are to be generated.  By default
   the complete list theory for the recognizer is created.  If the option is
   given as (:THEORY) then the entire theory will be suppressed,
   except for the :FORWARD-CHAINING rule from the recognizer to TRUE-LISTP.</dd>

<dt>:BINDING-EQUAL-RULE-CLASSES</dt>
<dd>This option specifies a value for the :RULE-CLASSES keyword for the
   DEFTHM generated for the BINDING-EQUAL function (and for CDRASSOC) applied to
   an alist recognized by the DEFALIST recognizer.  The default is :REWRITE.</dd>

<dt>:DOMAIN-TYPE</dt>
<dd>This option specifies a predicate that recognizes a list of domain elements.
   It may be either a symbol or LAMBDA form. If present, and when not prevented
   by a :THEORY specification, a rewrite rule for the type of the domain
   will be generated. A lemma will be generated to check the compatibility
   of the specified domain type and domain element type.</dd>

<dt>:RANGE-TYPE</dt>
<dd>This option specifies a predicate that recognizes a list of range elements.
   It may be either a symbol or LAMBDA form. If present, and when not prevented
   by a :THEORY specification, a rewrite rule for the type of the range
   will be generated.  A lemma will be generated to check the compatibility
   of the specified range type and domain element type.</dd>

<dt>:THEORY-NAME</dt>
<dd>This option allows the user to define the name of the deftheory event
   that is automatically generated, and which includes the defthms that
   are generated.</dd>
</dl>")
defalistmacro
(defmacro defalist
  (name formals &rest body)
  (let* ((syntax-err (defalist-check-syntax name formals body)) (last-form (car (last body)))
      (options? (and (>= (len body) 2)
          (true-listp last-form)
          (eq (car last-form) :options)))
      (option-list (if options?
          (cdr last-form)
          nil))
      (type-pair (if options?
          (car (last (butlast body 1)))
          last-form))
      (dom-elemtype (if (eq (car type-pair) t)
          *defalist-true-fn*
          (car type-pair)))
      (ran-elemtype (if (eq (cdr type-pair) t)
          *defalist-true-fn*
          (cdr type-pair)))
      (l (nth (position-equal "L" (mapcar-string formals))
          formals))
      (guard (get-guards-from-body body))
      (ctx 'defalist)
      (option-err (get-option-check-syntax ctx
          option-list
          *defalist-options*
          nil
          nil))
      (omit-defun (get-option-as-flag ctx :omit-defun option-list))
      (theory (get-option-subset ctx
          :theory option-list
          *defalist-theory-options*
          *defalist-theory-options*))
      (binding-equal-rule-classes (get-option-argument ctx
          :binding-equal-rule-classes option-list
          :form :rewrite :rewrite))
      (domain-type (get-option-argument ctx
          :domain-type option-list
          :form *defalist-true-fn*
          *defalist-true-fn*))
      (range-type (get-option-argument ctx
          :range-type option-list
          :form *defalist-true-fn*
          *defalist-true-fn*))
      (theory-name (get-option-argument ctx
          :theory-name option-list
          :form (pack-intern name name "-THEORY")
          (pack-intern name name "-THEORY"))))
    (or syntax-err
      option-err
      (and (equal dom-elemtype *defalist-true-fn*)
        (not (equal domain-type *defalist-true-fn*)))
      (and (equal ran-elemtype *defalist-true-fn*)
        (not (equal range-type *defalist-true-fn*)))
      (let ((theory1 (union-equal '(alistp)
             (filter-alist-theory theory
               dom-elemtype
               ran-elemtype
               domain-type
               range-type))))
        `(encapsulate nil
          (local (deflabel defalist-reserved-label))
          ,@(IF OMIT-DEFUN
      NIL
      (LIST
       `(DEFUN ,NAME ,FORMALS
          ,@(BUTLAST BODY
                     (IF OPTIONS?
                         2
                         1))
          (COND ((ATOM ,L) (NULL ,L))
                (T
                 (AND (CONSP (CAR ,L)) (,DOM-ELEMTYPE (CAAR ,L))
                      (,RAN-ELEMTYPE (CDAR ,L))
                      (,NAME ,@(REPLACE-EQUAL L `(CDR ,L) FORMALS))))))))
          ,@(DEFALIST-DEFTHMS NAME FORMALS DOM-ELEMTYPE RAN-ELEMTYPE DOMAIN-TYPE
   RANGE-TYPE GUARD THEORY1 BINDING-EQUAL-RULE-CLASSES)
          (deftheory ,THEORY-NAME
            ',(PACK-INTERN-ALL-NAMES NAME THEORY1)))))))