Filtering...

csort

books/misc/csort
other
(in-package "ACL2")
firstnfunction
(defun firstn
  (n l)
  "The sublist of L consisting of its first N elements."
  (declare (xargs :guard (and (true-listp l) (integerp n) (<= 0 n))))
  (cond ((endp l) nil)
    ((zp n) nil)
    (t (cons (car l) (firstn (1- n) (cdr l))))))
member-equal-car-firstntheorem
(defthm member-equal-car-firstn
  (implies (and (integerp n) (<= 1 n) (consp lst))
    (member-equal (car (firstn n lst)) lst)))
firstn-too-longtheorem
(defthm firstn-too-long
  (implies (and (true-listp lst) (integerp n) (<= (len lst) n))
    (equal (firstn n lst) lst)))
repeatfunction
(defun repeat
  (n x)
  (cond ((zp n) nil) (t (cons x (repeat (1- n) x)))))
evnpfunction
(defun evnp
  (n)
  (cond ((zp n) t) ((zp (1- n)) nil) (t (evnp (1- (1- n))))))
revfunction
(defun rev
  (x)
  (cond ((endp x) nil)
    (t (append (rev (cdr x)) (list (car x))))))
car-appendtheorem
(defthm car-append
  (equal (car (append a b))
    (if (consp a)
      (car a)
      (car b))))
assoc-of-appendtheorem
(defthm assoc-of-append
  (equal (append (append a b) c) (append a (append b c))))
true-listp-append-rewritetheorem
(defthm true-listp-append-rewrite
  (equal (true-listp (append a b)) (true-listp b)))
len-appendtheorem
(defthm len-append
  (equal (len (append x y)) (+ (len x) (len y))))
+-constant-foldingtheorem
(defthm +-constant-folding
  (implies (syntaxp (and (quotep x) (quotep y)))
    (equal (+ x (+ y z)) (+ (+ x y) z))))
delete-equalfunction
(defun delete-equal
  (x lst)
  (cond ((endp lst) nil)
    ((equal x (car lst)) (cdr lst))
    (t (cons (car lst) (delete-equal x (cdr lst))))))
permfunction
(defun perm
  (lst1 lst2)
  (cond ((endp lst1) (endp lst2))
    ((member-equal (car lst1) lst2) (perm (cdr lst1) (delete-equal (car lst1) lst2)))
    (t nil)))
perm-reflexivetheorem
(defthm perm-reflexive (perm x x))
perm-constheorem
(defthm perm-cons
  (implies (member-equal a x)
    (equal (perm x (cons a y)) (perm (delete-equal a x) y)))
  :hints (("Goal" :induct (perm x y))))
perm-symmetrictheorem
(defthm perm-symmetric (implies (perm x y) (perm y x)))
member-equal-delete-equaltheorem
(defthm member-equal-delete-equal
  (implies (member-equal a (delete-equal b x))
    (member-equal a x)))
perm-member-equaltheorem
(defthm perm-member-equal
  (implies (and (perm x y) (member-equal a x))
    (member-equal a y)))
member-equal-delete-equal2theorem
(defthm member-equal-delete-equal2
  (implies (and (member-equal a x) (not (equal a b)))
    (member-equal a (delete-equal b x))))
comm-delete-equaltheorem
(defthm comm-delete-equal
  (equal (delete-equal a (delete-equal b x))
    (delete-equal b (delete-equal a x))))
perm-delete-equaltheorem
(defthm perm-delete-equal
  (implies (perm x y)
    (perm (delete-equal a x) (delete-equal a y))))
perm-transitivetheorem
(defthm perm-transitive
  (implies (and (perm x y) (perm y z)) (perm x z)))
other
(defequiv perm)
other
(defcong perm perm (cons x y) 2)
other
(defcong perm iff (member-equal x y) 2)
atom-permtheorem
(defthm atom-perm
  (implies (not (consp x)) (perm x nil))
  :rule-classes :forward-chaining)
member-equal-appendtheorem
(defthm member-equal-append
  (iff (member-equal e (append x y))
    (or (member-equal e x) (member-equal e y))))
other
(defcong perm perm (append x y) 1)
other
(defcong perm perm (append x y) 2)
perm-append-constheorem
(defthm perm-append-cons
  (perm (append x (cons i y)) (cons i (append x y))))
other
(defcong perm equal (len x) 1)
perm-revtheorem
(defthm perm-rev (perm (rev x) x))
perm-append-niltheorem
(defthm perm-append-nil (perm (append x nil) x))
append-repeattheorem
(defthm append-repeat
  (equal (append (repeat n x) (list x)) (cons x (repeat n x))))
rev-repeattheorem
(defthm rev-repeat (equal (rev (repeat n x)) (repeat n x)))
len-repeattheorem
(defthm len-repeat (equal (len (repeat n x)) (nfix n)))
consp-repeattheorem
(defthm consp-repeat
  (equal (consp (repeat n x)) (not (zp n)))
  :hints (("Goal" :expand (repeat 1 x))))
car-repeattheorem
(defthm car-repeat
  (equal (car (repeat n x))
    (if (zp n)
      nil
      x))
  :hints (("Goal" :expand (repeat 1 x))))
datafunction
(defun data (pair) (cdr pair))
in-theory
(in-theory (disable data))
orderedfunction
(defun ordered
  (lst)
  (cond ((endp lst) t)
    ((endp (cdr lst)) t)
    ((>= (data (car lst)) (data (cadr lst))) (ordered (cdr lst)))
    (t nil)))
all-gtefunction
(defun all-gte
  (pair lst)
  (cond ((endp lst) t)
    ((and (>= (data pair) (data (car lst)))
       (all-gte pair (cdr lst))))
    (t nil)))
all-gte-delete-equal-1theorem
(defthm all-gte-delete-equal-1
  (implies (all-gte pair lst)
    (all-gte pair (delete-equal x lst))))
all-gte-delete-equal-2theorem
(defthm all-gte-delete-equal-2
  (implies (and (all-gte pair (delete-equal x lst))
      (>= (data pair) (data x)))
    (all-gte pair lst)))
encapsulate
(encapsulate nil
  (local (defthm lemma
      (implies (and (perm lst lst-equiv)
          (syntaxp (not (term-order lst lst-equiv)))
          (all-gte pair lst))
        (all-gte pair lst-equiv))))
  (defcong perm equal (all-gte pair lst) 2))
all-gte-implies-gte-member-equaltheorem
(defthm all-gte-implies-gte-member-equal
  (implies (and (all-gte pair1 a) (member-equal pair2 a))
    (<= (data pair2) (data pair1)))
  :rule-classes :linear)
gte-car-firstntheorem
(defthm gte-car-firstn
  (implies (and (all-gte pair a)
      (perm a b)
      (integerp n)
      (<= 1 n)
      (consp b))
    (<= (data (car (firstn n b))) (data pair)))
  :rule-classes nil)
all-gte-helpertheorem
(defthm all-gte-helper
  (implies (and (all-gte pair1 lst) (<= (data pair1) (data pair2)))
    (all-gte pair2 lst)))
all-all-gtefunction
(defun all-all-gte
  (lst1 lst2)
  (cond ((endp lst1) t)
    (t (and (all-gte (car lst1) lst2)
        (all-all-gte (cdr lst1) lst2)))))
other
(defcong perm equal (all-all-gte lst1 lst2) 1)
other
(defcong perm equal (all-all-gte lst1 lst2) 2)
all-all-gte-cdrtheorem
(defthm all-all-gte-cdr
  (implies (all-all-gte lst acc) (all-all-gte lst (cdr acc))))
ordered-constheorem
(defthm ordered-cons
  (implies (all-gte pair1 s)
    (equal (ordered (cons pair1 s)) (ordered s))))
all-gte-append-constheorem
(defthm all-gte-append-cons
  (equal (all-gte pair1 (append s (list pair2)))
    (and (all-gte pair1 s) (>= (data pair1) (data pair2)))))
ordered-appendtheorem
(defthm ordered-append
  (equal (ordered (append lst1 lst2))
    (and (ordered lst1) (ordered lst2) (all-all-gte lst1 lst2))))
ordered-cons-append-constheorem
(defthm ordered-cons-append-cons
  (implies (and (all-gte pair1 s)
      (>= (data pair1) (data pair2))
      (all-all-gte s (list pair2))
      (ordered s))
    (ordered (cons pair1 (append s (list pair2)))))
  :rule-classes nil)
all-all-gte-constheorem
(defthm all-all-gte-cons
  (implies (syntaxp (not (equal acc ''nil)))
    (equal (all-all-gte s (cons pair acc))
      (and (all-all-gte s (list pair)) (all-all-gte s acc)))))
all-all-gte-append1theorem
(defthm all-all-gte-append1
  (equal (all-all-gte s (append lst1 lst2))
    (and (all-all-gte s lst1) (all-all-gte s lst2)))
  :hints (("Goal" :induct (append lst1 lst2))))
all-all-gte-append2theorem
(defthm all-all-gte-append2
  (equal (all-all-gte (append lst1 lst2) lst3)
    (and (all-all-gte lst1 lst3) (all-all-gte lst2 lst3))))
all-all-gte-niltheorem
(defthm all-all-gte-nil (all-all-gte lst nil))
ordered-repeattheorem
(defthm ordered-repeat (ordered (repeat n x)))
all-all-gte-repeattheorem
(defthm all-all-gte-repeat
  (equal (all-all-gte (repeat n maxpair) acc)
    (or (zp n) (all-gte maxpair acc)))
  :hints (("Goal" :induct (repeat n maxpair))))
all-all-gte-repeat-2theorem
(defthm all-all-gte-repeat-2
  (implies (all-all-gte lst (list minpair))
    (all-all-gte lst (repeat n minpair))))
max-pairfunction
(defun max-pair
  (pair1 pair2)
  (cond ((<= (data pair1) (data pair2)) pair2) (t pair1)))
min-pairfunction
(defun min-pair
  (pair1 pair2)
  (cond ((<= (data pair1) (data pair2)) pair1) (t pair2)))
cstepfunction
(defun cstep
  (acc)
  (cond ((endp acc) nil)
    ((endp (cdr acc)) acc)
    (t (cons (max-pair (cadr acc) (car acc))
        (cons (min-pair (cadr acc) (car acc)) (cstep (cddr acc)))))))
cfeedfunction
(defun cfeed
  (lst acc)
  (cond ((endp lst) acc)
    (t (cfeed (cdr lst) (cstep (cons (car lst) acc))))))
cdrainfunction
(defun cdrain
  (n acc)
  (cond ((zp n) acc)
    (t (cons (car acc) (cdrain (1- n) (cstep (cdr acc)))))))
csortfunction
(defun csort (lst) (cdrain (len lst) (cfeed lst nil)))
perm-csteptheorem
(defthm perm-cstep (perm (cstep acc) acc))
perm-cfeedtheorem
(defthm perm-cfeed
  (perm (cfeed lst acc) (append lst acc))
  :hints (("Goal" :induct (cfeed lst acc))))
perm-cdraintheorem
(defthm perm-cdrain
  (implies (<= n (len acc)) (perm (cdrain n acc) acc)))
len-csteptheorem
(defthm len-cstep (equal (len (cstep acc)) (len acc)))
len-cfeedtheorem
(defthm len-cfeed
  (equal (len (cfeed lst acc)) (+ (len lst) (len acc))))
true-listp-csteptheorem
(defthm true-listp-cstep
  (implies (true-listp acc) (true-listp (cstep acc)))
  :hints (("Goal" :induct (cstep acc))))
true-listp-cfeedtheorem
(defthm true-listp-cfeed
  (implies (true-listp acc) (true-listp (cfeed lst acc))))
true-listp-cdraintheorem
(defthm true-listp-cdrain
  (implies (true-listp acc) (true-listp (cdrain n acc))))
len-cdraintheorem
(defthm len-cdrain
  (implies (and (integerp n) (<= n (len acc)))
    (equal (len (cdrain n acc)) (len acc))))
cfeed-openertheorem
(defthm cfeed-opener
  (equal (cfeed (cons pair lst) acc)
    (cfeed lst (cstep (cons pair acc)))))
cstep-append1theorem
(defthm cstep-append1
  (implies (evnp (len a))
    (equal (cstep (append a b)) (append (cstep a) (cstep b))))
  :rule-classes nil
  :hints (("Goal" :induct (cstep a))))
cstep-nooptheorem
(defthm cstep-noop
  (implies (and (true-listp acc) (ordered acc))
    (equal (cstep acc) acc))
  :hints (("Goal" :induct (cstep acc))))
cstep-append2theorem
(defthm cstep-append2
  (implies (and (true-listp b) (ordered b) (all-all-gte a b))
    (equal (cstep (append a b)) (append (cstep a) b)))
  :hints (("Goal" :induct (cstep a))))
phifunction
(defun phi
  (acc)
  (cond ((endp acc) t)
    ((endp (cdr acc)) t)
    (t (and (all-gte (car acc) (cdr acc)) (phi (cddr acc))))))
all-gte-csteptheorem
(defthm all-gte-cstep
  (implies (all-gte x acc) (all-gte x (cstep acc)))
  :hints (("Goal" :induct (cstep acc))))
phi-csteptheorem
(defthm phi-cstep
  (implies (phi (cdr acc)) (phi (cstep acc))))
phi-constheorem
(defthm phi-cons
  (equal (phi (cons pair acc))
    (or (endp acc) (and (all-gte pair acc) (phi (cdr acc))))))
phi-cfeedtheorem
(defthm phi-cfeed
  (implies (phi acc) (phi (cfeed lst acc)))
  :hints (("Goal" :induct (cfeed lst acc) :in-theory (disable cstep))))
perm-csorttheorem
(defthm perm-csort (perm (csort acc) acc))
ordered-firstn-cdraintheorem
(defthm ordered-firstn-cdrain
  (implies (and (integerp n) (<= n (len acc)) (phi acc))
    (ordered (firstn (+ 2 n) (cdrain n acc))))
  :hints (("Subgoal *1/5" :use (:instance gte-car-firstn
       (a (cdr acc))
       (b (cdrain (+ -1 n) (cstep (cdr acc))))
       (pair (car acc))
       (n (+ 1 n)))) ("Subgoal *1/1" :expand ((firstn (+ 2 n) acc) (firstn (+ 1 n) (cdr acc))))))
ordered-csorttheorem
(defthm ordered-csort
  (ordered (csort lst))
  :hints (("Goal" :use (:instance ordered-firstn-cdrain
       (n (len lst))
       (acc (cfeed lst nil))))))
bridgetheorem
(defthm bridge
  (implies (and (all-gte pair1 s)
      (>= (data pair1) (data pair2))
      (all-all-gte s (list pair2))
      (evnp (len s))
      (ordered s))
    (equal (cstep (cons pair1 (append s (cons pair2 acc))))
      (cons pair1 (append s (cons pair2 (cstep acc))))))
  :hints (("Goal" :use ((:instance cstep-append1
        (a (cons pair1 (append s (list pair2))))
        (b acc)) (:instance ordered-cons-append-cons
         (pair1 pair1)
         (s s)
         (pair2 pair2))))))
positive-infinity-hintfunction
(defun positive-infinity-hint
  (lst s acc)
  (cond ((endp lst) (list s acc))
    (t (positive-infinity-hint (cdr lst)
        (cons (car lst) (append s (list (car acc))))
        (cstep (cdr acc))))))
positive-infinity-gentheorem
(defthm positive-infinity-gen
  (implies (and (<= (len lst) (len acc))
      (evnp (len s))
      (ordered s)
      (ordered (rev lst))
      (phi acc)
      (all-all-gte lst s)
      (all-all-gte lst acc)
      (all-all-gte s acc))
    (equal (cfeed lst (append s acc))
      (append (rev lst) s (cdrain (len lst) acc))))
  :hints (("Goal" :induct (positive-infinity-hint lst s acc)))
  :rule-classes nil)
positive-infinitytheorem
(defthm positive-infinity
  (implies (and (integerp n)
      (<= n (len acc))
      (all-gte maxpair acc)
      (phi acc))
    (equal (cfeed (repeat n maxpair) acc)
      (append (repeat n maxpair) (cdrain n acc))))
  :hints (("Goal" :use (:instance positive-infinity-gen
       (lst (repeat n maxpair))
       (s nil)
       (acc acc)))))
negative-infinity-gentheorem
(defthm negative-infinity-gen
  (implies (and (true-listp acc)
      (all-all-gte lst acc)
      (all-all-gte s acc)
      (ordered acc))
    (equal (cfeed lst (append s acc))
      (append (cfeed lst s) acc)))
  :hints (("Goal" :induct (cfeed lst s)))
  :rule-classes nil)
negative-infinitytheorem
(defthm negative-infinity
  (implies (all-all-gte lst (list minpair))
    (equal (cfeed lst (repeat n minpair))
      (append (cfeed lst nil) (repeat n minpair))))
  :hints (("Goal" :use (:instance negative-infinity-gen
       (lst lst)
       (s nil)
       (acc (repeat n minpair))))))