An ordinal measure for admitting functions: lexicographic ordering of a list of natural numbers.
(nat-list-measure a) constructs an ordinal that can be used to prove that recursive functions terminate. It essentially provides a lexicographic order of a list of naturals. That is,
(o< (nat-list-measure (list a1 b1 c1)) (nat-list-measure (list a2 b2 c2)))
Will be true when either:
Typical usage is, e.g.,:
(defun f (a b c) (declare (xargs :measure (nat-list-measure (list a b c)))) ...)
See also the simpler (but more limited) two-nats-measure for some additional discussion on how such a measure might be useful.
See also nat-list-< for a somewhat fancier alternative.
Function:
(defun nat-list-measure (a) (declare (xargs :guard t)) (if (atom a) (nfix a) (make-ord (len a) (+ 1 (nfix (car a))) (nat-list-measure (cdr a)))))
Theorem:
(defthm consp-nat-list-measure (equal (consp (nat-list-measure a)) (consp a)))
Theorem:
(defthm atom-caar-nat-list-measure (equal (caar (nat-list-measure a)) (and (consp a) (len a))))
Theorem:
(defthm o-p-of-nat-list-measure (o-p (nat-list-measure a)))
Function:
(defun cons-list-or-quotep (x) (if (atom x) (equal x nil) (case (car x) 't (cons (and (eql (len x) 3) (cons-list-or-quotep (third x)))))))
Theorem:
(defthm o<-of-nat-list-measure (implies (syntaxp (and (cons-list-or-quotep a) (cons-list-or-quotep b))) (equal (o< (nat-list-measure a) (nat-list-measure b)) (or (< (len a) (len b)) (and (equal (len a) (len b)) (if (consp a) (or (< (nfix (car a)) (nfix (car b))) (and (equal (nfix (car a)) (nfix (car b))) (o< (nat-list-measure (cdr a)) (nat-list-measure (cdr b))))) (< (nfix a) (nfix b))))))))