• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
      • Theories
      • Rule-classes
      • Proof-builder
      • Hons-and-memoization
      • Events
      • History
      • Parallelism
      • Programming
        • Defun
        • Declare
        • System-utilities
        • Stobj
        • State
        • Memoize
        • Mbe
        • Io
        • Defpkg
        • Apply$
        • Mutual-recursion
        • Loop$
        • Programming-with-state
        • Arrays
        • Characters
        • Time$
        • Loop$-primer
        • Fast-alists
        • Defmacro
        • Defconst
        • Evaluation
        • Guard
        • Equality-variants
        • Compilation
        • Hons
        • ACL2-built-ins
        • Developers-guide
        • System-attachments
        • Advanced-features
        • Set-check-invariant-risk
        • Numbers
        • Irrelevant-formals
        • Efficiency
        • Introduction-to-programming-in-ACL2-for-those-who-know-lisp
        • Redefining-programs
        • Lists
        • Invariant-risk
        • Errors
        • Defabbrev
        • Conses
        • Alists
        • Set-register-invariant-risk
        • Strings
        • Program-wrapper
        • Get-internal-time
        • Basics
        • Packages
        • Defmacro-untouchable
        • Primitive
        • <<
          • Alphorder
            • Fast-alphorder
            • Hons-alphorder-merge
          • Lexorder
          • Fast-<<
        • Revert-world
        • Set-duplicate-keys-action
        • Unmemoize
        • Symbols
        • Def-list-constructor
        • Easy-simplify-term
        • Defiteration
        • Defopen
        • Sleep
      • Start-here
      • Real
      • Debugging
      • Miscellaneous
      • Output-controls
      • Macros
      • Interfacing-tools
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • <<
  • ACL2-built-ins

Alphorder

Total order on atoms

Alphorder is a non-strict total order, a ``less than or equal,'' on atoms. By ``non-strict total order'' we mean a function that always returns t or nil and satisfies the following properties.

  • Antisymmetry: XrY & YrX -> X=Y
  • Transitivity: XrY & YrZ -> XrZ
  • Trichotomy: XrY v YrX

Also see lexorder, which extends alphorder to all objects.

(Alphorder x y) has a guard of (and (atom x) (atom y)).

Within a single type: rationals are compared arithmetically, complex rationals are compared lexicographically, characters are compared via their char-codes, and strings and symbols are compared with alphabetic ordering. Across types, rationals come before complexes, complexes come before characters, characters before strings, and strings before symbols. We also allow for ``bad atoms,'' i.e., atoms that are not legal Lisp objects but make sense in the ACL2 logic; these come at the end, after symbols.

Function: alphorder

(defun alphorder (x y)
       (declare (xargs :guard (and (atom x) (atom y))))
       (cond ((real/rationalp x)
              (cond ((real/rationalp y) (<= x y))
                    (t t)))
             ((real/rationalp y) nil)
             ((complex/complex-rationalp x)
              (cond ((complex/complex-rationalp y)
                     (or (< (realpart x) (realpart y))
                         (and (= (realpart x) (realpart y))
                              (<= (imagpart x) (imagpart y)))))
                    (t t)))
             ((complex/complex-rationalp y) nil)
             ((characterp x)
              (cond ((characterp y)
                     (<= (char-code x) (char-code y)))
                    (t t)))
             ((characterp y) nil)
             ((stringp x)
              (cond ((stringp y) (and (string<= x y) t))
                    (t t)))
             ((stringp y) nil)
             (t (cond ((symbolp x)
                       (cond ((symbolp y) (not (symbol< y x)))
                             (t t)))
                      ((symbolp y) nil)
                      (t (bad-atom<= x y))))))

Subtopics

Fast-alphorder
Probably faster alternative to alphorder.
Hons-alphorder-merge
Merge two already-alphordered lists of atoms.