Merge Sort as a Query Program #
Merge sort implemented as a FreeM (LEQuery α), making all comparison queries explicit.
The definitions mirror List.merge and List.mergeSort exactly: the list is split into
contiguous halves and the merge prefers the left element on ties. Consequently evaluating
the query program against any oracle produces literally the same list as List.mergeSort
with the comparator induced by the oracle (eval_mergeSort in
Cslib.Algorithms.Lean.Query.Sort.Merge.Lemmas); in particular the sort is stable.
The recursive calls of mergeSort are not structural, since the two halves are not
syntactic subterms, and are justified separately using their lengths.
Split a list into contiguous halves; if the length is odd, the first half is one element
longer. This agrees with List.MergeSort.Internal.splitInTwo, so that mergeSort agrees
with List.mergeSort.
Instances For
Merge two sorted lists using comparison queries.
Equations
- Cslib.Query.merge xs ys = xs.mergeM ys Cslib.Query.LEQuery.ask
Instances For
Sort a list using merge sort with comparison queries.