• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
      • Gl
      • Esim
      • Vl2014
      • Sv
      • Vwsim
      • Fgl
      • Vl
        • Syntax
        • Loader
        • Warnings
        • Getting-started
        • Utilities
          • Name-database
          • Vl-gc
          • Symbol-list-names
          • Ints-from
          • Nats-from
          • Make-lookup-alist
          • Redundant-mergesort
            • Longest-common-prefix
            • Vl-remove-keys
            • Vl-plural-p
            • Vl-merge-contiguous-indices
            • Sum-nats
            • Vl-edition-p
            • Vl-maybe-integer-listp
            • Fast-memberp
            • Nat-listp
            • Max-nats
            • Longest-common-prefix-list
            • Character-list-listp
            • Vl-character-list-list-values-p
            • Remove-from-alist
            • Prefix-of-eachp
            • Vl-string-keys-p
            • Vl-maybe-nat-listp
            • Vl-string-list-values-p
            • String-list-listp
            • Vl-string-values-p
            • Explode-list
            • True-list-listp
            • Symbol-list-listp
            • All-have-len
            • Pos-listp
            • Min-nats
            • Debuggable-and
            • Vl-starname
            • Remove-equal-without-guard
            • String-fix
            • Anyp
            • Vl-maybe-string-list
            • Longer-than-p
            • Fast-alist-free-each-alist-val
            • Not*
            • Free-list-of-fast-alists
            • *nls*
          • Printer
          • Kit
          • Mlib
          • Transforms
        • X86isa
        • Svl
        • Rtl
      • Software-verification
      • Testing-utilities
      • Math
    • Utilities

    Redundant-mergesort

    Same as mergesort, but avoids re-sorting lists that are already sorted.

    Signature
    (redundant-mergesort x &key warnp from) → *
    Arguments
    warnp — print warnings when x is not sorted?.
        Guard (booleanp warnp).
    from — context to help track down warnings.
        Guard (symbolp from).

    In the logic, (redundant-mergesort x) is just (mergesort x). But in the execution, it first checks to see if x is already sorted, and if so returns x unchanged.

    I use this function when I do not want to go to the trouble of proving that some cons-based operation happens to produce a set. In practice, this should be much faster than a mergesort because checking (setp x) is linear and requires no consing.

    By default, redundant-mergesort is silent and will simply sort its argument if necessary. However, you can also instruct it to emit warnings. A typical example is:

    (defun my-function (x)
       (let ((x-sort (redundant-mergesort x :warnp t :from 'my-function)))
          ...))

    Definitions and Theorems

    Function: redundant-mergesort-fn

    (defun
     redundant-mergesort-fn (x warnp from)
     (declare (xargs :guard (and (booleanp warnp) (symbolp from))))
     (let
      ((__function__ 'redundant-mergesort))
      (declare (ignorable __function__))
      (mbe
       :logic (mergesort x)
       :exec
       (if
        (setp x)
        x
        (prog2$
         (cond
             ((and warnp from)
              (cw "; Redundant-mergesort given unsorted list by ~s0.~%"
                  from))
             (warnp (cw "; Redundant-mergesort given unsorted list.~%"))
             (t nil))
         (mergesort x))))))