(mergesort x) converts the list
Our implementation is probably not blisteringly fast. Folklore says we should switch to using a bubblesort when we get down to some threshold, say 40 elements. I'm not going to bother with any of that. If you find that the mergesort's performance is inadequate, which is unlikely, you can work on making it faster.
There are a few points of interest. If you look at the actual sort code, mergesort-exec, you will see that it is actually using the set library's own union function to perform the union. This is pretty slick because union is linear complexity, and yet is easy to reason about since we have already got a lot of theory in place about it.
In any case, our strategy for proving the equality of this mergesort with a simple insertion sort is the exact same trick we use everywhere else in the sets library. We begin by showing that both produce sets, and then show that membership in either is true exactly when an element is member-equal in the original list.
(defun mergesort (x) (declare (xargs :guard t)) (mbe :logic (if (endp x) nil (insert (car x) (mergesort (cdr x)))) :exec (mergesort-exec x)))
(defthm mergesort-set (setp (mergesort x)))
(defthm in-mergesort (equal (in a (mergesort x)) (if (member a x) t nil)))
(defthm mergesort-set-identity (implies (setp x) (equal (mergesort x) x)))