(defun inlistp (elt L)
  (if (true-listp L)
      (cond ((null L) nil)
	    ((= elt (car L)) t)
	    (t (inlistp elt (cdr L))))
    'error))

(defun okay-listp (L)
  (if (true-listp L)
      (cond ((null L) t)
	    ((null (cdr L)) t)
	    ((= (car L) (cadr L)) (okay-listp (cdr L)))
	    (t (and (not (inlistp (car L) (cdr L)))
		    (okay-listp (cdr L)))))
    'error))

(defun numlistp (val)
  (or (null val) 
      (and (consp val) (integerp (car val)) (numlistp (cdr val)))))

(defun insert (elt sorted)
  (if (and (integerp elt) (numlistp sorted))
      (cond ((null sorted) (list elt))
	    ((<= elt (car sorted)) (cons elt sorted))
	    (t (cons (car sorted) (insert elt (cdr sorted)))))
    'error))

(defun isort (unsorted)
  (if (numlistp unsorted)
      (cond ((null unsorted) nil)
	    (t (insert (car unsorted) (isort (cdr unsorted)))))
    'error))

(defun sortedp (L)
  (if (numlistp L)
      (cond ((null L) t)
	    ((null (cdr L)) t)
	    (t (and (<= (car L) (cadr L))
		    (sortedp (cdr L)))))
    'error))

(defthm isort-sorts-thm
  (implies (numlistp L) (sortedp (isort L))))

(defthm insert-sorted-thm
  (implies (and (sortedp L)
		(integerp n))
	   (sortedp (insert n L))))

(defthm insert-numlist-thm
  (implies (and (sortedp L)
		(numlistp L)
		(integerp n))
	   (numlistp (insert n L))))

(defthm sorted-okay-thm
  (implies (and (numlistp L) (sortedp L))
	   (okay-listp L)))

(defthm okay-insert-thm
  (implies (and (numlistp L)
		(integerp n)
		(sortedp L))
	   (okay-listp (insert n L))))

(defthm isort-numlist-thm
  (implies (numlistp L)
	   (numlistp (isort L))))

(defthm okay-sort-thm
  (implies (numlistp L) (okay-listp (isort L))))
